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.
#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;
}#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;
}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");//@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.