//+------------------------------------------------------------------+
//|                                              SkyHookBreakout.mq4 |
//|                                    Copyright © 2007, Skyhook     |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Skyhook"
#property link      ""
#define  GLOBVAR_TRIGGERED1    "WeAreInATrade1"

//---- input parameters
extern int       PipBox=9;
extern int       TakeProfit1=30;
extern int       TakeProfit2=60;
extern int       TakeProfit3=120;
extern double    LotSize=0.5;
extern string    StartTime1 = "05:00";
extern string    EndTime1   = "09:00";
extern string    ExpirationTime1 = "14:57";
double StopLoss1,StopLoss2;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   //------------------------------------------------------------------------
   // Creat Global variables to remember some of our info between "ticks"
   //------------------------------------------------------------------------
   
   GlobalVariableSet("OpeningRange1",0);
   GlobalVariableSet("OpeningRange2",0);
   GlobalVariableSet("LowestPriceOfRange1",0);
   GlobalVariableSet("HighestPriceOfRange1",0);
   GlobalVariableSet("LowestPriceOfRange2",0);
   GlobalVariableSet("HighestPriceOfRange2",0);
   GlobalVariableSet("RangeTimeComplete1",FALSE);
   GlobalVariableSet("RangeTimeComplete2",FALSE);
   GlobalVariableSet("TicketNumber",0);
   GlobalVariableSet("TicketNumber2",0);
   GlobalVariableSet("TicketNumber3",0);
   GlobalVariableSet("TicketNumber4",0);
   GlobalVariableSet("Trade1",0);
   GlobalVariableSet("Trade2",0);
   GlobalVariableSet("Trade3",0);
   GlobalVariableSet("StaticStopLoss",FALSE);
//----
   
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
int start()
  {
   datetime TimeBegin1, TimeEnd1, dtTradeDate=TimeCurrent(), TimeExpire1, TimeBegin2, TimeEnd2, TimeExpire2;
   double   LowestPriceOfRange1, HighestPriceOfRange1, OpeningRange1, LowestPriceOfRange2, HighestPriceOfRange2, OpeningRange2, DynProfitL, DynProfitS, BarPrev, BarCur;
   bool     RangeTimeComplete1,RangeTimeComplete2,WeAreInATrade1,WeAreInATrade2,WeAreInATrade3, StaticStopLoss;
  
   OpeningRange1         =GlobalVariableGet("OpeningRange");
   LowestPriceOfRange1   =GlobalVariableGet("LowestPriceOfRange1" );
   HighestPriceOfRange1  =GlobalVariableGet("HighestPriceOfRange1");
   RangeTimeComplete1    =GlobalVariableGet("RangeTimeComplete1");
   WeAreInATrade1        =GlobalVariableGet("WeAreInATrade1");
   WeAreInATrade2        =GlobalVariableGet("WeAreInATrade2");
   WeAreInATrade3        =GlobalVariableGet("WeAreInATrade3");
   
  TimeBegin1 = StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + StartTime1);
  TimeEnd1 = StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + EndTime1);
  TimeExpire1 = StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + ExpirationTime1);
 
 if(TimeCurrent()==TimeEnd1 && RangeTimeComplete1==FALSE)
     {
      RangeTimeComplete1=TRUE;
      RangeTimeComplete2=FALSE;

      LowestPriceOfRange1   =Low  [Lowest  (NULL,0,MODE_LOW  ,24 ,0)];
      HighestPriceOfRange1  =High [Highest (NULL,0,MODE_HIGH ,24 ,0)];
      OpeningRange1         =HighestPriceOfRange1-LowestPriceOfRange1;
      
      GlobalVariableSet("OpeningRange1",OpeningRange1);
      GlobalVariableSet("LowestPriceOfRange1" , LowestPriceOfRange1);
      GlobalVariableSet("HighestPriceOfRange1", HighestPriceOfRange1);
      GlobalVariableSet("RangeTimeComplete1",TRUE);
      GlobalVariableSet("RangeTimeComplete2",FALSE);
     
      if(TimeCurrent()==TimeEnd2 && RangeTimeComplete2==FALSE)

      RangeTimeComplete2=TRUE;

      LowestPriceOfRange2   =Low  [Lowest  (NULL,0,MODE_LOW  ,8 ,0)];
      HighestPriceOfRange2  =High [Highest (NULL,0,MODE_HIGH ,8 ,0)];
      OpeningRange2         =HighestPriceOfRange2-LowestPriceOfRange2;
      
      GlobalVariableSet("OpeningRange2",OpeningRange2);
      GlobalVariableSet("LowestPriceOfRange2" , LowestPriceOfRange2);
      GlobalVariableSet("HighestPriceOfRange2", HighestPriceOfRange2);
      GlobalVariableSet("RangeTimeComplete2",TRUE);
     }

   if(TimeCurrent()==TimeExpire1 && RangeTimeComplete1==TRUE)
     {
      RangeTimeComplete1=FALSE;
      GlobalVariableSet("RangeTimeComplete1",FALSE);
      WeAreInATrade1=FALSE;
      GlobalVariableSet("WeAreInATrade1",FALSE);
      
      if(TimeCurrent()==TimeExpire2 && RangeTimeComplete2==TRUE)
      RangeTimeComplete2=FALSE;
      GlobalVariableSet("RangeTimeComplete2",FALSE);
      WeAreInATrade2=FALSE;
      GlobalVariableSet("WeAreInATrade2",FALSE);
     }
     
    StopLoss1 = HighestPriceOfRange1-NormalizeDouble(HighestPriceOfRange1-LowestPriceOfRange1,2)*(.65)*Point-(PipBox*Point);
    StopLoss2 = LowestPriceOfRange1+NormalizeDouble(HighestPriceOfRange1-LowestPriceOfRange1,2)*(.65)*Point+(PipBox*Point);
    DynProfitL = 0;
   if(RangeTimeComplete1==TRUE && WeAreInATrade1==FALSE)
     {
      if(Close[0]>=HighestPriceOfRange1+(PipBox*Point))
        {
         int ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,5,StopLoss2,(HighestPriceOfRange1+(PipBox+TakeProfit1)*Point),"First Target Buy",1856,TimeExpire1,Yellow);
         int ticket2=OrderSend(Symbol(),OP_BUY,LotSize,Ask,5,StopLoss2,(HighestPriceOfRange1+(PipBox+TakeProfit2)*Point),"Second Target Buy",1856,TimeExpire1,Yellow);
         int ticket3=OrderSend(Symbol(),OP_BUY,LotSize,Ask,5,StopLoss2,(HighestPriceOfRange1+(PipBox+TakeProfit3)*Point),"Third Target Buy",1856,TimeExpire1,Yellow);
            {
             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               {
               Print("BUY orders opened : ",OrderOpenPrice());
               WeAreInATrade1=TRUE;
               GlobalVariableSet("WeAreInATrade1",TRUE);
               GlobalVariableSet("TicketNumber" ,ticket); 
               GlobalVariableSet("TicketNumber2",ticket2);
               GlobalVariableSet("TicketNumber3",ticket3);
               if(ticket != 0)
               {
               OrderSelect(ticket,SELECT_BY_TICKET);
               if(OrderCloseTime() == 0)
               {
               OrderSelect(ticket2,SELECT_BY_TICKET);
               if(OrderCloseTime() == 0)
                  {
                  OrderModify(ticket2,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
                  }
                ticket = 0;
                }
               }
            }
            return(0);
        }
     if(RangeTimeComplete1==TRUE && WeAreInATrade2==FALSE)
      if(Close[0]<=LowestPriceOfRange1-(PipBox*Point))
        {
         ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,5,StopLoss1,(LowestPriceOfRange1-(PipBox+TakeProfit1)*Point),"First Target Sell",1856,TimeExpire1,Yellow);
         ticket2=OrderSend(Symbol(),OP_SELL,LotSize,Bid,5,StopLoss1,(LowestPriceOfRange1-(PipBox+TakeProfit2)*Point),"Second Target Sell",1856,TimeExpire1,Yellow);
         ticket3=OrderSend(Symbol(),OP_SELL,LotSize,Ask,5,StopLoss1,(LowestPriceOfRange1-(PipBox+TakeProfit3)*Point),"Third Target Buy",1856,TimeExpire1,Yellow);
         if(ticket>0)
            {
             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               {
               Print("SELL order opened : ",OrderOpenPrice());
               WeAreInATrade2=TRUE;
               GlobalVariableSet("WeAreInATrade2",TRUE);
               GlobalVariableSet("TicketNumber" ,ticket);
               GlobalVariableSet("TicketNumber2" ,ticket2);
               GlobalVariableSet("TicketNumber3",ticket3);
               if(ticket != 0)
               {
               OrderSelect(ticket,SELECT_BY_TICKET);
               if(OrderCloseTime() != 0)
               {
               OrderSelect(ticket2,SELECT_BY_TICKET);
               if(OrderCloseTime() == 0)
                  {
                  OrderModify(ticket2,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
                  }
                ticket = 0;
                }
               }
            }
            else Print("Error opening SELL order : ",GetLastError()); 
            return(0); 
        }

     }
   return(0);
  }
}
}
//+------------------------------------------------------------------+