Issue with Looping Through Function Calls (SuperTrend Cross)

Questions about MultiCharts and user contributed studies.
JMat83
Posts: 1
Joined: 20 Oct 2023

Issue with Looping Through Function Calls (SuperTrend Cross)

Postby JMat83 » 15 Nov 2023

Hello MultiCharts Community,

I've encountered a puzzling issue with a PowerLanguage script for the SuperTrend indicator. The script involves a function 'SuperTrend_Cross_fn' which identifies crosses in the SuperTrend indicator and returns +1 for a cross-up and -1 for a cross-down.

When I manually sum the function calls over different ATRLength values, the results are as expected. For instance:

Code: Select all

CrossCounter = SuperTrend_Cross_fn(ATRLength, ATRMult, AFStep, MaxAF, LookbackPeriod) + // ... additional calls with ATRLength+1, ATRLength+2, etc. SuperTrend_Cross_fn(ATRLength+9, ATRMult, AFStep, MaxAF, LookbackPeriod);
The manual addition correctly reflects the number of crosses. However, when I attempt to automate this with a loop that increments ATRLength, the counter invariably equals the number of iterations (e.g., 10 for cross-ups and -10 for cross-downs), regardless of the actual number of crosses:

Code: Select all

Vars: myvar1(0), CrossCounter(0); For myvar1 = 0 to 9 begin CrossCounter = CrossCounter + SuperTrend_Cross_fn(ATRLength + myvar1, ATRMult, AFStep, MaxAF, LookbackPeriod); end;
I've made sure to reset CrossCounter to 0 before the loop and ensure the loop is triggered once per bar with If BarStatus(1) = 2 then begin... end;. Despite these checks, the loop seems to always produce a total count equal to the number of loop iterations, which is not the case with manual addition.

I feel it can't be an issue with the function itself as the manual addition returns the correct output.

Has anyone experienced a similar issue or can offer insight into why the loop might not be working as intended? Any help or guidance would be greatly appreciated.

Return to “MultiCharts”