IndicatorDataSeries not always synchronized
IndicatorDataSeries not always synchronized
10 Mar 2021, 13:11
Hi
there are cases where IndicatorDataSeries appear to be not synchronized with Bars,
due to differed execution of Enumerable i believe.
To reproduce this, i'm posting an indi and a bot referencing the indi.
The indi has a parametered and a created series both filled with ClosePrices.
The bot attempts to read the created series and fails unless the parametered one is read first.
I would like to know when (approx) this issue was introduced
as this would explain various problems i'm facing recently when recompiling existing code.
Regards
Takis
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 TestProbIndi : Indicator
{
[Parameter(DefaultValue = 0)]
public int Parameter { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
public IndicatorDataSeries Buff;
protected override void Initialize()
{
Buff = CreateDataSeries();
}
public override void Calculate(int index)
{
Result[index] = Bars.ClosePrices.LastValue;
Buff[index] = Bars.ClosePrices.LastValue;
}
}
}
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 TestProbBot : Robot
{
[Parameter(MinValue = 0, MaxValue = 1, DefaultValue = 0)]
public int Parameter { get; set; }
TestProbIndi iTest;
protected override void OnStart()
{
iTest = Indicators.GetIndicator<TestProbIndi>(Parameter);
}
protected override void OnBar()
{
bool bLastValue = iTest.Buff.LastValue == Bars.ClosePrices.LastValue;
bool bCountB = iTest.Buff[iTest.Buff.Count - 1] == Bars.ClosePrices.LastValue;
bool bCountR = iTest.Buff[iTest.Result.Count - 1] == Bars.ClosePrices.LastValue;
// moving this line on top of the others fixes the issue
bool rLastValue = iTest.Result.LastValue == Bars.ClosePrices.LastValue;
Print(rLastValue + " " + bCountR + " " + bCountB + " " + bLastValue);
}
protected override void OnStop()
{
}
}
}
Replies
gennimatas
10 Mar 2021, 17:43
RE:
PanagiotisCharalampous said:
Hi Takis,
This is caused due to a lazy loading design of the IndicatorDataSeries. Calculate function is only called when Last() method and LastValue property are called for an output IndicatorDataSeries. This was always the case.
Best Regards,
Panagiotis
Thanks Panagiotis
@gennimatas
PanagiotisCharalampous
10 Mar 2021, 14:25
Hi Takis,
This is caused due to a lazy loading design of the IndicatorDataSeries. Calculate function is only called when Last() method and LastValue property are called for an output IndicatorDataSeries. This was always the case.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous