Category Trend  Published on 08/04/2023

Mogalef Bands indicator

Description

The technical indicator Mogalef Bands projects the high, median, and low levels of the most probable range into the future by taking into account the market environment. This indicator can be applied immediately to any financial instrument you may be working with.

The indicator checks the values of linear regression, compares them to the extremes of past deviation bands, and if a breakout occurs in either direction, it adjusts the bands to the most probable range in which the price will fluctuate.

Usage; it can be used as a target for breakouts or to set take profit/stop losses


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class mMogalefBands : Indicator
    {
        [Parameter("Period Linear Regression (3)", DefaultValue = 3)]
        public int inpPeriodLinearRegression { get; set; }
        [Parameter("Period Standard Deviation (7)", DefaultValue = 7)]
        public int inpPeriodStandardDeviation { get; set; }
        [Parameter("Multiplier (2.0)", DefaultValue = 2.0)]
        public double inpMultiplier { get; set; }

        [Output("Mogalef Median", LineColor = "FF33C1F3", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries outCentral { get; set; }
        [Output("Mogalef High", LineColor = "FF33C1F3", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries outTop { get; set; }
        [Output("Mogalef Low", LineColor = "FF33C1F3", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries outBottom { get; set; }
        
        private IndicatorDataSeries _price, _central, _top, _bottom;
        private double x, y, xy, x2, m, yint;
        private StandardDeviation _stddev;
        

        protected override void Initialize()
        {
            _price = CreateDataSeries();
            _central = CreateDataSeries();
            _top = CreateDataSeries();
            _bottom = CreateDataSeries();
            _stddev = Indicators.StandardDeviation(Bars.ClosePrices, inpPeriodStandardDeviation, MovingAverageType.Simple);
        }

        public override void Calculate(int i)
        {
            _price[i] = (2.0 * Bars.ClosePrices[i] + Bars.OpenPrices[i] + Bars.HighPrices[i] + Bars.LowPrices[i]) / 5.0;

            x = y = xy = x2 = 0;
            for(int j=0; j<inpPeriodLinearRegression; j++)
            {
                y += _price[i-j];
                xy += _price[i-j] * j;
                x += j;
                x2 += j * j;
            }

            m = (inpPeriodLinearRegression * xy - x * y) / (inpPeriodLinearRegression * x2 - x * x);
            yint = (y + m * x) / inpPeriodLinearRegression;
            _central[i] = yint - m * inpPeriodLinearRegression;
            _top[i] = _central[i] + inpMultiplier * _stddev.Result[i];
            _bottom[i] = _central[i] - inpMultiplier * _stddev.Result[i];

            if(_central[i]<_top[i-1] && _central[i]>_bottom[i-1])
            {
                _central[i] = _central[i-1];
                _top[i] = _top[i-1];
                _bottom[i] = _bottom[i-1];
            }
                  
            outCentral[i] = _central[i];
            outTop[i] = _top[i];
            outBottom[i] = _bottom[i];
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mMogalefBands.algo
  • Rating: 5
  • Installs: 525
Comments
Log in to add a comment.
No comments found.