Indicator onCalculate not called every tick
Indicator onCalculate not called every tick
19 Feb 2021, 14:43
Hi,
I noticed this strange behaviour relative to the onCalculate indicator method.
When i attach my own indicator to a chart, the onCalculate method is called only on every new bar open and it is not fired for each tick as expected.
This leads to a wrong indicator data.
The index is increased by 1 on every onCalculate call for all the past candles values. (in fact in the log there is no same values for 2 continous prints)
The Indicator starts to work correctly from the candle that is forming in that moment on.
This happens both on spotware ctrader v4.0 and TopFX ctrader v3.8
Here is the log relative to the following Print:
Print("index={0} newBarOpened={1} ", index, newBarOpened);
19/02/2021 12:21:20.008 | index=1140 newBarOpened=False
19/02/2021 12:21:19.914 | index=1140 newBarOpened=False
19/02/2021 12:20:38.789 | index=1140 newBarOpened=False
19/02/2021 12:20:38.039 | index=1140 newBarOpened=False
19/02/2021 12:20:31.711 | index=1140 newBarOpened=False
19/02/2021 12:20:30.680 | index=1140 newBarOpened=False
19/02/2021 12:20:07.742 | index=1140 newBarOpened=False
19/02/2021 12:20:06.789 | index=1140 newBarOpened=False
19/02/2021 12:20:02.680 | index=1140 newBarOpened=False
19/02/2021 12:20:02.586 | index=1140 newBarOpened=False
19/02/2021 12:19:55.836 | index=1140 newBarOpened=False
19/02/2021 12:19:55.633 | index=1140 newBarOpened=False
19/02/2021 12:19:54.696 | index=1140 newBarOpened=False
19/02/2021 12:19:50.508 | index=1140 newBarOpened=True
19/02/2021 12:19:50.508 | index=1139 newBarOpened=True
19/02/2021 12:19:50.508 | index=1138 newBarOpened=True
19/02/2021 12:19:50.508 | index=1137 newBarOpened=True
19/02/2021 12:19:50.508 | index=1136 newBarOpened=True
19/02/2021 12:19:50.508 | index=1135 newBarOpened=True
19/02/2021 12:19:50.508 | index=1134 newBarOpened=True
19/02/2021 12:19:50.508 | index=1133 newBarOpened=True
19/02/2021 12:19:50.508 | index=1132 newBarOpened=True
19/02/2021 12:19:50.508 | index=1131 newBarOpened=True
19/02/2021 12:19:50.508 | index=1130 newBarOpened=True
19/02/2021 12:19:50.508 | index=1129 newBarOpened=True
19/02/2021 12:19:50.508 | index=1128 newBarOpened=True
19/02/2021 12:19:50.508 | index=1127 newBarOpened=True
19/02/2021 12:19:50.508 | index=1126 newBarOpened=True
19/02/2021 12:19:50.508 | index=1125 newBarOpened=True
19/02/2021 12:19:50.508 | index=1124 newBarOpened=True
19/02/2021 12:19:50.508 | index=1123 newBarOpened=True
19/02/2021 12:19:50.508 | index=1122 newBarOpened=True
19/02/2021 12:19:50.508 | index=1121 newBarOpened=True
Here is the simple code:
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 DeltaVolWeisv1 : Indicator
{
[Output("Delta Volume", PlotType = PlotType.Histogram, Color = Colors.Blue, Thickness = 3)]
public IndicatorDataSeries oDelta { get; set; }
private bool newBarOpened;
private int index_old;
private bool first_scan;
private double prev_tick_close;
private double prev_tick_vol;
private LastVolAssign prev_vol;
protected override void Initialize()
{
newBarOpened = false;
index_old = -1;
first_scan = true;
}
public override void Calculate(int index)
{
newBarOpened = isNewBar(index, ref index_old);
Print("index={0} newBarOpened={1} ", index, newBarOpened);
}
private bool isNewBar(int idx, ref int idx_old)
{
if (idx > idx_old)
{
idx_old = idx;
return true;
}
else
{
return false;
}
}
}
}