From the previous articles, you have come to know that the core concept of Expert Advisor is event-handling functions. In this article, we will write our first code lines and put into practice what we have just learned from this concept.

Step by step “Hello World!” practical examples

  • Create a new Expert Advisor with the default settings
  • Identify the core event-handling functions OnInit, OnTick and OnDeinit
  • Print in the log “Hello World!” one time only when starting the Expert Advisor.
  • Print in the log “Hello World!” every time the price changes.
  • Print in the log “Hello World!” one time only when interrupting the Expert Advisor.

OnInit “Hello World!”

As you know now, that OnInit function only runs once at the start of the Expert Advisor. Write your code in OnInit to execute when you need your code to only run once when your robot starts. In this example, we will demonstrate “Hello World!” as an example printed in the log. As per fig.20, type the following line in the OnInit function: Print(“Hello World!”);

OnInit function: “Hello World!” will only print once when the Expert Advisor starts

Fig.20 OnInit function: “Hello World!” will only print once when the Expert Advisor starts.

MetaEditor toolbar

Fig.21 MetaEditor toolbar

To validate your code and compile it into an executable program, press the “Compile” button from the MetaEditor toolbar. The compilation is the process of converting the code you wrote into a machine language that the computer could understand. The compilation outcome will be logged in the toolbox that you can find at the bottom panel of the MetaEditor (fig.22). In case you have an error, the compiler will log the error and the reason why it could not compile your code. In fig.23, a semicolon is missing at the end of the instruction Print(“Hello World!”);.

MetaEditor Toolbox panel: Compilation successful, no errors returned

Fig.22 MetaEditor Toolbox panel: Compilation successful, no errors returned

MetaEditor Toolbox panel: Compilation unsuccessful due to error(s) in the code.

Fig.23 MetaEditor Toolbox panel: Compilation unsuccessful due to error(s) in the code.

Once you have successfully compiled your program, press the “Start/Resume” green button to test your code (fig.24), a window will pop up displaying the name and the version of your Expert Advisor. Press “OK” to go ahead (fig.25).

Start/resume debugging on real data. You can alternatively press F5.
Fig.24 Start/resume debugging on real data. You can alternatively press F5.

Start testing your robot by pressing “OK.”
Fig.25 Start testing your robot by pressing “OK.”

Expert Advisor attached to a EUR/USD chart and data feed
Fig.26 Expert Advisor attached to a EUR/USD chart and data feed

If you have enabled all the “auto-trading” settings, you should now be able to see your Expert Advisor running. Expert Advisors are attached to a chart that contains data from the live feed of the corresponding chart’s symbol. In this example, it is connected to the tick-event from EUR/USD (fig.26).

Remember, we have written the code Print(“Hello World!”); in the OnInit function, which is the function that executes code only once at the start of the Expert Advisor. Now to see the result, in your Metatrader’s Toolbox panel, click on the tab “Experts” (fig.27). What you can see is the name of your robot and the printed text you wrote in the code “Hello World!”. Note that the instruction ran only once as you can see only one time the “Hello World!” message printed.

Metatrader, Toolbox, tab “Experts.”
Fig.27 Metatrader, Toolbox, tab “Experts.”

OnTick “Hello World!”

Now let us try the same print instruction but within the OnTick function. As a reminder, the OnTick event-handling function is permanently executed if the price is “ticking” or change. Now let us clear the log by pressing “clear” from the contextual menu in the tab “Experts” for our next test (Fig.28).

Right-click in tab “Experts” and press “Clear” to remove the history of the log
Fig.28 Right-click in tab “Experts” and press “Clear” to remove the history of the log.

Now back to the MetaEditor to stop the Expert Advisor execution by pressing the stop button (fig.30). Then modify the code and remove the previous Print(“Hello World!”); instruction from the OnInit function. Write again the “Hello World!” print instruction but this time within the OnTick function (Fig.29).

OnTick function: “Hello World!” will print in the log every time the price is changing
Fig.29 OnTick function: “Hello World!” will print in the log every time the price is changing.

Press the red button to stop the Expert Advisor execution
Fig.30 Press the red button to stop the Expert Advisor execution.

Compile your code and press the green button “start/resume” to test your code. Note that this time, the “Hello World!” text will be printed every time the price of the attached chart will change. It is a practical example that shows you that the OnTick function is called every time and indefinitely when the price is changing or ticking.

OnDeinit “Hello World!”

Now repeat the same process to test the “Hello World!” with the OnDeinit function. As you realize, there is no “Hello World!” message printed in the “Experts” log. To test the execution of code in the OnDeinit function, change the time frame of the chart which the Expert Advisor is attached to interrupt it with a change in settings. You will then see the “Hello World!” text appears in the log every time you switch the chart timeframe, which demonstrates the execution of OnDeinit.

Looking for the best forex robot?Discover top-performing systems selected by ForexRobotExpert team

Leave a Reply

1  +    =  3