Category Trend  Published on 07/03/2020

Variable Moving Average

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!


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];
        }
    }
}


CY
cysecsbin.01

Joined on 10.11.2018 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: VariableMovingAverage.algo
  • Rating: 5
  • Installs: 1708
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
No comments found.