TradingTimes

Download script
Author: Robert Hill
TradingTimes

This script is designed as a sophisticated time-management utility for MetaTrader 4, specifically built to regulate when automated trades are allowed to execute based on global market sessions. It acts as a gatekeeper, ensuring that your trading activity aligns with specific times of the day rather than running continuously.

Core Functionality

The primary purpose of this tool is to filter trading opportunities by geographic market regions. It allows users to define specific "active windows" for three major financial hubs: Asia, Europe, and New York. By setting these parameters, you can ensure your strategy only interacts with the markets during their peak liquidity hours or avoids volatile periods that may not suit your trading style.

How the Logic Works

The script operates through a multi-layered verification process every time it runs:

1. Active Order Monitoring Before making any decisions about timing, the script first scans your current account to see if there are any open positions associated with this specific tool (identified by a unique Magic ID).

  • If you already have active trades, the script moves into "management mode" (allowing for potential modification or closing of those trades).
  • If there are no active trades, it proceeds to evaluate whether a new trade is permitted to open.

2. Time Session Validation If the "Trading Hours Filter" is enabled, the script converts the current chart time into a standardized format to compare against your predefined settings. It checks the current time against the specific start and end points for each market you have activated:

  • Asia Market: Checks if the current time falls within the designated Asian window.
  • Europe Market: If the Asian window is closed (or not used), it checks the European window.
  • New York Market: If previous windows are also closed, it checks the New York window.

3. Dynamic Feedback System The script provides real-time communication on your trading screen:

  • If the current time falls outside of your allowed windows, it displays a notification stating that trading has been stopped due to the "wrong time of day."
  • If the current time is within an approved window, it confirms that trading has resumed.
  • If the filter is turned off entirely, it informs you that no time restrictions are being applied.

User Control and Flexibility

The logic is built to be highly customizable. You can independently toggle each market (Asia, Europe, or New York) on or off. Furthermore, because the script handles "wraparound" times (e.g., a session that starts at 10:00 PM and ends at 6:00 AM), it intelligently calculates whether the current time is valid even when the session crosses over midnight.

Orders Execution
Checks for the total of open orders
10 Views
0 Downloads
0 Favorites
TradingTimes
//+------------------------------------------------------------------+
//|                                                 TradingTimes.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Robert Hill"


extern int MagicID = 12345;

extern string  sm0="--Trading Hours Filter--";
extern string  sm1=" Times are your chart times";
extern string  sm2="UseTradingHours - Enter 0 for false, 1 for true";
extern int    UseTradingHours = 0;
extern string  sm4="TradeAsian - Enter 0 for false, 1 for true";
extern int    TradeAsianMarket = 1;
extern int     AsianStart = 2200;       // Start trades after time
extern int     AsianStop = 100;      // Stop trading after time
extern string  sm5="Trade Europe - Enter 0 for false, 1 for true";
extern int    TradeEuropeanMarket = 1;
extern int     EurStart = 700;       // Start trades after time
extern int     EurStop = 900;      // Stop trading after time
extern string  sm6="Trade NY - Enter 0 for false, 1 for true";
extern int    TradeNewYorkMarket = 0;
extern int     NYStart = 1300;       // Start trades after time
extern int     NYStop = 1500;      // 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 StartHour, int EndHour, int ct)
{
  if(StartHour<=EndHour)
  {
    if(ct < StartHour || ct > EndHour) return(true) ;
  }
  else
  {
    if(ct > EndHour && ct < StartHour) return(true) ;
  }
  return(false) ;
}

bool CheckTradingTimes()
{
   bool StopTrading;
   int ct;
   ct = Hour() * 100 + Minute();
   
     StopTrading = true;
// Check trading Asian Market
     if (TradeAsianMarket == 1)
     {
        StopTrading = CheckOutsideSession(AsianStart, AsianStop, ct);
     }
     if (StopTrading == true)
     {
// Check trading European Market
       if (TradeEuropeanMarket == 1)
       {
         StopTrading = CheckOutsideSession(EurStart, EurStop, ct);
       }
     }
     if (StopTrading == true)
     {
// Check trading New York Market
       if (TradeNewYorkMarket == 1)
       {
         StopTrading = CheckOutsideSession(NYStart, NYStop, 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);
  }




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




Comments