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

 

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 ?

 
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.

 
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.

 
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.
 

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
 

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)
 
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.

 
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.
 
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
 
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.
 
I tested it you're right it doesnt work
Reason: