Handle LookBack exceeded

Posted By: AndrewAMD

Handle LookBack exceeded - 05/25/22 13:01

Can there be a new feature for handling lookback exceeded?

For example, Script sets LookBack to 100. But in fact, 200 was required. So during cleanup(), the script will launch the same Zorro script (different instance), this time with Lookback = 200.

This can be useful for when all kinds of TA functions are mixed and matched, and it is difficult to calculate maximum lookback.
Posted By: Petra

Re: Handle LookBack exceeded - 05/25/22 19:51

Isnt the needed lookback displayed in the error message?
Posted By: AndrewAMD

Re: Handle LookBack exceeded - 05/25/22 20:01

Yes, but then I’d have to punch it in manually.

But imagine that I’m running a batch script where I input many variables at the command line. If a lookback error occurs, I can launch a sub-process that automatically corrects the issue with a sufficient memory allocation.
Posted By: jcl

Re: Handle LookBack exceeded - 05/26/22 07:53

We could implement a variable that contains the minimum lookback after the initial run, and a method to restart by script. You can then set Lookback like this:

LookBack = max(100,LookBackMinimum);
Posted By: AndrewAMD

Re: Handle LookBack exceeded - 05/28/22 15:43

Already done? Nice.

Quick question: If quit "+" is called, can the length of series calls change, or do I need to terminate the script to change the length?
Posted By: AndrewAMD

Re: Handle LookBack exceeded - 05/28/22 20:08

So it looks like series length is determined during INITRUN but not allocated until FIRSTRUN:
https://zorro-project.com/manual/en/series.htm
Quote
Since the LookBack value is normally only known after the INITRUN, series are allocated in the FIRSTRUN. During the INITRUN they are set to a temporary pointer and filled with the initial value. This temporary content is overwritten by the series allocation in the FIRSTRUN.
Plus my tests seem to confirm that quit "+" allows me to change series length without issue. This is nice!

One more thing: there's a bug in this sample code from the manual:
https://zorro-project.com/manual/en/lookback.htm
Code
// restart script when lookback period is exceededvoid run(){
  LookBack = max(30,LookBackNeeded);  ...
  SMA(seriesC(),50); // need more than 30 bars!
  if(!Init && LookBack < LookBackNeeded)
    return quit("+Restart with new LookBack");  ...}
quit(+) gets skipped because this can only be detected in Init. So more like this:
Code
if(Init && LookBack < LookBackNeeded)
Posted By: AndrewAMD

Re: Handle LookBack exceeded - 05/30/22 20:22

It's worth noting that invoking indicator functions to calculate lookback can still be problematic, even when restarting the script with quit(+).

Namely, if a vars of insufficient length is supplied to an indicator function, even in Init, this can cause many bad memory writes and strange errors. The workaround here is to supply a large dummy buffer during init only.
Posted By: jcl

Re: Handle LookBack exceeded - 05/31/22 13:41

With which indicator does this happen? The indicators from the ta-lib cannot read past the lookback period.
Posted By: AndrewAMD

Re: Handle LookBack exceeded - 05/31/22 13:50

That would require testing to determine, but I’d assume the ones from indicators.c.
Posted By: jcl

Re: Handle LookBack exceeded - 05/31/22 14:04

Hmm. We could limit them also to the lookback period, but many are also used with general data arrays. This would then not work anymore. A large dummy buffer is maybe indeed the best solution, only question is how large it should be.
Posted By: AndrewAMD

Re: Handle LookBack exceeded - 05/31/22 14:27

Or I can set the Lookback period to a ridiculously large number, then check lookback required, then call quit(+) and set lookback to the small number. (I'm guessing Zorro does some dummy buffer trick anyways.)
Posted By: jcl

Re: Handle LookBack exceeded - 05/31/22 16:32

Yes, that could work.
Posted By: AndrewAMD

Re: Handle LookBack exceeded - 06/01/22 11:14

One more question: I see that checkLookBack is used in indicators.c but is not documented. I assume that it sets LookBackNeeded, but does it return anything useful?
Code
C long F(checkLookBack)(long num);
Posted By: jcl

Re: Handle LookBack exceeded - 06/01/22 12:02

It returns LookBackNeeded, but you can also access that variable directly.
© 2024 lite-C Forums