Moving average API

MQL4 iMA and iMAOnArray explained

Use iMA when the moving average source is symbol and timeframe price data. Use iMAOnArray when the source is a numeric array you prepared. iMA includes symbol, timeframe, applied price, and bar shift; iMAOnArray depends on array order and calculates from left to right unless you change series indexing.

Open the Coding Agent, then choose MQL4 from the language menu.

MovingAverageValues.mq4.mq4
#property strict

void OnStart()
  {
   if(Bars < 30)
     {
      Print("Not enough chart history.");
      return;
     }

   double chart_ema = iMA(NULL, 0, 20, 0,
                          MODE_EMA, PRICE_CLOSE, 1);

   double values[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
   double array_sma = iMAOnArray(values, 5, 3, 0, MODE_SMA, 0);

   PrintFormat("Closed-bar EMA=%.5f Array SMA=%.2f",
               chart_ema, array_sma);
  }
  • iMA reads chart or symbol price data
  • iMAOnArray reads a prepared array
  • ma_shift moves the plotted line
  • shift chooses the returned value

Do not confuse ma_shift with bar shift

In iMA, ma_shift offsets the moving-average line relative to the chart. The final shift argument selects which calculated bar value the function returns. Using ma_shift to request the previous bar changes the indicator definition instead of selecting history.

The method comes from ENUM_MA_METHOD and the source price comes from ENUM_APPLIED_PRICE. These choices must match the chart indicator used for comparison.

ArgumentMeaningTypical value
ma_periodNumber of values in the average20
ma_shiftHorizontal line offset0
ma_methodSMA, EMA, SMMA, or LWMAMODE_EMA
applied_pricePrice series used by iMAPRICE_CLOSE
shiftCurrent or historical returned bar1 for latest closed bar

iMAOnArray inherits your array contract

iMAOnArray does not choose a symbol, timeframe, or applied price. The caller owns the data, element count, and indexing direction. The function calculates left to right by default.

If the array is treated as a series, use ArraySetAsSeries deliberately and verify the returned shift against a hand-calculated sample. Do not mix a normal array assumption with a reverse-indexed indicator buffer.

  • Prepare enough values for the averaging period
  • Pass 0 as total only when the full array is intended
  • Keep logical array size separate from reserve capacity
  • Compare several known values before using the output in a signal

A crossover needs two bars and explicit timing

A crossover is not established by fast greater than slow on one bar. The rule usually compares both averages on the current decision bar and the preceding bar. Define whether those bars are closed before connecting the result to an order.

Validate sufficient history for the slow period plus the comparison bars, then log values before adding trade execution.

Review the complete example

The first call reads a 20-period EMA from the latest closed chart bar. The second calculates a 3-element simple moving average on a prepared left-to-right array.

The example calculates values only. A crossover or trading rule needs additional history guards, signal timing, order ownership, and execution tests.

MovingAverageValues.mq4.mq4
#property strict

void OnStart()
  {
   if(Bars < 30)
     {
      Print("Not enough chart history.");
      return;
     }

   double chart_ema = iMA(NULL, 0, 20, 0,
                          MODE_EMA, PRICE_CLOSE, 1);

   double values[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
   double array_sma = iMAOnArray(values, 5, 3, 0, MODE_SMA, 0);

   PrintFormat("Closed-bar EMA=%.5f Array SMA=%.2f",
               chart_ema, array_sma);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Choose iMA or iMAOnArray

    Use chart data for iMA and caller-prepared numeric data for iMAOnArray.

  2. 2

    Set the averaging contract

    Choose period, method, source, and line shift.

  3. 3

    Choose the returned bar

    Use the final shift argument for current or historical values.

  4. 4

    Compare known output

    Log and compare the value against MT4 or a hand-calculated array.

Frequently asked questions

What does iMA return in MQL4?

iMA returns one moving-average value for the chosen symbol, timeframe, method, applied price, and bar shift.

What is the difference between iMA and iMAOnArray?

iMA reads symbol price data. iMAOnArray calculates on a numeric array prepared by the caller.

What is ma_shift in iMA?

ma_shift horizontally offsets the moving-average line. It is not the historical bar index returned by the function.

Does iMAOnArray calculate as a timeseries?

It calculates left to right by default. Use ArraySetAsSeries only when reverse indexing is intentionally required.

Which shift should a closed-bar crossover use?

A closed-bar rule commonly compares shifts 1 and 2, but the exact timing must match the strategy specification.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent