Skip to main content
BTC / USDTCRYPTO107,400+2.19%ETH / USDTCRYPTO3,840+2.13%SOL / USDTCRYPTO182.40−1.99%BNB / USDTCRYPTO652.30+0.66%XRP / USDTCRYPTO2.2150+1.61%DOGE / USDTCRYPTO0.3850−1.79%TON / USDTCRYPTO5.240+2.34%AVAX / USDTCRYPTO42.60−2.07%LINK / USDTCRYPTO22.40+2.28%ADA / USDTCRYPTO1.0520−1.68%TRX / USDTCRYPTO0.3300+0.92%DOT / USDTCRYPTO8.420+2.93%BTC / USDTCRYPTO107,400+2.19%ETH / USDTCRYPTO3,840+2.13%SOL / USDTCRYPTO182.40−1.99%BNB / USDTCRYPTO652.30+0.66%XRP / USDTCRYPTO2.2150+1.61%DOGE / USDTCRYPTO0.3850−1.79%TON / USDTCRYPTO5.240+2.34%AVAX / USDTCRYPTO42.60−2.07%LINK / USDTCRYPTO22.40+2.28%ADA / USDTCRYPTO1.0520−1.68%TRX / USDTCRYPTO0.3300+0.92%DOT / USDTCRYPTO8.420+2.93%
Prezzi

EMA Cross

Fast/slow exponential moving average trend filter — MT4, MT5, TradeStation and TradingView code.

Two exponential moving averages of different lengths: the faster one reacts sooner. When the fast EMA crosses above the slow EMA it signals emerging bullish momentum, and below it, bearish — a simple, widely used trend-following filter. Copy the version for your platform.

MQL4 · EMACross.mq4
#property indicator_chart_window
#property indicator_buffers 2

extern int Fast_Period = 9;
extern int Slow_Period = 21;
double FastBuffer[], SlowBuffer[];

int OnInit() {
  SetIndexBuffer(0, FastBuffer);
  SetIndexBuffer(1, SlowBuffer);
  IndicatorShortName("EMA Cross(" + Fast_Period + "," + Slow_Period + ")");
  return 0;
}

int OnCalculate(const int rates_total, const int prev_calculated, const double &close[]) {
  for (int i = prev_calculated; i < rates_total; i++) {
    FastBuffer[i] = iMA(NULL, 0, Fast_Period, 0, MODE_EMA, PRICE_CLOSE, i);
    SlowBuffer[i] = iMA(NULL, 0, Slow_Period, 0, MODE_EMA, PRICE_CLOSE, i);
  }
  return rates_total;
}
MQL5 · EMACross.mq5
#property indicator_chart_window
#property indicator_buffers 2

input int Fast_Period = 9;
input int Slow_Period = 21;
double FastBuffer[], SlowBuffer[];

int OnInit() {
  SetIndexBuffer(0, FastBuffer);
  SetIndexBuffer(1, SlowBuffer);
  return INIT_SUCCEEDED;
}

int OnCalculate(const int rates_total, const int prev_calculated,
  const datetime &time[], const double &open[], const double &high[],
  const double &low[], const double &close[], const long &tick_volume[],
  const long &volume[], const int &spread[]) {
  for (int i = prev_calculated; i < rates_total; i++) {
    FastBuffer[i] = iMA(NULL, 0, Fast_Period, 0, MODE_EMA, PRICE_CLOSE, i);
    SlowBuffer[i] = iMA(NULL, 0, Slow_Period, 0, MODE_EMA, PRICE_CLOSE, i);
  }
  return rates_total;
}
EasyLanguage
inputs: FastLen(9), SlowLen(21);
variables: FastMA(0), SlowMA(0);
FastMA = XAverage(Close, FastLen);
SlowMA = XAverage(Close, SlowLen);
Plot1(FastMA, "Fast EMA");
Plot2(SlowMA, "Slow EMA");
Pine Script
//@version=5
indicator("EMA Cross", overlay=true)
fast = ta.ema(close, input.int(9, "Fast"))
slow = ta.ema(close, input.int(21, "Slow"))
plot(fast, title="Fast EMA", color=color.green, linewidth=2)
plot(slow, title="Slow EMA", color=color.red, linewidth=2)

To use it inside dtcharts, paste any version into the Script Converter and it becomes a native indicator on your chart.