Custom Fractal feeding bot no value

Created at 12 May 2022, 23:32
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!
IC

icollocollo

Joined 26.11.2021

Custom Fractal feeding bot no value
12 May 2022, 23:32


This is my fractals indicator gets 4hr values

I am trying to get .last (1) and last (0) using the for loop below

Values needed is UpFractal 1 and 2

                            DownFractal 1 and 2

i have tried it prints last values but then repeats them. need to get last values at a particular time.

 

 


@icollocollo
Replies

amusleh
13 May 2022, 10:21

Hi,

We can only help you if you post a code sample that can reproduce this issue.

Please post the code for your cBot and the custom indicator you are using.


@amusleh

icollocollo
13 May 2022, 16:16

RE:

amusleh said:

Hi,

We can only help you if you post a code sample that can reproduce this issue.

Please post the code for your cBot and the custom indicator you are using.

Updated Please check, really need these values. 


@icollocollo

amusleh
16 May 2022, 09:36

Hi,

The index is the current chart bars index, not the index of time frame that you loaded.

You have to change the index to the loaded time frame bars index first then use it, try this:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.EAfricaStandardTime)]
    public class Spring : Indicator
    {
        [Output("Up Fractal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpFractal { get; set; }

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

        private Bars HSeries;

        protected override void Initialize()
        {
            HSeries = MarketData.GetBars(TimeFrame.Hour4);
        }

        public override void Calculate(int index)
        {
            if (index == 1)
                return;

            DrawUpFractal(index);
            DrawDownFractal(index);
        }

        private void DrawUpFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];

            bool up = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex + 1])
                {
                    up = true;
                    break;
                }
            }

            if (up)
                UpFractal[index] = middleValue;
        }

        private void DrawDownFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];
            bool down = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex + 1])
                {
                    down = true;
                    break;
                }
            }
            if (down)
                DownFractal[index] = middleValue;
        }
    }
}

 


@amusleh

icollocollo
23 May 2022, 19:08 ( Updated at: 21 Dec 2023, 09:22 )

RE:

amusleh said:

Hi,

The index is the current chart bars index, not the index of time frame that you loaded.

You have to change the index to the loaded time frame bars index first then use it, try this:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.EAfricaStandardTime)]
    public class Spring : Indicator
    {
        [Output("Up Fractal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpFractal { get; set; }

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

        private Bars HSeries;

        protected override void Initialize()
        {
            HSeries = MarketData.GetBars(TimeFrame.Hour4);
        }

        public override void Calculate(int index)
        {
            if (index == 1)
                return;

            DrawUpFractal(index);
            DrawDownFractal(index);
        }

        private void DrawUpFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];

            bool up = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex + 1])
                {
                    up = true;
                    break;
                }
            }

            if (up)
                UpFractal[index] = middleValue;
        }

        private void DrawDownFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];
            bool down = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex + 1])
                {
                    down = true;
                    break;
                }
            }
            if (down)
                DownFractal[index] = middleValue;
        }
    }
}

 

Thank you @Amusleh only problem is that the index uses the candle that has not closed. See example in the image below. so the lastvalue is definitely wrong


@icollocollo

amusleh
24 May 2022, 10:13

Hi,

Then decrease the index by 1:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.EAfricaStandardTime)]
    public class Spring : Indicator
    {
        [Output("Up Fractal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpFractal { get; set; }

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

        private Bars HSeries;

        protected override void Initialize()
        {
            HSeries = MarketData.GetBars(TimeFrame.Hour4);
        }

        public override void Calculate(int index)
        {
            if (index == 1)
                return;

            DrawUpFractal(index);
            DrawDownFractal(index);
        }

        private void DrawUpFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - 1;

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];

            bool up = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex + 1])
                {
                    up = true;
                    break;
                }
            }

            if (up)
                UpFractal[index] = middleValue;
        }

        private void DrawDownFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - 1;

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];
            bool down = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex + 1])
                {
                    down = true;
                    break;
                }
            }
            if (down)
                DownFractal[index] = middleValue;
        }
    }
}

 


@amusleh

icollocollo
24 May 2022, 14:26 ( Updated at: 21 Dec 2023, 09:22 )

RE:

amusleh said:

Hi,

Then decrease the index by 1:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.EAfricaStandardTime)]
    public class Spring : Indicator
    {
        [Output("Up Fractal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpFractal { get; set; }

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

        private Bars HSeries;

        protected override void Initialize()
        {
            HSeries = MarketData.GetBars(TimeFrame.Hour4);
        }

        public override void Calculate(int index)
        {
            if (index == 1)
                return;

            DrawUpFractal(index);
            DrawDownFractal(index);
        }

        private void DrawUpFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - 1;

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];

            bool up = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex + 1])
                {
                    up = true;
                    break;
                }
            }

            if (up)
                UpFractal[index] = middleValue;
        }

        private void DrawDownFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - 1;

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];
            bool down = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex + 1])
                {
                    down = true;
                    break;
                }
            }
            if (down)
                DownFractal[index] = middleValue;
        }
    }
}

 

About this indicator, it worked when i used middle index instead of index in the result part. Would that affect how the cbot check the values when i use last 0 and last 1?

private void DrawUpFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];

            bool up = false;

            for (int i = 1; i <= HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex + 1])
                {
                    up = true;
                    break;
                }
            }

            if (up)
                UpFractal[middleIndex] = middleValue;
        }

#Update 

i tested and still don't get exact values. This are from GBPJPY

Print("DownFractal:{0}", i_fractal.DownFractal.Last(0));
Print("DownFractal2:{0}", i_fractal.DownFractal.Last(1));

As you can see, .Last (1) gives NaN

Could there be a problem with output?


@icollocollo

amusleh
25 May 2022, 09:06

Hi,

Fractal can lag x number of bars based on your provided period number, so try to use earlier bar index when accessing data.

I can only guide you and help you regarding specific API related questions, I can't help you to fully develop your cBot or indicator.

If a value on a data series in NAN it means you haven't set the value for that index, and you have to check your code and find what's causing it. 


@amusleh

icollocollo
25 May 2022, 13:36

RE:

amusleh said:

Hi,

Fractal can lag x number of bars based on your provided period number, so try to use earlier bar index when accessing data.

I can only guide you and help you regarding specific API related questions, I can't help you to fully develop your cBot or indicator.

If a value on a data series in NAN it means you haven't set the value for that index, and you have to check your code and find what's causing it. 

Thank you for all your input. I have gone through data series API and have tried this before. It just that when it comes to this particular situation i don't get the value.

Also note i used a method that does not put period to use. I wanted to be sure when backtesting. A while ago i noticed when you test a cbot using periods it will give different results when you adjust the time of backtesting. 

Just need this small input for last 1 and last 0. Stuck here for months. This index issue. 


@icollocollo

amusleh
26 May 2022, 09:31

RE: RE:

icollocollo said:

amusleh said:

Hi,

Fractal can lag x number of bars based on your provided period number, so try to use earlier bar index when accessing data.

I can only guide you and help you regarding specific API related questions, I can't help you to fully develop your cBot or indicator.

If a value on a data series in NAN it means you haven't set the value for that index, and you have to check your code and find what's causing it. 

Thank you for all your input. I have gone through data series API and have tried this before. It just that when it comes to this particular situation i don't get the value.

Also note i used a method that does not put period to use. I wanted to be sure when backtesting. A while ago i noticed when you test a cbot using periods it will give different results when you adjust the time of backtesting. 

Just need this small input for last 1 and last 0. Stuck here for months. This index issue. 

Hi,

I can't see your cBot code, can you post it again, I will try once more.


@amusleh

icollocollo
26 May 2022, 10:09

RE: RE: RE:

amusleh said:

icollocollo said:

amusleh said:

Hi,

Fractal can lag x number of bars based on your provided period number, so try to use earlier bar index when accessing data.

I can only guide you and help you regarding specific API related questions, I can't help you to fully develop your cBot or indicator.

If a value on a data series in NAN it means you haven't set the value for that index, and you have to check your code and find what's causing it. 

Thank you for all your input. I have gone through data series API and have tried this before. It just that when it comes to this particular situation i don't get the value.

Also note i used a method that does not put period to use. I wanted to be sure when backtesting. A while ago i noticed when you test a cbot using periods it will give different results when you adjust the time of backtesting. 

Just need this small input for last 1 and last 0. Stuck here for months. This index issue. 

Hi,

I can't see your cBot code, can you post it again, I will try once more.

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class springg : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        private Spring i_fractal;

        protected override void OnStart()
        {
            i_fractal = Indicators.GetIndicator<Spring>();
        }

        protected override void OnBar()
        {
            for (int i = i_fractal.UpFractal.Count; i > i_fractal.UpFractal.Count - 10; i--)
            {

                for (int j = i_fractal.DownFractal.Count; j > i_fractal.DownFractal.Count - 10; j--)
                {
                    Print("UpFractal : {0}", i_fractal.UpFractal.Last(0));
                    Print("UpFractal2: {0}", i_fractal.UpFractal.Last(1));
                    Print("DownFractal:{0}", i_fractal.DownFractal.Last(0));
                    Print("DownFractal2:{0}", i_fractal.DownFractal.Last(1));
                }
            }
        }

    }
}

 


@icollocollo

amusleh
27 May 2022, 09:36

Hi,

I just tested and it works fine, indicator and cBot output results are matching.

Test below cBot on visual back test mode.

Test this please:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class springg : Robot
    {
        private Spring i_fractal;

        protected override void OnStart()
        {
            i_fractal = Indicators.GetIndicator<Spring>();
        }

        protected override void OnBar()
        {
            var index = Bars.Count - 2;

            if (i_fractal.UpFractal[index] > 0)
            {
                Chart.DrawVerticalLine(index.ToString(), index, Color.Red);
            }

            if (i_fractal.DownFractal[index] > 0)
            {
                Chart.DrawVerticalLine(index.ToString(), index, Color.Blue);
            }
        }
    }
}

 

Indicator:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.EAfricaStandardTime)]
    public class Spring : Indicator
    {
        [Output("Up Fractal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpFractal { get; set; }

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

        private Bars HSeries;

        protected override void Initialize()
        {
            HSeries = MarketData.GetBars(TimeFrame.Hour4);
        }

        public override void Calculate(int index)
        {
            if (index == 1)
                return;

            DrawUpFractal(index);
            DrawDownFractal(index);
        }

        private void DrawUpFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - 1;

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];

            bool up = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex + 1])
                {
                    up = true;
                    break;
                }
            }

            if (up)
            {
                UpFractal[index] = middleValue;
            }
            else
            {
                UpFractal[index] = double.NaN;
            }
        }

        private void DrawDownFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - 1;

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];
            bool down = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex + 1])
                {
                    down = true;
                    break;
                }
            }
            if (down)
            {
                DownFractal[index] = middleValue;
            }
            else
            {
                DownFractal[index] = double.NaN;
            }
        }
    }
}

 


@amusleh

icollocollo
27 May 2022, 22:32 ( Updated at: 21 Dec 2023, 09:22 )

RE:

amusleh said:

Hi,

I just tested and it works fine, indicator and cBot output results are matching.

Test below cBot on visual back test mode.

Test this please:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class springg : Robot
    {
        private Spring i_fractal;

        protected override void OnStart()
        {
            i_fractal = Indicators.GetIndicator<Spring>();
        }

        protected override void OnBar()
        {
            var index = Bars.Count - 2;

            if (i_fractal.UpFractal[index] > 0)
            {
                Chart.DrawVerticalLine(index.ToString(), index, Color.Red);
            }

            if (i_fractal.DownFractal[index] > 0)
            {
                Chart.DrawVerticalLine(index.ToString(), index, Color.Blue);
            }
        }
    }
}

 

Indicator:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.EAfricaStandardTime)]
    public class Spring : Indicator
    {
        [Output("Up Fractal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpFractal { get; set; }

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

        private Bars HSeries;

        protected override void Initialize()
        {
            HSeries = MarketData.GetBars(TimeFrame.Hour4);
        }

        public override void Calculate(int index)
        {
            if (index == 1)
                return;

            DrawUpFractal(index);
            DrawDownFractal(index);
        }

        private void DrawUpFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - 1;

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];

            bool up = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] > HSeries.ClosePrices[middleIndex + 1])
                {
                    up = true;
                    break;
                }
            }

            if (up)
            {
                UpFractal[index] = middleValue;
            }
            else
            {
                UpFractal[index] = double.NaN;
            }
        }

        private void DrawDownFractal(int index)
        {
            var hSeriesIndex = HSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - 1;

            int middleIndex = hSeriesIndex - 2;
            double middleValue = HSeries.ClosePrices[middleIndex];
            bool down = false;

            for (int i = 1; i < HSeries.ClosePrices.Count - 1; i++)
            {
                if (HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex - 1] && HSeries.ClosePrices[middleIndex] < HSeries.ClosePrices[middleIndex + 1])
                {
                    down = true;
                    break;
                }
            }
            if (down)
            {
                DownFractal[index] = middleValue;
            }
            else
            {
                DownFractal[index] = double.NaN;
            }
        }
    }
}

 

still doesn't work, it doesn't get those value i want.

Last clarification we close this. How do an indicator store values of the output. I have tried using a for loop of all the values but still it only works for only last value and i want the last 2 valueS. This gets me thinking like once you start onbar it can only access 1 last indicator value. Look at the images below. The red and blue values are the ones i want printed I.E 2 Red values and 2 last blue values. but all i can achieve is only index -1. all other values cannot be printed be it index-2, index, index-5, etc. Please note that the value i_fractal.DownFractal[indexD - 1] prints the correct Last(0) value. i used GBPjpY.


@icollocollo

amusleh
30 May 2022, 09:12

Hi,

Not sure what you are looking for, If the issue was mismatch between indicator and cBot values then it's resolved on the code I posted.


@amusleh

icollocollo
30 May 2022, 14:44

RE:

amusleh said:

Hi,

Not sure what you are looking for, If the issue was mismatch between indicator and cBot values then it's resolved on the code I posted.

Yes, let me try to put it in a way you will understand,

I am trying to get the last two values at any point. But i can only get the last value. Thats why i am asking if indicator series saves value. 

 


@icollocollo

amusleh
31 May 2022, 11:16

RE: RE:

icollocollo said:

amusleh said:

Hi,

Not sure what you are looking for, If the issue was mismatch between indicator and cBot values then it's resolved on the code I posted.

Yes, let me try to put it in a way you will understand,

I am trying to get the last two values at any point. But i can only get the last value. Thats why i am asking if indicator series saves value. 

 

Hi,

All added values inside indicator data series are saved and any other indicator or cBot that uses that indicator can access all of the available values.

 


@amusleh