Hi guys,
I want my EA to avoid taking any trade from 21:00 to 01:00 (when spreads are high).
Regards,
Saurabh Barhate
(P. S. - I am using MT5)
- Time filters, ETC.
- when I use backtest in mt5 , how to set symbol's spread to other number ?
- [ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5.
I found one thread about timefilter: Opening Positions only between 3 a.m and 9 a.m

Opening Positions only between 3 a.m and 9 a.m
- 2020.03.07
- www.mql5.com
I am learning MQL5, i have a small expert that buys with a tp and sl, i want the ea to buy only when the time is between input time 1 and input ti...
This is my solution... maybe you have some simpler ways
input bool timeFilter = true; //Time filter input int gmtOffset = 2; //GMT Offset (0 = London) input string timeStart = "14:00"; //Start time for trading input string timeEnd = "22:00"; //End time for trading MqlDateTime startTime, endTime;
int OnInit() { //--- Time filter if(timeFilter == true) { //--- Chck start time if(!String2Time(timeStart, startTime.hour, startTime.min)) { MessageBox("Error: Wrong input parameters for the start time!\nPleas fill in your start time in the hh:mm format.", "Nasty 100 v"+ VERSION +" | "+"#"+IntegerToString(INIT_PARAMETERS_INCORRECT), MB_OK|MB_ICONERROR); return(INIT_PARAMETERS_INCORRECT); } //--- Chck end time if(!String2Time(timeEnd, endTime.hour, endTime.min)) { MessageBox("Error: Wrong input parameters for the end time!\nPleas fill in your end time in the hh:mm format.", "Nasty 100 v"+ VERSION +" | "+"#"+IntegerToString(INIT_PARAMETERS_INCORRECT), MB_OK|MB_ICONERROR); return(INIT_PARAMETERS_INCORRECT); } } return(INIT_SUCCEEDED); }
void OnTick() { if(AllowTradesByTime() && IsNewCandle()) { CheckForSignal(); } }
bool AllowTradesByTime(void) { static const int minuteStart = startTime.hour*60 + startTime.min; //Filter start time (in minutes) static const int minuteEnd = endTime.hour*60 + endTime.min; //Filter end time (in minutes) static const bool overMidnight = minuteStart > minuteEnd ? true : false; //Allowed tarding time range over midnight if(minuteStart == minuteEnd) { return true; } //No time difference const datetime timeNow = TimeGMT() + gmtOffset * 3600; //Current selected gmt time const int minuteNow = TimeHour(timeNow)*60 + TimeMinute(timeNow); switch(overMidnight) { case false: //Allowed trading time doesn't cross midnight if(minuteNow >= minuteStart && minuteNow <= minuteEnd) { return true; } //Check if time now is in the allowed range return false; case true: //Allowed trading time is over midnight if(minuteNow < minuteStart && minuteNow > minuteEnd) { return false; } //Check if time now is in the allowed range return true; } return false; }
bool String2Time (const string _string, int &_hour, int &_min) { uchar array[]; StringToCharArray(_string, array); string hour = NULL; string min = NULL; int mode = 0; for(int i = 0; i < ArraySize(array); i++) { //--- Numbers 0 - 9 if(array[i] >= 48 && array[i] <= 57) { switch(mode) { case 0: if(StringLen(hour) <= 2) { hour += CharToString(array[i]); } //Add number to hour string break; case 1: if(StringLen(min) <= 2) { min += CharToString(array[i]); } //Add number to minute string break; } } //--- Seperators [, . : ;] else if(array[i] == 44 || array[i] == 46 || array[i] == 58 || array[i] == 59) //Change mode after seperator { mode = 1; } }//End for if(hour == "" && min == "") { return(false); } //Check that not both strings are empty _hour = (int)StringToInteger(hour); //Convert string to integer _min = (int)StringToInteger(min); //Convert string to integer if(_hour > 24 || _hour < 0) { return(false); } //Wrong input if(_min > 60 || _min < 0) { return(false); } //Wrong input if(_min == 60) { _min = 0; _hour++; } //Correct min if(_hour == 24) { _hour = 0; } //Correct hour return(true); }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register