//+------------------------------------------------------------------+
//|                                 TradingTimesFilter_EA.mq4        |
//|                                              Copyright © 2007    |
//|                                                                  |
//|    Written by Robert Hill aka MrPip for yahoo group              |
//|                                                                  |
//|  Currently allow 3 trading sessions                              |
//|                                                                  |
//|  The code would need to be copied to any EA that you want        |
//|  to use a time to trade filter                                   |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Robert Hill"
#property link      "http://www.forex-tsd.com/"
#include <stdlib.mqh>


extern int     GMT_Offset = 0;
extern string  sm="--Trading Hours--";
extern bool    UseTradingHours = true;
extern bool    TradeAsianMarket = true;
extern int     StartHour1 = 0;       // Start trades after time
extern int     StopHour1 = 3;      // Stop trading after time
extern bool    TradeEuropeanMarket = true;
extern int     StartHour2 = 9;       // Start trades after time
extern int     StopHour2 = 11;      // Stop trading after time
extern bool    TradeNewYorkMarket = false;
extern int     StartHour3 = 15;       // Start trades after time
extern int     StopHour3 = 17;      // Stop trading after time

//+---------------------------------------------------+
//|General controls                                   |
//+---------------------------------------------------+
bool YesStop;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }


// This function uses 3 trading sessions based of start and end hours
// GMT Offset is used to allow the same hours to be used with different brokers
// The GMT_Offset is added to the hours entered as inputs
bool CheckTradingTimes()
{
   bool StopTrading;
   int offsetStartHour1, offsetStopHour1;
   int offsetStartHour2, offsetStopHour2;
   int offsetStartHour3, offsetStopHour3;
   
   offsetStartHour1 = StartHour1 + GMT_Offset;
   offsetStopHour1 = StopHour1 + GMT_Offset;
   offsetStartHour2 = StartHour2 + GMT_Offset;
   offsetStopHour2 = StopHour2 + GMT_Offset;
   offsetStartHour3 = StartHour3 + GMT_Offset;
   offsetStopHour3 = StopHour3 + GMT_Offset;
   
     StopTrading = true;
// Check trading Asian Market
     if (TradeAsianMarket)
     {
        if (offsetStartHour1 > 18)
        {
// Check broker that uses Asian open before 0:00

          if (Hour() >= offsetStartHour1) StopTrading = false;
          if (!StopTrading)
          {
            if (offsetStopHour1 < 24)
            {
              if ( Hour() <= offsetStopHour1) StopTrading = false;
            }
// These cannot be combined even though the code looks the same
            if (offsetStopHour1 >=0)
            {
              if ( Hour() <= offsetStopHour1) StopTrading = false;
            }
          }
        }
        else
        {
          if (Hour() >= offsetStartHour1 && Hour() <= offsetStopHour1) StopTrading = false;
        }
     }
     if (StopTrading)
     {
// Check trading European Market
       if (TradeEuropeanMarket)
       {
         if (Hour() >= offsetStartHour2 && Hour() <= offsetStopHour2) StopTrading = false;
       }
     }
     if (StopTrading)
     {
// Check trading New York Market
       if (TradeNewYorkMarket)
       {
         if (Hour() >= offsetStartHour3 && Hour() <= offsetStopHour3) StopTrading = false;
       }
     }
     
     return(StopTrading);
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   
     

// Make sure you have any code to handle open positions before the code to check trading hours
// Otherwise trades will only be closed or modified during the hours you input!!

// You need to place the following code before any code that opens new trades

//+------------------------------------------------------------------+
//| Check if OK to make new trades                                   |
//+------------------------------------------------------------------+



   YesStop = false;
   if (UseTradingHours) YesStop = CheckTradingTimes();
   
   if (YesStop)
   {
//      Comment ("Trading has been stopped as requested - wrong time of day");
       return (0);
   }
     
//----
   return(0);
  }





