IR
    
        
            Fractal Calculate problem
            
                 03 Jan 2023, 21:16
            
                    
i make code to draw line between fractal and show distance , pip and percenge
but it has problem to calculate fracral.
using cAlgo.API;
using System;
using System.Collections.Generic;
using System.Text;
namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AwFractal : Indicator
    {
        private class SwingPoint
        {
            public int Index { get; set; }
            public double Price { get; set; }
            public SwingPoint()
            {
                Index = 0;
                Price = double.MaxValue;
            }
            public SwingPoint(int index, double price)
            {
                Index = index;
                Price = price;
            }
        }
        #region Fields
        // Bar Index
        private int _index;
        // Direction of the Fractal
        private Direction direction = Direction.Down;
        // Price of recent High
        private double high;
        // Price of recent Low
        private double low;
        private bool isBullish;
        private SwingPoint swingPoint;
        private List<SwingPoint> swingPointList;
        private enum Direction
        {
            Up,
            Down
        }
        #endregion Fields
        #region Parameters
        [Parameter("Period", DefaultValue = 5, MinValue = 5, Group = "Fractal")]
        public int Period { get; set; }
        [Parameter("Show Distance?", DefaultValue = false, Group = "Fractal")]
        public bool ShowDistance { get; set; }
        [Parameter("Show Pip?", DefaultValue = false, Group = "Fractal")]
        public bool ShowPip { get; set; }
        [Parameter("Show Percentage?", DefaultValue = true, Group = "Fractal")]
        public bool ShowPercentChange { get; set; }
        #endregion Parameters
        #region Output
        [Output("Fractal", LineColor = "Red", Thickness = 2, PlotType = PlotType.Line)]
        public IndicatorDataSeries Result { get; set; }
        #endregion Output
        public override void Calculate(int index)
        {
            _index = index;
            low = Bars.LowPrices.LastValue;
            high = Bars.HighPrices.LastValue;
            if (Bars.ClosePrices.Count < 2)
                return;
            switch (direction)
            {
                case Direction.Up:
                    DrawUpFractal();
                    break;
                case Direction.Down:
                default:
                    DrawDownFractal();
                    break;
            }
        }
        protected override void Initialize()
        {
            swingPoint = new SwingPoint();
            swingPointList = new List<SwingPoint>();
        }
        #region Append Text
        public string GetFractalText(double currentValue, double previousValue, double thirdValue)
        {
            StringBuilder sb = new StringBuilder();
            if (ShowDistance)
                AppendDistance(sb);
            if (ShowPip)
                AppendPip(sb);
            if (ShowPercentChange)
                AppendPercentageChange(currentValue, previousValue, thirdValue, sb);
            return sb.ToString();
        }
        private void AppendPercentageChange(double currentValue, double previousValue, double thirdValue, StringBuilder sb)
        {
            if (ShowDistance || ShowPip)
                sb.AppendLine();
            string sign = isBullish ? "+" : "-";
            string percentage = (isBullish ? CalculatePercentageChange(thirdValue, previousValue, currentValue) : CalculatePercentageChange(thirdValue, previousValue, currentValue)).ToString("N0");
            sb.Append(string.Format("{0}{1}%", sign, percentage));
        }
        private void AppendDistance(StringBuilder sb)
        {
            sb.Append((swingPointList[swingPointList.Count - 1].Index - swingPointList[swingPointList.Count - 2].Index).ToString(""));
        }
        private void AppendPip(StringBuilder sb)
        {
            if (ShowDistance)
                sb.AppendLine();
            sb.Append(((swingPointList[swingPointList.Count - 1].Price - swingPointList[swingPointList.Count - 2].Price) / Symbol.PipSize).ToString("N1"));
        }
        private double CalculatePercentageChange(double third, double previous, double current)
        {
            double lastChange = Math.Abs(third - previous);
            double change = Math.Abs(current - previous);
            return 100 * (double)change / lastChange;
        }
        #endregion Append Text
        #region Create Fractal
        public void ShowFractalLabel()
        {
            // Get last value
            double currentValue = swingPointList[swingPointList.Count - 1].Price;
            // Get second last value
            double previousValue = swingPointList[swingPointList.Count - 2].Price;
            //get thirdValue
            double thirdValue = swingPointList[swingPointList.Count - 3].Price;
            isBullish = currentValue > previousValue;
            string objName = "FractalLabel" + swingPoint.Index;
            Color textColour = isBullish ? "Green" : "Red";
            string labelText = GetFractalText(currentValue, previousValue, thirdValue);
            ChartText text = Chart.DrawText(name: objName, text: labelText, barIndex: swingPoint.Index, y: swingPoint.Price, color: textColour);
            text.VerticalAlignment = isBullish ? VerticalAlignment.Top : VerticalAlignment.Bottom;
            text.HorizontalAlignment = HorizontalAlignment.Center;
        }
        private void DrawDownFractal()
        {
            int period = Period % 2 == 0 ? Period - 1 : Period;
            int middleIndex = _index - period / 2;
            double middleValue = Bars.LowPrices[middleIndex];
            bool down = true;
            for (int i = 0; i < period; i++)
            {
                if (middleValue > Bars.LowPrices[_index - i])
                {
                    down = false;
                    break;
                }
            }
            if (down)
            {
                bool moveExtremum = low <= swingPoint.Price;
                bool changeDirection = high >= swingPoint.Price;
                if (moveExtremum)
                    MoveExtremum(low);
                else if (changeDirection)
                {
                    SetExtremum(high);
                    direction = Direction.Up;
                }
            }
        }
        private void DrawUpFractal()
        {
        
            int period = Period % 2 == 0 ? Period - 1 : Period;
            int middleIndex = -_index - period / 2;
            double middleValue = Bars.HighPrices[middleIndex];
            bool up = true;
            for (int i = 0; i < period; i++)
            {
                if (middleValue < Bars.HighPrices[-_index - i])
                {
                    up = false;
                    break;
                }
            }
            if (up)
            {
                bool moveExtremum = high >= swingPoint.Price;
                bool changeDirection = low <= swingPoint.Price;
                if (moveExtremum)
                    MoveExtremum(high);
                else if (changeDirection)
                {
                    SetExtremum(low);
                    direction = Direction.Down;
                }
            }
        }
        private void MoveExtremum(double price)
        {
            Result[swingPoint.Index] = double.NaN;
            if (swingPointList.Count > 0)
                swingPointList.Remove(swingPointList[swingPointList.Count - 1]);
            Chart.RemoveObject("FractalLabel" + swingPoint.Index);
            SetExtremum(price);
        }
        private void SetExtremum(double price)
        {
            swingPoint = new SwingPoint(_index, price);
            swingPointList.Add(swingPoint);
            Result[swingPoint.Index] = swingPoint.Price;
            if (swingPointList.Count < 2)
                return;
            ShowFractalLabel();
        }
        #endregion Create Fractal
    }
}
Replies
                     IRCtrader
                     05 Jan 2023, 12:48
                                            ( Updated at: 21 Dec 2023, 09:23 )
                                    
RE:
it shows wrong fractal based fractal fomula
Blue line is correct fractal
but my code show red line.
i think DrawUpFractal() and DrawDownFractal() have problems.

PanagiotisChar said:
Hi there,
I cannot understand the problem. Can you explain the issue better?
Need help? Join us on Telegram
Need premium support? Trade with us
@IRCtrader

PanagiotisChar
04 Jan 2023, 09:28
Hi there,
I cannot understand the problem. Can you explain the issue better?
Aieden Technologies
Need help? Join us on Telegram
Need premium support? Trade with us
@PanagiotisChar