Announcement

Collapse
No announcement yet.

why initialize study once?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • why initialize study once?

    in the minimal code below, why would i declare xSMA=sma(21) only the first time? is this because xSMA is an object and it saves memory so that object is not created again for each bar? it gives the same data whether or not i use bInit.

    if that is so, why can't i declare it globally where i initialize it at the top to null? thanks!

    ********************************
    var xSMA = null;
    var bInit = false;

    function main() {
    if (getCurrentBarIndex() == 0 ) return;

    if (!bInit) {
    xSMA = sma(21);
    bInit = true;
    }

    return xSMA.getValue(0);

    }

  • #2
    Re: why initialize study once?

    thinkingfield

    ...why would i declare xSMA=sma(21) only the first time? is this because xSMA is an object and it saves memory so that object is not created again for each bar?
    In your example even if you initialize the series object without using the bInit [or similar] routine the efs engine will still create it only once however on each iteration it will need to check if that object already exists so as not to create another instance of it. By using the bInit [or similar] routine you preempt the efs engine from having to perform those checks thereby increasing the efficiency of execution of the script

    if that is so, why can't i declare it globally where i initialize it at the top to null?
    You certainly can if you so wish
    Alex


    Originally posted by thinkingfield
    in the minimal code below, why would i declare xSMA=sma(21) only the first time? is this because xSMA is an object and it saves memory so that object is not created again for each bar? it gives the same data whether or not i use bInit.

    if that is so, why can't i declare it globally where i initialize it at the top to null? thanks!

    ********************************
    var xSMA = null;
    var bInit = false;

    function main() {
    if (getCurrentBarIndex() == 0 ) return;

    if (!bInit) {
    xSMA = sma(21);
    bInit = true;
    }

    return xSMA.getValue(0);

    }

    Comment

    Working...
    X