Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Indicator | HTF EMA No Lookahead | Source Code Included

Explore the latest free tools from the MQL5 community. Here's a new indicator, expert advisor, or script for MetaTrader.

A common way to show a higher-timeframe moving average on a lower-timeframe chart is to take the value of the higher-timeframe bar that contains the current bar. On history this looks fine, but it is a hidden lookahead: on an H1 chart at 10:00, the H4 bar that started at 08:00 is not closed yet, and its EMA value is only known at 12:00. An indicator or an Expert Advisor that uses it in the past sees information from the future, and the backtest looks better than live trading.

This indicator draws the higher-timeframe EMA using only data that was available at that moment. For every chart bar it takes the last higher-timeframe bar that closed strictly before the chart bar ends. The result is a step line that never repaints: what you see on history is exactly what you would have seen live.

Image for HTF EMA No Lookahead

Experience adaptive trading with the Fluid Expert Advisor for MT4/MT5. Dynamic money management and trend detection. Click for info.

Input parameters

  • Higher timeframe (default H4). If it is not higher than the chart timeframe, a normal EMA of the chart is drawn.
  • Fast EMA period (default 50, 0 = off).
  • Slow EMA period (default 200, 0 = off).
  • Applied price (default Close).

Buffers for Expert Advisors

  • 0 = fast EMA
  • 1 = slow EMA
  • 2 = trend: +1 fast above slow, -1 fast below slow, 0 = no data
int h = iCustom(_Symbol, PERIOD_H1, "HTF_EMA_NoLookahead", PERIOD_H4, 50, 200, PRICE_CLOSE);
double trend[1];
//--- bar 1 = last closed bar
if(CopyBuffer(h, 2, 1, 1, trend) == 1 && trend[0] > 0)
  {
   //--- H4 trend is up
  }

How it was checked

On 2000 bars of XAUUSD H1 every value was compared with an independent calculation (iBarShift of the last second of the H1 bar, then the previous H4 bar). There were no differences. The indicator loads higher-timeframe history in the background and redraws itself when it arrives, also at the weekend when there are no ticks.

Level up your trading with professional RobotFX expert advisors and indicators. Visit robotfx.org for proven MT4/MT5 tools.

80298

Best MetaTrader Indicators + Profitable Expert Advisors