//+------------------------------------------------------------------+
//|                                                 GBP_Breakout.mq4 |
//|                                         Copyright © 2006, Funbot |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Funbot"
#property link      "http://www.metaquotes.net"
#include <stdlib.mqh>
/*
//---- input parameters
extern int       StartTime=8;    // hour of day to to place orders based on base hour's close price.
extern int       BaseTime=8;
extern int       CloseTime = 23; // hour of day to close any open orders. -1 means do not close. 0 to 23.

extern int       BuyOffset=20;
extern int       BuySL=20;

extern int       SellOffset=20;
extern int       SellSL=20;

extern double    LotSize = 0.1;

extern int       TP1=14;
extern int       TP2=30;
extern int       TP3=80;
extern int       SLOffset = 0;  // ...adjust SL on last orders to this profit level after TP2 is reached.

extern int       Slippage = 1; // max allowable slippage on orders.
*/
int       StartTime=10;    // hour of day to to place orders based on base hour's close price.
int       BaseTime=10;
int       CloseTime = 23; // hour of day to close any open orders. -1 means do not close. 0 to 23.

int       BuyOffset=20;
int       BuySL=20;

int       SellOffset=20;
int       SellSL=20;

double    LotSize = 0.1;
int       TP1=14;
int       TP2=30;
int       TP3=80;
int       SLOffset = 0;  // ...adjust SL on last orders to this profit level after TP2 is reached.

int       Slippage = 1; // max allowable slippage on orders.


int ticketL1 = -1, // long positions
    ticketL2 = -1,
    ticketL3 = -1,
    ticketS1 = -1, // short positions
    ticketS2 = -1,
    ticketS3 = -1;
double BasePrice=0, BuyPrice, SellPrice;
double buy_sl,
       sell_sl,
       buy_tp1,
       buy_tp2,
       buy_tp3,                          
       sell_tp1,
       sell_tp2,
       sell_tp3; 
datetime inittime;

//+------------------------------------------------------------------+
//| Select an order by ticket and check if (pending) order
//| has triggered but was not yet closed.
//+------------------------------------------------------------------+
bool IsOrderOpen(int ticket)
{
   return(OrderSelect(ticket,SELECT_BY_TICKET)
      && (OrderType()==OP_BUY || OrderType()==OP_SELL)
      &&  OrderCloseTime()==0);
}

bool IsOrderClosed(int ticket)
{

   return(!OrderSelect(ticket,SELECT_BY_TICKET) || OrderCloseTime()!=0);
      
}

bool IsLoss(int ticket)
{
   return(OrderSelect(ticket,SELECT_BY_TICKET)
      &&  OrderProfit()<=0
      &&  OrderCloseTime()!=0);
}


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {



// initial data checks

   
// move sl to break even or slight profit on last orders
   if(!IsOrderOpen(ticketL2))
      if(IsOrderOpen(ticketL3))
         if(Bid-OrderOpenPrice() >= TP2*Point && OrderStopLoss() < OrderOpenPrice()+SLOffset*Point)
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+SLOffset*Point,OrderTakeProfit(),OrderExpiration());

   if(!IsOrderOpen(ticketS2))
      if(IsOrderOpen(ticketS3))
         if(OrderOpenPrice()-Ask > TP2*Point && OrderStopLoss() > OrderOpenPrice()-SLOffset*Point)
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-SLOffset*Point,OrderTakeProfit(),OrderExpiration());
 
 
 // Continue only once every new candle
   static datetime lastCandle = 0;

   if(CurTime()!=Time[0] || Time[0]==lastCandle)
      return(0);

   lastCandle = Time[0];
   

//   Print(iTime(NULL,0,0),"  Low: ",iLow(NULL,PERIOD_M5,0),"High: ",iHigh(NULL,PERIOD_M5,0));
   
 // close orders at specified endofday
   if(CloseTime != -1)
   {
      if(IsOrderOpen(ticketL1) && (Hour() == CloseTime || Day() > TimeDay(OrderOpenTime())))
         OrderClose(ticketL1, LotSize, Bid, Slippage);
      if(IsOrderOpen(ticketL2) && (Hour() == CloseTime || Day() > TimeDay(OrderOpenTime())))
         OrderClose(ticketL2, LotSize, Bid, Slippage);
      if(IsOrderOpen(ticketL3) && (Hour() == CloseTime || Day() > TimeDay(OrderOpenTime())))
         OrderClose(ticketL3, LotSize, Bid, Slippage);                 

      if(IsOrderOpen(ticketS1) && (Hour() == CloseTime || Day() > TimeDay(OrderOpenTime())))
         OrderClose(ticketS1, LotSize, Ask, Slippage);
      if(IsOrderOpen(ticketS2) && (Hour() == CloseTime || Day() > TimeDay(OrderOpenTime())))
         OrderClose(ticketS2, LotSize, Ask, Slippage);
      if(IsOrderOpen(ticketS3) && (Hour() == CloseTime || Day() > TimeDay(OrderOpenTime())))
         OrderClose(ticketS3, LotSize, Ask, Slippage);
   }

 // setup new pending orders
 // will be changing this to not just execute at beginning of day.
 
 
//   if(TimeHour(CurTime())==StartTime && TimeMinute(CurTime())==0)
   if(TimeHour(CurTime())==StartTime)
   {
      inittime=CurTime();
      
   // on first bar of day calculate all price levels
      BasePrice = iClose(NULL,PERIOD_H1,StartTime-BaseTime+1);
      BuyPrice = BasePrice + (BuyOffset * Point);
      SellPrice = BasePrice - (SellOffset * Point);
 
   // SL and TP targets
      buy_sl = BuyPrice - BuySL*Point;
      sell_sl= SellPrice + SellSL*Point;
             
      buy_tp1 = BuyPrice + TP1*Point;
      buy_tp2 = BuyPrice + TP2*Point;
      buy_tp3 = BuyPrice + TP3*Point;                          
      sell_tp1 = SellPrice - TP1*Point;
      sell_tp2 = SellPrice - TP2*Point;
      sell_tp3 = SellPrice - TP3*Point;                          
      
      // Don't use stoploss?
      if(BuySL<=0)
      {
         buy_sl = 0;
      }
      if(SellSL<=0)
      {
         sell_sl = 0;
      }

      // Don't use takeprofit?
      if(TP1 <= 0)
      {
         buy_tp1 = 0;
         sell_tp1= 0;
      }
      if(TP2 <= 0)
      {
         buy_tp2 = 0;
         sell_tp2= 0;
      }
      if(TP3 <= 0)
      {
         buy_tp3 = 0;
         sell_tp3= 0;
      }            
   }
//if we need to open the tickets open them

//double SMAChange=iMA(NULL,0,3300,0,MODE_SMA,PRICE_MEDIAN,0)-iMA(NULL,0,3300,0,MODE_SMA,PRICE_MEDIAN,1);

   if(BasePrice != 0 && TimeHour(CurTime()) >= StartTime && TimeHour(CurTime())<= 22)
   {
      if(IsOrderClosed(ticketL3))
      {
         if((IsLoss(ticketL3) && CurTime()>=(OrderCloseTime()+(60*(120-TimeMinute(OrderCloseTime()))))) || !IsLoss(ticketL3))
         {
         ticketL1 = OrderSend(Symbol(), OP_BUYSTOP, LotSize, BuyPrice, Slippage, buy_sl, buy_tp1, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Blue);
//         if(ticketL1<0 && (Ask-BuyPrice)<=5*Point && (Ask-BuyPrice)>=0) ticketL1 = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, buy_sl, Ask+TP1*Point, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Blue);

         ticketL2 = OrderSend(Symbol(), OP_BUYSTOP, LotSize, BuyPrice, Slippage, buy_sl, buy_tp2, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Blue);
//         if(ticketL2<0 && (Ask-BuyPrice)<=5*Point && (Ask-BuyPrice)>=0) ticketL2 = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, buy_sl, Ask+TP2*Point, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Blue);

         ticketL3 = OrderSend(Symbol(), OP_BUYSTOP, LotSize, BuyPrice, Slippage, buy_sl, buy_tp3, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Blue);
//         if(ticketL3<0 && (Ask-BuyPrice)<=5*Point && (Ask-BuyPrice)>=0) ticketL3 = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, buy_sl, Ask+TP3*Point, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Blue);
         }
 
      }
      
      if(IsOrderClosed(ticketS3))
      {
         if((IsLoss(ticketS3) && CurTime()>=(OrderCloseTime()+(60*(120-TimeMinute(OrderCloseTime()))))) || !IsLoss(ticketS3))
         {
            ticketS1 = OrderSend(Symbol(), OP_SELLSTOP,LotSize, SellPrice, Slippage, sell_sl, sell_tp1, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Red);
//            if(ticketS1<0 && (SellPrice-Bid)<=5*Point && (SellPrice-Bid)>=0) ticketS1 = OrderSend(Symbol(), OP_SELL,LotSize, Bid, Slippage, sell_sl, Bid-TP1*Point, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Red); 


            ticketS2 = OrderSend(Symbol(), OP_SELLSTOP,LotSize, SellPrice, Slippage, sell_sl, sell_tp2, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Red);
//            if(ticketS2<0 && (SellPrice-Bid)<=5*Point && (SellPrice-Bid)>=0) ticketS2 = OrderSend(Symbol(), OP_SELL,LotSize, Bid, Slippage, sell_sl, Bid-TP2*Point, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Red); 

            ticketS3 = OrderSend(Symbol(), OP_SELLSTOP,LotSize, SellPrice, Slippage, sell_sl, sell_tp3, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Red);            
//            if(ticketS3<0 && (SellPrice-Bid)<=5*Point && (SellPrice-Bid)>=0) ticketS3 = OrderSend(Symbol(), OP_SELL,LotSize, Bid, Slippage, sell_sl, Bid-TP3*Point, "GBP_Breakout EA", 0, inittime+60*PERIOD_H1*(CloseTime-StartTime), Red);             
         }
      }
   }

// end of program
   return(0);
  }
//+------------------------------------------------------------------+ 