How to become an algorithmic trader - Lesson 3

How to become an algorithmic trader - Lesson 3

Become an Algorithmic Trader - Learn how code an Expert Advisor and run a Backtest

Step 1: Variables declaration

The first thing needed to code an expert advisor is variables.

Variables can be declared globally or locally:

  • a global variable is declared at the beginning of your code, before the OnInit() function
  • a local variable, instead, is declared inside a function and it works just inside that function.

It is no possible to use a local variable value within another function where it has not been defined. Let’s now start declaring the variables for an Expert Advisor whose aim is to buy and sell as a fast Moving Average crosses a slow one and vice versa. Following the sample of code where we declared some variables we will use to code our Expert Advisor:

Variables Declaration MQL4

we are declaring just integer variables, that means just numbers without decimal positions. We declared 5 variables: openPosition, longPosition, shortPosition, ticketLong, ticketShort. Pay attention to end each line of code with a semicolon, otherwise your Expert Advisor will incur in errors.

Step 2: OnInit Function

Our Expert Advisor needs some variables to be valued other than declared. When initializing the Code, it can be useful to give value to some of the variables previously declared, because when you run your Expert Advisor you can need some default values to give to some variables that then change value while running. In our case we just need to give 0 value to three variables, just how it is shown below:

OnInit Metatrader

Step 3: OnTick Function

Now that all the stuff is set, we can finally write the operating code by using the two functions you learnt in the previous lesson: iMA() and OrderSend().

Inside the OnTick() function we need to declare four local variables that are the moving averages we will need to calculate the crossovers. The four moving averages are composed by two fast and two slow moving averages. We need to calculate four moving averages because two of them are current and two are calculated on the previous period (otherwise we could not compute the crossing). The following image shows the declaration of the four moving averages inside the OnTick() function:

OnTick Metatrader 4

once we have defined the moving averages, we need to define the entry and exit conditions for the Expert Advisor. We will do it by the "if statement", which allows us to define a set of conditions to be respected before sending an order with the function OrderSend().

Following the example of our Expert Advisor:

Expert Advisor MT4 example

First two if statements control whether the long or short positions are set to 0, that means that no position is opened. Then the "ifs" control if the previous period Fast moving average was below the Slow one and in the current period it is exactly the opposite. In this case it means that the moving averages have crossed each other and it is the entry signal. The same for the short order, but with the inverted signs.

The third and fourth "ifs" are used to sell. The logic behind is the same as the first two "if statements".

We will make some videos to explain visually what we tried to explain easily and fastly via our lessons. We did not want to bore you, so we decided to write just some pills to give you hints about how to trade an Expert Advisor. If you wish to go deeper, follow our videos to our youtube channel (they will be available soon) and do not hesitate to write us

Related Products