Public properties on Custom Indicator

Created at 05 Aug 2024, 10:02
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!
CC

ccobostkisoft

Joined 05.08.2024

Public properties on Custom Indicator
05 Aug 2024, 10:02


I'm building a Custom Indicator that analize the movements of the price and calculates it on each bar.

The result are builded into a object, which I want to returned to my cBot.

There is a example about what I'm talking about.
 

Object to return:
namespace cAlgo.MyIndicators

{

    public class ObjectReturned

    {

        public int Index { get; set; }

        public string Text { get; set; }


 

        public ObjectReturned(int index, string text)

        {

            Index = index;

            Text = text;

        }


 

        public override string ToString()

        {

            return Text + Index;

        }

    }

}
Indicator:
using cAlgo.API;

using cAlgo.MyIndicators;


 

namespace cAlgo

{

    [Indicator(IsOverlay =true, AccessRights = AccessRights.None)]

    public class IndicatorTest : Indicator

    {

        public ObjectReturned Object { get; set; }


 

        protected override void Initialize()

        {

            Object = new ObjectReturned(0, "Initial Text");

        }


 

        public override void Calculate(int index)

        {

            Object.Index = index;

            Object.Text = "Calculated Text";

        }

    }

}


Robot:
using System;
using cAlgo.API;
using cAlgo.MyIndicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.RomanceStandardTime, AccessRights = AccessRights.None, AddIndicators = true)]
    public class Prueba : Robot
    {
        [Parameter("EMA Corta", DefaultValue = 50)]
        public int EMAMShortPeriods { get; set; }

        [Parameter("EMA Larga", DefaultValue = 200)]
        public int EMALongPeriods { get; set; }

        private IndicatorTest MyIndicatorTest;

        protected override void OnStart()
        {
            MyIndicatorTest = Indicators.GetIndicator<IndicatorTest>();
        }

        protected override void OnTick()
        {
            Print("IndicatorTest value on tick: " + MyIndicatorTest.Object.ToString());
        }

        protected override void OnBar()
        {
            Print("IndicatorTest value on bar: " + MyIndicatorTest.Object.ToString());
        }
        
        protected override void OnStop()
        {
            Print("The Robot Stopped");
        }

        protected override void OnError(Error error) {
            Print("Something happened while running... -> " + error.ToString());
        }
    }
}

 

The Problem is that, when I try to get de property into my cBot, it seems like its just initialize, not calculated. 

I know that its correctly calculated, because I can see it into the chart draws, but I cannot access to the actual value of this custom indicator properties.


Please, let me know if you have any idea about how can I do it working 


@ccobostkisoft
Replies

PanagiotisCharalampous
05 Aug 2024, 12:46

Hi there,

This happens due to lazy loading. For the Calculate() method to be called, you need to access an Output IndicatorDataSeries. Else you will need to call the method explicitly from your cBot.

Best regards,

Panagiotis


@PanagiotisCharalampous