Category Trend  Published on 09/05/2016

ZigZag Linear Regression Channels

Description

DonateDescription:

  • Based on SwingZigZag and LinearRegressionChannel indicators.
  • The Indicator tries to find and plot potential channels in price movement.
  • Channels repaint with the new swing of current direction (just same as ZigZag indicator).
  • Experimental version. Any feedback and suggestions appreciated.

 

Updates:

  • 22/03/2016 - Released.

 

Inputs:

  • Period - Defines period for ZigZag calculations.

 

Screenshots:

  • SwingZigZag

  • LinearRegressionChannel

 

Make a Donation

  • If you like my work and effort then please consider to make a kind donation thru PayPal or any Credit Card at the top right corner.

//#reference: SwingZigZag.algo

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 ZZ_LRC : Indicator
    {
        [Parameter(DefaultValue = 10)]
        public int Period { get; set; }

        private IndicatorDataSeries zzSeries;
        private SwingZigZag zz;

        protected override void Initialize()
        {
            zzSeries = CreateDataSeries();
            zz = Indicators.GetIndicator<SwingZigZag>(Period);
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar)
                return;

            int x1 = -1;
            int x2 = -1;

            for (int i = 1; i < MarketSeries.Open.Count; i++)
            {
                zzSeries[i] = zz.ZigZag[i];

                if (!double.IsNaN(zzSeries[i]))
                {
                    if (x1 == -1 || x2 == -1)
                    {
                        x1 = i;
                        x2 = i;
                    }
                    else
                    {
                        x1 = x2 - 1;
                        x2 = i;
                    }

                    var Source = MarketSeries.Close;
                    int period = x2 - x1 + 1;

                    double sumX = 0;
                    double sumX2 = 0;
                    double sumY = 0;
                    double sumXY = 0;

                    for (int count = x1; count <= x2; count++)
                    {
                        sumX += count;
                        sumX2 += count * count;
                        sumY += Source[count];
                        sumXY += Source[count] * count;
                    }

                    double divisor = (period * sumX2 - sumX * sumX);
                    double slope = (period * sumXY - sumX * sumY) / divisor;
                    double intercept = (sumY - slope * sumX) / period;

                    double deviation = 0;

                    for (int count = x1; count <= x2; count++)
                    {
                        double regression = slope * count + intercept;
                        deviation = Math.Max(Math.Abs(Source[count] - regression), deviation);
                    }


                    double y1 = slope * x1 + intercept;
                    double y2 = slope * x2 + intercept;


                    ChartObjects.DrawLine("Middle" + x1, x1, y1, x2, y2, Colors.Black, 0.5, LineStyle.Lines);
                    ChartObjects.DrawLine("Upper" + x1, x1, y1 + deviation, x2, y2 + deviation, Colors.Black, 1, LineStyle.Solid);
                    ChartObjects.DrawLine("Lower" + x1, x1, y1 - deviation, x2, y2 - deviation, Colors.Black, 1, LineStyle.Solid);
                }
            }
        }
    }
}


Jiri's avatar
Jiri

Joined on 31.08.2015

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: .ZigZag LinearRegressionChannel.algo
  • Rating: 4
  • Installs: 4282
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
Jiri's avatar
Jiri · 7 years ago

Hi, you need to install the SwingZigZag indicator.

BJ
bjorn · 8 years ago

Hello !

Seems interesting job you've done. 

However, in trying to run it, there is complaint about "SwingZigZag zz". 

What has to be done to make it run according to the display of the historical channels?'

Sincerely, 

B Bernau 

Sweden

BJ
bjorn · 8 years ago

Hello !

Seems interesting job you've done. 

However, in trying to run it, there is complaint about "SwingZigZag zz". 

What has to be done to make it run according to the display of the historical channels?'

Sincerely, 

B Bernau 

Sweden

johnwboyd's avatar
johnwboyd · 8 years ago

Looks interesting.