DINEXSPACEXAUUSD SpecialistEAs available to rentCustom EA: Strategy → Code → BacktestView rentable EA bots
// MOVING_AVERAGE.MQ5

What is a moving average (MA)?

Moving Average (SMA / EMA)Trend

The base indicator of almost every trend system: it flattens price so you can see the real direction.

What is Moving Average?

Smooths price with the average of the last N candles, filtering noise and showing the direction of the trend.

Moving Average (SMA / EMA) — SMA(14) 2,409.8 / EMA(14) 2,413.1 — SIMULATEDDINEXSPACE / TrendSMA(14) 2,409.8 / EMA(14) 2,413.1XAUUSD · 1H / SIMULATEDRESSUP2,336.12,364.32,448.92,420.1▲ BUYCandle 67: BUY▼ SELLCandle 85: SELL▲ BUYCandle 102: BUYVOL*CANDLE 1CANDLE 29CANDLE 57CANDLE 85CANDLE 112MA · EMA 14 ━━━━ / VOL* · ILLUSTRATIVE CANDLE RANGESMA 14EMA 14Illustrative data — not real market data, not a trading signalMoving Average (SMA / EMA) — SMA(14) 2,409.8 / EMA(14) 2,413.1 — SIMULATEDSMA / EMA 14XAUUSD · 1H / SIMULATEDRESSUP2,336.12,364.32,448.92,420.1▲ BUYCandle 102: BUYVOL*Moving Average (SMA / EMA) — SMA(14) 2,409.8 / EMA(14) 2,413.1 — SIMULATEDSMA / EMA 14XAUUSD · 1H / SIMULATEDRESSUP2,336.12,364.32,448.92,420.1▲ BUYCandle 102: BUYVOL*Moving Average (SMA / EMA) — SMA(14) 2,409.8 / EMA(14) 2,413.1 — SIMULATEDSMA / EMA 14XAUUSD · 1H / SIMULATEDRESSUP2,336.12,364.32,448.92,420.1▲ BUYCandle 102: BUYVOL*SMA 14EMA 14Illustrative data — not real market data, not a trading signal

Illustrative signals of the rule on simulated data — not a trading recommendation.

Rule: EMA(14) crosses above SMA(14): buy; crosses below: sell. Entry = close of the signal candle; SL = entry ∓ 1.5 × ATR(14); TP1/TP2/TP3 = 1R/2R/3R.

Use this indicator to create a Custom EA brief →
// WHY_USE_IT

Where this indicator is strong

Three reasons Moving Average is still used after decades.

Readable at a glance

Price above the line is one direction, below it is the other. No complex interpretation needed.

A good filter

Put it in front of another system to remove trades that go against the main trend.

Only one parameter

There is only the period to adjust, so it is very hard to over-optimize it into uselessness.

// READING_SIGNALS

How to read Moving Average signals

How Moving Average (SMA / EMA) produces its number, and what each shape on the chart means.

Formula and how it works

SMA takes the simple arithmetic average of the close over N periods, with every candle weighted equally. EMA gives more weight to recent candles through the factor k = 2/(N+1), so it reacts faster to price moves. Price above the MA usually signals an uptrend, and the opposite for a downtrend.

How to use it in an EA

Use it as a trend filter: only buy when price is above the MA, only sell when price is below it. A crossover of two MAs (fast/slow) is the classic entry signal.

Fits when

The market trends long enough for price to move clearly away from the average.

Struggles when

The market moves sideways in a narrow range. Price keeps crossing back and forth over the MA, and every cross is a false signal and another fee.

Price crosses above the MAThe short-term trend turns up relative to the average of N candles.
Fast MA crosses above slow MAThe classic buy entry signal; a cross below is the opposite signal.
The MA goes flat and price wraps around itNo trend. Every cross during this phase is suspect.
The MA slope steepensThe trend is strengthening, not just price being higher.
Common mistakes

Shortening the period to get earlier signals. That only increases how often the lines cross, not how often they cross correctly.

// PARAMETERS

Recommended MT5 parameters for Moving Average

Default values of the sample file; test them for your market and timeframe.

ParameterDescription
InpPeriodNumber of candles used for the average (default 14).
InpMethodCalculation type: MODE_SMA (simple) or MODE_EMA (exponential).
// INSTALL

Put the indicator into MetaTrader 5

Four steps, no software needed besides MT5.

01Download the .mq5 file

Click the download button above; the file saves to your computer under the indicator's name.

02Open the data folder

In MetaTrader 5 choose File → Open Data Folder, then go to MQL5/Indicators.

03Compile

Open the file in MetaEditor and click Compile. No errors means the indicator is ready.

04Drag it onto the chart

Go back to MT5, refresh the Navigator and drag the indicator onto the price chart.

📄 moving_average.mq5

DinexSpace wrote this file from the indicator's public formula, not from any commercial source. You are free to edit and reuse it.

// THE_DIFFERENCE

How it differs from MetaTrader 5's built-in indicator

The short answer: the formula is not different. What differs is what you can do with it.

Moving Average here gives exactly the same number as MetaTrader 5's built-in version — on purpose. The four points below are what differs, and all four can be read from the very moving_average.mq5 you download.

Calls a built-in indicator
No — the calculation sits in OnCalculate and can be copied straight into an EA
External libraries
Not a single #import declaration
Code length
52 lines · 2 input parameters
How it draws
Drawn over the price chart · calculated incrementally, does not redraw the past

Why this library promises no secret formula — read the full explanation on the library page.

// COMBINATIONS

Which indicators to pair Moving Average with, and what you gain

Each indicator only answers one question. Pairing well means covering the right weakness, not stacking more.

What you gain
An on/off filter. The MA gives the direction, ADX says whether that direction has enough strength to continue — only follow MA signals when ADX is above 25.
Note
This pair fixes the MA's biggest weakness: the run of false signals during sideways phases.
See Average Directional Index
What you gain
Enter with the trend on pullbacks. The MA sets the allowed direction, RSI shows the pullback into the oversold zone within an uptrend.
Note
Only take RSI signals in the MA's direction. In this pair, an RSI signal against the trend should be ignored.
See Relative Strength Index
Moving AverageAverage True Range
What you gain
A stop-loss distance that flexes with the market. The MA decides whether to enter, ATR decides how far away the stop goes.
Note
ATR gives no signal at all; it only sets size. Do not wait for ATR to say buy or sell.
See Average True Range
// FAQ

Frequently asked questions about Moving Average

Should I use SMA or EMA?

EMA reacts faster because it weights recent candles more, which suits short timeframes. SMA is smoother with fewer false signals, which suits higher timeframes. The sample file has an InpMethod parameter to switch between the two without editing the code.

What MA period is best?

There is no ready-made best number. 20 and 50 are a common pair for medium-term trends, 200 is the long-term trend mark. More important than the number is keeping it long enough to evaluate, instead of changing it after a few losing trades.

Does Moving Average on this page use real or simulated data?

The chart on this page uses a deterministic simulated candle series; it is not market prices or trading results.

// SAME_GROUP

Other trend indicators

Indicators in the same trend group, often used instead of or alongside Moving Average.

Turn Moving Average into an automated EA

An indicator draws signals, but when to enter, where to stop out and how much to risk are still your set of rules. Send those rules and DinexSpace builds them into an EA.

Use this indicator to create a Custom EA brief