Announcement

Collapse
No announcement yet.

BARSTATE_NEWBAR issue

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

  • BARSTATE_NEWBAR issue

    Merry Christmas, hope you all had a good year, and I wish all the best ( buys and sells ) for the next year.

    Here's another snippet of code I've been stumbling over. I don't really understand how to pass a number variable out of a " if new bar " sequence.

    I'm playing with average true range, and I know I can just use the E-Signal function but ...

    I would like to calculate " A " once at the new bar, and have it available for each tick after that during the current bar.

    The following code does work, but it calculates " A " every tick.

    I have two lines of code " // " commented out. When I remove the two " // " I get an error message that specifies, " A is not defined ".


    Many many thanks in advance to the fine efs Guru who, well, doesn't quite enlighten me, but, helps me along the way another little step.



    function preMain() {
    setPriceStudy(true);
    setDefaultBarFgColor(Color.RGB(250,250, 0),0);
    setDefaultBarFgColor(Color.RGB(250,250, 0),1);
    }

    var bInit = false;

    function main() {
    if ( bInit == false ) {
    E10 = middleDonchian(6);
    bInit = true;
    }

    //if(getBarState()==BARSTATE_NEWBAR){
    A1 = high(-1) - low(-1);
    A2 = high(-2) - low(-2);
    A3 = high(-3) - low(-3);
    A = (A1+A2+A3) / 3 ;
    //}

    E = E10.getValue(0);

    Ep1 = E + A;
    En1 = E - A;


    return new Array(Ep1,En1);
    }

  • #2
    Re: BARSTATE_NEWBAR issue

    fastflyer
    Declare A as a global variable so that its value will persist on every iteration of the script
    BTW as a matter of good programming practice you should explicitly declare variables with a var statement
    Alex


    Originally posted by fastflyer
    Merry Christmas, hope you all had a good year, and I wish all the best ( buys and sells ) for the next year.

    Here's another snippet of code I've been stumbling over. I don't really understand how to pass a number variable out of a " if new bar " sequence.

    I'm playing with average true range, and I know I can just use the E-Signal function but ...

    I would like to calculate " A " once at the new bar, and have it available for each tick after that during the current bar.

    The following code does work, but it calculates " A " every tick.

    I have two lines of code " // " commented out. When I remove the two " // " I get an error message that specifies, " A is not defined ".


    Many many thanks in advance to the fine efs Guru who, well, doesn't quite enlighten me, but, helps me along the way another little step.



    function preMain() {
    setPriceStudy(true);
    setDefaultBarFgColor(Color.RGB(250,250, 0),0);
    setDefaultBarFgColor(Color.RGB(250,250, 0),1);
    }

    var bInit = false;

    function main() {
    if ( bInit == false ) {
    E10 = middleDonchian(6);
    bInit = true;
    }

    //if(getBarState()==BARSTATE_NEWBAR){
    A1 = high(-1) - low(-1);
    A2 = high(-2) - low(-2);
    A3 = high(-3) - low(-3);
    A = (A1+A2+A3) / 3 ;
    //}

    E = E10.getValue(0);

    Ep1 = E + A;
    En1 = E - A;


    return new Array(Ep1,En1);
    }

    Comment


    • #3
      Thank You Alexis

      Thanks again Alexis,

      I thought it was likely going to be something simple. If it was complex, I wouldn't have thought of the question !

      Best Wishes,

      Glenn

      Comment


      • #4
        Re: Thank You Alexis

        Glenn
        You are welcome
        Alex


        Originally posted by fastflyer
        Thanks again Alexis,

        I thought it was likely going to be something simple. If it was complex, I wouldn't have thought of the question !

        Best Wishes,

        Glenn

        Comment

        Working...
        X