Announcement

Collapse
No announcement yet.

Limit execution to one time per bar

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

  • Limit execution to one time per bar

    I am using a study that runs continously as a bar is built (I am not using setComputeOnClose(true)). There are some lines of code that I only want to execute on the first tick of a new bar.

    This is what I tried:

    if (getBarState() == BARSTATE_NEWBAR)
    {
    if (OrderPlacedBarCount == 2)
    {
    OrderPlacedBarCount = 3;
    }
    }

    if ((getBarState() == BARSTATE_NEWBAR))
    {
    if (OrderPlacedBarCount == 1)
    {
    OrderPlacedBarCount = 2;
    }
    }

    When I run this and OrderPlacedBarCount was set to 1 on the previous bar, OrderPlacedBarCount returns 3 instead of 2. Since the formula is run each time a new tick comes in, I put the code that sets the OrderPlacedBarCount value to 3 first, assuming that it could only be run after the first tick of a new bar and would therefore not execute in the current bar. It appears that BARSTATE_NEWBAR does not limit execution only to the first tick of a new bar. Does anyone know of a function that will work for this?

    I am running version 7.91.

    Thanks

  • #2
    werosen
    BARSTATE_NEWBAR executes only once on the first tick of a new bar. Run the script enclosed below and in the Formula Output window you will see that vCounter increases by 1 only once per bar.
    Alex

    PHP Code:
    function preMain(){
       
    setPriceStudy(true);
       
    setStudyTitle("NEWBAR test");
    }

    var 
    vCounter =1;

    function 
    main(){
        
        if(
    getBarState() == BARSTATE_NEWBAR){
            
    debugPrintln("NEWBAR formed at "+getHour()+":"+getMinute());
            
    vCounter +=1;
        }
        
    debugPrintln("Counter total is "+vCounter)

    Comment


    • #3
      Code does increment by one

      I pasted your code into an empty editor and ran it, but it does not increment the valure of vCounter for each bar by 1. It increments by a value of 543, which looks like the total of all the previous bars in the study, plus one. Do you have any other suggestions on how to make a counter only increment by 1 for each bar as I was trying to do in my original code?

      Comment


      • #4
        Figured out the answer

        Alex:

        Thanks for the help. I just added a getBarIndex condition to make the counter increment by exactly one for each bar. I don't suppose you saw my other thread regarding ths sar function. That one is a real mystery.

        Warren

        Comment


        • #5
          werosen
          Run the code I posted in real time or in Tick Replay and you will see that BARSTATE_NEWBAR executes only once per bar on the first tick and in that instant increases the counter by a value of 1. On every other tick of that bar BARSTATE_NEWBAR does not execute and the counter remains unchanged at the last value (see attached image)
          Alex

          Comment

          Working...
          X