MQL4 TUTORIAL – How to code a shifted SMA Expert Advisor

video
play-sharp-fill
  1. Open the MetaEditor window by clicking the button or pressing F4.
  2. Select “File” -> “New” -> “Expert Advisor (template)”.
  3. Name the file “SimpleSMAShift” and click “Finish”.
  4. Delete everything above the “OnTick” function and remove the two comment lines.
  5. Create a string variable called “signal” with no assigned value.
  6. Calculate the first SMA using the following MQL4 code:

double MyMovingAverage1 = iMA(_Symbol, PERIOD_M1, 20, 0, MODE_SMA, PRICE_CLOSE, 1);
  1. Calculate the second SMA using the following MQL4 code:

double MyMovingAverage2 = iMA(_Symbol, PERIOD_M1, 20, 20, MODE_SMA, PRICE_CLOSE, 1);
  1. If the first SMA is greater than the second SMA, set the “signal” variable to “buy”. Otherwise, set it to “sell”. Use the following MQL4 code:

   // if MyMovingAverage1 is above MyMovingAverage2
   if (MyMovingAverage1>MyMovingAverage2)
   {
      // set the signal to buy
      signal="buy";
   }
   
   // if MyMovingAverage1 is below MyMovingAverage2
   if (MyMovingAverage1<MyMovingAverage2)
   {
      // set the signal to sell
      signal="sell";
   }
  1. Output the signal on the chart using the “Comment” function with the following MQL4 code:

   // Output the signal on the chart
   Comment ("The current signal is: ",signal);
  1. Compile the Expert Advisor by clicking the “Compile” button or pressing F7.
  2. In Metatrader, add two Moving Average indicators to the chart: one with a period of 20 and shift of 0, and one with a period of 20 and shift of 20.
  3. Save the chart template as “tester.tpl” or “default TPL”.
  4. Open the Strategy Tester by clicking “View” -> “Strategy Tester” or pressing CTRL and R.
  5. Select the “SimpleSMAShift.ex4” file and set the visual mode mark.
  6. Run the test and observe the signal being generated when the green line is above the red one and changing when there is a cross over.

Long Description

In this tutorial, we will be creating a simple Expert Advisor (EA) in MQL4 that calculates a Simple Moving Average (SMA) and generates a signal based on the shift between two SMAs. This is a great project for beginners who are new to MQL4 programming.

First, we will open the MetaEditor window by clicking the button or pressing F4. Then, we will select “File” -> “New” -> “Expert Advisor (template)”. We will name the file “SimpleSMAShift” and click “Finish”. Next, we will delete everything above the “OnTick” function and remove the two comment lines.

Now, we will create a string variable called “signal” with no assigned value. This variable will be used to store the signal that will be generated based on the shift between the two SMAs.

Next, we will calculate the first SMA using the “iMA” function. The “iMA” function takes several parameters, including the symbol, period, shift, mode, and price. For the first SMA, we will use a period of 20, a shift of 0, and the mode and price of “MODE_SMA” and “PRICE_CLOSE” respectively.

We will then calculate the second SMA using the same “iMA” function, but with a shift of 20. This will cause the second SMA to be shifted 20 candles into the future.

Next, we will use an if-else statement to check if the first SMA is greater than the second SMA. If it is, we will set the “signal” variable to “buy”. Otherwise, we will set it to “sell”.

Finally, we will output the signal on the chart using the “Comment” function. This will allow us to see the signal being generated in real-time.

Once we have finished coding the EA, we will compile it by clicking the “Compile” button or pressing F7. If the compilation is successful, we will then open a chart in Metatrader and add two Moving Average indicators: one with a period of 20 and shift of 0, and one with a period of 20 and shift of 20.

We will then save the chart template as “tester.tpl” or “default TPL” so that we can easily load it in the Strategy Tester.

In the Strategy Tester, we will select the “SimpleSMAShift.ex4” file, set the visual mode mark, and run the test. We will observe the signal being generated when the green line is above the red one and changing when there is a cross over.

By following these steps, you will have successfully created a simple Expert Advisor that calculates a Simple Moving Average and generates a signal based on the shift between two SMAs. This is a great foundation for further learning and experimenting with MQL4 programming.

Video Time Stamps

0:00 – 0:06: Introduction to the topic of Simple SMA Shift function in MQL4
0:06 – 0:22: Explanation of the red and green lines representing Moving Averages for 20 candles
0:22 – 0:36: Observation of the shifted SMA and its use as a buy/sell signal
0:36 – 0:41: Introduction to the creation of an Expert Advisor
0:41 – 1:01: Opening the Metaeditor window and creating a new Expert Advisor
1:01 – 1:18: Deleting unnecessary code and creating a string variable for the signal
1:18 – 2:00: Calculating the first Simple Moving Average using the “iMA” function
2:00 – 2:11: Explanation of the shift value parameter in the “iMA” function
2:11 – 2:57: Changing the shift value and observing the effect on the SMA
2:57 – 3:55: Calculating the second Simple Moving Average with a shift value of 20
3:55 – 4:41: Creating a buy/sell signal based on the comparison of the two SMAs
4:41 – 4:59: Outputting the signal on the chart using the “Comment” function
4:59 – 5:17: Compiling the Expert Advisor and returning to Metatrader
5:17 – 6:10: Adding two Moving Averages to the chart and saving the template
6:10 – 6:39: Opening the Strategy Tester and selecting the SimpleSMAShift.ex4 file
6:39 – 7:10: Running the test and observing the signal being generated