MQL5 TUTORIAL BASICS – 20 HOW TO CODE A SIMPLE CROSSOVER EXPERT ADVISOR

video
play-sharp-fill

 

  1. Introduction to the Crossover Expert Advisor (00:00 – 00:13)
    • Introduction to creating a standalone version of a crossover Expert Advisor that produces and trades buy and sell signals directly on a chart.
  2. Starting with MetaEditor (00:13 – 00:33)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleCrossoverEA”.
  3. Setting Up the Expert Advisor (00:33 – 00:53)
    • Steps to delete unnecessary parts of the code and include the “Trade.mqh” file for trading functions.
  4. Creating Input Values for Moving Averages (00:53 – 01:18)
    • Creating two input values for small and big Moving Average and calculating the Ask and Bid prices.
  5. Defining Properties for Moving Averages (01:18 – 02:10)
    • Defining properties for small and big Moving Averages using the “iMA” function and setting up the arrays.
  6. Filling Arrays with Moving Average Values (02:10 – 03:12)
    • Using “CopyBuffer” to fill arrays with values for small and big Moving Averages.
  7. Generating Buy and Sell Signals (03:12 – 04:06)
    • Logic for generating buy and sell signals based on the crossover of the Moving Averages.
  8. Executing Trades Based on Signals (04:06 – 04:33)
    • Using the “trade.Sell” and “trade.Buy” functions to execute trades based on the generated signals.
  9. Displaying the Current Signal (04:33 – 04:51)
    • Using the “Comment” function to display the current trading signal on the chart.
  10. Compiling and Testing the Expert Advisor (04:51 – 06:16)
  • Instructions on compiling the Expert Advisor and setting up the Strategy Tester in MetaTrader for testing.
  1. Visualizing and Testing in MetaTrader (06:16 – 07:05)
  • Visualizing the Expert Advisor in action and adjusting the Moving Average values in the Strategy Tester.
  1. Conclusion and Practical Application (07:05 – 07:37)
  • Summary of the video, highlighting the ability of the Expert Advisor to calculate, draw, and trade based on Simple Moving Averages.

 

In this video we are going to create a standalone version for a crossover Expert Advisor that is producing buy and sell signals directly on a chart and it is actually going to trade those signals whenever crossover happens, we will get a signal here, so let’s find out how to do that with MQL5.

First you need to click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleCrossoverEA”, click on “Continue”, “Continue” and “Finish”.

Now you can delete everything that is above the “OnTick” function and let’s also delete the two comment lines here.

We start by including the file: “Trade.mqh” that will help us to create an instance of the class: “CTrade” that will be called: “trade”.

Now we want to create two input values, if you mark “input” and press F1 you will see that it defines an external variable, basically it will create a user input. We need two; one small Moving Average (SmallMovingAverage ) and one big Moving Average value (BigMovingAverage).

Now let’s calculate the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_ASK” or “SYMBOL_BID”. “NormalizeDouble” and “_Digits” will give us the number of digits behind the dot.
Let’s create an empty value for the signal (signal) that needs to be calculated.
We start by creating an array for several prices; one for the small Moving Average (SmallMovingAverageArray) and one for the big Moving Average (BigMovingAverageArray), first we need to define the properties of the small Moving Average (SmallMovingAverageDefinition)  – I need to reduce the font size here – we use the function “iMA” for the current symbol on the chart and the currently selected period on that chart, this is the number of candles that we want to use to calculate the small Moving Average – in our case it’s 20 candles – we don’t use a shift value and with “MODE_SMA” and “PRICE_CLOSE” we define that we want to calculate a Simple Moving Average that is based on the close price.

Now let’s repeat that for the big Moving Average (BigMovingAverageDefinition), this is basically the same line – that is 50 by default – afterwards we use “CopyBuffer” to fill our small Moving Average array (SmallMovingAverageArray) according to the definition (SmallMovingAverageDefinition) that we have created here for the buffer 0 (zero) – that’s the only line the Moving Average has – we start with candle 0 (zero), copy the value for 3 candles and store them in a small Moving Average array (SmallMovingAverageArray), afterwards you might have guessed it, we do the same thing for the big Moving Average according to the definition (BigMovingAverageDefinition) we have created here, we store the values in our big Moving Average array (BigMovingAverageArray) and if the big Moving Average is greater than the small Moving Average for candle 1 and it was below the small Moving Average for candle 2 that would be a crossover, now we want to buy so we assign the word: “buy” to our signal.

Otherwise if the big Moving Average is below the small Moving Average for candle 1 and if the big Moving Average was above the small Moving Average for candle 2 that’s a crossover in the other direction, now we would assign the word: “sell” to our signal and if our signal equals sell and if we have no open positions we use “trade.Sell” to sell 10 micro lot. Otherwise if the signal equals buy and we have no open positions we would use “trade.Buy” to buy 10 micro lot. 

Finally we want to use the “Comment” function, this will output “The signal is now:” followed by the signal that we have calculated.

That’s about it, if this is too fast for you you might consider to become a member of the premium course or repeat the other basic videos in this series. 

When you’re done you can click on the “Compile” button or press F7 on your keyboard, you should not get any errors and in that case you can click here or press F4 to go back to Metatrader, and in Metatrader we are going to use a little trick please click on: “Insert/ Indicators/ Trend/ Moving Average”, the small Moving Average in our case is for 20 candles, the shift is 0 (zero), we want to calculate a simple moving average that is based on the close price and I would like to have a red line, so let’s click on “OK”, now we repeat the process, pick another Moving Average, now I would like to have 50 candles and this time I would like to have a yellow color, so let’s click on “OK”, let’s right click into the chart, select “Templates/ Save Template” and save it as “tester.tpl”  because this is the template that is going to be used in the next strategy test.
So let’s bring up the Strategy Tester by clicking on: “View/ Strategy Tester” or pressing CTRL and R, here you want to pick the new file: “SimpleCrossoverEA.ex5”, please mark the visualization option here and start a test.

Here is our Expert Advisor, its running, we see the yellow and the red Moving Average and as soon as we see a crossover here, we should get a signal, there it was… 

One more thing; if you click on the “Inputs” tab you could change the values here, you might use something like 30 for the small Moving Average now, but please don’t forget to also change the properties for the Moving Average in the template and afterwards you need to save the template as “tester.tpl” again.
Now the red Moving Average is for 30 candles, we should get a signal in a few seconds, there it is, here is your first sell trade and in this little video you have learned how to create an Expert Advisor that is able to not only calculate and draw Simple Moving Averages on the chart but it’s actually trading them and you can change the values and you have coded it yourself with a few lines of MQL5 code.

 

Download “CODE - SIMPLE CROSSOVER EA”

SimpleCrossoverEA.mq5 – Downloaded 111 times – 2.25 KB