Category Trend  Published on 03/04/2023

Standard Error Bands indicator

Description

The Standard Error Bands indicator is a smoothed linear regression line, where the period linear regression curve is smoothed by a three-period simple moving average.

The upper channel line is the linear regression line plus 2 standard errors, while the lower channel line is the linear regression line minus 2 standard errors.

The indicator was described by John Andersen in the "Stocks and Commodities" magazine in September 1996.


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class mStandardErrorBands : Indicator
    {
        [Parameter("Regression Periods (20)", DefaultValue = 20)]
        public int inpPeriodsRegression { get; set; }
        [Parameter("Smooth Periods (3)", DefaultValue = 3)]
        public int inpPeriodsSmooth { get; set; }
        [Parameter("Multiplier (2.0)", DefaultValue = 2.0)]
        public double inpMultiplier { get; set; }
        [Parameter("Data Source (close)")]
        public DataSeries inpDataSource { get; set; }

        [Output("Standard Error Bands Top", LineColor = "Green", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outSEBtop { get; set; }
        [Output("Standard Error Bands Main", LineColor = "Silver", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outSEBmain { get; set; }
        [Output("Standard Error Bands Bottom", LineColor = "Red", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outSEBbottom { get; set; }
        
        private MovingAverage _pr, _masmooth;
        private IndicatorDataSeries _lreg;
        private StandardDeviation _stddev;
        double x, y, xy, x2, m;
        

        protected override void Initialize()
        {
            _pr = Indicators.MovingAverage(inpDataSource, 1, MovingAverageType.Simple);
            _lreg = CreateDataSeries();
            _stddev = Indicators.StandardDeviation(_lreg, inpPeriodsSmooth, MovingAverageType.Simple);
            _masmooth = Indicators.MovingAverage(_lreg, inpPeriodsSmooth, MovingAverageType.Simple);
        }

        public override void Calculate(int i)
        {
            x = y = xy = x2 = 0;
            for(int j=0; j<inpPeriodsRegression && i>inpPeriodsRegression; j++)
            {
                y += _pr.Result[i-j];
                xy += _pr.Result[i-j] * j;
                x += j;
                x2 += j * j;
            }

            m = (inpPeriodsRegression * xy - x * y) / (inpPeriodsRegression * x2 - x * x);
            _lreg[i] = i>inpPeriodsRegression ? ((y + m * x) / inpPeriodsRegression) - m * inpPeriodsRegression : Bars.TypicalPrices[i];
            
            outSEBmain[i] = _masmooth.Result[i];
            outSEBtop[i] = _masmooth.Result[i] + (inpMultiplier * _stddev.Result[i]);
            outSEBbottom[i] = _masmooth.Result[i] - (inpMultiplier * _stddev.Result[i]);
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mStandardErrorBands.algo
  • Rating: 5
  • Installs: 532
  • Modified: 01/04/2023 16:02
Comments
Log in to add a comment.
No comments found.