Category Trend  Published on 31/07/2016

ZigZag

Description

Another ZigZag indicator for CTrader.

I found that the other implementations available were incorrect and written with more lines of code than necessary.

http://www.onlinetradingconcepts.com/TechnicalAnalysis/ZigZag.htm
 


// -------------------------------------------------------------------------------------------------
//
//    The ZigZag indicator filters out small price movements below a percentage threshold
//    
//    Author: Michael Ourednik
//    mike.ourednik at gmail dot com
//
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AutoRescale = true, AccessRights = AccessRights.None)]
    public class ZigZag : Indicator
    {
        [Parameter("DeviationPercent", DefaultValue = 0.3, MinValue = 0.01)]
        public double deviationPercent { get; set; }

        [Output("ZigZag", Color = Colors.LightGray, Thickness = 1, PlotType = PlotType.Line)]
        public IndicatorDataSeries Value { get; set; }

        enum Direction
        {
            up,
            down
        }

        private Direction direction = Direction.down;
        private double extremumPrice = 0.0;
        private int extremumIndex = 0;

        private void moveExtremum(int index, double price)
        {
            Value[extremumIndex] = Double.NaN;
            setExtremum(index, price);
        }

        private void setExtremum(int index, double price)
        {
            extremumIndex = index;
            extremumPrice = price;
            Value[extremumIndex] = extremumPrice;
        }

        public override void Calculate(int index)
        {
            double low = MarketSeries.Low[index];
            double high = MarketSeries.High[index];

            if (extremumPrice == 0.0)
                extremumPrice = high;

            if (MarketSeries.Close.Count < 2)
                return;

            if (direction == Direction.down)
            {
                if (low <= extremumPrice)
                    moveExtremum(index, low);
                else if (high >= extremumPrice * (1.0 + deviationPercent * 0.01))
                {
                    setExtremum(index, high);
                    direction = Direction.up;
                }
            }
            else
            {
                if (high >= extremumPrice)
                    moveExtremum(index, high);
                else if (low <= extremumPrice * (1.0 - deviationPercent * 0.01))
                {
                    setExtremum(index, low);
                    direction = Direction.down;
                }
            }
        }
    }
}


MI
mike.ourednik

Joined on 30.07.2016

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ZigZag.algo
  • Rating: 3.33
  • Installs: 7379
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
cibornowak97's avatar
cibornowak97 · 2 months ago

!!!

TH
thomas.myinvestments · 9 months ago

Note to future guys trying this, it doesn't work, doesn't show on the chart at all

SK
sktradingfx · 8 years ago

Hi Mike,

nice implementation of zig zag. Highly appreciated!

Would it be possible to display the following parameters for each leg of the zig zag:

  • time taken to develop the leg,
  • pips from high to low or low to high of the leg,
  • tick volume of the leg.

Thanks a lot in advance!

Cheers

sk