Announcement

Collapse
No announcement yet.

Execution after initial load

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

  • Execution after initial load

    Hi,

    As I understand it anything within:
    if(getBarState()==BARSTATE_NEWBAR){code}
    should only execute on the first tick of a new bar but when I run the code below it plots a red circle above all candles even if I preceed it with:
    if(getBarState()==BARSTATE_ALLBARS){return;}

    Am I missing something?

    Only if I include -getCurrentBarIndex() == 0- as a condition does it then execute only on new bars, i.e.,:
    if(getBarState()== BARSTATE_NEWBAR && getCurrentBarIndex() == 0){code}

    Thanks in advance.

    This is my test code:
    PHP Code:
    function preMain(){
        
    setPriceStudy(true);
    }
    var 
    Cntr 0;
    function 
    main() {

        var 
    nState getBarState();

        if(
    nState == BARSTATE_ALLBARS ) return;
    //    else if(nState == BARSTATE_NEWBAR && getCurrentBarIndex() == 0){
        
    else if(nState == BARSTATE_NEWBAR){
            
    drawShapeRelative0high(0), Shape.CIRCLEnullColor.redShape.TOP Shape.ONTOP ); 
        }
    }
    This is what plots on the chart
    Wayne
    Attached Files

  • #2
    Re: Execution after initial load

    waynecd
    You can find a complete explanation of the getBarState() function in the Beginner Tutorial 4: Understanding Bar State and Bar Indexing which is in the Help Guides and Tutorials folder of the EFS KnowledgeBase
    Alex


    Originally posted by waynecd
    Hi,

    As I understand it anything within:
    if(getBarState()==BARSTATE_NEWBAR){code}
    should only execute on the first tick of a new bar but when I run the code below it plots a red circle above all candles even if I preceed it with:
    if(getBarState()==BARSTATE_ALLBARS){return;}

    Am I missing something?

    Only if I include -getCurrentBarIndex() == 0- as a condition does it then execute only on new bars, i.e.,:
    if(getBarState()== BARSTATE_NEWBAR && getCurrentBarIndex() == 0){code}

    Thanks in advance.

    This is my test code:
    PHP Code:
    function preMain(){
        
    setPriceStudy(true);
    }
    var 
    Cntr 0;
    function 
    main() {

        var 
    nState getBarState();

        if(
    nState == BARSTATE_ALLBARS ) return;
    //    else if(nState == BARSTATE_NEWBAR && getCurrentBarIndex() == 0){
        
    else if(nState == BARSTATE_NEWBAR){
            
    drawShapeRelative0high(0), Shape.CIRCLEnullColor.redShape.TOP Shape.ONTOP ); 
        }
    }
    This is what plots on the chart
    Wayne

    Comment


    • #3
      Alex,

      Looks like I need a trip back to the basics.

      Thanks,

      Wayne

      Comment


      • #4
        In case it helps others;

        In addition to the Tutorial I found a good tool to evaluate how -getBarState- functions in a script by SteveHare2003 found at:



        Wayne

        Comment

        Working...
        X