MQL4 TUTORIAL – WHY THE ONINIT FUNCTION WON’T SAVE YOU

video
play-sharp-fill

 

In this video I would like to talk about something that you can call the blinding flash of the obvious. When you test something with the strategy tester, you may overlook things that play an important role in your everyday real time trading. This is one of those examples. Whenever you run a strategy test, you probably forget one or two things that might harm you in real time trading.

 

The first thing is inconsistency. While you are using the strategy tester, you might not have inconsistent conditions, but in real time trading those are part of the game. You might have an outage of power or a problem with your internet connection and your Expert Advisor might not run for a few hours, or even days. We had that happen often. And if one thing is likely to happen, it is that one of those outages will happen at exactly the worst time and under exactly the worst conditions. For example, it might happen while you are on vacation.

 

Here is what happened a few years ago. The Expert Advisor had a value for the last buy price and the last sell price, but then it was interrupted. During the outage the market conditions changed, but the Expert Advisor didn’t notice that. Later, the Expert Advisor continued to calculate the decisions based on the wrong and outdated conditions. That is unlikely to happen in a strategy test, because you have no option to simulate a 7 hour internet outage.

 

When I started out with MQL4 I was often heavily criticized by people because I didn’t use the OnInit function. Yes, the calculation of everything whenever a new Tick comes in takes more processing power. But it is well spend CPU calculation time when you want to avoid inconsistent values and assumptions. Think of a real world example. You might have found yourself dressed up totally wrong, because the conditions change while you didn’t notice it. For example, you might come back from your summer vacation and as soon as you have landed you notice that a Tshirt is not enough when the airport of your home country is drowning in snow.

 

The OnInit function will only run once, at the start of the Expert Advisor so it is a good idea to check if some critical conditions are still true whenever the price changes. In the Platin System we use a function called CheckForTick to do that. If you use your own Expert Advisor with just a single file and no library imports, you can do that right inside of the OnTick function that comes with MQL. And don’t only rely on strategy test results.

 

Those are a good indicator if something is likely to work. But nothing replaces real time operation, even if it is on a Cent account. So please make sure that you check the conditions and assumptions you have during real time trading, because as Mark Twain once said: “What gets us into trouble is not what we don’t know. It’s what we know for sure that just ain’t so.”