Category Oscilators  Published on 15/07/2020

Mean Reversion Averages Momentum

Description

What about averages momentum? 


using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MeanReversionAveragesMomentum : Indicator
    {
        [Output("Result1", LineColor = "LightCoral")]
        public IndicatorDataSeries Result1 { get; set; }

        [Output("Result2", LineColor = "lightGreen")]
        public IndicatorDataSeries Result2 { get; set; }

        [Output("Result3", LineColor = "Aqua")]
        public IndicatorDataSeries Result3 { get; set; }

        private Bars tf;

        private int idx;
        private int previousIdx;
        private int buyPeriod;
        private int sellPeriod;

        private double buySum;
        private double sellSum;
        private IndicatorDataSeries buyAverage;
        private IndicatorDataSeries sellAverage;
        private int savedbp;
        private int savedsp;

        protected override void Initialize()
        {
            tf = MarketData.GetBars(Bars.TimeFrame);
            buyAverage = CreateDataSeries();
            sellAverage = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            idx = tf.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            if (idx > previousIdx)
            {
                buyPeriod++;
                savedbp = buyPeriod;
                sellPeriod++;
                savedsp = sellPeriod;
            }

            if (buyPeriod == 0)
                buyPeriod = savedbp;

            buySum = 0;
            for (int i = index - buyPeriod + 1; i <= index; i++)
                buySum += Bars.OpenPrices[i];

            if (sellPeriod == 0)
                sellPeriod = savedsp;

            sellSum = 0;
            for (int i = index - sellPeriod + 1; i <= index; i++)
                sellSum += Bars.OpenPrices[i];

            buyAverage[index] = buySum / buyPeriod;
            sellAverage[index] = sellSum / sellPeriod;

            if (Bars.ClosePrices[index] > buyAverage[index])
            {
                buyAverage[index] = Bars.ClosePrices[index];
                buyPeriod = 0;
            }

            if (Bars.ClosePrices[index] < sellAverage[index])
            {
                sellAverage[index] = Bars.ClosePrices[index];
                sellPeriod = 0;
            }


            Result1[index] = sellAverage[index] - sellAverage[index - sellPeriod];
            Result2[index] = buyAverage[index] - buyAverage[index - buyPeriod];
            Result3[index] = Result1[index] + Result2[index];


            previousIdx = idx;
        }
    }
}


srm_bcn's avatar
srm_bcn

Joined on 01.09.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: MeanReversionAveragesMomentum.algo
  • Rating: 0
  • Installs: 1465
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
KA
kashifs · 1 year ago

At least put some points or comments as to how does it work, when you create an indicator