Announcement

Collapse
No announcement yet.

Referencing historical value of variable or recursive error

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

  • Referencing historical value of variable or recursive error

    Hi. A beginner coder here, looking for some guidance on a piece of code I'm trying to write.

    I'd like to write a function that uses the previous day's value of a variable to calculate today's variable (for several variables). Even though I have 6 or 7 variables, it only needs to look back 1 day so I searched here and found what seems to be the simplest solution which is to declare global variables and then use getBarState() to store the T-1 values (rather than use ref() or some array solution). Here's a snippet of the code I wrote:

    PHP Code:
    function preMain() {
        
    setStudyTitle("test");
        
    setCursorLabelName("test"0);
     
    }

        var 
    i1;
        var 
    i1_1;
        var 
    i2;
        var 
    i2_1;

    function 
    main(constAconstB) {

        if (
    constA == null) {
         
    constA 10;
    }
        if (
    constB == null) {
         
    constB 1;
    }
        
        if ( 
    isNull(i1) ) {
        
    i1 0;
    }
        if ( 
    isNull(i2) ) {
        
    i2 0;
    }
      
        if ( 
    isNull(i1_1) ) {
        
    i1_1 0;
    }
        if ( 
    isNull(i2_1) ) {
        
    i2_1 0;
    }


        if ( 
    getBarState() == BARSTATE_NEWBAR ) {
            
            
    i1_1 i1;
            
    i2_1 i2;
            
    }

    //Const Variables

    var c0 = (constA 1.0) * constB 1.0;
    var 
    c1 / (c0 1.0);
    var 
    c2 c1;

    //Calc
        
    if ( close(0) > ) {

    i1 c1 close(0) + c2 i1_1 ;
    i2 c1 i1 c2 i2_1;

    }
            return new Array (
    i1i2)


    As it stands the function returns nothing on the chart. I have tested it and the function returns values correctly for c0, c1 and c2. It also returns a value if I take out the (i1_1) value in the final calculation of i1. So this led me to believe that either I have the getBarState() function wrong or some kind of null/recursive error in calculating i_1.

    Can anyone give some pointers? Also if anyone can suggest a more elegant way of doing the null checks (or for the entire setup of the script for that matter), I would be most grateful. Thank you.

  • #2
    Cyprus2010
    Your formula is not returning anything because the null checks for i1, i1_1 etc never return true as those variables are undefined hence they never get assigned a value.
    To verify this add the following line
    if(getCurrentBarCount()==1) debugPrintln(i1);
    immediately before this code in your formula
    if ( isNull(i1) ) {
    i1 = 0;
    }

    and you will see that in the Formula Output you get undefined
    Now add = null to each global variable declaration ie
    var i1 = null;
    var i1_1 = null;
    var i2 = null;
    var i2_1 = null;

    and you will see that your formula will return values (whether these are what you ultimately want is a different issue)
    Anyhow enclosed below is a simplified version of your formula (it returns the same values as your corrected formula)
    Alex

    Edit after post: I replaced fP1 with fP2 in the second FunctionParameter

    PHP Code:
    function preMain() {
             
    setStudyTitle("test");
             
    setCursorLabelName("test"0);
        
            var 
    fP1 = new FunctionParameter("constA",FunctionParameter.NUMBER);
            
    fP1.setName("Constant 1");
            
    fP1.setDefault(10);

            var 
    fP2 = new FunctionParameter("constB",FunctionParameter.NUMBER);
            
    fP2.setName("Constant B");
            
    fP2.setDefault(1);
     }

    var 
    i1 0;
    var 
    i1_1 0;
    var 
    i2 0;
    var 
    i2_1 0;

    function 
    main(constAconstB) {
        
    //Const Variables
        
    var c0 = (constA 1.0) * constB 1.0;
        var 
    c1 / (c0 1.0);
        var 
    c2 c1;
        
        if ( 
    getBarState() == BARSTATE_NEWBAR ) {
            
    i1_1 i1;
            
    i2_1 i2;
        }

    //Calc
        
    if ( close(0) > ) {
            
    i1 c1 close(0) + c2 i1_1 ;
            
    i2 c1 i1 c2 i2_1;
        }

        return new Array (
    i1i2)
     } 


    Originally posted by Cyprus2010 View Post
    Hi. A beginner coder here, looking for some guidance on a piece of code I'm trying to write.

    I'd like to write a function that uses the previous day's value of a variable to calculate today's variable (for several variables). Even though I have 6 or 7 variables, it only needs to look back 1 day so I searched here and found what seems to be the simplest solution which is to declare global variables and then use getBarState() to store the T-1 values (rather than use ref() or some array solution). Here's a snippet of the code I wrote:

    PHP Code:
    function preMain() {
        
    setStudyTitle("test");
        
    setCursorLabelName("test"0);
     
    }

        var 
    i1;
        var 
    i1_1;
        var 
    i2;
        var 
    i2_1;

    function 
    main(constAconstB) {

        if (
    constA == null) {
         
    constA 10;
    }
        if (
    constB == null) {
         
    constB 1;
    }
        
        if ( 
    isNull(i1) ) {
        
    i1 0;
    }
        if ( 
    isNull(i2) ) {
        
    i2 0;
    }
      
        if ( 
    isNull(i1_1) ) {
        
    i1_1 0;
    }
        if ( 
    isNull(i2_1) ) {
        
    i2_1 0;
    }


        if ( 
    getBarState() == BARSTATE_NEWBAR ) {
            
            
    i1_1 i1;
            
    i2_1 i2;
            
    }

    //Const Variables

    var c0 = (constA 1.0) * constB 1.0;
    var 
    c1 / (c0 1.0);
    var 
    c2 c1;

    //Calc
        
    if ( close(0) > ) {

    i1 c1 close(0) + c2 i1_1 ;
    i2 c1 i1 c2 i2_1;

    }
            return new Array (
    i1i2)


    As it stands the function returns nothing on the chart. I have tested it and the function returns values correctly for c0, c1 and c2. It also returns a value if I take out the (i1_1) value in the final calculation of i1. So this led me to believe that either I have the getBarState() function wrong or some kind of null/recursive error in calculating i_1.

    Can anyone give some pointers? Also if anyone can suggest a more elegant way of doing the null checks (or for the entire setup of the script for that matter), I would be most grateful. Thank you.

    Comment


    • #3
      Alex. Thanks for the prompt response. I immediately see that I get values on the chart after making the changes you suggested. I will incorporate your ideas into my code and see where it leads me. Thanks again.

      Comment


      • #4
        Cyprus2010
        You are welcome
        Alex


        Originally posted by Cyprus2010 View Post
        Alex. Thanks for the prompt response. I immediately see that I get values on the chart after making the changes you suggested. I will incorporate your ideas into my code and see where it leads me. Thanks again.

        Comment

        Working...
        X