Download MetaTrader 5

How to open a trade x seconds before the close of a candle in the Strategy Tester ?

To add comments, please log in or register
Alain Verleyen
Moderator
32062
Alain Verleyen  

Hi,

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 ?

Nathan
154
Nathan  
angevoyageur:

Hi,

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.

Alain Verleyen
Moderator
32062
Alain Verleyen  
niqmadu:

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.

Nathan
154
Nathan  
angevoyageur:

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.


Your welcome. I greatly appreciated your help on my other issues.
Ian Venner
2584
Ian Venner  

This is the only way I know to do it in strategy tester.

void OnTick()
  {
//--- for hoursly chart

   if(TimeCurrent() >= Time[0]+3590) //if there is a tick in the ten second period before next bar starts
   Print(TimeToString(TimeCurrent(),TIME_SECONDS));
  }
I had an idea which I have not tested which is for ensuring the EA does not miss the time because the lack of a tick before the end of the bar. To put the EA in a loop for a period before the time you want to place the trade. Keep calling the TimeCurrent until it is the time to place the trade. I dont know if that would cause it to crash though. TimeCurrent() might only update when a tick is generated
honest_knave
Moderator
2358
honest_knave  

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[0]+PeriodSeconds()-10)
Alain Verleyen
Moderator
32062
Alain Verleyen  
SDC:

This is the only way I know to do it in strategy tester.

I had an idea which I have not tested which is for ensuring the EA does not miss the time because the lack of a tick before the end of the bar. To put the EA in a loop for a period before the time you want to place the trade. Keep calling the TimeCurrent until it is the time to place the trade. I dont know if that would cause it to crash though. TimeCurrent() might only update when a tick is generated

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.

Alain Verleyen
Moderator
32062
Alain Verleyen  
honest_knave:

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:

I fear you are right.
Ian Venner
2584
Ian Venner  
angevoyageur:

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.

Yes using a loop would depend on how strategy tester models the time we should probably test it to check the possibility
Alain Verleyen
Moderator
32062
Alain Verleyen  
SDC:
Yes using a loop would depend on how strategy tester models the time we should probably test it to check the possibility
Which loop ? You can't use Sleep(), so you will only burn cpu cycle. On time only advance with new tick.
Ian Venner
2584
Ian Venner  
I tested it you're right it doesnt work
12
To add comments, please log in or register