TradingTimesFiveSessions
Download scriptThis script is designed to serve as a gatekeeper for automated trading, specifically managing when your trades are allowed to open based on the time of day. It acts as a "Trading Hours Filter" that ensures your automation only operates during periods you have deemed active and safe.
Core Functionality
The primary purpose of this script is to check the current market time against a set of pre-defined "Trading Sessions." If the current time falls outside of these permitted windows, the script will prevent any new trades from being opened. It provides a clear visual status on your chart, informing you whether trading is currently active or restricted due to the time.
How the Logic Works
The script follows a structured sequence to decide if it should allow a trade:
1. Order Monitoring Before checking any rules, the script first looks at your account to see if there are already open trades associated with this specific tool (identified by a unique "Magic Number").
- If you already have active trades, the script remains passive to avoid interfering with them.
- If there are no active trades, it proceeds to check the time filters to decide if a new one can be started.
2. Time Session Management The script allows for up to five different trading sessions. Each session can be turned on or off individually. You can define specific start and end times for these windows (for example, "Session 1" might be a late-night window, while "Session 2" is a morning window).
3. The Filtering Process The script evaluates the current market time against your enabled sessions using a "restrictive" logic:
- It checks the first active session. If the current time is outside that window, it flags the status as "Stop."
- If the status is flagged as "Stop," it moves on to check the next active session.
- If the market time remains outside of all enabled sessions, the script confirms that trading should be paused.
4. User Feedback To keep you informed without needing to read complex data, the script displays a real-time comment on your trading chart:
- "Trading has been stopped - wrong time of day": This appears when the current time is outside your allowed windows.
- "Trading has resumed - time is OK": This appears when the market time aligns with your permitted sessions.
- "Trading Time Filter not in use": This appears if you have disabled the overall trading hours feature.
Summary for the User
Think of this script as a programmable "On/Off" switch for your trading robot. Instead of your robot trading 24/7, you can use this to ensure it only works during high-volume sessions (like the London or New York opens) or stays completely offline during volatile periods like the late-night markets.
//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+
Comments