Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnBarClose running multiple times per bar

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

    OnBarClose running multiple times per bar

    Hello,

    I am having some trouble with the OnBarUpdate method running multiple times on the same bar, even though Calculate is set to OnBarClose.

    Code:
    protected override void OnBarUpdate()
            {
                if (CurrentBar < 1)
                   return;
                
                //Add your custom indicator logic here.
                
                if(TagFlag == true)
                {
                    
                    if(High[0] > Diamante)
                    {
                        PriceVar = High[0];
                        Draw.Line(this, "HF400" + TagVar, false, 2, PriceVar, -10, PriceVar, Brushes.DarkRed, DashStyleHelper.Solid, 1);
                        TagFlag = false;
                        
                        return;
                    }
    
                    return;
                }
                
                else
                {        
                    BarLow = Low[1];
                    Diamante = BarLow - 3;
                                                
                    if(Low[0] < Diamante)
                    {
                        // Draws a green line from 1 bar back through the following bar.
                        Draw.Line(this, "725400" + TagVar, false, 2, Diamante, -1, Diamante, Brushes.Green, DashStyleHelper.Solid, 2);
                        
                        // Draws a thin green line at the level of the previous bar's low.
                        Draw.Line(this, "391400" + TagVar, false, 2, BarLow, -10, BarLow, Brushes.Green, DashStyleHelper.Solid, 1);
                                                            
                        // Paints a yellow diamond on the current bar at the level of the condition being met.
                        Draw.Diamond(this, "tag" + TagVar, true, 0, Diamante, Brushes.Yellow);
                        
                        TagVar++;
                        TagFlag = true;
                                                                
                        return;
                        
                    }
                    
                    return;
                    
                }
                
            }​
    TagFlag is intialized to false. It is supposed to run the "else" condition on each bar until it creates the two green lines and the diamond marker. Then it sets TagFlag to true, and should then exit the method and wait for the next bar.

    The behavior that I am seeing, however, is that it sets the variable to true, but instead of exiting the method, it runs again on the same bar and executes the true commands, which includes setting the variable back to false. So, what is meant to be a global variable to control whether only one or the other set of instructions is executed becomes useless, and both sets of instructions are executed each time the first one is triggered.

    I have, at one point, gotten it to draw the red line on a subsequent bar which triggered the true condition, but it still drew the red line on the first bar. And in any case, whatever I changed since then has now made it so even that doesn't work.

    I thought OnBarClose meant that OnBarUpdate would only run once per bar. What am I doing wrong?

    Thank you in advance!
    Last edited by NjTMatthew; 08-25-2024, 03:30 PM.

    #2
    Does this script make any calls to AddDataSeries?

    Comment


      #3
      Hello NjTMatthew,

      Thank you for your post.

      If you add a print to the top of OnBarUpdate that simply prints out the Time[0] of the bar, are you seeing this happen multiple times per bar?

      As bltdavid mentioned, does your script make any calls to AddDataSeries()? If so, please note that OnBarUpdate() will be called for every bar update event for each Bars object added to the script.

      Comment


        #4
        Originally posted by NinjaTrader_Gaby View Post
        If you add a print to the top of OnBarUpdate that simply prints out the Time[0] of the bar, are you seeing this happen multiple times per bar?
        I did have Print commands everywhere. but I deleted them before I posted the code. And yes, I did see it happen multiple times per bar.

        I'm not sure about the AddDataSeries question, but I finally found that other post again and was able to make it work.

        What I ended up doing was to change the order so that the false condition evaluates first, and then when it triggers, it sets another global flag which (through the use of an && condition) skips over the true condition. Then, I updated the second variable to allow the true condition to run.

        Code:
        if(TagFlag == false && Low[0] < BarLow - 3)
                    {
                        //** all of the stuff the indicator primarily looks for and does **//
        
                        TagFlag = true;
                        LoopNum = 2;
                    }
                        
                    else if(TagFlag == true && LoopNum == 1 && Close[0] > IndVar)
                    {
                       //** a bit more indicator logic **//
        
                            TagFlag = false;
                    }
                                
                    else { LoopNum = 1; }​
        Still not sure why I had to do it this way, but it works.

        Thank you!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        628 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        360 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        564 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X