Category Trend  Published on 28/10/2020

ZigZag - Modded for Harmonics bot

Description

This is the modified ZigZag indicator, Credits to kkostaki for the code.

The Yellow Line is just a 2nd copy of the code running at once. (just ignore if you are using the Harmonics bot)


///////////////////////////////////////////////////////////////////
//
//      
//      v1 
//      Modified by KRN
//
//      Credits:
//      Original by kkostaki
//
//
///////////////////////////////////////////////////////////////////

using System;
using cAlgo.API;
using cAlgo.API.Internals;


namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class ZigDouble : Indicator
    {
        [Parameter("Depth", DefaultValue = 12, Group = "ZigZag")]
        public int Depth { get; set; }

        [Parameter("Deviation", DefaultValue = 5, Group = "ZigZag")]
        public int Deviation { get; set; }

        [Parameter("BackStep", DefaultValue = 3, Group = "ZigZag")]
        public int BackStep { get; set; }
    
        [Parameter("Depth", DefaultValue = 150, Group = "Head")]
        public int Depth2 { get; set; }

        [Parameter("Deviation", DefaultValue = 5, Group = "Head")]
        public int Deviation2 { get; set; }

        [Parameter("BackStep", DefaultValue = 3, Group = "Head")]
        public int BackStep2 { get; set; }

        [Output("Result", LineColor = "White")]
        public IndicatorDataSeries Result { get; set; }

        [Output("Result2", LineColor = "Yellow")]
        public IndicatorDataSeries Result2 { get; set; }

        public IndicatorDataSeries _zigZag;
        public IndicatorDataSeries _idx;
        
        public IndicatorDataSeries _zigZag2;
        public IndicatorDataSeries _idx2;

        #region Private fields

        private double _lastLow;
        private double _lastHigh;
        private double _low;
        private double _high;
        private int _lastHighIndex;
        private int _lastLowIndex;
        private int _type;
        private double _point;
        private double _currentLow;
        private double _currentHigh;

        private int prevIdx;
        private int idx;
        private int tf;

        private IndicatorDataSeries _highZigZags;
        private IndicatorDataSeries _lowZigZags;

        private double _lastLow2;
        private double _lastHigh2;
        private double _low2;
        private double _high2;
        private int _lastHighIndex2;
        private int _lastLowIndex2;
        private int _type2;
        private double _point2;
        private double _currentLow2;
        private double _currentHigh2;

        private int prevIdx2;
        private int idx2;

        private IndicatorDataSeries _highZigZags2;
        private IndicatorDataSeries _lowZigZags2;

        #endregion

        protected override void Initialize()
        {
            _highZigZags = CreateDataSeries();
            _lowZigZags = CreateDataSeries();
            _zigZag = CreateDataSeries();
            _idx = CreateDataSeries();

            _point = Symbol.TickSize;
            
            idx = 0;
            prevIdx = -1;
            
            _highZigZags2 = CreateDataSeries();
            _lowZigZags2 = CreateDataSeries();
            _zigZag2 = CreateDataSeries();
            _idx2 = CreateDataSeries();

            _point2 = Symbol.TickSize;

            idx2 = 0;
            prevIdx2 = -1;

            string tfString = Bars.TimeFrame.ToString();
            switch (tfString)
            {
                case "Minute":
                    tf = 1;
                    break;
                case "Minute5":
                    tf = 5;
                    break;
                case "Minute10":
                    tf = 10;
                    break;
                case "Minute15":
                    tf = 15;
                    break;
                case "Minute30":
                    tf = 30;
                    break;
                case "Minute45":
                    tf = 45;
                    break;
                case "Hour":
                    tf = 60;
                    break;
                case "Hour2":
                    tf = 60 * 2;
                    break;
                case "Hour3":
                    tf = 60 * 3;
                    break;
                case "Hour4":
                    tf = 60 * 4;
                    break;
                case "Hour6":
                    tf = 60 * 6;
                    break;
                case "Hour8":
                    tf = 60 * 8;
                    break;
                case "Hour12":
                    tf = 60 * 12;
                    break;
                case "Daily":
                    tf = 60 * 24;
                    break;
                case "Day2":
                    tf = 60 * 24 * 2;
                    break;
                case "Day3":
                    tf = 60 * 24 * 3;
                    break;
                case "Weekly":
                    tf = 60 * 24 * 7;
                    break;
                case "Monthly":
                    tf = 60 * 24 * 7 * 4;
                    break;
                default:
                    Print("Error! Not Correct TimeFrame");
                    break;
            }
        }

        public override void Calculate(int index)
        {
            if (index < Depth)
            {
                Result[index] = 0;
                _highZigZags[index] = 0;
                _lowZigZags[index] = 0;
                _zigZag[idx] = 0;
                _idx[idx] = 0;
                
                return;
            }
            if (index < Depth2)
            {
                Result2[index] = 0;
                _highZigZags2[index] = 0;
                _lowZigZags2[index] = 0;
                _zigZag2[idx2] = 0;
                _idx2[idx2] = 0;
                
                return;
            }

            _currentLow = Functions.Minimum(Bars.LowPrices, Depth);
            if (Math.Abs(_currentLow - _lastLow) < double.Epsilon)
                _currentLow = 0.0;
            else
            {
                _lastLow = _currentLow;

                if ((Bars.LowPrices[index] - _currentLow) > (Deviation * _point))
                    _currentLow = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep; i++)
                    {
                        if (Math.Abs(_lowZigZags[index - i]) > double.Epsilon && _lowZigZags[index - i] > _currentLow)
                            _lowZigZags[index - i] = 0.0;
                    }
                }
            }
            if (Math.Abs(Bars.LowPrices[index] - _currentLow) < double.Epsilon)
                _lowZigZags[index] = _currentLow;
            else
                _lowZigZags[index] = 0.0;

            _currentHigh = Bars.HighPrices.Maximum(Depth);

            if (Math.Abs(_currentHigh - _lastHigh) < double.Epsilon)
                _currentHigh = 0.0;
            else
            {
                _lastHigh = _currentHigh;

                if ((_currentHigh - Bars.HighPrices[index]) > (Deviation * _point))
                    _currentHigh = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep; i++)
                    {
                        if (Math.Abs(_highZigZags[index - i]) > double.Epsilon && _highZigZags[index - i] < _currentHigh)
                            _highZigZags[index - i] = 0.0;
                    }
                }
            }

            if (Math.Abs(Bars.HighPrices[index] - _currentHigh) < double.Epsilon)
                _highZigZags[index] = _currentHigh;
            else
                _highZigZags[index] = 0.0;


            switch (_type)
            {
                case 0:
                    if (Math.Abs(_low - 0) < double.Epsilon && Math.Abs(_high - 0) < double.Epsilon)
                    {
                        if (Math.Abs(_highZigZags[index]) > double.Epsilon)
                        {
                            _high = Bars.HighPrices[index];
                            _lastHighIndex = index;
                            _type = -1;
                            Result[index] = _high;
                            _zigZag[idx] = _high;
                            _idx[idx] = index;
                            idx++;
                        }
                        if (Math.Abs(_lowZigZags[index]) > double.Epsilon)
                        {
                            _low = Bars.LowPrices[index];
                            _lastLowIndex = index;
                            _type = 1;
                            Result[index] = _low;
                            _zigZag[idx] = _low;
                            _idx[idx] = index;
                            idx++;
                        }
                    }
                    break;
                case 1:
                    if (Math.Abs(_lowZigZags[index]) > double.Epsilon && _lowZigZags[index] < _low && Math.Abs(_highZigZags[index] - 0.0) < double.Epsilon)
                    {
                        Result[_lastLowIndex] = double.NaN;
                        _lastLowIndex = index;
                        _low = _lowZigZags[index];
                        Result[index] = _low;
                        _zigZag[idx - 1] = _low;
                        _idx[idx - 1] = index;
                    }
                    if (Math.Abs(_highZigZags[index] - 0.0) > double.Epsilon && Math.Abs(_lowZigZags[index] - 0.0) < double.Epsilon)
                    {
                        _high = _highZigZags[index];
                        _lastHighIndex = index;
                        Result[index] = _high;
                        _zigZag[idx] = _high;
                        _idx[idx] = index;
                        idx++;
                        _type = -1;
                    }
                    break;
                case -1:
                    if (Math.Abs(_highZigZags[index]) > double.Epsilon && _highZigZags[index] > _high && Math.Abs(_lowZigZags[index] - 0.0) < double.Epsilon)
                    {
                        Result[_lastHighIndex] = double.NaN;
                        _lastHighIndex = index;
                        _high = _highZigZags[index];
                        Result[index] = _high;
                        _zigZag[idx - 1] = _high;
                        _idx[idx - 1] = index;
                    }
                    if (Math.Abs(_lowZigZags[index]) > double.Epsilon && Math.Abs(_highZigZags[index]) <= double.Epsilon)
                    {
                        _low = _lowZigZags[index];
                        _lastLowIndex = index;
                        Result[index] = _low;
                        _zigZag[idx] = _low;
                        _idx[idx] = index;
                        idx++;
                        _type = 1;
                    }
                    break;
                default:
                    return;
            }

            _currentLow2 = Functions.Minimum(Bars.LowPrices, Depth2);
            if (Math.Abs(_currentLow2 - _lastLow2) < double.Epsilon)
                _currentLow2 = 0.0;
            else
            {
                _lastLow2 = _currentLow2;

                if ((Bars.LowPrices[index] - _currentLow2) > (Deviation2 * _point2))
                    _currentLow2 = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep2; i++)
                    {
                        if (Math.Abs(_lowZigZags2[index - i]) > double.Epsilon && _lowZigZags2[index - i] > _currentLow2)
                            _lowZigZags2[index - i] = 0.0;
                    }
                }
            }
            if (Math.Abs(Bars.LowPrices[index] - _currentLow2) < double.Epsilon)
                _lowZigZags2[index] = _currentLow2;
            else
                _lowZigZags2[index] = 0.0;

            _currentHigh2 = Bars.HighPrices.Maximum(Depth2);

            if (Math.Abs(_currentHigh2 - _lastHigh2) < double.Epsilon)
                _currentHigh2 = 0.0;
            else
            {
                _lastHigh2 = _currentHigh2;

                if ((_currentHigh2 - Bars.HighPrices[index]) > (Deviation2 * _point2))
                    _currentHigh2 = 0.0;
                else
                {
                    for (int i = 1; i <= BackStep2; i++)
                    {
                        if (Math.Abs(_highZigZags2[index - i]) > double.Epsilon && _highZigZags2[index - i] < _currentHigh2)
                            _highZigZags2[index - i] = 0.0;
                    }
                }
            }

            if (Math.Abs(Bars.HighPrices[index] - _currentHigh2) < double.Epsilon)
                _highZigZags2[index] = _currentHigh2;
            else
                _highZigZags2[index] = 0.0;


            switch (_type2)
            {
                case 0:
                    if (Math.Abs(_low2 - 0) < double.Epsilon && Math.Abs(_high2 - 0) < double.Epsilon)
                    {
                        if (Math.Abs(_highZigZags2[index]) > double.Epsilon)
                        {
                            _high2 = Bars.HighPrices[index];
                            _lastHighIndex2 = index;
                            _type2 = -1;
                            Result2[index] = _high2;
                            _zigZag2[idx] = _high2;
                            _idx2[idx2] = index;
                            idx2++;
                        }
                        if (Math.Abs(_lowZigZags2[index]) > double.Epsilon)
                        {
                            _low2 = Bars.LowPrices[index];
                            _lastLowIndex2 = index;
                            _type2 = 1;
                            Result2[index] = _low2;
                            _zigZag2[idx] = _low2;
                            _idx2[idx2] = index;
                            idx2++;
                        }
                    }
                    break;
                case 1:
                    if (Math.Abs(_lowZigZags2[index]) > double.Epsilon && _lowZigZags2[index] < _low2 && Math.Abs(_highZigZags2[index] - 0.0) < double.Epsilon)
                    {
                        Result2[_lastLowIndex2] = double.NaN;
                        _lastLowIndex2 = index;
                        _low2 = _lowZigZags2[index];
                        Result2[index] = _low2;
                        _zigZag2[idx2 - 1] = _low2;
                        _idx2[idx2 - 1] = index;
                    }
                    if (Math.Abs(_highZigZags2[index] - 0.0) > double.Epsilon && Math.Abs(_lowZigZags2[index] - 0.0) < double.Epsilon)
                    {
                        _high2 = _highZigZags2[index];
                        _lastHighIndex2 = index;
                        Result2[index] = _high2;
                        _zigZag2[idx2] = _high2;
                        _idx2[idx2] = index;
                        idx2++;
                        _type2 = -1;
                    }
                    break;
                case -1:
                    if (Math.Abs(_highZigZags2[index]) > double.Epsilon && _highZigZags2[index] > _high2 && Math.Abs(_lowZigZags2[index] - 0.0) < double.Epsilon)
                    {
                        Result2[_lastHighIndex2] = double.NaN;
                        _lastHighIndex2 = index;
                        _high2 = _highZigZags2[index];
                        Result2[index] = _high2;
                        _zigZag2[idx2 - 1] = _high2;
                        _idx2[idx2 - 1] = index;
                    }
                    if (Math.Abs(_lowZigZags2[index]) > double.Epsilon && Math.Abs(_highZigZags2[index]) <= double.Epsilon)
                    {
                        _low2 = _lowZigZags2[index];
                        _lastLowIndex2 = index;
                        Result2[index] = _low2;
                        _zigZag2[idx2] = _low2;
                        _idx2[idx2] = index;
                        idx2++;
                        _type2 = 1;
                    }
                    break;
                default:
                    return;
            }

            if (prevIdx != index && (DateTime.UtcNow - Bars.OpenTimes[index]).TotalMinutes < tf)
            {
                Print("I am today ", tf);
                //for (int i = 0; i < _zigZag.Count; i++)
                //Print(_idx[i], " ", _zigZag[i], " ", Bars.OpenTimes[Convert.ToInt32(_idx[i])]);
            }
            prevIdx = index;
            prevIdx2 = index;
        }
    }
}


KA
kaFX45

Joined on 04.01.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Zig Double.algo
  • Rating: 0
  • Installs: 1834
Comments
Log in to add a comment.
No comments found.