Announcement

Collapse
No announcement yet.

Why won't this output?

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

  • Why won't this output?

    Why does this code output the variable vsRSP, but not the variable x?

    What is the right way to accumulate x over many bars?

    Thanks, PD

    function preMain() { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >

    setStudyTitle("TSTRsumm");
    setCursorLabelName("TSTRsumm",0); //set label names
    setDefaultBarFgColor(Color.grey,0); //
    setDefaultBarFgColor(Color.black,1); //
    setDefaultBarThickness(3,0); //
    setDefaultBarThickness(3,1);


    } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<

    var I2=0;
    var P2=0;

    var vsRSP=0;
    var summ=0;

    function main() { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>

    I2=close(0,sym("IBM")); //
    P2=close(0);


    vsRSP = ( (P2/I2) )*100;
    summ=summ+vsRSP;


    return new Array(vsRSP, summ); //output

    } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<

  • #2
    perrydolia
    I am assuming that by x you are actually referring to the variable summ
    There are a number of ways you can accomplish what you want.
    The simplest in this case is to use the ref() function to call the value of a variable returned by a function at the prior bar(s). For information on the ref() function (complete with examples) see this article in the EFS KnowledgeBase
    Another way to accomplish this can be found in the OBV.efs which is installed with eSignal in the Formulas/Built-in Studies Formulas/Source Formulas folder
    Also if you search the forums for keywords such as cumulative you will find other examples as the topic has been covered before
    Alex


    Originally posted by perrydolia View Post
    Why does this code output the variable vsRSP, but not the variable x?

    What is the right way to accumulate x over many bars?

    Thanks, PD

    function preMain() { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >

    setStudyTitle("TSTRsumm");
    setCursorLabelName("TSTRsumm",0); //set label names
    setDefaultBarFgColor(Color.grey,0); //
    setDefaultBarFgColor(Color.black,1); //
    setDefaultBarThickness(3,0); //
    setDefaultBarThickness(3,1);


    } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<

    var I2=0;
    var P2=0;

    var vsRSP=0;
    var summ=0;

    function main() { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>

    I2=close(0,sym("IBM")); //
    P2=close(0);


    vsRSP = ( (P2/I2) )*100;
    summ=summ+vsRSP;


    return new Array(vsRSP, summ); //output

    } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    Comment


    • #3
      ACM (or anyone),

      Could you please explain a little bit WHY accumulating the variable summ this way doesn't work?

      The way I basically understand it, summ is defined outside the main() function, so it should retain its value as main() iterates through the bars. Therefore, it should be able to accumulate to higher summs.

      Summ+1 displays. Summ+I2 displays. Summ+P2 displays. Why doesn't the more complex expression work?

      Thanks for your help.

      PD

      BTW OBV.efs is very helpful in answering my original question. Thanks.

      Comment


      • #4
        perrydolia
        Add the following line of code after the one where you calculate summ
        if(getCurrentBarCount()==1) debugPrintln(I2+" "+P2+" "+vsRSP+" "+summ);
        then run the formula in a chart and the issue should become evident once you see the output in the Formula Output window.
        As to the reason why on the first bar/execution you get a null (hence Infinity when you divide by null in your equation for vsRSP – side note you should always check for nulls or 0 before a division) read the explanation I provide in this post
        Alex


        Originally posted by perrydolia View Post
        ACM (or anyone),

        Could you please explain a little bit WHY accumulating the variable summ this way doesn't work?

        The way I basically understand it, summ is defined outside the main() function, so it should retain its value as main() iterates through the bars. Therefore, it should be able to accumulate to higher summs.

        Summ+1 displays. Summ+I2 displays. Summ+P2 displays. Why doesn't the more complex expression work?

        Thanks for your help.

        PD

        BTW OBV.efs is very helpful in answering my original question. Thanks.

        Comment

        Working...
        X