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

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 cre8able, Today, 01:16 PM
        2 responses
        9 views
        0 likes
        Last Post cre8able  
        Started by chbruno, 04-24-2024, 04:10 PM
        3 responses
        48 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by samish18, Today, 01:01 PM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by WHICKED, Today, 12:56 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by WHICKED, Today, 12:45 PM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X