//+------------------------------------------------------------------+
//|                                   Opening Range Trader V0_01.mq4 |
//|                                    Copyright © 2006, Bundyraider |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Bundyraider"
#property link      ""

//---- input parameters
extern int  BeginTime=6;
extern int  EndTime=8;
extern int       PipsAbove=10;
extern int       PipsBelow=10;
extern int       TakeProfit=20;
extern int       StopLoss=20;
extern double    LotSize=0.1;
extern int       TrailingStop=20;
extern int       Breakeven=20;
extern int       MaxRange=40;
extern int  StopTime=14;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   //------------------------------------------------------------------------
   // Creat Global variables to remember some of our info between "ticks"
   //------------------------------------------------------------------------
   
   GlobalVariableSet("OpeningRange",0);
   GlobalVariableSet("LowestPriceOfRange" , 0);
   GlobalVariableSet("HighestPriceOfRange", 0);
   GlobalVariableSet("RangeTimeComplete",FALSE);
   GlobalVariableSet("WeAreInATrade",FALSE); 
   GlobalVariableSet("TicketNumber" ,0);

  
//----
   
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function (executed every tick)                                         |
//+------------------------------------------------------------------+
int start()
  {
   //-------------------------------------------------------------
   //    Declare local variables
   //-------------------------------------------------------------
   int      BeginHour,BeginMinute,EndHour,EndMinute,StopHour,StopMinute, NumberOfBarsInRange;
   double   LowestPriceOfRange, HighestPriceOfRange, OpeningRange ,EMA200, PreviousEMA200;
   bool     RangeTimeComplete,WeAreInATrade;
  
   //-------------------------------------------------------------
   //    Reload GlobalVariable into Local ones
   //-------------------------------------------------------------
   
   OpeningRange         =GlobalVariableGet("OpeningRange");
   LowestPriceOfRange   =GlobalVariableGet("LowestPriceOfRange" );
   HighestPriceOfRange  =GlobalVariableGet("HighestPriceOfRange");
   RangeTimeComplete    =GlobalVariableGet("RangeTimeComplete");
   WeAreInATrade        =GlobalVariableGet("WeAreInATrade"); 
   
   //-------------------------------------------------------------
   //    Calculate the basic variables we have inputs for...
   //-------------------------------------------------------------
   BeginHour   =BeginTime;
   BeginMinute =0;
   EndHour     =EndTime;
   EndMinute   =0;
   
   NumberOfBarsInRange=((EndTime-BeginTime)*60)/Period();

   StopHour    =StopTime;
   StopMinute  =0;
   
   //#######################################################################################
   //    Let's check for range completion (time) and calculate that range if so.
   //#######################################################################################
 
   if(TimeHour(Time[0])==EndHour && TimeMinute(Time[0])==EndMinute && RangeTimeComplete==FALSE)
     {
      RangeTimeComplete=TRUE;

      LowestPriceOfRange   =Low  [Lowest  (NULL,0,MODE_LOW  ,NumberOfBarsInRange ,0)];
      HighestPriceOfRange  =High [Highest (NULL,0,MODE_HIGH ,NumberOfBarsInRange ,0)];
      OpeningRange         =HighestPriceOfRange-LowestPriceOfRange;
      
      // We need the EA to remember all this, so save as global variables
      
      GlobalVariableSet("OpeningRange",OpeningRange);
      GlobalVariableSet("LowestPriceOfRange" , LowestPriceOfRange);
      GlobalVariableSet("HighestPriceOfRange", HighestPriceOfRange);
      GlobalVariableSet("RangeTimeComplete",TRUE); 
     }   

   //-----------------------------------------------------------------------------
   //    If we have hit our stop time then set RangeTimeComplete to FALSE to 
   //    prevent any new trades till next range end.
   //-----------------------------------------------------------------------------

   if(TimeHour(Time[0])==StopHour && TimeMinute(Time[0])==StopMinute && RangeTimeComplete==TRUE)
     {
      RangeTimeComplete=FALSE;
      GlobalVariableSet("RangeTimeComplete",FALSE); 
      WeAreInATrade=FALSE;
      GlobalVariableSet("WeAreInATrade",FALSE);
     }  
     
   //#########################################################################################
   //     OK.  If we have our range now, then watch for trading opportunities.
   //     BUT, only if range is within MaxRange
   //#########################################################################################
   if(OpeningRange>MaxRange*Point)
     {
     // Print("Out OF RANGE!!!!!!");
     }
   if(RangeTimeComplete==TRUE && OpeningRange<=(MaxRange*Point) && WeAreInATrade==FALSE)
     {
      //-------------------------------------------------------------
      //       Check for BUY...
      //-------------------------------------------------------------
      if(Close[0]>HighestPriceOfRange+(PipsAbove*Point))
        {
         int ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,5,HighestPriceOfRange-StopLoss*Point,HighestPriceOfRange+TakeProfit*Point,"Opening-Range Trade BUY",16384,0,Yellow);
         if(ticket>0)
            {
             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
               {
               Print("BUY order opened : ",OrderOpenPrice());
               WeAreInATrade=TRUE;
               GlobalVariableSet("WeAreInATrade",TRUE);     //...To prevent multiple entries per day.
               GlobalVariableSet("TicketNumber" ,ticket);   //...Remember ticket number for manageing trade later on.
                                                            //   This will help with Break even, trailing stop coed etc
               }
            }
            else Print("Error opening BUY order : ",GetLastError()); 
            return(0); 
        }
        
        
      //-------------------------------------------------------------
      //       Check for SELL...
      //-------------------------------------------------------------
      //  

      if(Close[0]<LowestPriceOfRange-(PipsBelow*Point))
        {
         ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,5,LowestPriceOfRange+StopLoss*Point,LowestPriceOfRange-TakeProfit*Point,"Opening-Range Trade SELL",16384,0,Yellow);
         if(ticket>0)
            {
             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
               {
               Print("SELL order opened : ",OrderOpenPrice());
               WeAreInATrade=TRUE;
               GlobalVariableSet("WeAreInATrade",TRUE);     //...To prevent multiple entries per day.
               GlobalVariableSet("TicketNumber" ,ticket);   //...Remember ticket number for manageing trade later on.
                                                            //   This will help with Break even, trailing stop coed etc
               }
            }
            else Print("Error opening SELL order : ",GetLastError()); 
            return(0); 
        }
       

     }

   //#########################################################################################
   //     ...End OF Trading opportunities   
   //     Now Let's manage any open trade...
   //#########################################################################################
 
   if(WeAreInATrade==TRUE)
      {
      
      } 
 
 
 
   
  
   //#########################################################################################
   //------------------------------------------------------------------------
   //   Per Tick routine all done and we're out of here (till next tick)
   //------------------------------------------------------------------------
   return(0);
  }
//+------------------------------------------------------------------+