Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 DJ888, Today, 10:57 PM
        0 responses
        6 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        158 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Today, 09:29 PM
        0 responses
        7 views
        0 likes
        Last Post Belfortbucks  
        Started by zstheorist, Today, 07:52 PM
        0 responses
        7 views
        0 likes
        Last Post zstheorist  
        Started by pmachiraju, 11-01-2023, 04:46 AM
        8 responses
        151 views
        0 likes
        Last Post rehmans
        by rehmans
         
        Working...
        X