How to pass Bars from a cBot to an Indicator

Created at 15 Jan 2022, 18:50
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!
sirinath's avatar

sirinath

Joined 25.11.2021

How to pass Bars from a cBot to an Indicator
15 Jan 2022, 18:50


How is the Bars series passed from a bot to an indicator?

That is, how is 

GetIndicator(Bars bars, Object[] parameterValues)

used from the side of the bot and indicator? How does the indicator access the passed bars?

Also how is Bars selected as a parameter from the UI?


@sirinath
Replies

amusleh
17 Jan 2022, 09:17

Hi,

When you run a cBot it gets the available data on your chart as Bars object.

That same object is shared with indicators that you use inside your cBot or on your chart.

You can't select the bars in UI parameters, a Bars object represent a time frame/period price data.

You have to use TimeFrame parameter and then your indicator/cBot can load that time frame bars.


@amusleh

sirinath
17 Jan 2022, 10:26 ( Updated at: 17 Jan 2022, 10:29 )

RE:

Many thanks for the reply. I currently have:

using System;

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class IndicatorTest : Indicator
    {
        [Parameter("Bars")]
        public Bars bars { get; set; }

        protected override void Initialize()
        {
            try
            {
                Print("Starting");

                Print("Bars - {0}", bars);

                Print("Started");
            } catch (Exception e)
            {
                Print(e, e.StackTrace);
            }
        }

        public override void Calculate(int index)
        {
        }
    }
}

I am wondering if you can share an example come on how 

GetIndicator(Bars bars, Object[] parameterValues)

is used?

I am trying to create multiple indicators for multiple symbols within the bot. Each indicator will get one timeseries of bars.


@sirinath

amusleh
17 Jan 2022, 12:10

Hi,

You can do it like this:

using System;
using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class IndicatorTest : Indicator
    {
        [Parameter("Bars TimeFrame")]
        public TimeFrame BarsTimeFrame { get; set; }
        
        private Bars _bars;

        protected override void Initialize()
        {
            try
            {
                _bars = MarketData.GetBars(BarsTimeFrame);
                
            } catch (Exception e)
            {
                Print(e, e.StackTrace);
            }
        }

        public override void Calculate(int index)
        {
        }
    }
}

Now, to use it on a cBot or another indicator:

Indicators.GetIndicator(TimeFrame.Daily);

It means it will use daily bars.


@amusleh