Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Understanding While loops in OnBarUpdate()

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

    Understanding While loops in OnBarUpdate()

    Hiya,

    If I have a While loop under OnBarUpdate(), does it mean that on each update event, the script will only run the While loop until it is satisfied? What I mean is this:

    Code:
    protected override void OnBarUpdate()
    {[INDENT] int x = 1;
    [/INDENT][INDENT]while (x == 1)
    [/INDENT][INDENT] {[INDENT] if (Close[0] > 10)
    x = 0;
    [/INDENT]}
    
    // Other code here
    [/INDENT]}
    
    }
    In the above example, if Close[0] does not > 10, does it mean that each bar update event will only run through the While loop, or will it run through the whole OnBarUpdate() code?

    Thanks!

    #2
    Hello nicbizz,

    It will run the While loop for each on bar update. The while loop only executes what's in the brackets { } that follows. If you have statements not connected to this while loop, they will still execute for each OnBarUpdate.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      nicbizz,

      It looks like you're setting up a possible endless loop. Each time OnBarUpdate() is called you're initializing x = 1. When it enters the loop Close[0] will never change, while inside the loop.

      So if Close[0] > 10, it will exit right away since x will be set to 0 because the condition will be met.
      If however, Close[0] were to be less than 10, it will always be less than 10 within the loop, and there will be no way to break out of the loop.

      IOW, for each call to OnBarUpdate() Close[0] will never change within OnBarUpdate().

      VT
      Last edited by VTtrader; 09-21-2011, 12:52 PM.

      Comment


        #4
        Ahh, I see. The whole block of code under OnBarUpdate() executes on each update event.

        Thanks for the help!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        635 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        365 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        106 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        567 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        571 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X