Category Other  Published on 15/05/2023

Fractals

Description

DCT - Fractals. 

To indicate fractal highs or lows using built in display or Arrow Up/Down icon.

Ability to change fractal periods 3,5,7,9 as needed.

Repaints current fractal until confirmed at close.

Built using CTrader 4.7.9 with c# .Net 6


using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using cAlgo.API;

// Copyright Deckchair Trader 2023
// Name:            DCT - Fractals
// Purpose:         To indicate fractal highs or lows using built in display or Arrow Up/Down icon
// Author:          TimT - Deckchair Trader, timt@deckchairtrader.com
// Version:         1.1
//                  Change Colour Types from string to Color - that way a colour picker appears in parameters.
// Date Created:    15 May 2023

namespace cAlgo.Indicators
{
#if DEBUG
    [Indicator(IsOverlay = true, AccessRights = AccessRights.FullAccess)]
#else
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
#endif

    
    public class Fractals : Indicator
    {
        [Parameter("Use Arrows", Group = "Fractal", DefaultValue = "true")] 
        public bool UseArrows { get; set; }

        [Parameter("Periods", Group = "Fractal",DefaultValue = 3, MinValue = 3, MaxValue = 9)]
        public int Periods { get; set; } // how many bars to form a fractal, usually 5 but can be 3 for quick fractals, at least must be an odd number

        [Parameter("Arrow Offset", Group = "Fractal",DefaultValue = 5, MinValue = 0)]
        public double ArrowOffset { get; set; } // how far away from bar to draw the arrow in pips. Only valid if UseArrows=true

        [Parameter("Up Fractal Arrow Colour", Group = "Fractal", DefaultValue = "#FF00843B")] 
        public Color UpFractalColour { get; set; }

        [Parameter("Down Fractal Arrow Colour", Group = "Fractal", DefaultValue = "#FFF15923")] 
        public Color DownFractalColour { get; set; }


        [Output("Up Fractal", LineColor = "#FF00843B", PlotType = PlotType.Points, Thickness = 0)] // by setting thickness 0, we have an invisible point, if we are not using Arrows set this to 5
        public IndicatorDataSeries UpFractal { get; set; } // if arrow used as fractal marker, then we still need these series if we want to use this indicator as part of another one in order to extract values

        [Output("Down Fractal", LineColor = "#FFF15923", PlotType = PlotType.Points, Thickness = 0)]
        public IndicatorDataSeries DownFractal { get; set; }


        protected override void Initialize()
        {
#if DEBUG
            System.Diagnostics.Debugger.Launch();
#endif

        }
        public override void Calculate(int index)
        {
            if (index < Periods)
                return;
            DrawFractals(index);
        }

        private void DrawFractals(int index)
        {
            int period = Periods % 2 == 0 ? Periods - 1 : Periods; // we want an odd number, min 3. left, middle, right
            int middleIndex = index - period / 2; // middle item where we draw the icon

            // see if we have an up fractal. check both side of the middle, left side we want highs lower than middle, right side we want highs lower than middle
            //  ^
            //^   ^
            //
            bool IsUp = UpBarsToHigh(middleIndex - period / 2, middleIndex, middleIndex);
            bool IsDown = UpBarsToHigh(middleIndex + period / 2, middleIndex, middleIndex);
            bool IsUpFractal = IsUp && IsDown;

            // see if we have an down fractal. check both side of the middle, left side we want lows lower than middle, right side we want lows lower than middle
            //
            IsDown = DownBarsToLow(middleIndex - period / 2, middleIndex, middleIndex);
            IsUp = DownBarsToLow(middleIndex + period / 2, middleIndex, middleIndex);
            bool IsDownFractal = IsUp && IsDown;

            if (IsUpFractal)
            {
                UpFractal[middleIndex] = Bars.HighPrices[middleIndex];
                if (UseArrows)
                {
                    Chart.DrawIcon($"DCT Up Fractal {middleIndex} {Bars.OpenTimes[middleIndex]}", ChartIconType.UpArrow, Bars.OpenTimes[middleIndex], Bars.HighPrices[middleIndex] + ArrowOffset * Symbol.PipSize, UpFractalColour);
                }
            }
            else
            {
                UpFractal[middleIndex] = double.NaN;
                Chart.RemoveObject($"DCT Up Fractal {middleIndex} {Bars.OpenTimes[middleIndex]}"); // the current period made need icon removing if not a valid fractal. Fractal is valid once bar closed
            }

            if (IsDownFractal)
            {
                DownFractal[middleIndex] = Bars.LowPrices[middleIndex];
                if (UseArrows)
                {
                    Chart.DrawIcon($"DCT Down Fractal {middleIndex} {Bars.OpenTimes[middleIndex]}", ChartIconType.DownArrow, Bars.OpenTimes[middleIndex], Bars.LowPrices[middleIndex] - ArrowOffset * Symbol.PipSize, DownFractalColour);
                }
            }
            else
            {
                DownFractal[middleIndex] = double.NaN;
                Chart.RemoveObject($"DCT Down Fractal {middleIndex} {Bars.OpenTimes[middleIndex]}"); // the current period made need icon removing if not a valid fractal. Fractal is valid once bar closed
            }
        }

        private bool UpBarsToHigh(int StartIndex, int MiddleIndex, int EndIndex)
        {
            if (StartIndex < EndIndex)
            {
                for (int n = StartIndex; n < EndIndex; n++)
                {
                    if (!(Bars.HighPrices[n] <= Bars.HighPrices[MiddleIndex]))
                    {
                        return false;
                    }
                }
            }
            else
            {
                for (int n = StartIndex; n > EndIndex; n--)
                {
                    if (!(Bars.HighPrices[n] <= Bars.HighPrices[MiddleIndex]))
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        private bool DownBarsToLow(int StartIndex, int MiddleIndex, int EndIndex)
        {
            if (StartIndex < EndIndex)
            {
                for (int n = StartIndex; n < EndIndex; n++)
                {
                    if (!(Bars.LowPrices[n] >= Bars.LowPrices[MiddleIndex]))
                    {
                        return false;
                    }
                }
            }
            else
            {
                for (int n = StartIndex; n > EndIndex; n--)
                {
                    if (!(Bars.LowPrices[n] >= Bars.LowPrices[MiddleIndex]))
                    {
                        return false;
                    }
                }
            }
            return true;
        }




    }
}

DeckchairTrader's avatar
DeckchairTrader

Joined on 15.05.2023

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: DCT - Fractals.algo
  • Rating: 5
  • Installs: 832
  • Modified: 15/05/2023 17:10
Comments
Log in to add a comment.
FA
favepaw679 · 1 year ago

Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. white label payment processing