//+------------------------------------------------------------------+
//|                                  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 int  Tenkan      =  9;
extern int  Kijun      =  26;
extern int  Senkou      =  52;
extern int  Buffer   = 0;  // buffer pip for cross
extern int  MAPeriod = 2;
extern string    ex        = "0=SMA,1=EMA,2=SSMA,3=LWMA";
extern int       MAMode    = MODE_SMA;
extern string    ex2       = "0=Close,1=Open,2=High,3=Low";
extern string    ex2a      = "4=Median,5=Typical,6=Weighted";
extern int       MAPrice   = PRICE_CLOSE;

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
   
  */
  // get variables for HAMA
  double ichimokuA = iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANA,1);
  double ichimokuA1 = iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANA,2);
  //----------------------------------------------------------------------------------------------------------------//
  double ichimokuB = iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANB,1);
  double ichimokuB1 = iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANB,2);
  //double VQz20=iCustom(NULL, 0, "VQzz2",60,6,2,2,5,false,3,1);
  double SMA1 = iMA(NULL,0,MAPeriod,0,MAMode,MAPrice,1);
  double SMA2 = iMA(NULL,0,MAPeriod,0,MAMode,MAPrice,2);
  // Set up the allowable bands based on the direction
  bool lb_Long=false;
  bool lb_Short=false;
  bool Break_out=false;
  bool Break_through=false;  
  switch (MarketRead) {

    // Range
  case MR_RANGE:
         if ( ichimokuA > ichimokuB) { //A>B
            if (( SMA1 > ichimokuA && SMA2 <= ichimokuA1 && SMA2 >= ichimokuB1 ) ) {
               // upward break
               Break_out = true;
               lb_Long=true;
               //Print("sft=", sft, "time=", TimeToStr(Time[sft], TIME_DATE|TIME_SECONDS), ", ichimokuA=", ichimokuA, ", ichimokuB=", ichimokuB, ", ichimokuA1=", ichimokuA1, ", ichimokuB1=", ichimokuB1);

            } else {
               if (( SMA1 < ichimokuB && SMA2 >= ichimokuB1 && SMA2 <= ichimokuA1) ) {
                  // downward break
                  Break_out = true;
                  lb_Short=true;
               } //
            }
            
            if (!Break_out) {
               if( (SMA1 > ichimokuA && SMA2 <= ichimokuB1) )  {
                  Break_through = true;            
                  lb_Long=true; 
               }            
               if( (SMA1 < ichimokuB && SMA2 >= ichimokuA1)) {
                  Break_through = true;      
                  lb_Short=true;       
               }            
               
            }
         } else {//B>A

            if (( SMA1 > ichimokuB && SMA2 <= ichimokuB1 && SMA2 >= ichimokuA1) ) {
               // upward break
               Break_out = true;
               lb_Long=true;
            } else {
               if (( SMA1 < ichimokuA && SMA2 >= ichimokuA1 && SMA2 <= ichimokuB1) ) {
                  // downward break
                  Break_out = true;
                  lb_Short=true;
               }
            }
            if (!Break_out) {
               if( (SMA1 > ichimokuB && SMA2 <= ichimokuA1) ) {
                  Break_through = true;   
                  lb_Long=true;          
               }         
               if( (SMA1 < ichimokuA && SMA2 >= ichimokuB1)) {
                  Break_through = true;   
                  lb_Short=true;          
               }            
                  
            }            
         //-------------------------------------
         }
         break;
  default:
    Alert("Input wrong MarketRead: ",MarketRead);
    break;

  }

  // Send any alerts necessary
  if (!Break_out && !Break_through)
    return(0);
  if (lb_Short) {
    Alert("Possible SHORT opportunity at ",DoubleToStr(Close[0], Digits)," ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
    Print("sma1=", DoubleToStr(SMA1, Digits), ", sma2=", DoubleToStr(SMA2, Digits), ", ichimokuA=", DoubleToStr(ichimokuA, Digits), ", ichimokuA1=", DoubleToStr(ichimokuA1, Digits), ", ichimokuB=", DoubleToStr(ichimokuB, Digits), ", ichimokuB1=", DoubleToStr(ichimokuB1, Digits) );
    LastAlert = Time[0];
    CB_Action[0] = OPEN_SELL;
    CB_Price[0] = 0;
    //lb_Short = true;
  }

   if (lb_Long) {
    Alert("Possible LONG opportunity at ",DoubleToStr(Close[0], Digits)," ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
    Print("sma1=", DoubleToStr(SMA1, Digits), ", sma2=", DoubleToStr(SMA2, Digits), ", ichimokuA=", DoubleToStr(ichimokuA, Digits), ", ichimokuA1=", DoubleToStr(ichimokuA1, Digits), ", ichimokuB=", DoubleToStr(ichimokuB, Digits), ", ichimokuB1=", DoubleToStr(ichimokuB1, Digits) );    
    LastAlert = Time[0];
    CB_Action[0] = OPEN_BUY;
    CB_Price[0] = 0;
    //lb_Long = true;
  }

  //----
  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
  bool Break_in=false;
  bool lb_Long=false;
  bool lb_Short=false;
  bool Break_out=false;  
  bool Break_through=false;
  // Get trend hama
  double ichimokuA = iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANA,1);
  double ichimokuA1 = iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANA,2);
  //----------------------------------------------------------------------------------------------------------------//
  double ichimokuB = iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANB,1);
  double ichimokuB1 = iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANB,2);
  //double VQz20=iCustom(NULL, 0, "VQzz2",60,6,2,2,5,false,3,1);
  double SMA1 = iMA(NULL,0,MAPeriod,0,MAMode,MAPrice,1);
  double SMA2 = iMA(NULL,0,MAPeriod,0,MAMode,MAPrice,2);
  /*
  if ( ichimokuA > ichimokuB) { //A>B

    if ( SMA1 < ichimokuA && SMA1 > ichimokuB ) {
      if ( SMA2 >= ichimokuA1 || SMA2 <= ichimokuB1 ) {
         Break_in = true;
      } 
    } 
    if (!Break_in) {
               if( (SMA1 > ichimokuA && SMA2 <= ichimokuB1) || (SMA1 < ichimokuB && SMA2 >= ichimokuA1)) {
                  Break_through = true;             
               }            
    }     

  } else {//B>A

    if ( SMA1 > ichimokuA && SMA1 < ichimokuB ) {
      if ( SMA2 <= ichimokuA1 || SMA2 >= ichimokuB1 ) {
         Break_in = true;
      } 
    } 
    if (!Break_in) {
               if( (SMA1 > ichimokuB && SMA2 <= ichimokuA1) || (SMA1 < ichimokuA && SMA2 >= ichimokuB1)) {
                  Break_through = true;             
               }            
    }  
  }
   */
   
         if ( ichimokuA > ichimokuB) { //A>B
            if (( SMA1 > ichimokuA && SMA2 <= ichimokuA1 && SMA2 >= ichimokuB1 ) ) {
               // upward break
               Break_out = true;
               lb_Long=true;
               //Print("sft=", sft, "time=", TimeToStr(Time[sft], TIME_DATE|TIME_SECONDS), ", ichimokuA=", ichimokuA, ", ichimokuB=", ichimokuB, ", ichimokuA1=", ichimokuA1, ", ichimokuB1=", ichimokuB1);

            } else {
               if (( SMA1 < ichimokuB && SMA2 >= ichimokuB1 && SMA2 <= ichimokuA1) ) {
                  // downward break
                  Break_out = true;
                  lb_Short=true;
               } //
            }
            
            if (!Break_out) {
               if( (SMA1 > ichimokuA && SMA2 <= ichimokuB1) )  {
                  Break_through = true;            
                  lb_Long=true; 
               }            
               if( (SMA1 < ichimokuB && SMA2 >= ichimokuA1)) {
                  Break_through = true;      
                  lb_Short=true;       
               }            
               
            }
         } else {//B>A

            if (( SMA1 > ichimokuB && SMA2 <= ichimokuB1 && SMA2 >= ichimokuA1) ) {
               // upward break
               Break_out = true;
               lb_Long=true;
            } else {
               if (( SMA1 < ichimokuA && SMA2 >= ichimokuA1 && SMA2 <= ichimokuB1) ) {
                  // downward break
                  Break_out = true;
                  lb_Short=true;
               }
            }
            if (!Break_out) {
               if( (SMA1 > ichimokuB && SMA2 <= ichimokuA1) ) {
                  Break_through = true;   
                  lb_Long=true;          
               }         
               if( (SMA1 < ichimokuA && SMA2 >= ichimokuB1)) {
                  Break_through = true;   
                  lb_Short=true;          
               }            
                  
            }            
         //-------------------------------------
         }
         
         
         
            
  // If the basket is a buy and we previous closed above the upper
  if (BasketType==OPEN_BUY  && (lb_Short) ) {
    Alert("close of long at ",DoubleToStr(Close[0], Digits)," ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
    Print("sma1=", DoubleToStr(SMA1, Digits), ", sma2=", DoubleToStr(SMA2, Digits), ", ichimokuA=", DoubleToStr(ichimokuA, Digits), ", ichimokuA1=", DoubleToStr(ichimokuA1, Digits), ", ichimokuB=", DoubleToStr(ichimokuB, Digits), ", ichimokuB1=", DoubleToStr(ichimokuB1, Digits) );        
    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 && (lb_Long)  ) {
    Alert("close of short at ",DoubleToStr(Close[0], Digits)," ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES));
    Print("sma1=", DoubleToStr(SMA1, Digits), ", sma2=", DoubleToStr(SMA2, Digits), ", ichimokuA=", DoubleToStr(ichimokuA, Digits), ", ichimokuA1=", DoubleToStr(ichimokuA1, Digits), ", ichimokuB=", DoubleToStr(ichimokuB, Digits), ", ichimokuB1=", DoubleToStr(ichimokuB1, Digits) );        
    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);
}
//+------------------------------------------------------------------+

