Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using foreach on BarsInProgress

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

    Using foreach on BarsInProgress

    Is it possible to use something like a foreach loop on the BarsInProgress? I have a complicated procedure that i need to do for 4 different time frames. it's a lot of code that could be made simpler if I could apply the same code to each data stream.

    Any ideas for how this might be done?

    the basic idea behind the code is that I'm checking some conditions on each of the 4 time frames, then making an evaluation based on those conditions and then tracking a total score for the 4 different conditions as evaluated together. The part of the code that checks for conditions could be much tidier if it were compressed into a single code block.

    any ideas?

    #2
    Originally posted by ShruggedAtlas View Post
    Is it possible to use something like a foreach loop on the BarsInProgress? I have a complicated procedure that i need to do for 4 different time frames. it's a lot of code that could be made simpler if I could apply the same code to each data stream.

    Any ideas for how this might be done?

    the basic idea behind the code is that I'm checking some conditions on each of the 4 time frames, then making an evaluation based on those conditions and then tracking a total score for the 4 different conditions as evaluated together. The part of the code that checks for conditions could be much tidier if it were compressed into a single code block.

    any ideas?
    Why not just use foreach to iterate through the BarsArray[] ?

    Comment


      #3
      Hi ShruggedAtlas,

      BarsInProgress is an integer value, so you could loop through with a standard for loop. Example below loops through the primary and two added series to print out their closing values.

      In Initialize():
      Code:
      Add("MSFT", PeriodType.Minute, 1);
      Add("CSCO", PeriodType.Minute, 1);
      OnBarUpdate()
      Code:
      if(CurrentBar < 0 || CurrentBars[1] < 0 || CurrentBars[2] < 0) return;
      			
      for (int x = 0; x <= 2; x++)	
      {	
      	if (BarsInProgress == x)
      	Print("Closing price for " + Instrument.FullName + ": " + Close[0]);
      }
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by koganam View Post
        Why not just use foreach to iterate through the BarsArray[] ?
        That's one way, I may experiment with that option. Looks like Ryan from NT gave me another option that uses a standard for loop. That looks like it might be easier to use, not sure until I try. Either way, both options will reduce my code by more than 2/3! I just need to wrap my mind around the form because I have a number of nested loops and conditionals within each timeframe so putting that all in yet another overarching loop will stretch my mental abilities. Thx!!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        579 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        334 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        554 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X