//+------------------------------------------------------------------+
//|                                  Copyright © 2011, John Wustrack |
//|                                        john_wustrack@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, John Wustrack"
#property link      "john_wustrack@hotmail.com"

#property indicator_chart_window

#property indicator_buffers 2


#define MR_NONE 0
#define MR_LONG 1
#define MR_SHORT 2
#define MR_RANGE 3
#define MR_CTLONG 4
#define MR_CTSHORT 5
#define MR_NOTAPPLICABLE 6

#define DFT_MARKETREAD 3

#define TLE_MODEOPEN 1
#define TLE_MODECLOSE 9

#define OPEN_BUY 1
#define OPEN_SELL 2
#define OPEN_BUYSTOP 3
#define OPEN_BUYLIMIT 4
#define OPEN_SELLSTOP 5
#define OPEN_SELLLIMIT 6

#define CLOSE_ORDER 9

#define DFT_RETURN -1

extern int MarketRead;
extern int TLEMode;
extern int BasketType;

// User Definitions for other indicators etc.
#define Shift 1
#define Indy1 "TMA CENTERED MARI mod"
#define Indy2 "NLMBB"

#define Indy1_Period 13
#define Indy1_AppliedPrice PRICE_CLOSE
#define Indy2_Period 13
#define Indy2_AppliedPrice PRICE_TYPICAL

double gd_Upper;
double gd_Lower;
double gd_Mid;
double gd_NonLagUp;
double gd_NonLagDn;
double gd_NonLagUpPrev;
double gd_NonLagDnPrev;

double CB_Action[];
double CB_Price[];

datetime LastAlert;
datetime LastBar;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
   IndicatorBuffers(2);
   SetIndexBuffer(0,CB_Action);
   SetIndexBuffer(1,CB_Price);
   IndicatorDigits(Digits);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   // Reset any close request on subsequent entry
   if (CB_Action[0] == CLOSE_ORDER)
      {
      CB_Action[0] = DFT_RETURN;
      // If an order cannot be opened on the same bar one is closed - comment out this line 
      LastBar = 0;
      }

   // Do nothing if the market read is NONE
   if (MarketRead == MR_NONE) return(0);
      
   // If the mode is not open or close - do nothing
   if (TLEMode != TLE_MODEOPEN && TLEMode != TLE_MODECLOSE) return(0);
   
   // Perform the trade logic
   if (TLEMode == TLE_MODEOPEN) Check_For_Open();
   if (TLEMode == TLE_MODECLOSE) Check_For_Close();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Check for open of new trade                                      |
//+------------------------------------------------------------------+
int Check_For_Open()
  {
//----
   bool lb_MidLong,lb_MidShort;
   bool lb_UppShort;
   bool lb_LowLong;
         
   // Decide if the routine is to be run every tick
   // if so, comment out the next if statement
   if (Time[0] == LastBar) return(0);  // Once per bar 
   
   LastBar = Time[0];
   
   if (Time[0] == LastAlert) return(0);

   // Reset the communication buffers
   CB_Action[0] = DFT_RETURN;
   CB_Price[0] = DFT_RETURN;
   
   // Set the default market read if the market read is not applicable
   if (MarketRead == MR_NOTAPPLICABLE) MarketRead = DFT_MARKETREAD;
   
/*

the rest of the code in this routine is the user defined code for the TLE

*/

   // Set up the allowable bands based on the direction
   switch (MarketRead)
      {
      // Long
      case MR_LONG:     lb_MidLong = true;
                        lb_LowLong = true;
                        break;
      // Short 
      case MR_SHORT:    lb_MidShort = true;
                        lb_UppShort = true;
                        break;
      // Range 
      case MR_RANGE:    lb_LowLong = true;
                        lb_UppShort = true;
                        break;
      // CT Long 
      case MR_CTLONG:   lb_LowLong = true;
                        lb_MidLong = true;
                        lb_UppShort = true;
                        break;
      // CT Short 
      case MR_CTSHORT:  lb_UppShort = true;
                        lb_MidShort = true;
                        lb_LowLong = true;
                        break;
      }
      

   //Read Indicators
   ReadIndicators();
   
   // Send any alerts necessary
   if (lb_UppShort && 
        Close[Shift] > gd_Upper &&
        gd_NonLagUpPrev != EMPTY_VALUE && gd_NonLagUp != EMPTY_VALUE && gd_NonLagDnPrev == EMPTY_VALUE && gd_NonLagDn!= EMPTY_VALUE)
      {
      //Alert("Possible SHORT opportunity at ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
      LastAlert = Time[0];
      CB_Action[0] = OPEN_SELL;
      CB_Price[0] = 0;
      }
   
   if (lb_MidShort &&
        Close[Shift] > gd_Mid && 
        gd_NonLagUpPrev != EMPTY_VALUE && gd_NonLagUp != EMPTY_VALUE && gd_NonLagDnPrev == EMPTY_VALUE && gd_NonLagDn!= EMPTY_VALUE)
      {
      //Alert("Possible SHORT opportunity at ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
      LastAlert = Time[0];
      CB_Action[0] = OPEN_SELL;
      CB_Price[0] = 0;
      }
          
   if (lb_LowLong &&
        Close[Shift] < gd_Lower && 
        gd_NonLagDnPrev != EMPTY_VALUE && gd_NonLagDn != EMPTY_VALUE && gd_NonLagUpPrev == EMPTY_VALUE && gd_NonLagUp!= EMPTY_VALUE)
      {
      //Alert("Possible LONG opportunity at ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
      LastAlert = Time[0];
      CB_Action[0] = OPEN_BUY;
      CB_Price[0] = 0;
      }
   
   if (lb_MidLong &&
       Close[Shift] < gd_Mid && 
        gd_NonLagDnPrev != EMPTY_VALUE && gd_NonLagDn != EMPTY_VALUE && gd_NonLagUpPrev == EMPTY_VALUE && gd_NonLagUp!= EMPTY_VALUE)
      {
      //Alert("Possible LONG opportunity at ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
      LastAlert = Time[0];
      CB_Action[0] = OPEN_BUY;
      CB_Price[0] = 0;
      }

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Check for close of a trade                                       |
//+------------------------------------------------------------------+
int Check_For_Close()
  {
//----

   if (BasketType != OPEN_BUY && BasketType != OPEN_SELL) return(0);

   // Decide if the routine is to be run every tick
   // if so, comment out the next if statement
   if (Time[0] == LastBar) return(0);  // Once per bar 
   
   LastBar = Time[0];

//   if (Time[0] == LastAlert) return(0);  

   // Reset the communication buffers
   CB_Action[0] = DFT_RETURN;

/*

the rest of the code in this routine is the user defined code for the TLE

*/
   // Read Indicators
   ReadIndicators();

   // If the basket is a buy and we previous closed above the upper
   if (BasketType == OP_BUY && Close[Shift] > gd_Upper &&
         gd_NonLagUpPrev != EMPTY_VALUE && gd_NonLagUp != EMPTY_VALUE && gd_NonLagDnPrev == EMPTY_VALUE && gd_NonLagDn!= EMPTY_VALUE)
      {
      //Alert("Possible close of long at ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
      LastAlert = Time[0];
      CB_Action[0] = CLOSE_ORDER;
      }
      
   // If the basket is a sell and we previous closed below the lower
   if (BasketType == OP_SELL && Close[Shift] < gd_Lower &&
         gd_NonLagDnPrev != EMPTY_VALUE && gd_NonLagDn != EMPTY_VALUE && gd_NonLagUpPrev == EMPTY_VALUE && gd_NonLagUp!= EMPTY_VALUE)
      {
      //Alert("Possible close of short at ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
      LastAlert = Time[0];
      CB_Action[0] = CLOSE_ORDER;
      }
      
   // We don't want to send any actions UNLESS the market read is range
   if (MarketRead != MR_RANGE)
      CB_Action[0] = DFT_RETURN;
      
//----
   return(0);
  }
//+------------------------------------------------------------------+

//Read Indicator
void ReadIndicators()
{

   // Get tma bands
   gd_Upper = iCustom(NULL,0,Indy1,"current time frame",Indy1_Period,Indy1_AppliedPrice,1.0,100,true,1,Shift);
   gd_Mid = iCustom(NULL,0,Indy1,"current time frame",Indy1_Period,Indy1_AppliedPrice,1.0,100,true,0,Shift);
   gd_Lower = iCustom(NULL,0,Indy1,"current time frame",Indy1_Period,Indy1_AppliedPrice,1.0,100,true,2,Shift);

   //nlmbb
   gd_NonLagUp= iCustom(NULL,0,Indy2,Indy2_AppliedPrice,Indy2_Period,0,0.0,"",1,1,0,"",0,"",0,0,1,Shift);
   gd_NonLagDn= iCustom(NULL,0,Indy2,Indy2_AppliedPrice,Indy2_Period,0,0.0,"",1,1,0,"",0,"",0,0,2,Shift);
   gd_NonLagUpPrev= iCustom(NULL,0,Indy2,Indy2_AppliedPrice,Indy2_Period,0,0.0,"",1,1,0,"",0,"",0,0,1,Shift+1);
   gd_NonLagDnPrev= iCustom(NULL,0,Indy2,Indy2_AppliedPrice,Indy2_Period,0,0.0,"",1,1,0,"",0,"",0,0,2,Shift+1);
   Print("gd_NonLagUpPrev",gd_NonLagUpPrev," gd_NonLagDnPrev ",gd_NonLagDnPrev," gd_NonLagUp ",gd_NonLagUp," gd_NonLagDn ",gd_NonLagDn);

}   


