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 r68cervera, Today, 05:29 AM
        0 responses
        2 views
        0 likes
        Last Post r68cervera  
        Started by geddyisodin, Today, 05:20 AM
        0 responses
        3 views
        0 likes
        Last Post geddyisodin  
        Started by JonesJoker, 04-22-2024, 12:23 PM
        6 responses
        35 views
        0 likes
        Last Post JonesJoker  
        Started by GussJ, 03-04-2020, 03:11 PM
        12 responses
        3,239 views
        0 likes
        Last Post Leafcutter  
        Started by AveryFlynn, Today, 04:57 AM
        0 responses
        6 views
        0 likes
        Last Post AveryFlynn  
        Working...
        X