Two seperate instances of the same indicator. Setting a property of one of them leaks into the second
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)
{
}
}
}
Replies
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
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
@PanagiotisCharalampous
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.
The result should be
Let me know if this helps.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous