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 Haiasi, 04-25-2024, 06:53 PM
        2 responses
        17 views
        0 likes
        Last Post Massinisa  
        Started by Creamers, Today, 05:32 AM
        0 responses
        5 views
        0 likes
        Last Post Creamers  
        Started by Segwin, 05-07-2018, 02:15 PM
        12 responses
        1,786 views
        0 likes
        Last Post Leafcutter  
        Started by poplagelu, Today, 05:00 AM
        0 responses
        3 views
        0 likes
        Last Post poplagelu  
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,408 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Working...
        X