if(str2.hour - str1.hour >= Time_to_Close) { Trade.PositionClose(Position.Ticket(), -1); Print ("Trade Closed by Max Time Allowed"); }
Surely hour can only be a number between 0 and 23? So the maximum difference will be 23.
input double Time_to_Close = 48;
It can never be 48.
Surely hour can only be a number between 0 and 23? So the maximum difference will be 23.
It can never be 48.
You are completely correct, I did not notice this at first, thanks Keith.
But in the backtest I ran it with Value = 2 hours and got the image result, where two trades exceeded that limit (I ran it for every tick)
You are completely correct, I did not notice this at first, thanks Keith.
But in the backtest I ran it with Value = 2 hours and got the image result, where two trades exceeded that limit (I ran it for every tick)
Sorry, I can't help you there.
I actually hate the MT5 tester compared to MT4 and have no idea what all the dots mean. I only use the tester to check that my EAs are executing correctly, so just look at the trade results and logs.
Same problem with code corrected
//+------------------------------------------------------------------+ void CloseTradesbyTime() { for(int i = PositionsTotal() - 1; i >= 0; i--) { Position.SelectByIndex(i); datetime Fecha1 = Position.Time(); datetime Fecha2 = TimeCurrent(); //Si ya han pasado X horas, cerrar la posición. if((Fecha2 - 3600*Time_to_Close) >= Fecha1) { Trade.PositionClose(Position.Ticket(), -1); Print ("Trade Closed by Max Time Allowed"); } } }
Sorry, I can't help you there.
I actually hate the MT5 tester compared to MT4 and have no idea what all the dots mean. I only use the tester to check that my EAs are executing correctly, so just look at the trade results and logs.
Thank you also for your help. Unfortunately, it was only a couple of weeks ago that I decided to learn mql5, and I felt much more comfortable with mql4, both with code and terminal.
Thank you very much for your time, best regards
No, I don't believe it is a tester bug! Are you forgetting about weekends and trade session times?
Depending on the symbol you are testing, there are times that the Market is closed (off-hours, weekends, etc.) and during those times, you can't close the position until later on when the trade session is active again.
Also, if you are using OnTick, then remember that your code will only run when there are quotes and that may be later than you want to close the trades.
More importantly, in your code you are not checking if the close operation is successful or not. Did you check the Journal log for failed attempts?
You just assume that it will close. Always check, and report the condition to log so you can analyse the issue.
input double Time_to_Close = 48; if((Fecha2 - 3600*Time_to_Close) >= Fecha1)
This closes after 48 real hours. Which means the first tick every Monday when the market reopens.
Don't you want 48 H1 candles, trading hours?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi friends, I'm trying a function to close trades by time, but I'm not sure it's working 100% times, due the backtest results.
Somebody is looking bad code? Thanks in advantage
Backtest info: