Popular results

Advertisements
$ £ ¥
¥ £ $

Moving Average Trailing Stop MT4/MT5 Expert Advisor

Table of contents

Following a moving average is a popular way to implement a trailing stop. Below you can download a free expert advisor for MetaTrader 4 or for MetaTrader 5 that can adjust the stop-loss level of your trades by following a moving average with set parameters.


What is trailing stop?

In case you are new to the concept of trailing stop: trailing stop is the practice of moving the stop-loss level closer to the current price when the price moves in your favor.

Trailing the stop-loss allows traders to limit their losses and safeguard their profits. It is a widely used practice.

As you may have read in books and forums, one of the first rules in Forex is Cut your losses short and let your profits run.


What is moving average?

A moving average is one of the simple and most popular indicators. It is often shorted as MA and is an average of the price of the last N candles. There are several types of moving averages available to traders. The most popular of them are simple and exponential.

In the chart screenshot below, you can see a purple line of a simple moving average with the period set to 25.

Example of Simple Moving Average with Period Set to 25
Example of a simple moving average with period set to 25

The function to retrieve the value of a moving average in MQL4 (the coding language used in MetaTrader 4) is iMA().

The official documentation provides more information about using the iMA() function.


Trailing stop following MA

About MT4 MA Trailing Stop EA

The expert advisor Moving Average Trailing Stop moves the stop-loss of the selected orders by assigning the value of the moving average on the current candle or any other candle of your choice.

It can be very useful in trending periods to follow the trend and cut losses.

Stop-Loss Following Moving Average

Why is MA Trailing Stop EA useful?

Risk management is one of the most important aspect in trading. Assigning a stop-loss to your trades can protect you from margin calls and extreme losses.

In many cases, it is useful to have a trailing stop, which is a sort of a moving stop. A trailing stop is a stop-loss, which has its value updated when the price moves in the trade's favor.

There are many types of trailing stop — it can be a fixed step change or it can follow some technical indicator.

In the case of our MetaTrader MA Trailing Stop expert advisor, the stop-loss follows the values of a moving average.

There are several advantages of using our MetaTrader MA Trailing Stop EA:

  1. Filtering options to select which trades are managed.
  2. A completely customizable moving average.
  3. A stop-loss is set automatically if there is none.
  4. Can start trailing when some given profit value is reached.
  5. The stop-loss follows the price when moving in the favor of the trade.
  6. It doesn't require human interaction.
  7. Limit losses.
  8. Secure profits.
  9. Avoid margin calls.
  10. Notifications when stop-loss is updated.

MT4 MA Trailing Stop EA Panel Looks

Parameters

MA Trailing Stop EA Input Parameters

This expert advisor has the following input parameters to configure its operation and interface:

  • Moving Average Period — an integer to set the period of the moving average.
  • Moving Average Shift — an integer to shift the moving average by a number of candles back.
  • Moving Average Method — any of the four moving average types supported by MetaTrader.
  • Moving Average Applied Price — the type of price to apply the moving average calculations to.
  • Shift In The MA Value — the number of candle to get the MA from for the stop-loss. 0 means the current candle.
  • Profit Points to Start Trailing — the number of points of profit to start trailing the stop-loss. 0 means that the profit/loss value is ignored.
  • Apply To Current Symbol Only — if true, only trades in the current symbol will be processed by this trailing stop EA. If set to false, the EA will calculate MA values for all symbols using the current chart's timeframe and trail all stop-losses.
  • Apply To — select which trade directions to apply a trailing stop to.
  • Filter By Magic Number — true or false depending on whether you want to modify only the orders with a specific magic number.
  • Magic Number — specify the magic number in case the above is true.
  • Filter By Comment — true or false depending on whether you want to modify only the orders with a specific comment.
  • Comment — specify the comment if the above is true.
  • Enable Trailing Stop — if true, the EA will be active on launch and will start trailing stop-losses immediately.
  • Enable Notifications feature — if true, the EA will send notifications upon triggering.
  • Send Alert Notification — if true, the EA will issue native on-screen alerts.
  • Send Notification to Mobile — if true, the EA will send push-notifications to your mobile device.
  • Send Notification via Email — if true, the EA will send emails when it updates a stop-loss.
  • Show Graphical Panel — if true, a graphical panel will appear on the chart to control the expert advisor.

The remaining input parameters determine the control panel's looks and position and have self-explanatory names.

Warning!

For the email and mobile notifications to work, you have to configure them in your platform beforehand. Native on-screen alerts do not require additional configuration.

Rules

The MA Trailing Stop expert advisor is configured with the following rules:

  • For Buy orders, the moving average value of the previous candle must be above the original stop-loss level and below the current price level.
  • For Sell orders, the moving average value of the previous candle must be below the original stop-loss level and above the current price level.

If the moving average is above the current price for a Buy order or below it for a Sell order, no stop-loss modification will occur.

It is important to remember that we want to move the stop-loss level always in the direction of the order and never to increase its distance. This helps to follow the best practice risk management rules.

Example

You can see an ideal example of this expert advisor at work below. The trailing stop moves the stop-loss level after each candle following the moving average (red line).

Trailing Stop Follows the MA (Red Line)
Trailing Stop Follows the MA (Red Line)
How Orders Get Updated According to the Moving Average
How Orders Get Updated According to the Moving Average

Download MA Trailing Stop EA for MT4

You can download the expert advisor using the link below and install it by following the presented instructions.

The installation is very easy to perform; the same is the use. Once you run the expert advisor, you can set all the necessary input parameters.

Make sure you enable Live Trading in the Common tab when attaching the EA and also in the platform's menu Tools→Options→Expert Advisors. Otherwise, it won't be able to modify orders.

How to Enable Live Trading for Expert Advisors in MT4?

MT4 MA Trailing Stop EA Installation

To install the EA, please follow the instructions below:

  1. Download the expert advisor archive file.
  2. Open the MetaTrader 4 data folder (via File→Open Data Folder).
  3. Open the MQL4 Folder.
  4. Copy all the folders from the archive directly to the MQL4 folder.
  5. Restart MetaTrader 4 or refresh the expert advisors list by right-clicking the Navigator subwindow of the platform and choosing Refresh.

Or you can read a more detailed instruction on how to perform the installation.

Download MA Trailing Stop EA for MT5

You can also download this trailing stop EA for MetaTrader 5. Please use the link below:

You will be able to customize its input parameters when you run attach the expert advisor to a chart.

Make sure to allow algorithmic trading on the Common tab when attaching the EA and also in the platform's menu Tools→Options→Expert Advisors. Otherwise, it won't be able to modify orders.

MT5 MA Trailing Stop EA Installation

To install the EA, please follow the instructions below:

  1. Download the expert advisor archive file.
  2. Open the MetaTrader 5 data folder (via File→Open Data Folder).
  3. Open the MQL5 Folder.
  4. Copy all the folders from the archive directly to the MQL5 folder.
  5. Restart MetaTrader 5 or refresh the expert advisors list by right-clicking the Navigator subwindow of the platform and choosing Refresh.

Conclusion

Using a trailing stop that follows a moving average can be ideal in a situation where there is a clear trend. This type of trailing stop will allow to follow the trend and to protect the order from unexpected spikes or reversals.

 

You can open a trading account with any of the MT4 Forex brokers to freely use the presented here expert advisor for MetaTrader 4. If you want to use an MT5 version of the expert advisor presented here, you would need to open an account with a broker that offers MetaTrader 5.

Discussion

Do you have any suggestions or questions regarding this expert advisor? You can always discuss Moving Average Trailing Stop with other FX traders and MQL programmers on the expert advisors forums.


Changelog

1.02 — 2025-05-26

  • Added an option to trail stop-losses only when a trade reaches some amount of profit first (ProfitPoints).
  • Added an MT5 version of the expert advisor.
  • Added an option to trail stop-losses for all symbols.
  • Added panel scaling on hi-DPI screens.
  • Improved reporting on disabled autotrading.
  • Improved the fluidness of button state update on the chart in MT5.
  • Fixed a bug that resulted in the stop-loss not being moved if the new value was too close to the old one.
  • Fixed the calculation process to take into account the symbol's tick size (useful for CFDs).