Making a data series in an indicator accessible by cBot

Created at 08 May 2023, 15:14
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!
JE

jens_c_nielsen

Joined 03.10.2022

Making a data series in an indicator accessible by cBot
08 May 2023, 15:14


Hi,

Sorry - I know this is a really basic question.

Just want to make an IndicatorDataSeries in an indicator accessible by the cBot without exporting it via Output - i.e. don't want the IndicatorDataSeries graphed by the indicator.

Thanks


@jens_c_nielsen
Replies

firemyst
09 May 2023, 03:06

Just remove the "OUTPUT" directive and keep it as a "public" property.

 

For example, instead of this:

 [Output("Referenced SMA Output")]
 public IndicatorDataSeries refSMA { get; set; }

 

Make it this. Note that when doing it this way you will have to do a CreateDataSeries() in the Initialize method:

public IndicatorDataSeries refSMA { get; set; }

@firemyst

jens_c_nielsen
09 May 2023, 11:32 ( Updated at: 21 Dec 2023, 09:23 )

Thank you very much for your response!

I tried doing this but somehow the data only comes through as NaN in the cBot.

The indicator is like this:

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

namespace cAlgo
{
    [Indicator(TimeZone = TimeZones.UTC, IsOverlay = true, AccessRights = AccessRights.FullAccess)]
    public class Sample_Indicator : Indicator
    {
        public IndicatorDataSeries DataToBeReadByIndicator { get; set; }
                

        protected override void Initialize()
        {
            DataToBeReadByIndicator = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            DataToBeReadByIndicator[0] = 10.0;
            DataToBeReadByIndicator[1] = 20.0;
            DataToBeReadByIndicator[2] = 30.0;
            
            Print("Sample_Indicator - values 0 from indicator: " + DataToBeReadByIndicator[0].ToString() ) ;
            Print("Sample_Indicator - values 1 from indicator: " + DataToBeReadByIndicator[1].ToString() ) ;
            Print("Sample_Indicator - values 2 from indicator: " + DataToBeReadByIndicator[2].ToString() ) ;
        }
    }
}

The cBot is like this:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class cBot_Sample : Robot
    {
    
        private Sample_Indicator mySample_Indicator;


        protected override void OnStart()
        {
            mySample_Indicator = Indicators.GetIndicator<Sample_Indicator>();
        }

        protected override void OnBar()
        {
            Print("cBot_Sample - values 0 from indicator: " + mySample_Indicator.DataToBeReadByIndicator[0].ToString() ) ;
            Print("cBot_Sample - values 1 from indicator: " + mySample_Indicator.DataToBeReadByIndicator[1].ToString() ) ;
            Print("cBot_Sample - values 2 from indicator: " + mySample_Indicator.DataToBeReadByIndicator[2].ToString() ) ;
        }
        
        protected override void OnStop()
        {
        }
        
    }
}

When I run the indicator then the output is:

 

When I run the cBot the output is this:

Somehow I must be doing something wrong.


@jens_c_nielsen

firemyst
09 May 2023, 12:30

You are indeed :-)

To get the values from the indicator, you need to call the indicator's "calculate" method. This can done by either of the following:

1) Call the Calculate method directly and pass in the index number

2) call the ".Last(x)" method on your indicator automatically calls the "Calculate" method to retrieve the values.

and you need to call the Calculate method before you use any of the resulting values.

If you don't do either, the indicator doesn't run (it's the 'lazy loading') and sits idle.


@firemyst