MQL4 Limit Trading Time to Sessions
Introduction
This is multi period allowance trade time filter. It’s usable when you only want to trade in Asian Session, and/or London session, and/or New York Session.
Parameters
AutoGMTOffset
Bool: Indicate if you want auto GMT offset turned on or off. Turn off during back testing.
Manual GMT Offset
Double: If AutoGMTOffset is turned off, indicate what your manual GMT offset would be.
UseTradingHours
Bool: Whether or not to use the time filter. It is defaulted as off.
TradeAsianMarket
Bool: Whether or not to trade Asian session.
StartTime1
Double: Don’t Trade before this time. Defaulted at 21:00, the start of Asian Session, if you include Australia. Sydney starts at 21.00, and Tokyo starts at 23.00 GMT.
EndTime1
Double: Don’t Trade after this time. Defaulted at 07.00, the start of the European Session. Note: The Tokyo session continues on for 1 more hour to end at 08.00, so if you are interested in trading this session in its entirety, you should indicate from 23:00 to 08.00.
TradeEuropeanMarket
Bool: Whether or not to trade European session.
StartTime2
Double: Don’t Trade before this time. Defaulted at 7.00 GMT, the start of the London Session, though Germany does not open till 08.00.
EndTime2
Double: Don’t Trade after this time. Defaulted at 12.00 GMT, the start of the New York Session. Note: The European Session continues till 16.00, so if you are interested in trading this session in entirety, you should have your defaults from 7.00 to 16.00.
TradeNewYorkMarket
Bool: Whether or not to trade the New York session.
StartTime3
Double: Don’t Trade before this time. Defaulted at 12:00, the start of the New York Session in GMT, which is 8:00 EST. Note that the NY stock exchange does not open till 9:30 EST, or 13.30 GMT, and the first hour and half (from 9:30 to 11:00 EST, or 13:30 to 15:00 GMT) is heavy trading, lots of liquidity.
EndTime3
Double: Don’t Trade after this time. Defaulted at 21.00, the end of the New York session, the closing bell in New York.
MT4 Code Snippets
Paste this code near top of source file
void GetSystemTime(int& a0[]);
Paste this code in define variables section
extern bool AutoGMTOffset = TRUE;
extern double ManualGMTOffset = 0;
extern bool UseTradingHours = true;
extern bool TradeAsianMarket = true;
extern double StartTime1 = 22.00;
extern double EndTime1 = 07.00;
extern bool TradeEuropeanMarket = true;
extern double StartTime2 = 07.00;
extern double EndTime2 = 12.00;
extern bool TradeNewYorkMarket = true;
extern double StartTime3 = 12.00; // 8:00 EST
extern double EndTime3 = 17.00;
int gmtoffset;
string gs_548 = “”;
Paste this code after start() function
else gmtoffset = ManualGMTOffset;
string ls_52 = “Your Strategy is Running.”;
string ls_60 = “Your Strategy is set up for time zone GMT ” + gmtoffset;
string ls_76 = “Account Balance= ” + DoubleToStr(AccountBalance(), 2);
string ls_84 = ” “;
Comment(“\n”,
“\n”, ” “,
“\n”, ” “,
“\n”, ” “, ls_52,
“\n”, ” “, ls_60,
“\n”, ” “, ls_76,
// “\n”, ” “, ls_77,
“\n”);
Paste this code by itself, at end of source file, outside of start() function
{
if (!IsTesting() && AutoGMTOffset == TRUE) gmtoffset = GMTOffset();
else gmtoffset = ManualGMTOffset;
int TradingTime=0;
int CurrentHour=Hour(); // Server time in hours
double CurrentMinute =Minute(); // Server time in minutes
double CurrentTime=CurrentHour + CurrentMinute/100; // Current time
double CurrentTime1 = CurrentTime + gmtoffset;
if (CurrentTime1==0) CurrentTime=00;
if (CurrentTime1<0) CurrentTime1 = CurrentTime1 + 24;
if (CurrentTime1 >= 24) CurrentTime1 = CurrentTime1 – 24;
if (!DaytoTrade()) return(false);
if (UseTradingHours==true)
{
if (TradeAsianMarket==true)
{
if(StartTime1 < endtime1){
if(CurrentTime1 >= StartTime1 && CurrentTime1 <= EndTime1)
TradingTime=1;}
if(StartTime1 > EndTime1){
if(CurrentTime1 >= StartTime1 || CurrentTime1 <= EndTime1)
TradingTime=1;}
}
if (TradeEuropeanMarket==true)
{
if(StartTime2 < endtime2){
if(CurrentTime1 >= StartTime2 && CurrentTime1 <= EndTime2)
TradingTime=1;}
if(StartTime2 > EndTime2){
if(CurrentTime1 >= StartTime2 || CurrentTime1 <= EndTime2)
TradingTime=1;}
}
if (TradeNewYorkMarket==true)
{
if(StartTime3 < endtime3){
if(CurrentTime1 >= StartTime3 && CurrentTime1 <= EndTime3)
TradingTime=1;}
if(StartTime3 > EndTime3){
if(CurrentTime1 >= StartTime3 || CurrentTime1 <= EndTime3)
TradingTime=1;}
}
}
else
TradingTime=1;
return(TradingTime);
}
Lastly, insert function TradeTime() someplace in code as part of entry condition
Example:
OpenBuy=true;