Category Other  Published on 27/01/2017

ZigZag + Fibo Levels

Description

 


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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AutoRescale = false, AccessRights = AccessRights.None)]
    public class ZigZagAndFiboLevels : Indicator
    {
        [Parameter("Deviation Percent", DefaultValue = 2, MinValue = 0.01)]
        public double deviationPercent { get; set; }

        [Parameter("Show Fibo Levels", DefaultValue = true)]
        public bool Allow_To_Show_Fibo_Levels { get; set; }

        [Parameter("Show Fibo Labels", DefaultValue = true)]
        public bool Allow_To_Show_Fibo_Labels { get; set; }

        [Parameter("Show Only Fibo %", DefaultValue = true)]
        public bool Allow_To_Show_Only_Fibo_Percent { get; set; }

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

        public enum Direction
        {
            Up,
            Down
        }

        private Direction direction = Direction.Up;
        private double Extremum_Price = 0.0;
        private double Extremum_Price_Up = 0.0;
        private double Extremum_Price_Down = 0.0;
        private int Extremum_Index = 0;

        /// used to remove objects
        private int Previous_Index = 0;

        //List<string> myCollection = new List<string>();
        //private string[] Objects = new string[1];

        public struct Result
        {
            public int? Index;
            public double? Value;
        }

        public Result Last_Extremum()
        {
            for (int index = Value.Count - 1; index >= 0; index--)
                if (!Value[index].Equals(Double.NaN))
                    return new Result 
                    {
                        Index = index,
                        Value = Value[index]
                    };

            return new Result 
            {
                Index = null,
                Value = null
            };
        }

        public double Extremum_Price_UP()
        {
            return Extremum_Price_Up;
        }

        public double Extremum_Price_DOWN()
        {
            return Extremum_Price_Down;
        }


        private void Move_Extremum(int index, double price)
        {
            //ChartObjects.RemoveObject("Info " + Extremum_Index);

            //for (int i = 0; i < Objects.Length; i++)
            //    if (Objects[i] != null)
            //    {
            //        Print("obj = {0}", Objects[i]);
            //        ChartObjects.RemoveObject(Objects[i]);
            //    }

            Value[Extremum_Index] = Double.NaN;
            Set_Extremum(index, price);
        }

        private void Set_Extremum(int index, double price)
        {
            if (Allow_To_Show_Fibo_Levels)
                Draw_Fibo_Levels(index, price);
            //ChartObjects.DrawText("Info " + index, price.ToString(), index, Extremum_Price < price ? price + 0.002 : price - 0.002, VerticalAlignment.Center, HorizontalAlignment.Center, Colors.Yellow);

            Extremum_Price_Up = direction == Direction.Up ? price : Extremum_Price_Up;



            Extremum_Price_Down = direction == Direction.Down ? price : Extremum_Price_Down;

            Print("Extremum_Price_Up = {0}       Extremum_Price_Down = {1}", Extremum_Price_Up, Extremum_Price_Down);

            Extremum_Index = index;
            Extremum_Price = price;
            Value[Extremum_Index] = Extremum_Price;
        }

        private void Draw_Fibo_Levels(int New_Index, double New_Extremum)
        {
            var LE = Last_Extremum();
            if (LE.Index == null || LE.Value == null)
                return;

            //0; 23,6; 38,2; 50; 61,8; 76,4; 100   
            //123.6 - 138.2 - 161.8 - 200.0 - 238.2 - 261.8 - 300.0 - 338.2 - 400.0 - 423.6 - 485.4 - 547.2 - 647.2 - 685.4 

            double First_Point = (double)LE.Value;
            double Second_Point = New_Extremum;

            double Value_0_00 = (double)LE.Value;
            double Value_23_6 = Fibo_Level(First_Point, Second_Point, 0.236);
            double Value_38_2 = Fibo_Level(First_Point, Second_Point, 0.382);
            double Value_50_0 = Fibo_Level(First_Point, Second_Point, 0.5);
            double Value_61_8 = Fibo_Level(First_Point, Second_Point, 0.618);
            double Value_76_4 = Fibo_Level(First_Point, Second_Point, 0.764);
            double Value_100_ = Fibo_Level(First_Point, Second_Point, 1.0);
            double Value_138_2 = Fibo_Level(First_Point, Second_Point, 1.382);
            double Value_161_8 = Fibo_Level(First_Point, Second_Point, 1.618);
            double Value_200_0 = Fibo_Level(First_Point, Second_Point, 2.0);
            double Value_261_8 = Fibo_Level(First_Point, Second_Point, 2.618);
            double Value_338_2 = Fibo_Level(First_Point, Second_Point, 3.382);
            double Value_423_6 = Fibo_Level(First_Point, Second_Point, 4.236);
            double Value_547_2 = Fibo_Level(First_Point, Second_Point, 5.472);
            double Value_685_4 = Fibo_Level(First_Point, Second_Point, 6.854);

            int Index = (int)LE.Index;

            Draw_Fibo_Level(Index, New_Index, Value_0_00, "0.00%", 1);
            Draw_Fibo_Level(Index, New_Index, Value_23_6, "23.6%", 2);
            Draw_Fibo_Level(Index, New_Index, Value_38_2, "38.2%", 3);
            Draw_Fibo_Level(Index, New_Index, Value_50_0, "50.0%", 4);
            Draw_Fibo_Level(Index, New_Index, Value_61_8, "61.6%", 5);
            Draw_Fibo_Level(Index, New_Index, Value_76_4, "76.4%", 6);
            Draw_Fibo_Level(Index, New_Index, Value_100_, "100.0%", 7);
            Draw_Fibo_Level(Index, New_Index, Value_138_2, "138.2%", 8);
            Draw_Fibo_Level(Index, New_Index, Value_161_8, "161.8%", 9);
            Draw_Fibo_Level(Index, New_Index, Value_200_0, "200.0%", 10);
            Draw_Fibo_Level(Index, New_Index, Value_261_8, "261.8%", 11);
            Draw_Fibo_Level(Index, New_Index, Value_338_2, "338.2%", 12);
            Draw_Fibo_Level(Index, New_Index, Value_423_6, "423.6%", 13);
            Draw_Fibo_Level(Index, New_Index, Value_547_2, "547.2%", 14);
            Draw_Fibo_Level(Index, New_Index, Value_685_4, "685.4%", 15);

            Previous_Index = (int)LE.Index;
        }

        private void Draw_Fibo_Level(int First_Point, int Second_Point, double Value, string Level, int Object_Number)
        {
            Colors color = Colors.DarkGray;
            double thickness = 0.5;
            LineStyle Line_Style = LineStyle.DotsRare;

            VerticalAlignment VA = VerticalAlignment.Center;
            HorizontalAlignment HA = HorizontalAlignment.Left;

            ChartObjects.DrawLine("Line" + Level + First_Point, First_Point, Value, Second_Point, Value, color, thickness, Line_Style);

            if (Allow_To_Show_Fibo_Labels)
                ChartObjects.DrawText("Text" + Level + First_Point, (Allow_To_Show_Only_Fibo_Percent ? "" : (Value.ToString() + " - ")) + Level, Second_Point, Value + 0.001, VA, HA, color);
        }
        /*
            Print("Objects.Length {0}", Objects.Length);
            Print("Objects Number {0}", Object_Number);

            if ((int)Objects.Length < Object_Number * 2)
            {
                Array.Resize(ref Objects, Object_Number * 2 + 1);
                Print("Objects.Length after {0}", Objects.Length);
            }
            Objects[Object_Number * 2 - 1] = "Line" + Level + First_Point;
            Objects[Object_Number * 2] = "Text" + Level + First_Point;
            */

        private double Fibo_Level(double Base_Point, double Target_Point, double Level)
        {
            return Math.Round(Base_Point > Target_Point ? (Base_Point - (Base_Point - Target_Point) * Level) : (Base_Point + (Target_Point - Base_Point) * Level), 5);
        }


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

            if (Extremum_Price == 0.0)
            {
                Extremum_Price = high;
                Extremum_Price_Up = high;
                Extremum_Price_Down = low;
            }

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

            if (direction == Direction.Down)
            {
                if (low <= Extremum_Price)
                    Move_Extremum(index, low);
                else if (high >= Extremum_Price * (1.0 + deviationPercent * 0.01))
                {
                    Set_Extremum(index, high);
                    direction = Direction.Up;
                }
            }
            else
            {
                if (high >= Extremum_Price)
                    Move_Extremum(index, high);
                else if (low <= Extremum_Price * (1.0 - deviationPercent * 0.01))
                {
                    Set_Extremum(index, low);
                    direction = Direction.Down;
                }
            }
        }
    }
}


diiptrade's avatar
diiptrade

Joined on 17.11.2016

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ZigZag And Fibo Levels.algo
  • Rating: 5
  • Installs: 10626
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
IA
IandelMar · 5 years ago

http://roboforex.ctrader.com/images/screens/Wygdn.png

 

IA
IandelMar · 5 years ago

Hi. im missing the retracements lines?

 

IA
IandelMar · 5 years ago

Thanks a lot for this Indicator, its possible to integrate a Notfication via Telegram. When the 100% is reached and the zig zag is closed? CAn you integrate a Buy or Sell arrow?