//+------------------------------------------------------------------+
//|                                  Copyright © 2011, John Wustrack |
//|                                        john_wustrack@hotmail.com |
//| www.forexfactory.com/showthread.php?t=313360&page=33
//+------------------------------------------------------------------+
#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;

extern string HAS   = "----HAS SETTINGS----";
extern int TimeFrame1= 15;
extern int MaMetod  = 2;
extern int MaPeriod = 15;
extern int MaMetod2  = 2;
extern int MaPeriod2 = 15;

extern int TimeFrame2= 240;
extern int MaMetod1  = 2;
extern int MaPeriod1 = 15;
extern int MaMetod22  = 2;
extern int MaPeriod22 = 15;

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()
  {
//----
   
   // 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


  //Lower Time Frame
      double HA00=iCustom(NULL, TimeFrame1, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,1);
      double HA10=iCustom(NULL, TimeFrame1, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,1);
      double HA20=iCustom(NULL, TimeFrame1, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,1);
      double HA30=iCustom(NULL, TimeFrame1, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,1);
      double HA01=iCustom(NULL, TimeFrame1, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,2);
      double HA11=iCustom(NULL, TimeFrame1, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,2);
      double HA21=iCustom(NULL, TimeFrame1, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,2);
      double HA31=iCustom(NULL, TimeFrame1, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,2);
      
  //Higher Time Frame
      double HA000=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,0,1);
      double HA100=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,1,1);
      double HA200=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,2,1);
      double HA300=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,3,1);
      double HA010=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,0,2);
      double HA110=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,1,2);
      double HA210=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,2,2);
      double HA310=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,3,2);
      
      
   // Set up the allowable bands based on the direction
  bool lb_Long, lb_Short;
  
  switch (MarketRead)
      {
      // Long
      case MR_LONG:     if ((HA00 < HA10 && HA20 < HA30) && (HA01 > HA11 || HA21 > HA31)&& (HA000 < HA100 && HA200 < HA300) && (HA010 > HA110 || HA210 > HA310))
                           lb_Long = true;
                        break;
      // Short 
      case MR_SHORT:    if ((HA00 > HA10 && HA20 > HA30) && (HA01 < HA11 || HA21 < HA31)&& (HA000 > HA100 && HA200 > HA300) && (HA010 < HA110 || HA210 < HA310))
                           lb_Short = true;
                        break;
      // Range 
      case MR_RANGE:    if ((HA00 < HA10 && HA20 < HA30) && (HA01 > HA11 || HA21 > HA31)&& (HA000 < HA100 && HA200 < HA300) && (HA010 > HA110 || HA210 > HA310))                           
                           lb_Long = true;
                            
                        if ((HA00 > HA10 && HA20 > HA30) && (HA01 < HA11 || HA21 < HA31)&& (HA000 > HA100 && HA200 > HA300) && (HA010 < HA110 || HA210 < HA310) )                           
                           lb_Short = true;
                        break;
      // CT Long 
      case MR_CTLONG:   if ((HA00 < HA10 && HA20 < HA30) && (HA01 > HA11 || HA21 > HA31)&& (HA000 < HA100 && HA200 < HA300) && (HA010 > HA110 || HA210 > HA310) )
                           lb_Long = true;
                        if ((HA00 > HA10 && HA20 > HA30) && (HA01 < HA11 || HA21 < HA31)&& (HA000 > HA100 && HA200 > HA300) && (HA010 < HA110 || HA210 < HA310) )                           
                           lb_Short = true;
                        break;
      // CT Short 
      case MR_CTSHORT:  if ((HA00 > HA10 && HA20 > HA30) && (HA01 < HA11 || HA21 < HA31)&& (HA000 > HA100 && HA200 > HA300) && (HA010 < HA110 || HA210 < HA310) )
                           lb_Short = true;
                        if ((HA00 < HA10 && HA20 < HA30) && (HA01 > HA11 || HA21 > HA31)&& (HA000 < HA100 && HA200 < HA300) && (HA010 > HA110 || HA210 > HA310) )                           
                           lb_Long = true;
                        break;
      }
  
   // Send any alerts necessary
   if (lb_Short)
      {
      Alert("Possible SHORT opportunity at ",DoubleToStr(Close[0], Digits),TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
      LastAlert = Time[0];
      CB_Action[0] = OPEN_SELL;
      CB_Price[0] = 0;
      }
   
   if (lb_Long )
      {
      Alert("Possible LONG opportunity at ",DoubleToStr(Close[0], Digits),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; //-1


// the rest of the code in this routine is the user defined code for the TLE

  //Lower Time Frame 
      double HA00=iCustom(NULL, 0, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,1);
      double HA10=iCustom(NULL, 0, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,1);
      double HA20=iCustom(NULL, 0, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,1);
      double HA30=iCustom(NULL, 0, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,1);
      double HA01=iCustom(NULL, 0, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,2);
      double HA11=iCustom(NULL, 0, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,2);
      double HA21=iCustom(NULL, 0, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,2);
      double HA31=iCustom(NULL, 0, "Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,2);
      
  //Higher Time Frame
      double HA000=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,0,1);
      double HA100=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,1,1);
      double HA200=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,2,1);
      double HA300=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,3,1);
      double HA010=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,0,2);
      double HA110=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,1,2);
      double HA210=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,2,2);
      double HA310=iCustom(NULL, TimeFrame2, "Heiken_Ashi_Smoothed",MaMetod1,MaPeriod1,MaMetod22,MaPeriod22,3,2);      
     
      
   // If the basket is a buy and we previous closed above the upper
   
   if (BasketType==OPEN_BUY  && (HA00 > HA10 && HA20 > HA30) && (HA01 < HA11 || HA21 < HA31)&& (HA000 > HA100 && HA200 > HA300) && (HA010 < HA110 || HA210 < HA310) ) 
      {
      Alert("close of long at ",DoubleToStr(Close[0], Digits),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==OPEN_SELL && (HA00 < HA10 && HA20 < HA30) && (HA01 > HA11 || HA21 > HA31)&& (HA000 < HA100 && HA200 < HA300) && (HA010 > HA110 || HA210 > HA310) ) 
         {
      Alert("close of short at ",DoubleToStr(Close[0], Digits),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);
  }
//+------------------------------------------------------------------+

