variables from the indicator for use in the bot

travkinsm1 since: 05 Apr 2018;

  14 Sep 2021, 16:12
variables from the indicator for use in the bot

Hello! I am facing the following problem. I needed a boolean variable from an indicator in a bot, which changes when Calculate is called. The only way I've found to call it is to create an IndicatorDataSeries that takes the value 1 if it's false, and 2 if it's true. It turned out that this is very inconvenient for testing, because all the time the graph is reset to the position where this variable is displayed, capturing the values ​​1 and 2. How can you solve my problem? Thanks in advance. I am attaching an example to illustrate my problem.

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestTI : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        public bool b;
        protected override void Initialize()
        {
            b = false;
        }

        public override void Calculate(int index)
        {
            b = !b;
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}
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 BoolBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private TestTI ind;
        protected override void OnStart()
        {
            ind = Indicators.GetIndicator<TestTI>(1.0);
        }

        protected override void OnTick()
        {
            Print(ind.b);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
amusleh's avatar

amusleh since: 01 Mar 2021;

  15 Sep 2021, 11:08

Hi,

Right now there is only one standard way to share memory data between an indicator and cBot, its IndicatorDataSeries.

But there is a workaround, change your cBot code to this:

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 BoolBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private TestTI ind;
        protected override void OnStart()
        {
            ind = Indicators.GetIndicator<TestTI>(1.0);
        }

        protected override void OnTick()
        {
            ind.Calculate(Bars.Count - 1);
            Print(ind.b);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Add the call to indicator Calculate method at the top of your cBot OnTick method and it should work.

Community Developer | Spotware | Telegram: @algodeveloper

travkinsm1 since: 05 Apr 2018;

  15 Sep 2021, 16:57
RE:

Thanks for the quick response. Do I understand correctly that the Calculate function will be called again?

amusleh's avatar

amusleh since: 01 Mar 2021;

  16 Sep 2021, 09:23
RE: RE:

travkinsm1 said:

Thanks for the quick response. Do I understand correctly that the Calculate function will be called again?

Hi,

No, its not called twice, indicators are lazy loaded, the API doesn't call an indicator Calculate method unless you access one of its data series values.

Try this:

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestTI : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        public bool b;
        protected override void Initialize()
        {
            b = false;
        }

        public override void Calculate(int index)
        {
            Result[index] = index;
        
            b = !b;
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}
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 BoolBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private TestTI ind;
        protected override void OnStart()
        {
            ind = Indicators.GetIndicator<TestTI>(1.0);
        }

        protected override void OnTick()
        {
            Print(ind.Result.LastValue);
            Print(ind.b);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
Community Developer | Spotware | Telegram: @algodeveloper