TradingTimesFiveSessions





//+------------------------------------------------------------------+
//|                                                 TradingTimes.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Robert Hill"


extern int MagicID = 12345;

extern string  sm0="--Trading Hours Filter--";
extern string  sm2="UseTradingHours - Enter 0 for false, 1 for true";
extern int    UseTradingHours = 0;
extern string  sm3="Trade Session 1 - Enter 0 for false, 1 for true";
extern int    TradeSession1 = 1;
extern int     Session1Start = 2200;       // Start trades after time
extern int     Session1Stop = 100;      // Stop trading after time
extern string  sm4="Trade Session 2 - Enter 0 for false, 1 for true";
extern int    TradeSession2 = 1;
extern int     Session2Start = 400;       // Start trades after time
extern int     Session2Stop = 600;      // Stop trading after time
extern string  sm5="Trade Session 3 - Enter 0 for false, 1 for true";
extern int    TradeSession3 = 0;
extern int     Session3Start = 700;       // Start trades after time
extern int     Session3Stop = 900;      // Stop trading after time
extern string  sm6="Trade Session 4 - Enter 0 for false, 1 for true";
extern int    TradeSession4 = 0;
extern int     Session4Start = 1330;       // Start trades after time
extern int     Session4Stop = 1500;      // Stop trading after time
extern string  sm7="Trade Session 5 - Enter 0 for false, 1 for true";
extern int    TradeSession5 = 0;
extern int     Session5Start = 1700;       // Start trades after time
extern int     Session5Stop = 1900;      // Stop trading after time


bool     YesStop;


int init()
{
   return(0);
}

int deinit()
{
   return(0);
}
  
//////////////////////////////////////////////////////////////////////
int start()
  {
   int NumOrders = 0;
   
   NumOrders = CalculateCurrentOrders();
   if(NumOrders == 0)
   {
      YesStop = false;
      
      if (UseTradingHours == 1)
      {
        YesStop = CheckTradingTimes();
   
        if (YesStop == true)
        {
          Comment ("Trading has been stopped - wrong time of day");
        }
        else
        {
          Comment ("Trading has resumed - time is OK");
        }
      }
      else
          Comment ("Trading Time Filter not in use");

      if (YesStop == false)
      {

// Add code here to place trades
      
      }
   }
   else
   {
// Add code here to modify or close trades
   }
   return(0);
}

//+------------------------------------------------------------------+


// Return true if time is not in session for trading
bool CheckOutsideSession(int SessionStart, int SessionStop, int ct)
{

   if (SessionStart < SessionStop)
   {
     if (ct < SessionStart || ct > SessionStop) return (true);
   }
   else
   {
      if (ct > SessionStop && ct < SessionStart) return (true);
   }
   return(false);}

bool CheckTradingTimes()
{
   bool StopTrading;
   int ct;
   ct = Hour() * 100 + Minute();
   
     StopTrading = true;

     if (TradeSession1 == 1)
     {
        StopTrading = CheckOutsideSession(Session1Start, Session1Stop, ct);
     }
     if (StopTrading == true)
     {

       if (TradeSession2 == 1)
       {
         StopTrading = CheckOutsideSession(Session2Start, Session2Stop, ct);
       }
     }
     if (StopTrading == true)
     {

       if (TradeSession3 == 1)
       {
         StopTrading = CheckOutsideSession(Session3Start, Session3Stop, ct);
       }
     }
     if (StopTrading == true)
     {

       if (TradeSession4 == 1)
       {
         StopTrading = CheckOutsideSession(Session4Start, Session4Stop, ct);
       }
     }
     if (StopTrading == true)
     {

       if (TradeSession5 == 1)
       {
         StopTrading = CheckOutsideSession(Session5Start, Session5Stop, ct);
       }
     }
     
     return(StopTrading);
}

int CalculateCurrentOrders()
  {
   int buys = 0, sells = 0, num = 0;
   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!= Symbol()) continue;
      if (OrderMagicNumber()!= MagicID) continue;
      if(OrderType() == OP_BUY)  buys++;
      if(OrderType() == OP_SELL) sells++;
     }
     
   num = buys + sells;
   return(num);
  }




//+------------------------------------------------------------------+








Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders


Other Features: