Almost all is in the title. On a live chart, there are several way to do it, for example with the OnTimer event handler.
But what about the Strategy Tester, timer doesn't work, sleep doesn't work, tick count is not reliable. I am looking for a way to open a trade (or run any code), x seconds (or whatever time) before the end of a candle. Not sure it's possible, maybe someone has a new idea ?
This is similar to something I was going to try next. You can manually track when the last bar was formed, check to see if the current time is X seconds less than the time the next bar should form, and execute the code. If strategy tester is using 1M data then X would need to be greater than 1M and the Period would need to be larger as well.
I typed a skeleton up to play with but don't know yet how it will work in strategy tester. I can post the code if interested, but it is not much to speak of.
Yes, this is the only solution I thought about too. But it's not very satisfactory.
By the way, thank you for sharing your idea.
This is the only way I know to do it in strategy tester.
//--- for hoursly chart
if(TimeCurrent() >= Time+3590) //if there is a tick in the ten second period before next bar starts
I think that until OnTimer works in Strategy Tester, you will struggle to find a reliable method.
As an aside, you can modify your code to work on all TFs:
if(TimeCurrent() >= Time+PeriodSeconds()-10)
Yes the problem of this method is you need a tick. I have to work on D1 candle, and you can be sure this doesn't work. How to put the EA in a loop with the Strategy Tester and from when ? I don't think this can work.