Category Trend  Published on 12/09/2021

ZigZag Percentage

Description

This indicator shows us the percentage of  retrace or expand of waves And it determines the Fibonacci ratios for you

zigzag is a awesome tool but we know it needs a little more to be extremely useful

I have created this indicator to give us a better and more accurate view of market relations

You can select the color of the retracement ratios and the expansion ratios separately

You can recognize harmonic patterns very quickly and easily

Using Fibonacci ratios will also be much easier for you

you can download source code here

but if you want to support us for more tools :

 



As you can see, it is much easier to understand the market movements with this indicator

and with percentage you can Diagnosis patterns very easy 

There are many customization options

 

 

 

 



//
// hello i am trader252 
//


using System;
using cAlgo.API;


namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AutoRescale = true, AccessRights = AccessRights.None)]
    public class ZigZag : Indicator
    {

        [Parameter()]
        public DataSeries High { get; set; }
        [Parameter()]
        public DataSeries Low { get; set; }
        [Parameter("Deviation", DefaultValue = 50, MinValue = 1)]
        public int deviationValue { get; set; }


        [Parameter("Errore", Group = "fibonacci Ratios", DefaultValue = 2, MinValue = 1)]
        public int errore { get; set; }
        [Parameter("normal Ratios", Group = "fibonacci Ratios ", DefaultValue = "Black")]
        public string normColor { get; set; }

        [Parameter("Fibo retracement Ratios", Group = "fibonacci Ratios", DefaultValue = "Green")]
        public string retColor { get; set; }
        [Parameter("Fibo extension Ratios", Group = "fibonacci Ratios ", DefaultValue = "Red")]
        public string exColor { get; set; }






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

        [Output("Swing High", Color = Colors.Gray, Thickness = 1, PlotType = PlotType.Line, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries SwingHigh { get; set; }

        [Output("Swing Low", Color = Colors.Gray, Thickness = 1, PlotType = PlotType.Line, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries SwingLow { get; set; }


        private const VerticalAlignment vAlign = VerticalAlignment.Top;
        private const HorizontalAlignment hAlign = HorizontalAlignment.Center;
        private Colors Colornorm;
        private Colors Colorret;
        private Colors Colorex;
        private double currentZigZagHigh = 0;
        private double currentZigZagLow = 0;
        private int lastSwingIndex = -1;
        private double lastSwingPrice = 0.0;
        private int trendDir = 0;
        private bool newnewhigh = false;
        private bool newnewlow = false;
        private bool afterbuglow = false;
        private bool afterbughigh = false;
        private double oneerorlownewdownv;
        private double oneerorlownewupv;
        private double thwoerorlownewdownv;
        private double thwoerorlownewupv;
        private int lasthigh = 0;
        private int lastlow = 0;
        private double downv;
        private double upv;
        private double newdownv;
        private double newupv;

        private double newnewdownv;
        private double newnewupv;
        private int htimmarker = 0;
        private int ltimmarker = 0;
        private int newhtimmarker = 0;
        private int newltimmarker = 0;
        private int CurrentBar;
        private double tickSize;
        private IndicatorDataSeries zigZagHighZigZags, zigZagLowZigZags;



        protected override void Initialize()
        {
            if (!Enum.TryParse(normColor, out Colornorm))
            {
                ChartObjects.DrawText("errorMsg1", "Invalid Color for normal Ratios (It should start with a capital letter)", StaticPosition.TopLeft, Colors.Red);
                Colornorm = Colors.Black;
            }

            if (!Enum.TryParse(retColor, out Colorret))
            {
                ChartObjects.DrawText("errorMsg2", "\nInvalid Color for Fibo retracement Ratios (It should start with a capital letter)", StaticPosition.TopLeft, Colors.Red);
                Colorret = Colors.Green;
            }
            if (!Enum.TryParse(exColor, out Colorex))
            {
                ChartObjects.DrawText("errorMsg3", "\n" + "\nInvalid Color for Fibo extension Ratios (It should start with a capital letter)", StaticPosition.TopLeft, Colors.Red);
                Colorex = Colors.Red;
            }
            zigZagHighZigZags = CreateDataSeries();
            zigZagLowZigZags = CreateDataSeries();

            tickSize = Symbol.TickSize;
        }

        public override void Calculate(int index)
        {
            CurrentBar = High.Count;
            string arrowName;
            Colors color;
            if (CurrentBar < 2)
            {
                zigZagHighZigZags[index] = 0;
                zigZagLowZigZags[index] = 0;
                return;
            }

            if (lastSwingPrice == 0.0)
                lastSwingPrice = Low[index] + (High[index] - Low[index]) / 2;

            bool isSwingHigh = High[index] >= High[index] - double.Epsilon && High[index] >= High[index - 1] - double.Epsilon;
            bool isSwingLow = Low[index] <= Low[index] + double.Epsilon && Low[index] <= Low[index - 1] + double.Epsilon;
            bool isOverHighDeviation = IsPriceGreater(High[index], (lastSwingPrice * (1.0 + deviationValue * 0.0001)));
            bool isOverLowDeviation = IsPriceGreater(lastSwingPrice * (1.0 - deviationValue * 0.0001), Low[index]);

            double saveValue = 0.0;

            bool addHigh = false;
            bool addLow = false;
            bool updateHigh = false;
            bool updateLow = false;

            zigZagHighZigZags[index] = 0;
            zigZagLowZigZags[index] = 0;

            if (!isSwingHigh && !isSwingLow)
            {
                return;
            }
            // first high
            if (trendDir <= 0 && isSwingHigh && isOverHighDeviation)
            {

                saveValue = High[index];


                addHigh = true;
                trendDir = 1;

            }
            // first low
            else if (trendDir >= 0 && isSwingLow && isOverLowDeviation)
            {

                saveValue = Low[index];

                addLow = true;
                trendDir = -1;


            }
            // continue high
            else if (trendDir == 1 && isSwingHigh && IsPriceGreater(High[index], lastSwingPrice))
            {
                saveValue = High[index];

                updateHigh = true;
                htimmarker = index;

            }
            // continue low
            else if (trendDir == -1 && isSwingLow && IsPriceGreater(lastSwingPrice, Low[index]))
            {
                saveValue = Low[index];

                updateLow = true;
                ltimmarker = index;
            }

            if (addHigh || addLow || updateHigh || updateLow)
            {
                if (updateHigh && lastSwingIndex >= 0)
                {
                    zigZagHighZigZags[lastSwingIndex] = 0;
                    SwingHigh[lastSwingIndex] = double.NaN;
                    Value[lastSwingIndex] = double.NaN;
                }
                else if (updateLow && lastSwingIndex >= 0)
                {
                    zigZagLowZigZags[lastSwingIndex] = 0;
                    SwingLow[lastSwingIndex] = double.NaN;
                    Value[lastSwingIndex] = double.NaN;
                }

                if (addHigh || updateHigh)
                {
                    // new high
                    if (addHigh)
                    {
                        thwoerorlownewdownv = High[newhtimmarker] - Low[newltimmarker];
                        downv = High[htimmarker] - Low[ltimmarker];
                        newdownv = High[htimmarker] - Low[newltimmarker];

                        newnewdownv = High[newhtimmarker] - Low[newltimmarker];
                        string xxx = string.Format("{0:n2}", (downv / upv));
                        string px = (High.Last(0) - SwingLow.Last(0)).ToString();
                        arrowName = string.Format("bulletSell {0}", index);
                        bool bugred = newhtimmarker < htimmarker && htimmarker < newltimmarker;
                        if (afterbuglow && !newnewhigh)
                        {
                            xxx = string.Format("{0:n2}", (downv / oneerorlownewupv));
                            double a = (downv / oneerorlownewupv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;

                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }


                            ChartObjects.DrawText(arrowName, xxx, ltimmarker, Low[ltimmarker] - 0.002, vAlign, hAlign, color);
                            lastlow = ltimmarker;
                        }

                        else if (newnewlow && newnewhigh)
                        {

                            xxx = string.Format("{0:n2}", (newnewdownv / thwoerorlownewupv));
                            double a = (newnewdownv / thwoerorlownewupv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;

                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxx, newltimmarker, Low[newltimmarker] - 0.002, vAlign, hAlign, color);
                            lastlow = newltimmarker;
                        }
                        else if (newnewlow)
                        {
                            xxx = string.Format("{0:n2}", (newdownv / oneerorlownewupv));
                            double a = (newdownv / oneerorlownewupv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;

                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxx, newltimmarker, Low[newltimmarker] - 0.002, vAlign, hAlign, color);
                            lastlow = newltimmarker;
                        }

                        else if (ltimmarker < newltimmarker)
                        {
                            xxx = string.Format("{0:n2}", (newdownv / upv));
                            double a = (newdownv / upv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;

                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxx, newltimmarker, Low[newltimmarker] - 0.002, vAlign, hAlign, color);
                            newnewlow = true;
                            lastlow = newltimmarker;
                        }
                        else if (!newnewlow && newnewhigh)
                        {
                            if (afterbuglow)
                            {
                                xxx = string.Format("{0:n2}", (oneerorlownewdownv / newnewupv));
                                double a = (oneerorlownewdownv / newnewupv) * 100;
                                bool siohasht = a < 38 + errore && a > 38 - errore;
                                bool nim = a < 50 + errore && a > 50 - errore;
                                bool shast = a < 62 + errore && a > 62 - errore;
                                bool haftad = a < 71 + errore && a > 71 - errore;
                                bool haftadohasht = a < 78 + errore && a > 78 - errore;
                                bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                                bool sadosizdah = a < 113 + errore && a > 113 - errore;
                                bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                                bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                                bool sadoshast = a < 161 + errore && a > 161 - errore;
                                bool devist = a < 200 + errore && a > 200 - errore;
                                bool devistobist = a < 224 + errore && a > 224 - errore;
                                bool devistoshast = a < 261 + errore && a > 261 - errore;
                                bool pii = a < 314 + errore && a > 314 - errore;
                                bool sisadoshast = a < 361 + errore && a > 361 - errore;

                                if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                                {
                                    color = Colorret;
                                }

                                else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                                {
                                    color = Colorex;
                                }
                                else
                                {
                                    color = Colornorm;
                                }
                                ChartObjects.DrawText(arrowName, xxx, ltimmarker, Low[ltimmarker] - 0.002, vAlign, hAlign, color);
                                lastlow = ltimmarker;

                            }
                            else
                            {
                                xxx = string.Format("{0:n2}", (oneerorlownewdownv / newupv));
                                double a = (oneerorlownewdownv / newupv) * 100;
                                bool siohasht = a < 38 + errore && a > 38 - errore;
                                bool nim = a < 50 + errore && a > 50 - errore;
                                bool shast = a < 62 + errore && a > 62 - errore;
                                bool haftad = a < 71 + errore && a > 71 - errore;
                                bool haftadohasht = a < 78 + errore && a > 78 - errore;
                                bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                                bool sadosizdah = a < 113 + errore && a > 113 - errore;
                                bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                                bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                                bool sadoshast = a < 161 + errore && a > 161 - errore;
                                bool devist = a < 200 + errore && a > 200 - errore;
                                bool devistobist = a < 224 + errore && a > 224 - errore;
                                bool devistoshast = a < 261 + errore && a > 261 - errore;
                                bool pii = a < 314 + errore && a > 314 - errore;
                                bool sisadoshast = a < 361 + errore && a > 361 - errore;
                                if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                                {
                                    color = Colorret;
                                }

                                else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                                {
                                    color = Colorex;
                                }
                                else
                                {
                                    color = Colornorm;
                                }
                                ChartObjects.DrawText(arrowName, xxx, ltimmarker, Low[ltimmarker] - 0.002, vAlign, hAlign, color);
                                lastlow = ltimmarker;
                            }

                        }
                        else
                        {
                            double a = (downv / upv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;

                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxx, ltimmarker, Low[ltimmarker] - 0.002, vAlign, hAlign, color);
                            lastlow = ltimmarker;
                        }



                        zigZagHighZigZags[index] = saveValue;
                        zigZagHighZigZags[index] = 0;

                        currentZigZagHigh = saveValue;
                        SwingHigh[index] = currentZigZagHigh;
                        Value[index] = currentZigZagHigh;
                        newhtimmarker = index;
                        afterbuglow = false;

                    }
                    // contniue high 
                    if (updateHigh)
                    {

                        zigZagHighZigZags[index] = saveValue;
                        zigZagHighZigZags[index] = 0;

                        currentZigZagHigh = saveValue;
                        SwingHigh[index] = currentZigZagHigh;
                        Value[index] = currentZigZagHigh;
                        if (newnewhigh)
                        {
                            afterbughigh = true;
                        }
                        newnewhigh = false;
                        htimmarker = index;
                        oneerorlownewupv = High[htimmarker] - Low[newltimmarker];
                    }

                }
                else if (addLow || updateLow)
                {
                    // new low
                    if (addLow)
                    {
                        thwoerorlownewupv = High[newhtimmarker] - Low[newltimmarker];
                        upv = High[htimmarker] - Low[ltimmarker];
                        newupv = High[newhtimmarker] - Low[ltimmarker];

                        newnewupv = High[newhtimmarker] - Low[newltimmarker];
                        string xxxx = string.Format("{0:n2}", (upv / downv));

                        string prxxxx = (SwingHigh.Last(0) - Low.Last(0)).ToString();
                        arrowName = string.Format("bulletSell {0}", index);
                        bool bugblue = newltimmarker < ltimmarker && ltimmarker < newhtimmarker;
                        if (afterbughigh && !newnewlow)
                        {
                            xxxx = string.Format("{0:n2}", (upv / oneerorlownewdownv));
                            double a = (upv / oneerorlownewdownv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;

                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxxx, htimmarker, High[htimmarker] + 0.001, vAlign, hAlign, color);
                            lasthigh = htimmarker;
                        }

                        else if (newnewlow && newnewhigh)
                        {
                            xxxx = string.Format("{0:n2}", (newnewupv / thwoerorlownewdownv));
                            double a = (newnewupv / thwoerorlownewdownv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;

                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxxx, newhtimmarker, High[newhtimmarker] + 0.001, vAlign, hAlign, color);
                            lasthigh = newhtimmarker;
                        }
                        else if (newnewhigh)
                        {
                            xxxx = string.Format("{0:n2}", (newupv / oneerorlownewdownv));
                            double a = (newupv / oneerorlownewdownv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;

                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxxx, newhtimmarker, High[newhtimmarker] + 0.001, vAlign, hAlign, color);
                            lasthigh = newhtimmarker;
                        }

                        else if (htimmarker < newhtimmarker)
                        {
                            xxxx = string.Format("{0:n2}", (newupv / downv));
                            double a = (newupv / downv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;
                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxxx, newhtimmarker, High[newhtimmarker] + 0.001, vAlign, hAlign, color);
                            newnewhigh = true;
                            lasthigh = newhtimmarker;
                        }
                        else if (newnewlow && !newnewhigh)
                        {
                            if (afterbughigh)
                            {
                                xxxx = string.Format("{0:n2}", (oneerorlownewupv / newnewdownv));
                                double a = (oneerorlownewupv / newnewdownv) * 100;
                                bool siohasht = a < 38 + errore && a > 38 - errore;
                                bool nim = a < 50 + errore && a > 50 - errore;
                                bool shast = a < 62 + errore && a > 62 - errore;
                                bool haftad = a < 71 + errore && a > 71 - errore;
                                bool haftadohasht = a < 78 + errore && a > 78 - errore;
                                bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                                bool sadosizdah = a < 113 + errore && a > 113 - errore;
                                bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                                bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                                bool sadoshast = a < 161 + errore && a > 161 - errore;
                                bool devist = a < 200 + errore && a > 200 - errore;
                                bool devistobist = a < 224 + errore && a > 224 - errore;
                                bool devistoshast = a < 261 + errore && a > 261 - errore;
                                bool pii = a < 314 + errore && a > 314 - errore;
                                bool sisadoshast = a < 361 + errore && a > 361 - errore;

                                if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                                {
                                    color = Colorret;
                                }

                                else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                                {
                                    color = Colorex;
                                }
                                else
                                {
                                    color = Colornorm;
                                }
                                ChartObjects.DrawText(arrowName, xxxx, htimmarker, High[htimmarker] + 0.001, vAlign, hAlign, color);
                                lasthigh = htimmarker;
                            }
                            else
                            {
                                xxxx = string.Format("{0:n2}", (oneerorlownewupv / newdownv));
                                double a = (oneerorlownewupv / newdownv) * 100;
                                bool siohasht = a < 38 + errore && a > 38 - errore;
                                bool nim = a < 50 + errore && a > 50 - errore;
                                bool shast = a < 62 + errore && a > 62 - errore;
                                bool haftad = a < 71 + errore && a > 71 - errore;
                                bool haftadohasht = a < 78 + errore && a > 78 - errore;
                                bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                                bool sadosizdah = a < 113 + errore && a > 113 - errore;
                                bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                                bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                                bool sadoshast = a < 161 + errore && a > 161 - errore;
                                bool devist = a < 200 + errore && a > 200 - errore;
                                bool devistobist = a < 224 + errore && a > 224 - errore;
                                bool devistoshast = a < 261 + errore && a > 261 - errore;
                                bool pii = a < 314 + errore && a > 314 - errore;
                                bool sisadoshast = a < 361 + errore && a > 361 - errore;
                                if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                                {
                                    color = Colorret;
                                }

                                else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                                {
                                    color = Colorex;
                                }
                                else
                                {
                                    color = Colornorm;
                                }
                                ChartObjects.DrawText(arrowName, xxxx, htimmarker, High[htimmarker] + 0.001, vAlign, hAlign, color);
                                lasthigh = htimmarker;
                            }

                        }
                        else
                        {
                            double a = (upv / downv) * 100;
                            bool siohasht = a < 38 + errore && a > 38 - errore;
                            bool nim = a < 50 + errore && a > 50 - errore;
                            bool shast = a < 62 + errore && a > 62 - errore;
                            bool haftad = a < 71 + errore && a > 71 - errore;
                            bool haftadohasht = a < 78 + errore && a > 78 - errore;
                            bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                            bool sadosizdah = a < 113 + errore && a > 113 - errore;
                            bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                            bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                            bool sadoshast = a < 161 + errore && a > 161 - errore;
                            bool devist = a < 200 + errore && a > 200 - errore;
                            bool devistobist = a < 224 + errore && a > 224 - errore;
                            bool devistoshast = a < 261 + errore && a > 261 - errore;
                            bool pii = a < 314 + errore && a > 314 - errore;
                            bool sisadoshast = a < 361 + errore && a > 361 - errore;
                            if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                            {
                                color = Colorret;
                            }

                            else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                            {
                                color = Colorex;
                            }
                            else
                            {
                                color = Colornorm;
                            }
                            ChartObjects.DrawText(arrowName, xxxx, htimmarker, High[htimmarker] + 0.001, vAlign, hAlign, color);
                            lasthigh = htimmarker;
                        }


                        zigZagLowZigZags[index] = saveValue;
                        zigZagLowZigZags[index] = 0;

                        currentZigZagLow = saveValue;
                        SwingLow[index] = currentZigZagLow;
                        Value[index] = currentZigZagLow;
                        newltimmarker = index;
                        afterbughigh = false;

                    }
                    // contniue low 
                    if (updateLow)
                    {


                        zigZagLowZigZags[index] = saveValue;
                        zigZagLowZigZags[index] = 0;

                        currentZigZagLow = saveValue;
                        SwingLow[index] = currentZigZagLow;
                        Value[index] = currentZigZagLow;
                        ltimmarker = index;
                        if (newnewlow)
                        {
                            afterbuglow = true;
                        }
                        newnewlow = false;
                        oneerorlownewdownv = High[newhtimmarker] - Low[ltimmarker];
                    }

                }

                {
                    lastSwingIndex = CurrentBar - 1;
                    lastSwingPrice = saveValue;



                    //going up
                    if (lastSwingPrice > currentZigZagLow)
                    {
                        double a = ((lastSwingPrice - currentZigZagLow) / (High[lasthigh] - Low[lastlow])) * 100;
                        bool siohasht = a < 38 + errore && a > 38 - errore;
                        bool nim = a < 50 + errore && a > 50 - errore;
                        bool shast = a < 62 + errore && a > 62 - errore;
                        bool haftad = a < 71 + errore && a > 71 - errore;
                        bool haftadohasht = a < 78 + errore && a > 78 - errore;
                        bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                        bool sadosizdah = a < 113 + errore && a > 113 - errore;
                        bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                        bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                        bool sadoshast = a < 161 + errore && a > 161 - errore;
                        bool devist = a < 200 + errore && a > 200 - errore;
                        bool devistobist = a < 224 + errore && a > 224 - errore;
                        bool devistoshast = a < 261 + errore && a > 261 - errore;
                        bool pii = a < 314 + errore && a > 314 - errore;
                        bool sisadoshast = a < 361 + errore && a > 361 - errore;
                        if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                        {
                            color = Colorret;
                        }

                        else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                        {
                            color = Colorex;
                        }
                        else
                        {
                            color = Colornorm;
                        }








                        string xxxx = string.Format("{0:n2}", ((lastSwingPrice - currentZigZagLow) / (High[lasthigh] - Low[lastlow])));

                        ChartObjects.DrawText("last value", xxxx, lastSwingIndex, High[htimmarker] + 0.001, vAlign, hAlign, color);

                    }
                    // going down
                    else if (lastSwingPrice < currentZigZagHigh)
                    {
                        double a = ((currentZigZagHigh - lastSwingPrice) / (High[lasthigh] - Low[lastlow])) * 100;
                        bool siohasht = a < 38 + errore && a > 38 - errore;
                        bool nim = a < 50 + errore && a > 50 - errore;
                        bool shast = a < 62 + errore && a > 62 - errore;
                        bool haftad = a < 71 + errore && a > 71 - errore;
                        bool haftadohasht = a < 78 + errore && a > 78 - errore;
                        bool hashtadohasht = a < 88 + errore && a > 88 - errore;
                        bool sadosizdah = a < 113 + errore && a > 113 - errore;
                        bool sadobistohaft = a < 127 + errore && a > 127 - errore;
                        bool sadocheloyek = a < 141 + errore && a > 141 - errore;
                        bool sadoshast = a < 161 + errore && a > 161 - errore;
                        bool devist = a < 200 + errore && a > 200 - errore;
                        bool devistobist = a < 224 + errore && a > 224 - errore;
                        bool devistoshast = a < 261 + errore && a > 261 - errore;
                        bool pii = a < 314 + errore && a > 314 - errore;
                        bool sisadoshast = a < 361 + errore && a > 361 - errore;
                        if (siohasht || nim || shast || haftad || haftadohasht || hashtadohasht)
                        {
                            color = Colorret;
                        }

                        else if (sadosizdah || sadobistohaft || sadocheloyek || sadoshast || devist || devistobist || devistoshast | pii || sisadoshast)
                        {
                            color = Colorex;
                        }
                        else
                        {
                            color = Colornorm;
                        }
                        double lastprice = lastSwingPrice;

                        double curenthigh = currentZigZagHigh;
                        double lastoldv = High[lasthigh] - Low[lastlow];
                        double lastv = curenthigh - lastprice;



                        string xxxx = string.Format("{0:n2}", ((currentZigZagHigh - lastSwingPrice) / (High[lasthigh] - Low[lastlow])));

                        ChartObjects.DrawText("last value", xxxx, lastSwingIndex, Low[lastSwingIndex] - 0.002, vAlign, hAlign, color);


                    }
                }
            }
        }

        private bool IsPriceGreater(double a, double b)
        {
            if (a > b && a - b > tickSize / 2)
                return true;
            else
                return false;
        }
    }
}


GH
Gholamia252

Joined on 29.03.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ZigZag Percentage.algo
  • Rating: 0
  • Installs: 2366
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
NA
naabujoshua · 7 months ago

thank you. its now working but can you pls make it in a way that the high and low is determined based on high and low rather than the default close.

NA
naabujoshua · 7 months ago

it doesn't work. can you please fix the bug