Automatic Fibonacci

Created at 31 Mar 2020, 12:00
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!
VE

velu130486

Joined 08.11.2019 Blocked

Automatic Fibonacci
31 Mar 2020, 12:00


Dear All,

I got the below code for drawing Fibonacci lines into the chart from this forum. This Indicator will help me to draw the Fibonacci automatically based on last 100 Candle High Low, however I required your support to modify the coding to draw the Fibonacci lines based on last 100 - X candles (say 15).

Also this code does not draw the Fibonacci Extension levels, please help me to draw the same automatically

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Collections.Generic;
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Autofibo : Indicator
    {
       [Parameter("Look Back Period ", DefaultValue = 100)]
        public int lookback { get; set; }
        [Parameter("Series Timeframe")]
        public TimeFrame tf { get; set; }
        private MarketSeries series1;
        protected override void Initialize()
        {
            // Initialize and create nested indicators
            series1 = MarketData.GetSeries(tf);
            OnTimer();
            Timer.Start(60);
        }
        protected override void OnTimer()
        {
            // Initialize and create nested indicators
            //find index
            var hidate = Time.Date;
            var lodate = Time.Date;
            var superhi = MarketSeries.Close.LastValue;
            var superlo = MarketSeries.Close.LastValue;
            var highval = series1.High;
            var loval = series1.Low;

            for (var i = 0; i < lookback; i++)
            {
               //find hi
                if (highval.Last(i) > superhi)
                {
                    superhi = highval.Last(i);
                    hidate = series1.OpenTime.Last(i);
                }
                //findlo
                if (loval.Last(i) < superlo)
                {
                    superlo = loval.Last(i);
                    lodate = series1.OpenTime.Last(i);
                }
                var bull = (hidate > lodate) ? true : false;
                var datechosen = (bull) ? lodate : hidate;

                //set value of line

                List<double> level = new List<double> 
                {
                    0.0,
                    23.6,
                    38.0,
                    50.0,
                    61.8,
                    100
                };
                var now = MarketSeries.OpenTime.LastValue;
                var distance = superhi - superlo;
//drawline 
                foreach (var lev in level)
                {
                    var dev = lev / 100 * distance;
                    var val = (bull) ? superhi - dev : superlo + dev;

                    ChartObjects.DrawLine(lev + "%", datechosen, val, now, val, (bull) ? Colors.Red : Colors.Blue, 1, LineStyle.Solid);
                    ChartObjects.DrawText(lev + "% text", lev + "%", MarketSeries.OpenTime.Count + 1, val + 0.5 * Symbol.PipSize, VerticalAlignment.Center, HorizontalAlignment.Right, (bull) ? Colors.Red : Colors.Blue);

                }
            }
        }
        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}

 


Replies

velu130486
01 Apr 2020, 08:37

RE:

velu130486 said:

Dear All,

I got the below code for drawing Fibonacci lines into the chart from this forum. This Indicator will help me to draw the Fibonacci automatically based on last 100 Candle High Low, however I required your support to modify the coding to draw the Fibonacci lines based on last 100 - X candles (say 15).

Also this code does not draw the Fibonacci Extension levels, please help me to draw the same automatically

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Collections.Generic;
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Autofibo : Indicator
    {
       [Parameter("Look Back Period ", DefaultValue = 100)]
        public int lookback { get; set; }
        [Parameter("Series Timeframe")]
        public TimeFrame tf { get; set; }
        private MarketSeries series1;
        protected override void Initialize()
        {
            // Initialize and create nested indicators
            series1 = MarketData.GetSeries(tf);
            OnTimer();
            Timer.Start(60);
        }
        protected override void OnTimer()
        {
            // Initialize and create nested indicators
            //find index
            var hidate = Time.Date;
            var lodate = Time.Date;
            var superhi = MarketSeries.Close.LastValue;
            var superlo = MarketSeries.Close.LastValue;
            var highval = series1.High;
            var loval = series1.Low;

            for (var i = 0; i < lookback; i++)
            {
               //find hi
                if (highval.Last(i) > superhi)
                {
                    superhi = highval.Last(i);
                    hidate = series1.OpenTime.Last(i);
                }
                //findlo
                if (loval.Last(i) < superlo)
                {
                    superlo = loval.Last(i);
                    lodate = series1.OpenTime.Last(i);
                }
                var bull = (hidate > lodate) ? true : false;
                var datechosen = (bull) ? lodate : hidate;

                //set value of line

                List<double> level = new List<double> 
                {
                    0.0,
                    23.6,
                    38.0,
                    50.0,
                    61.8,
                    100
                };
                var now = MarketSeries.OpenTime.LastValue;
                var distance = superhi - superlo;
//drawline 
                foreach (var lev in level)
                {
                    var dev = lev / 100 * distance;
                    var val = (bull) ? superhi - dev : superlo + dev;

                    ChartObjects.DrawLine(lev + "%", datechosen, val, now, val, (bull) ? Colors.Red : Colors.Blue, 1, LineStyle.Solid);
                    ChartObjects.DrawText(lev + "% text", lev + "%", MarketSeries.OpenTime.Count + 1, val + 0.5 * Symbol.PipSize, VerticalAlignment.Center, HorizontalAlignment.Right, (bull) ? Colors.Red : Colors.Blue);

                }
            }
        }
        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}

Dear All,

Could you please help me to fix this.

Thanks and Regards

R. Vadivelan


firemyst
05 Apr 2020, 09:52

RE:

velu130486 said:

Dear All,

I got the below code for drawing Fibonacci lines into the chart from this forum. This Indicator will help me to draw the Fibonacci automatically based on last 100 Candle High Low, however I required your support to modify the coding to draw the Fibonacci lines based on last 100 - X candles (say 15).

Also this code does not draw the Fibonacci Extension levels, please help me to draw the same automatically

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Collections.Generic;
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Autofibo : Indicator
    {
       [Parameter("Look Back Period ", DefaultValue = 100)]
        public int lookback { get; set; }
        [Parameter("Series Timeframe")]
        public TimeFrame tf { get; set; }
        private MarketSeries series1;
        protected override void Initialize()
        {
            // Initialize and create nested indicators
            series1 = MarketData.GetSeries(tf);
            OnTimer();
            Timer.Start(60);
        }
        protected override void OnTimer()
        {
            // Initialize and create nested indicators
            //find index
            var hidate = Time.Date;
            var lodate = Time.Date;
            var superhi = MarketSeries.Close.LastValue;
            var superlo = MarketSeries.Close.LastValue;
            var highval = series1.High;
            var loval = series1.Low;

            for (var i = 0; i < lookback; i++)
            {
               //find hi
                if (highval.Last(i) > superhi)
                {
                    superhi = highval.Last(i);
                    hidate = series1.OpenTime.Last(i);
                }
                //findlo
                if (loval.Last(i) < superlo)
                {
                    superlo = loval.Last(i);
                    lodate = series1.OpenTime.Last(i);
                }
                var bull = (hidate > lodate) ? true : false;
                var datechosen = (bull) ? lodate : hidate;

                //set value of line

                List<double> level = new List<double> 
                {
                    0.0,
                    23.6,
                    38.0,
                    50.0,
                    61.8,
                    100
                };
                var now = MarketSeries.OpenTime.LastValue;
                var distance = superhi - superlo;
//drawline 
                foreach (var lev in level)
                {
                    var dev = lev / 100 * distance;
                    var val = (bull) ? superhi - dev : superlo + dev;

                    ChartObjects.DrawLine(lev + "%", datechosen, val, now, val, (bull) ? Colors.Red : Colors.Blue, 1, LineStyle.Solid);
                    ChartObjects.DrawText(lev + "% text", lev + "%", MarketSeries.OpenTime.Count + 1, val + 0.5 * Symbol.PipSize, VerticalAlignment.Center, HorizontalAlignment.Right, (bull) ? Colors.Red : Colors.Blue);

                }
            }
        }
        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}

 

 

For this issue:

"I required your support to modify the coding to draw the Fibonacci lines based on last 100 - X candles (say 15)."

Just change the value in the look back.

Eg, in your example, you want to draw 100 - 15 == 85 lookback, then set the lookback value to 85 so it'll only be based on the last 85. If you want it to only do the last 15, then set the lookback parameter to 15.

There isn't any code modification required for that.


@firemyst