//+------------------------------------------------------------------+
//|                                                     EA Ranges.mq4|
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "KMK"


#include <stderror.mqh>
#include <stdlib.mqh>

extern int MagicNumber =1234;
extern double LotSize = 0.01;
extern string R1Start = "2009.06.04 13:00";
extern string R1End = "2009.06.04 20:00";
extern string R2Start = "2009.06.04 21:00";
extern string R2End = "2009.06.05 04:00";
extern int R2MaximumRange = 60;
extern int MaximumExpantion = 5;
extern string OrderCancelationTime = "12:45";
extern string TakeProfitTime = "13:00";
extern int TakeProfit = 120;
extern int OrderPlacementFromLine = 5;
extern int StopPlacementFromLine = 5;
extern bool DrawLines = true;

double takeprofit, R2Top, R2Bot, R1Bot, R1Top;

int trade, tradestoday=0, lasttd;
int TradesPerDay = 1;

//string R1Start;
//string R1End;
//string R2Start;
//string R2End;
//string mydate;
//string lastdate;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      //lastdate = TimeToStr(iTime(NULL,PERIOD_D1,1),TIME_DATE);
      //mydate = TimeToStr(iTime(NULL,PERIOD_D1,0),TIME_DATE);
      
      //R1Start = StringConcatenate(lastdate, " ", R1Start_);
      //R1End = StringConcatenate(lastdate, " ", R1End_);
      //R2Start = StringConcatenate(lastdate, " ", R2Start_);
      //R2End = StringConcatenate(mydate, " ", R2End_);
      
      if(TimeDay(TimeCurrent()) !=  lasttd)tradestoday = 0;

      R1Top = iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,iBarShift(NULL,0,StrToTime(R1Start))-iBarShift(NULL,0,StrToTime(R1End))+1,iBarShift(NULL,0,StrToTime(R1End))));
      R2Top = iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,iBarShift(NULL,0,StrToTime(R2Start))-iBarShift(NULL,0,StrToTime(R2End))+1,iBarShift(NULL,0,StrToTime(R2End))));
      R1Bot = iLow(NULL,0,iLowest(NULL,0,MODE_LOW,iBarShift(NULL,0,StrToTime(R1Start))-iBarShift(NULL,0,StrToTime(R1End))+1,iBarShift(NULL,0,StrToTime(R1End))));
      R2Bot = iLow(NULL,0,iLowest(NULL,0,MODE_LOW,iBarShift(NULL,0,StrToTime(R2Start))-iBarShift(NULL,0,StrToTime(R2End))+1,iBarShift(NULL,0,StrToTime(R2End))));
   
      Comment("R1Top ",R1Top,"\n",
      "R1Low ",R1Bot,"\n",
      "R2Top ",R2Top,"\n",
      "R2Low ",R2Bot);
      
      /*Comment("R1Top ",R1Start,"\n",
      "R1Low ",R1End,"\n",
      "R2Top ",R2Start,"\n",
      "R2Low ",R2End);*/
      
      if(R2Top <= R1Top+MaximumExpantion*Point && R2Bot >= R1Bot-MaximumExpantion*Point)
      {
         if(R2Top - R2Bot <= R2MaximumRange*Point && tradestoday < TradesPerDay && TimeCurrent() > StrToTime(R2End))
         {
            if(Open[iBarShift(NULL,0,StrToTime(R1Start))] > Close[iBarShift(NULL,0,StrToTime(R1End))])OpenSellOrder();
            if(Open[iBarShift(NULL,0,StrToTime(R1Start))] < Close[iBarShift(NULL,0,StrToTime(R1End))])OpenBuyOrder();
         }
      }
      
      TrailStops();
//----
   return(0);
  }
//+------------------------------------------------------------------+
void OpenBuyOrder()
{
     int ticket;
     takeprofit = R2Top+OrderPlacementFromLine*Point+TakeProfit*Point;
     if(TakeProfit <= MarketInfo(Symbol(),MODE_STOPLEVEL))takeprofit=0;
     ticket=OrderSend(Symbol(),OP_BUYSTOP,LotSize,R2Top+OrderPlacementFromLine*Point+MarketInfo(Symbol(),MODE_SPREAD)*Point,0,R2Bot-StopPlacementFromLine*Point,takeprofit,0,MagicNumber,0,Green);
     if(ticket<0) { Print("Failed to open Buy with error :",ErrorDescription(GetLastError())); }
     else {trade = Bars;tradestoday++;lasttd = TimeDay(TimeCurrent());}
      if(DrawLines == true && (ObjectFind("R2Top"+TimeDay(TimeCurrent()))<0 || ObjectFind("R2Bot"+TimeDay(TimeCurrent()))<0))
      {
         ObjectCreate("R2Top"+TimeDay(TimeCurrent()),OBJ_HLINE,0,0,R2Top);
         ObjectCreate("R2Bot"+TimeDay(TimeCurrent()),OBJ_HLINE,0,0,R2Bot);
      }
}
//+------------------------------------------------------------------+
void OpenSellOrder()
{
     int ticket;
     takeprofit = R2Bot-OrderPlacementFromLine*Point-TakeProfit*Point;
     if(TakeProfit <= MarketInfo(Symbol(),MODE_STOPLEVEL))takeprofit=0;
     ticket=OrderSend(Symbol(),OP_SELLSTOP,LotSize,R2Bot-OrderPlacementFromLine*Point,0,R2Top+StopPlacementFromLine*Point+MarketInfo(Symbol(),MODE_SPREAD)*Point,takeprofit,0,MagicNumber,0,Green);
     if(ticket<0) { Print("Failed to open Sell with error :",ErrorDescription(GetLastError())); }
     else {trade = Bars;tradestoday++;lasttd = TimeDay(TimeCurrent());}
      if(DrawLines == true && (ObjectFind("R2Top"+TimeDay(TimeCurrent()))<0 || ObjectFind("R2Bot"+TimeDay(TimeCurrent()))<0))
      {
         ObjectCreate("R2Top"+TimeDay(TimeCurrent()),OBJ_HLINE,0,0,R2Top);
         ObjectCreate("R2Bot"+TimeDay(TimeCurrent()),OBJ_HLINE,0,0,R2Bot);
      }
}
//+------------------------------------------------------------------+
void TrailStops()
{
//----
   for(int stp=0;stp<OrdersTotal();stp++)
   {   
      if(OrderSelect(stp,SELECT_BY_POS, MODE_TRADES)==false) continue;
      if(OrderMagicNumber() == MagicNumber)
      {
         if(OrderType() > 1 && TimeCurrent() > StrToTime(OrderCancelationTime))OrderDelete(OrderTicket());
         if(OrderType() == 0 && TimeCurrent() > StrToTime(TakeProfitTime))OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
         if(OrderType() == 1 && TimeCurrent() > StrToTime(TakeProfitTime))OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
      }
   } 
   return(0);
//----
}
//+------------------------------------------------------------------+

