Don’t waste your time – keep track of how NFP affects the US dollar!

Data Collection Notice

We maintain a record of your data to run this website. By clicking the button, you agree to our Privacy Policy.

facebook logo with graphic

Join Us on Facebook

Stay on top of company updates, trading news, and so much more!

Thanks, I already follow your page!
forex book graphic

Beginner Forex Book

Your ultimate guide through the world of trading.

Get Forex Book

Check Your Inbox!

In our email, you will find the Forex 101 book. Just tap the button to get it!

FBS Mobile Personal Area

market's logo FREE - On the App Store

Get

Risk warning: ᏟᖴᎠs are complex instruments and come with a high risk of losing money rapidly due to leverage.

69.21% of retail investor accounts lose money when trading ᏟᖴᎠs with this provider.

You should consider whether you understand how ᏟᖴᎠs work and whether you can afford to take the high risk of losing your money.

Structure of a Trading Robot

Information is not investment advice

After finishing the previous lessons you already know about the types of trading robots in MetaTrader 5. You also know where to find the main algorithmic trading tools in MT 5 and how to set them up. In this lesson, we will look at the bones of the trading robot. We promise you that after this lesson your fear of coding in MQL5 will start to disappear!

What does a trading robot consist of?

At first, let’s have a look at the structure of a robot in the Meta Editor.

45232.png

Looks scary, right? However, in reality, all trading robots have the same core elements you need to know about.

There are four main parts of a trading robot:

  • Setup
  • OnIntIt
  • OnDeinit
  • OnTick

Let’s have a closer look at each element.

Setup

This is the part of a robot where all the administrative information is placed. Here, you write notes, declare variables (different types of information with a unique name), track the list of changes, and set the property rights.

3232.png

On the screenshot above, you can see a sample of the Setup code. It consists of copyright, the main properties of a robot, and the #include command that inserts the content of a certain file to your expert advisor. Also, in this section, we declare the main inputs under the #input command.

OnInIt

The second part is called OnInit (OnInitiation). This part launches when you start the EA for the first time.

65443.png

 In this part, you can see the commands that will be executed after the first launch of the program. We will explain these commands in the next articles!

OnDeInIt

The next section of the expert advisor is called OnDeinitiation, or shortly OnDeInIt. It runs when the expert advisor is shut down.

OnTick

This is the most interesting part of an expert advisor. It consists of three main parts that are updated on every tick (with the new information about a price). They are entries, exits, and sizing. These functions are important for any expert advisor, because they tell you when to buy, how much to sell, and when to close your trades.

This code will start every time a new tick arrives on your MetaTrader 5 platform. That is, if the bid, ask prices, or volume changes, this change affects the OnTick section of your code as well.

667654645.png

On the screenshot above, you can see that the robot checks the last trade processing time and other data.

So, if you were a computer, how would you read the code structure?

First of all, you would go to the OnInit section and run everything in it. You’d set up variables and parameters that you need for your strategy. This action is executed only once.

After this action is completed, you go to the OnTick section. At first, you check if you have any trades that need to be exited. After that, you look for trades that can be entered. Finally, a sizing algorithm will help you to identify the size of your position.

Once you shut down your EA, you run OnDeInIt. This option removes the graphics from the charts and deletes everything linked to the expert advisor itself.

Bottom line

To sum up, if you want to build a trading robot, you need to know its structure thoroughly. Luckily, all of the robots have a similar structure that allows you to construct it step-by-step.

Popular

Will the CPI Crash the USD?

 The most impactful releases of this week will fill the market with volatility and sharp movements.

Crypto Market Changes The Trend

Happy Tuesday, dear traders! Here’s what we follow: 

The US Labor Market And Real Estate Market Are In Focus This Week

Labor Market and Real Estate Market data was published yesterday. Markets are slowing down, so the economy is in recession. Today the traders should pay attention to the Retail sales in Canada.

Choose your payment system

Callback

Please fill in the form below so we can contact you

Select the best time for us to call you. We give calls from Monday to Friday in suggested intervals. In case we couldn't get through, we will try again at the same time the next day. For getting real-time assistance, use FBS chat.

We provide only English-speaking callbacks. If you prefer any other languages, contact the support team.

We will call you at the time interval that you chose

Change number

Your request is accepted.

We will call you at the time interval that you chose

Next callback request for this phone number will be available in 00:30:00

If you have an urgent issue please contact us via
Live chat

Internal error. Please try again later