Structure of a Trading Robot

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.

Frequently asked questions

  • How to start trading?

    If you are 18+ years old, you can join FBS and begin your FX journey. To trade, you need a brokerage account and sufficient knowledge on how assets behave in the financial markets. Start with studying the basics with our free educational materials and creating an FBS account. You may want to test the environment with virtual money with a Demo account. Once you are ready, enter the real market and trade to succeed.

  • How to open an FBS account?

    Click the 'Open account' button on our website and proceed to the Trader Area. Before you can start trading, pass a profile verification. Confirm your email and phone number, get your ID verified. This procedure guarantees the safety of your funds and identity. Once you are done with all the checks, go to the preferred trading platform, and start trading.

  • How to withdraw the money you earned with FBS?

    The procedure is very straightforward. Go to the Withdrawal page on the website or the Finances section of the FBS Trader Area and access Withdrawal. You can get the earned money via the same payment system that you used for depositing. In case you funded the account via various methods, withdraw your profit via the same methods in the ratio according to the deposited sums.

Latest news

USD: Powell Speaks on Cutting Interest Rates

Jerome H. Powell, the Federal Reserve chair, stated that the central bank can afford to be patient in deciding when to cut interest rates, citing easing inflation and stable economic growth. Powell emphasized the Fed's independence from political influences, particularly relevant as the election season nears. The Fed had raised interest rates to 5.3 ...

BTCUSD: Rich Dad Says Buy

Robert Kiyosaki, the author of "Rich Dad Poor Dad," has updated his bitcoin price forecast, now projecting the cryptocurrency to hit $100,000 by September. He plans to acquire more bitcoin before April, attributing his decision to the upcoming halving event. Kiyosaki advises investors to consider adding bitcoin to their portfolios and suggests...

WTT: Currency Pairs To Trade In April

Hello again my friends, it’s time for another episode of “What to Trade,” this time, for the month of April. As usual, I present to you some of my most anticipated trade ideas for the month of April, according to my technical analysis style. I therefore encourage you to do your due diligence, as always, and manage your risks appropriately.

Deposit with your local payment systems

Data collection notice

FBS maintains a record of your data to run this website. By pressing the “Accept” button, you agree to our Privacy policy.

Callback

 1
 93
 355
 213
 1684
 376
 244
 1264
 672
 1268
 54
 374
 297
 61
 43
 994
 1242
 973
 880
 1246
 375
 32
 501
 229
 1441
 975
 591
 387
 267
 55
 246
 673
 359
 226
 257
 855
 237
 1
 238
 1345
 236
 235
 56
 86
 61
 61
 57
 269
 242
 243
 682
 506
 225
 385
 53
 357
 420
 45
 253
 1767
 1809
 593
 20
 503
 240
 291
 372
 251
 500
 298
 679
 358
 33
 594
 689
 241
 220
 995
 49
 233
 350
 30
 299
 1473
 590
 1671
 502
 224
 245
 592
 509
 39
 504
 852
 36
 354
 91
 62
 98
 964
 353
 44
 972
 39
 1876
 81
 962
 7
 254
 686
 850
 82
 965
 996
 856
 371
 961
 266
 231
 218
 423
 370
 352
 853
 389
 261
 265
 60
 960
 223
 356
 692
 596
 222
 230
 262
 52
 691
 373
 377
 976
 382
 1664
 212
 258
 95
 264
 674
 977
 31
 599
 687
 64
 505
 227
 234
 683
 672
 1670
 47
 968
 92
 680
 970
 507
 675
 595
 51
 63
 64
 48
 351
 1787
 974
 262
 40
 7
 250
 590
 290
 1869
 1758
 590
 508
 1784
 685
 378
 239
 966
 221
 381
 248
 232
 65
 421
 386
 677
 252
 27
 500
 34
 94
 249
 597
 268
 46
 41
 963
 886
 992
 255
 66
 670
 228
 690
 676
 1868
 216
 90
 993
 1649
 688
 256
 380
 971
 44
 1
 1
 598
 998
 678
 58
 84
 1284
 1
 681
 2
 967
 260
 263
00:00
00:00
00:00
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:00
09:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00
23:00
23:00
00:00
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:00
09:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00

A manager will call you shortly.

Change number

Your request is accepted.

A manager will call you shortly.

Next callback request for this phone number
will be available in

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

Internal error. Please try again later

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

You are using an older version of your browser.

Update it to the latest version or try another one for a safer, more comfortable and productive trading experience.

Safari Chrome Firefox Opera