A weighted moving average weights recent bars more heavily than older ones (weights N, N−1, … 1), so it turns faster than a simple MA while staying smoother than an EMA. Traders use it as a responsive trend line and in fast/slow crossover systems. Copy the version for your platform.
//@version=5
indicator("Weighted Moving Average", overlay=true)
length = input.int(20, "Length")
wma = ta.wma(close, length)
plot(wma, title="WMA", color=color.green, linewidth=2)#property indicator_chart_window
#property indicator_buffers 1
extern int WMA_Period = 20;
double WmaBuffer[];
int OnInit() {
SetIndexBuffer(0, WmaBuffer);
return 0;
}
int OnCalculate(const int rates_total, const int prev_calculated, const double &close[]) {
for (int i = prev_calculated; i < rates_total; i++)
WmaBuffer[i] = iMA(NULL, 0, WMA_Period, 0, MODE_LWMA, PRICE_CLOSE, i);
return rates_total;
}#property indicator_chart_window
#property indicator_buffers 1
input int WMA_Period = 20;
double WmaBuffer[];
int OnInit() {
SetIndexBuffer(0, WmaBuffer);
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++)
WmaBuffer[i] = iMA(NULL, 0, WMA_Period, 0, MODE_LWMA, PRICE_CLOSE, i);
return rates_total;
}inputs: Length(20);
variables: MyWMA(0);
MyWMA = WAverage(Close, Length);
Plot1(MyWMA, "Weighted MA");{Weighted Moving Average - period 20}
wma := Mov(C, 20, W);
wmaTo use it inside dtcharts, paste any version into the Script Converter and it becomes a native indicator on your chart.