draw a aline between last 2 Fractals

Created at 30 Oct 2018, 09:47
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
swingfish's avatar

swingfish

Joined 25.06.2016

draw a aline between last 2 Fractals
30 Oct 2018, 09:47


does anyone know how can access/read the last 2 points where fractals are drawn ? 

the idea is to connect the last 2 fracrals with a line and get some additional data about this price, but i kind of stuck on how i can actually get the information in which candle they been places eg. the number of past candles they where drawn .. 

 

any hint's ? 

 


@swingfish
Replies

PanagiotisCharalampous
30 Oct 2018, 09:54

Hi swingfish,

Could you please explain which fractals you are talking about?

Best Regards,

Panagiotis


@PanagiotisCharalampous

oneplusoc
30 Oct 2018, 22:11

hello 

This one can help you 

thanks 

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


namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.UTC)]
    public class FractalsAutoTrendLine : Indicator
    {
        [Parameter(DefaultValue = 48, MinValue = 3)]
        public int Calc_Max_Bars_For_TL { get; set; }

        [Parameter(DefaultValue = 4, MinValue = 3)]
        public int Period { get; set; }

        [Output("Up Fractal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries UpFractal { get; set; }

        [Output("Down Fractal", Color = Colors.Blue, PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries DownFractal { get; set; }

        //High Line
        private int index_high_1 = 0;
        private int index_high_2 = 0;

        //Low Line
        private int index_low_1 = 0;
        private int index_low_2 = 0;

        public override void Calculate(int index)
        {
            index_high_1 = 0;
            index_high_2 = 0;
            index_low_1 = 0;
            index_low_2 = 0;

            for (int calc = (MarketSeries.Close.Count - 1); calc > (MarketSeries.Close.Count - Calc_Max_Bars_For_TL); calc--)
            {
                DrawUpFractal(calc);
                DrawDownFractal(calc);
            }
            int count = MarketSeries.Close.Count;

            int maxIndex1 = index_high_1;
            int maxIndex2 = index_high_2;

            int minIndex1 = index_low_1;
            int minIndex2 = index_low_2;

            int startIndex = Math.Min(maxIndex2, minIndex2) - 100;
            int endIndex = count + 100;

            if (index_high_1 > 0 && index_high_2 > 0)
            {
                DrawTrendLine("Down Trend", startIndex, endIndex, maxIndex1, MarketSeries.High[maxIndex1], maxIndex2, MarketSeries.High[maxIndex2], true);
            }
            if (index_low_1 > 0 && index_low_2 > 0)
            {
                DrawTrendLine("Up Trend", startIndex, endIndex, minIndex1, MarketSeries.Low[minIndex1], minIndex2, MarketSeries.Low[minIndex2], false);
            }

            return;

        }

        private void DrawTrendLine(string lineName, int startIndex, int endIndex, int index1, double value1, int index2, double value2, bool color_up_trend)
        {
            double gradient = (value2 - value1) / (index2 - index1);

            double startValue = value1 + (startIndex - index1) * gradient;
            double endValue = value1 + (endIndex - index1) * gradient;
            if (color_up_trend == true)
            {
                ChartObjects.DrawLine(lineName, index2, value2, endIndex, endValue, Colors.DimGray, 1, LineStyle.LinesDots);
            }
            else
            {
                ChartObjects.DrawLine(lineName, index2, value2, endIndex, endValue, Colors.DimGray, 1, LineStyle.LinesDots);
            }
        }

        private void DrawUpFractal(int index)
        {
            //int period = Period % 2 == 0 ? Period - 1 : Period;
            int period = Period;
            int middleIndex = index - period / 2;
            double middleValue = MarketSeries.High[middleIndex];

            bool up = true;

            for (int i = 0; i < period; i++)
            {
                if (middleValue < MarketSeries.High[index - i])
                {
                    up = false;
                    break;
                }
            }
            if (up == true)
            {
                if (index_high_1 == 0)
                {
                    index_high_1 = middleIndex;
                }
                else
                {
                    if (index_high_2 == 0 && MarketSeries.High[index_high_1] < MarketSeries.High[middleIndex])
                    {
                        index_high_2 = middleIndex;
                    }
                }
                UpFractal[middleIndex] = middleValue;
            }
        }

        private void DrawDownFractal(int index)
        {
            //int period = Period % 2 == 0 ? Period - 1 : Period;
            int period = Period;
            int middleIndex = index - period / 2;
            double middleValue = MarketSeries.Low[middleIndex];
            bool down = true;

            for (int i = 0; i < period; i++)
            {
                if (middleValue > MarketSeries.Low[index - i])
                {
                    down = false;
                    break;
                }
            }
            if (down == true)
            {
                if (index_low_1 == 0)
                {
                    index_low_1 = middleIndex;
                }
                else
                {
                    if (index_low_2 == 0 && MarketSeries.Low[index_low_1] > MarketSeries.Low[middleIndex])
                    {
                        index_low_2 = middleIndex;
                    }
                }
                DownFractal[middleIndex] = middleValue;
            }
        }


    }
}


@oneplusoc

PanagiotisCharalampous
31 Oct 2018, 09:49

Hi oneplusoc,

Thank you for the indicator. swingfish if you need to read the start and end points of lines on the chart, you can try something like the following

            foreach (var obj in Chart.Objects)
            {
                if (obj is ChartTrendLine)
                {
                    var line = (ChartTrendLine)obj;
                    Print(line.Y1);
                    Print(line.Time1);
                    Print(line.Y2);
                    Print(line.Time2);
                }
            }

Note that this only works for 3.3.

Best Regards,

Panagiotis


@PanagiotisCharalampous

swingfish
31 Oct 2018, 15:44

i'm already have deplayed an algo that does take profit based on trendlines, sadly most larger brokers still running Version 3.0

i was told you guys (Spotware) handling the rollout.

 

kawase as example is at 3.3 already ... 

would be really nice if you guys could push the other brokers at least to 3.02 so the Chart.Objects would work .. 

 


@swingfish

PanagiotisCharalampous
31 Oct 2018, 15:55

Hi swingfish,

We are trying to roll out updates to brokers as soon as they are available. However, this not always possible. Some brokers will be upgraded to 3.3 later on.

Best Regards,

Panagiotis


@PanagiotisCharalampous