Two seperate instances of the same indicator. Setting a property of one of them leaks into the second

Created at 17 Feb 2021, 12:42
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!
SH

Shares4us

Joined 01.04.2020

Two seperate instances of the same indicator. Setting a property of one of them leaks into the second
17 Feb 2021, 12:42


Two seperate instances of the same indicator.
Setting a property of one of them leaks into the second.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class Test : Robot
    {
        TestIndic2 i_1, i_2;

        protected override void OnStart()
        {
           string _prt = "Init i_1 => Mode3 ";
           i_1 = Indicators.GetIndicator<TestIndic2>( );
           Print(_prt + "=> i_1.Mode "+i_1.Mode );
           
            _prt = "Set i_1 => Mode1 ";
           i_1.Mode = TestIndic2.Modes.Mode1;
           Print(_prt + "=> i_1.Mode "+i_1.Mode );
           
            _prt = "Set i_1 => Mode2 ";
           i_1.Mode = TestIndic2.Modes.Mode2;
           Print(_prt + "=> i_1.Mode "+i_1.Mode );
       
           _prt = "Init i_2 => Mode3 ";
           i_2 = Indicators.GetIndicator<TestIndic2>( );
           Print(_prt + "=> i_2.Mode "+i_2.Mode );
           
           _prt = "Set i_2 => Mode1 ";
           i_2.Mode = TestIndic2.Modes.Mode1;
           Print(_prt + "=> i_2.Mode " + i_2.Mode);
           
           if( i_1.Mode== i_2.Mode) Print("** OOPS **\ti_1:" + i_1.Mode+"\ti_2:" + i_2.Mode);
 
            Print("i_1.Mode " + i_1.Mode+"\ti_2.Mode " + i_2.Mode);
        } 
    }
}


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

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    
    public class TestIndic2 : Indicator
    {
        public  Modes Mode { get; set; }
        public enum  Modes{Mode1 = 1, Mode2 = 2, Mode3 = 3}
        protected override void Initialize()
        {
            Mode= Modes.Mode3;
        }
        public override void Calculate(int index = -1)
        {   
        }
    }
}

 


@Shares4us
Replies

PanagiotisCharalampous
18 Feb 2021, 11:13

Hi Shares4us,

We investigated this issue and it seems that when you ask for an indicator with the exact same parameters (none in this case) for a second time, the constructor will return you the previous instance, for memory efficiency purposes. It seems that the way you use the indicators was not anticipated while designing this behavior. I will discuss it with the product team. In the meanwhile the workaround is to pass some kind of a dummy parameter to the constructor to force it to generate a new instance of the indicator e.g.

        [Parameter(DefaultValue = Modes.Mode3)]
        public Modes ModeParameter { get; set; }
        
        public  Modes Mode { get; set; }
        
        protected override void Initialize()
        {
            Mode = ModeParameter;
        }

The result should be

_indicator1 = Indicators.GetIndicator<IndicatorWithModes>(Modes.Mode2);
_indicator2 = Indicators.GetIndicator<IndicatorWithModes>(Modes.Mode3);
Print("Object indicator1 reference equals object indicator2 {0}", object.ReferenceEquals(_indicator1, _indicator2)); //returns FALSE

Let me know if this helps.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

Shares4us
18 Feb 2021, 11:17

RE:

We thought you'd either share som data array between the instances or return the same for efficiency purposes.
We'll make it work.

Thanks!

 


@Shares4us

Shares4us
18 Feb 2021, 11:17 ( Updated at: 18 Feb 2021, 11:18 )

RE:

slow site pressed update twice srry

 


@Shares4us

Shares4us
07 Mar 2021, 13:56

RE:

Any outcome of the prodcutteam discussion yet?

 


@Shares4us

PanagiotisCharalampous
08 Mar 2021, 09:05

Hi Shares4us,

This functionality was designed as a feature for better memory management, since in the vast majority of the cases, when calling the indicator with the same parameters the previous instance should provide the information you need. You use the indicator in an unorthodox way, therefore this issue popped up. We did not have any other reports regarding this till now. We will consider changing it if more issues like this are reported.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous