View Full Version : Help neede on OnCalculate method

05-23-2014, 10:47 AM

I am facing problems in coding an indicator. Specifically on the OnCalculate method.

The design of the code is like this.
On start it calculates the indicator values for all bars. There is 6/7 plots and corresponding buffers.

After that Ontick event handles developing bar's indicator values. First OnTick event calls the function "setMyParamArrays" whick updates the price arrays for the indicator calculations. It updates only the last bar values of the arrays. Then it calls "myPTSPcci" which calculates the indicator. Then it calls "setMyPTSPcciValues" where the last bar values of indicator buffers are set. Then it calls another function to display alerts if any alert is formed on last bar.

Up to this everything works fine. At OnCalculate the same methodology is repeated. As new bar is formed, the arrays holding information to calculate the indicator as well as the result arrays are re defined with " reDim preserve " key word. Then again the functions detailed above are called to update the new bar data. However this does not work as expected and the indicator value calculated fails and becomes zero, and further on tick calls also fails.

Here if I use copyHigh functions to copy high/low arrays and then calculate whole formula with all data, it works. In this case there is many loops which will prepare the input arrays for the indicator calculation. But to avoid all the recalculation for efficiency calculating the last bar data is enough.

I can't make where it is going wrong. Still debugging the code to find out the cause. Hope some one experienced can help me.


05-23-2014, 02:04 PM
I think I sorted out the issue, The redim statement can be used at procedure level only, so the array variables declared at the top of the script, will not work, when redim statement is used at OnCalculate subroutine.
However the redim statement increased the array size but all previous data is lost.

05-23-2014, 06:04 PM

You can use Redim Preserve to save the values of the array

05-24-2014, 04:16 AM
I am using redim preserve in the code
The issue is the array is declared at the top level of the script, so when Redim Preserve is used in oncalculate sub, all data is lost in OnCalculate and other subroutines.

05-24-2014, 05:30 AM

I will study your code, and i will update you soon, please standby

05-24-2014, 09:27 AM
Take your time, thanks

05-24-2014, 10:23 AM

I believe that the calculations that happen on each tick cause a CPU overload (not that happen on each new bar), so no problem to calculate the indicator from the scratch on each bar.

05-24-2014, 12:50 PM
Hello Hassan,

Thanks for the info.