Calculate starts running before Initialize ends (in an Indicator if its Initialize had a non-critical Exception)
Calculate starts running before Initialize ends (in an Indicator if its Initialize had a non-critical Exception)
11 Oct 2024, 16:14
After running a backtest of a Robot (that eventually will use the particular Indicator), I attach the Indicator to the Backtest chart. The Indicator had several Log statements, and I can see many Calculate calls are being made BEFORE the Initialize has ended. The Initialize is doing things like GetBars of a slower TimeFrame and calling a method to gather statistics from them (as their 200 bar history covers a much longer period) up to the first OpenTime of the normal, faster, 200 history Bars that Calculate will see, with Calculate calling the same routine to update stats that were supposed to have been initialized before any Calculate calls ran.
Surely the first Calculate call should never happen before Initialize has returned, whatever the logic in either method is?
(ctrader version is 4.9.2)
Edited Update:
Solved, partly: Calculate only starts running early, i.e. before Initialize has finished, if there's an exception error raised in Initialize processing. The Initialize carries on, regardless of whether you have an OnException handler or not, with possibly only one Crash message in the Log which might go unnoticed amongst many other normal or debugging messages.
This can be modelled with a deliberate zero divide half way through a for loop in Initialize, with a Print in there and in Calculate. In my case the early running of Calculate caused other errors related to index going beyond the number of the Bars in the slower TimeFrame.
Is this a known feature?
"an exception in Initialize causes Calculate calls to start early and at the same time as Initialize carries on to completion"
Or possibly has been fixed in v5? Or is there something we could do in the OnException handler to tell Algo not to start the Calculate calls yet (other than putting a loopy wait in the beginning of Calculate to check whether Initialize has set something it does just before ending, to cause the first Calculate to wait)?
If none of these, please could the feature be looked into perhaps, or at least documented (other than this?). Thanks.
PanagiotisCharalampous
13 Oct 2024, 05:56
Hi there,
Please check v5.0 and let us know if you can still reproduce the error there.
Best regards,
Panagiotis
@PanagiotisCharalampous