MQL5 TUTORIAL – WHY THE ONINIT FUNCTION WILL NOT SAVE YOU

video
play-sharp-fill

 

In this video, I’d like to talk about something that seems so obvious that it’s almost blinding.

 

When you use the strategy tester to test something, you might miss things that are important when you trade in real time.

 

This is an example of that.

 

When you test your strategy, you probably forget one or two things that could hurt you when trading in real time.

 

The first thing is that real time conditions are not the same.

 

When you’re using the strategy tester, you might not have a lot of different conditions, but that’s part of the game when you’re trading in real time.

 

If you lose power or have trouble connecting to the internet, your Expert Advisor might not work for a few hours or even days.

 

That happened to us a lot.

 

And if there’s one thing that’s likely to happen, it’s that one of these power outages will happen at the worst time and in the worst way.

 

It could happen while you are on vacation, for example. What happened a few years ago is as follows.

 

The Expert Advisor knew what the last price to buy was and what the last price to sell was, but then it was disconnected.

 

During the outage, the market changed, but the Expert Advisor didn’t notice.

 

Later, the Expert Advisor kept making decisions based on conditions that were no longer true.

 

You can’t simulate a 7-hour internet outage in a strategy test, so that’s not tested in backtests.

 

People were very hard on me when I first started using MQL4 because I didn’t use the OnInit function.

 

Yes, it takes more processing power to figure out everything every time a new Tick comes in.

 

But it is worth the CPU time when you want to avoid values and assumptions that don’t match up.

 

Think of something from the real world.

 

You might have ended up wearing the wrong clothes because the weather changed while you weren’t paying attention.

 

For example, you might come home from a summer vacation and find that a T-shirt is not enough because the airport in your home country is covered in snow.

 

The OnInit function will only run once, when the Expert Advisor starts up, so it is a good idea to check every time the price changes to see if some important conditions are still true.

 

In our Platin System, we do this with a function called CheckForTick.

 

If you only use one file and don’t import any libraries, you can use your own Expert Advisor right in the MQL OnTick function.

 

And don’t just go by the results of strategy tests. Those are good ways to tell if something will work or not.

 

But nothing beats trading in real time, even on a Cent account.

 

So, when you trade in real time, make sure you check the conditions and assumptions you have, because as Mark Twain once said:

 

“It’s not what we don’t know that gets us into trouble. It’s the things we know for sure that aren’t true.”