Category Trend  Published on 12/10/2020

DrawPriceMove V3

Description

 

Went straight to version three.

Same idea as ZigZag, but adjusts based upon high-low relationships of previous bars.

If the bar is an inner bar - change trend direction.
If the bar is an outer bar - go in direction of first breakout (whether that was above the previous high, or below the previous low).
Else, the direction changes depending on whether the current high price exceeds or falls short of the previous bar's high.

Let us know if there are any bugs.

Find us at: fxtradersystems.com/services/


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DrawPriceMoveV3 : Indicator
    {
        [Output("ZigZag", Thickness = 2, PlotType = PlotType.Line, LineColor = "Lime")]
        public IndicatorDataSeries Value { get; set; }


        private Bars tf1;
        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;
        }

        private bool isInside(int index)
        {
            return (Bars.LowPrices[index] >= Bars.LowPrices[index - 1]) && (Bars.HighPrices[index] <= Bars.HighPrices[index - 1]);
        }

        private bool isOutside(int index)
        {
            return (Bars.LowPrices[index] <= Bars.LowPrices[index - 1]) && (Bars.HighPrices[index] >= Bars.HighPrices[index - 1]);
        }


        private int isHighFirst(int index)
        {
            // Get data from last bar.
            double prevHigh = Bars.HighPrices[index - 1];
            double prevLow = Bars.LowPrices[index - 1];

            // Find where we're searching between:
            DateTime barOpen = Bars.OpenTimes[index];
            int minuteStartIndex = tf1.OpenTimes.GetIndexByTime(barOpen);

            int signal = 0;
            int count = minuteStartIndex;

            while (signal == 0)
            {
                signal = compareMinMax(prevHigh, prevLow, count);
                count++;
            }

            return signal;
        }

        private int compareMinMax(double high, double low, int minuteIndex)
        {
            double barHigh = tf1.HighPrices[minuteIndex];
            double barLow = tf1.LowPrices[minuteIndex];

            if (barHigh > high)
                return 1;
            else if (barLow < low)
                return -1;
            else
                return 0;
        }

        protected override void Initialize()
        {
            tf1 = MarketData.GetBars(TimeFrame.Minute);
        }

        public override void Calculate(int index)
        {
            double low = Bars.LowPrices[index];
            double high = Bars.HighPrices[index];

            if (extremumPrice == 0.0)
                extremumPrice = high;

            if (Bars.OpenTimes.Count < 2)
                return;

            if (direction == Direction.down)
            {
                if (isInside(index))
                {
                    setExtremum(index, high);
                    direction = Direction.up;
                }
                else if (isOutside(index))
                {
                    if (isHighFirst(index) == 1)
                    {
                        setExtremum(index, high);
                        direction = Direction.up;
                    }
                    else
                        moveExtremum(index, low);
                }
                else if (Bars.HighPrices[index] <= Bars.HighPrices[index - 1])
                    moveExtremum(index, low);
                else
                {
                    setExtremum(index, high);
                    direction = Direction.up;
                }
            }
            else
            {
                if (isInside(index))
                {
                    setExtremum(index, low);
                    direction = Direction.down;
                }
                else if (isOutside(index))
                {
                    if (isHighFirst(index) == -1)
                    {
                        setExtremum(index, low);
                        direction = Direction.down;
                    }
                    else
                        moveExtremum(index, high);
                }
                else if (Bars.HighPrices[index] >= Bars.HighPrices[index - 1])
                    moveExtremum(index, high);
                else
                {
                    setExtremum(index, low);
                    direction = Direction.down;
                }
            }
        }
    }
}


fxtradersystems's avatar
fxtradersystems

Joined on 10.09.2020

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