Variable Moving Average free

Description

Simple conversion (from LazyBear) of a fascinating MA: the variable MA.

It is a self-adjusting, volatility-based moving average. Is it useful? i don't have the slightest idea, probably not; is it cool? you can bet on it!

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
´╗┐using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class VariableMovingAverage : Indicator
    {
        [Parameter(DefaultValue = 6)]
        public int Period { get; set; }

        [Output("Main", LineColor = "Cyan")]
        public IndicatorDataSeries Result { get; set; }

        double K = 1;
        IndicatorDataSeries pdmS, mdmS, pdiS, mdiS, iS, tempResult;
        protected override void Initialize()
        {
            K /= Period;
            pdmS = CreateDataSeries();
            mdmS = CreateDataSeries();
            pdiS = CreateDataSeries();
            mdiS = CreateDataSeries();
            iS = CreateDataSeries();
            tempResult = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            double pdm = Math.Max(Bars[index].Close - Bars[index - 1].Close, 0), mdm = Math.Max(Bars[index - 1].Close - Bars[index].Close, 0);
            pdmS[index] = ((1 - K) * (double.IsNaN(pdmS[index - 1]) ? 0 : pdmS[index - 1]) + K * pdm);
            mdmS[index] = ((1 - K) * (double.IsNaN(mdmS[index - 1]) ? 0 : mdmS[index - 1]) + K * mdm);
            double pdi = pdmS[index] / (pdmS[index] + mdmS[index]);
            double mdi = mdmS[index] / (pdmS[index] + mdmS[index]);
            pdiS[index] = ((1 - K) * (double.IsNaN(pdiS[index - 1]) ? 0 : pdiS[index - 1]) + K * pdi);
            mdiS[index] = ((1 - K) * (double.IsNaN(mdiS[index - 1]) ? 0 : mdiS[index - 1]) + K * mdi);
            iS[index] = ((1 - K) * (double.IsNaN(iS[index - 1]) ? 0 : iS[index - 1]) + K * Math.Abs(pdiS[index] - mdiS[index]) / (pdiS[index] + mdiS[index]));
            double hhv = iS.Maximum(Period);
            double llv = iS.Minimum(Period);
            tempResult[index] = (1 - K * (iS[index] - llv) / (hhv - llv)) * (double.IsNaN(tempResult[index - 1]) ? 0 : tempResult[index - 1]) + Bars[index].Close * K * (iS[index] - llv) / (hhv - llv);
            if (index > Period * 10)
                Result[index] = tempResult[index];
        }
    }
}
Comments
0