Centaur wrote: Mon Oct 24, 2022 5:02 pm
All timeframes at midnight, please your assistance will be greatly appreciated...
Sorry for the late post. It was a bit busy at work and at home.
Disclaimer - This is what I observed in my case and how I fixed the issue I was facing. If this fix does not work for you, let me know. I am happy to take a look at your issue.
Let's first understand what is causing this error. Let's use the GBPUSD example on IC Markets international. If you look at the symbol specification, then you will notice that the symbol cannot be traded for 2 minutes between 23:59 and 00:01. Midnight is bang in the middle of this.
If you are like me and using the OnTick function in your EA, you would naively think this is ok as you would not get a tick during those 2 minutes. But you would be wrong. You will get ticks in those 2 minutes. This is for 2 reasons
(1) Those times are untradable for you as a retail trader but IC Markets may have trading activity going on during those 2 minutes. It is just that you are not allowed to participate
(2) It is possible that these trading times were not always like that. They have changed recently but your historical data will not be changed to reflect this. Meaning, you may have historical data for those 2 minutes which results in the [market closed] error when you run your EA in the strategy tester
How did I fix this?
I will not be able to share all my code as is. It contains a lot of other pieces that will come in the way of explaining this. I will instead use pseudocode to describe.
My OnTick function looks like this
Code: Select all
void OnTic() {
Strategy.OnTick();
if(NewBar()) {
Strategy.OnNewBar();
}
}
So I pass every tick to my Strategy class and then if this tick is on a new bar then I invoke the OnNewBar function of my Strategy class. The tick that comes at midnight, triggers my new bar logic resulting in the OnNewBar function in my Strategy class being called. If my entry conditions are met, then the Strategy class tries to place an order and fails with error [marekt closed]
You can fix this by using a timer here. I hope you can imagine how messy that code would have become with a lot of symbol specific hard-coding.
I needed a way to invoke OnNewBar() function during the same bar but after the market has opened but only if I have received a [market closed] error before. What I ended up doing was returning a boolean true/false from my Strategy.OnNewBar() function. It returns false if the order send fails for any reason (you can be specific and return false only when [market closed] error is returned). I then changed my OnTick function below
Code: Select all
bool LastOnNewBarResult = true;
void OnTic() {
Strategy.OnTick();
if(LastOnNewBarResult == false) {
LastOnNewBarResult = Strategy.OnNewBar();
}
if(NewBar()) {
LastOnNewBarResult = Strategy.OnNewBar();
}
}
What this is doing is - if Strategy.OnNewBar() returns false, then Strategy.OnNewBar() is called on every tick until it returns true.