Category Trend  Published on 22/02/2022

Andrew's Pitchfork from Zigzag indicator

Description

This indicator is an integration of two other indicators viz, zigzag by Jiri and cTrader automate ChartAndrewspitchfork 

Last update : 08-Mar-2024

 


using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
 

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

        #region Properties
         [Parameter("High", DefaultValue = "High")]
        public DataSeries High { get; set ; }

        [Parameter("Low", DefaultValue = "Low")]
        public DataSeries Low { get; set; }

        [Parameter("Period", DefaultValue = 16, MinValue = 1)]
        public int Period { get; set; }

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

        [Parameter("Show Andrews Pitchfork", DefaultValue = true)]
        public bool ShowAndrewsPitchfork { get; set; }

        [Parameter("AndrewsPitchfork Color" ,DefaultValue = Colors.Red )]
        public Colors AndrewsPitchforkColor{ get; set; }

        #endregion

        #region Variables
        double currentZigZagHigh, currentZigZagLow = 0;
        int lastSwingIndex = -1;
        double lastSwingPrice = 0.0;
        int trendDir, CurrentBar = 0;
        #endregion

        protected override void Initialize()
        {
            
        }

        public override void Calculate(int index)
        {
            CalculateZigZag(index) ;
       
            if (ShowAndrewsPitchfork)
                DrawAndrewsPitchfork(index);
        }

        public void CalculateZigZag(int index)
        {
            CurrentBar = High.Count;

            if (CurrentBar < 2)
                return;

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

            bool isSwingHigh = High[index] == Functions.Maximum(High, Period);
            bool isSwingLow = Low[index] == Functions.Minimum(Low, Period);
            double saveValue = 0.0;
            bool addHigh= false;
            bool addLow= false;
            bool updateHigh= false;
            bool updateLow = false;

            if (trendDir == 1 && isSwingHigh && High[index] >= lastSwingPrice)
            {
                saveValue = High[index];
                updateHigh = true;
            }
            else if (trendDir == -1 && isSwingLow && Low[index] <= lastSwingPrice)
            {
                saveValue = Low[index];
                updateLow = true;
            }
            else if (trendDir <= 0 && isSwingHigh)
            {
                saveValue = High[index];
                addHigh = true;
                trendDir = 1;
            }
            else if (trendDir >= 0 && isSwingLow)
            {
                saveValue = Low[index];
                addLow = true;
                trendDir = -1;
            }

            if (addHigh || addLow || updateHigh || updateLow)
            {
                if (updateHigh && lastSwingIndex >= 0)
                {
                    ZigZagSeries[lastSwingIndex] = double.NaN;
                }
                else if (updateLow && lastSwingIndex >= 0)
                {
                    ZigZagSeries[lastSwingIndex] = double.NaN;
                }

                if (addHigh || updateHigh)
                {
                    currentZigZagHigh = saveValue;
                    ZigZagSeries[index] = currentZigZagHigh;
                }
                else if (addLow || updateLow)
                {
                    currentZigZagLow = saveValue;
                    ZigZagSeries[index] = currentZigZagLow;
                }

                lastSwingIndex = CurrentBar - 1;
                lastSwingPrice = saveValue;
            }
        }
        public void DrawAndrewsPitchfork(int index)
        {
            int barIndex1 = 0;
            int barIndex2 = 0;
            int barIndex3 = 0;
            int barIndex4 = 0;

            for (int i = index; i >= 0; i--)
            {
                if (ZigZagSeries[i] > 0)
                {
                    if (barIndex4 == 0)
                    {
                        barIndex4 = i;
                    }
                    else if (barIndex3 == 0)
                    {
                        barIndex3 = i;
                    }
                    else if (barIndex2 == 0)
                    {
                        barIndex2 = i;
                    }
                    else if (barIndex1 == 0)
                    {
                        barIndex1 = i;
                        break;
                    }
                }
            }

            var y1 = ZigZagSeries[barIndex1];
            var y2 = ZigZagSeries[barIndex2];
            var y3 = ZigZagSeries[barIndex3];

            Chart.DrawAndrewsPitchfork("AndrewsPitchFork", barIndex1, y1, barIndex2, y2, barIndex3, y3, Color.FromName(AndrewsPitchforkColor.ToString()) ); 
              
        }
    }
}

IF
iForex2015

Joined on 17.03.2015

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: AndrewsPitchforkZigZag.algo
  • Rating: 5
  • Installs: 139
Comments
Log in to add a comment.
IF
iForex2015 · 4 months ago

@ M_Anjom  You are right, pitchfork wont disappear without Chart.RemoveObject("AndrewsPitchFork");

I will edit ASAP. I would also like to add a few more options like option to change fork color, switch zigzag on/off etc. Let me see. 

Thanks btw. 

M_Anjom's avatar
M_Anjom · 6 months ago

     hi,   ShowAndrewsPitchFork  doesn't work must be add this “else” to your cod

    if (ShowAndrewsPitchFork)
                DrawAndrewsPitchFork(index);
            else
                Chart.RemoveObject("AndrewsPitchFork");

AL
almaza24map · 2 years ago
MO
monicaphillpa1122 · 2 years ago

This Andrew's Pitchfork from Zigzag indicator, which is so reflective and desirable for all the new individuals but there are also very beneficial and fantastic guest post backlinks, from them so easily all over the US.