This script functions as a specialized "Time Gatekeeper" for automated trading. Its primary purpose is to ensure that your trading robot only opens new positions during specific, pre-defined windows of the day, effectively preventing the system from trading during low-volume periods or volatile sessions that you might wish to avoid.
Core Logic Overview
The tool operates on a simple "Permission" logic: by default, it assumes trading is prohibited. It then checks the current time against three distinct market sessions (Asian, European, and New York). If the current time falls within any of your authorized windows, it "unlocks" the gate to allow a trade to proceed.
Key Components of the Logic
-
Time Zone Synchronization: Because different trading brokers operate in different time zones, the script includes a GMT Offset feature. This allows you to align the script's internal clock with your specific broker’s server time, ensuring that the hours you set correspond accurately to the actual market movements.
-
Customizable Market Windows: The logic is divided into three independent modules. You can choose to enable or disable each one individually:
- Asian Market: Includes specialized logic to handle "overnight" sessions (times that cross the midnight boundary).
- European Market: A standard window for mid-day trading.
- New York Market: A standard window for late-day trading.
-
Independent Filtering: The script evaluates these windows sequentially. If any of the enabled sessions are currently active, the system grants permission to trade. If none of the active conditions are met, the script triggers a "Stop" command, preventing any new trades from being initiated until the next valid window opens.
How it Integrates with your Trading
This script is designed as a filter rather than a standalone robot. It sits at the very beginning of your trading process. Before your main strategy can execute an order, it "asks" this script if the current time is allowed. If the answer is "No," the trade is blocked immediately. This ensures that even if your primary strategy generates a signal during an off-hour, no action will be taken until your designated trading times arrive.
//+------------------------------------------------------------------+
//| 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);
}
Comments