Category Oscilators  Published on 15/07/2020

Mean Reversion Momentum

Description

Just Another idea to explore and inprove


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 MeanReversionMomentum : 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 double buyAverage;
        private double sellAverage;
        private int savedbp;
        private int savedsp;

        private IndicatorDataSeries main;

        protected override void Initialize()
        {
            tf = MarketData.GetBars(Bars.TimeFrame);
            main = 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 = buySum / buyPeriod;
            sellAverage = sellSum / sellPeriod;

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

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

            main[index] = 2 * Bars.ClosePrices[index] - Bars.OpenPrices[index - sellPeriod] - Bars.OpenPrices[index - buyPeriod];

            Result1[index] = main.Maximum(sellPeriod);
            Result2[index] = main.Minimum(buyPeriod);
            Result3[index] = main[index];


            previousIdx = idx;
        }
    }
}


srm_bcn's avatar
srm_bcn

Joined on 01.09.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: MeanReversionMomentum.algo
  • Rating: 5
  • Installs: 1300
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
VA
vaguthun · 3 months ago

Hi, Thank you for your kind contribution.

Can you please explain the logic behind this indicator.

I really like the concept of momentum trading. I think this is an interesting approach.

Regards