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

Опубликовано: 14 Апрель 2026
на канале: MQL5 Tutorial
13,355
177

https://mql5tutorial.com/?s=crossover
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.
Starting with MetaEditor (00:13 – 00:33)

Instructions on opening MetaEditor and creating a new Expert Advisor file named "SimpleCrossoverEA".
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.
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.
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.
Filling Arrays with Moving Average Values (02:10 – 03:12)

Using "CopyBuffer" to fill arrays with values for small and big Moving Averages.
Generating Buy and Sell Signals (03:12 – 04:06)

Logic for generating buy and sell signals based on the crossover of the Moving Averages.
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.
Displaying the Current Signal (04:33 – 04:51)

Using the "Comment" function to display the current trading signal on the chart.
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.
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.
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