Hello this is the implementation of the formula from John Ehlers Projected moving average indicator, a new simple but elegant solution to remove the lag of a moving average:
// Parameters
Length = Param("Length", 20, 5, 50, 1);
// Array initialization
PMA = Null;
Slope_val = Null;
SMA_val = Null;
Predict = Null;
// bars calculation
for (i = Length - 1; i < BarCount; i++)
{
// variables initialization
Sx = 0;
Sy = 0;
Sxx = 0;
Syy = 0;
Sxy = 0;
// Cycle to calculate sums
for (count = 1; count <= Length; count++)
{
Sx = Sx + count;
Sy = Sy + Close[i - (count - 1)];
Sxx = Sxx + count * count;
Syy = Syy + Close[i - (count - 1)] * Close[i - (count - 1)];
Sxy = Sxy + count * Close[i - (count - 1)];
}
// Slope Calculation
Slope_val[i] = -(Length * Sxy - Sx * Sy) / (Length * Sxx - Sx * Sx);
// SMA Calculation
SMA_val[i] = Sy / Length;
// PMA Calculation
PMA[i] = SMA_val[i] + Slope_val[i] * Length / 2;
// Predict Calculation
if (i >= Length + 1) // Slope[i-2] at least
{
Predict[i] = PMA[i] + 0.5 * (Slope_val[i] - Slope_val[i-2]) * Length;
}
}
// Plot results
Plot(PMA, "PMA(" + Length + ")", coloryellow, styleLine | styleThick);
Plot(Predict, "Predict", colorRed, styleLine);
Plot(Close, "Close", colorBlack, styleCandle);
// Plot optional for SMA
// Plot(SMA_val, "SMA", colorGreen, styleLine);