• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


BarsArray question

  • Filter
  • Time
  • Show
Clear All
new posts

    BarsArray question

    I have a question regarding the BarsArray. I'm hoping to get further clarification on what is reported in the documentation.
    protected override  void Initialize()
        // Add a 5 minute Bars object which is added to the  BarArray 
        // which will take  index 1 since the primary Bars object of the strategy 
        // will be index 0 
         Add(PeriodType.Minute, 5); 
    protected override void OnBarUpdate() 
        // Ignore bar update events for the supplementary  Bars object added above 
        if (BarsInProgress ==  1) 
         // Pass in a Bars object as input for  the simple moving average method 
         // Checks if the 20 SMA of the primary  Bars is greater than 
        // the 20 SMA of the secondary Bars added above  
        if (SMA(20)[0] >  SMA(BarsArray[1], 20)[0]) 
    Question #1
    Can I have further clarification on the comment under the OnBarUpdate that if(BarsInProgress == 1) "Ignore bar update events for the supplementary Bars object added above "?
    Question #2
    Can I simply use "if (SMA(20)[0] > SMA(BarsArray[1], 20)[0])
    EnterLong(); " without checking the BarsInProgress as long as I use the proper BarsArray[]? I ask this because I don't have live data currently so I can't check it out myself?

    Hello CaptainAmericaXX,

    The comment indicates that if the added 5 minute series bar has closed and has triggered OnBarUpdate to prevent the code below from being evaluated.

    In other words, this would only allow the code below to run for the primary series and not the secondary series which is a 5 minute series.

    You can call a condition that checks to see if the SMA using which ever series is processing as the input to the SMA of the secondary series without the check.

    However, these use the same period. So if BarsInProgress is 1, you would be comparing the SMA of the secondary series to the SMA of the secondary series. In other words you are comparing the same series to itself. These will always have the same value and would never cross..
    Chelsea B.NinjaTrader Customer Service


      So if I say:
      Add(PeriodType.Range, 8);
      Add(PeriodType.Range, 4);
      if(SMA(BarsArray[1], 20)  > SMA(BarsArray[2], 20) )
      do I need to use the BarsInProgress?


        Hello CaptainAmericaXX,

        The code would be evaluated 3 times (once for each series) but would use the same values and would trigger the same action.

        Meaning that action would be triggered 3 times as well.

        So it depends on what you want to have happen.
        Last edited by NinjaTrader_ChelseaB; 11-16-2017, 03:27 PM.
        Chelsea B.NinjaTrader Customer Service


          Chelsea thank you for helping me out with this. I'm trying to wrap my mind around the concept of using different time elements to make on call.
          To begin with I defined a range bound situation. I then printed a square when the condition was true. The result was 4 squares on the same bar. I originally added 3 range bar instances so l'm assuming, from what you said earlier, that the OnBarUpdate() was called for each instance plus the default.
          I can see that I need to use the BarsInProgress but I'm not sure how. The end result that I'm hoping for uses a progressive code that relies on conditions from each range instance to be true before the next can be evaluated. I know this is very vague question, but if you follow me,do you know of an example code you can share would clarify this concept?


            Hello CaptainAmericaXX,

            You could have a set of triggers that are triggered on each BarsInProgress and progress through them.

            int triggerInt = 0;

            if (BarsInProgress == 0 /* && condition here*/)
            triggerInt = 1;

            if (triggerInt == 1 && BarsInProgress == 1 /* && next condition here */)
            triggerInt = 2;

            if (triggerInt == 3 && BarsInProgress == 2 /* && next condition here */)
            //execute code

            With this example, BarsInProgress 0 would process for the primary series on the chart, and if the condition is true will set the trigger to 1.

            Until this condition is trigger though, no other conditions will evaluate as the trigger has to be 1 before it will progress.

            Once the condition is true on the primary series, then the first added series will evaluate (BarsInProgress 1) and if the condition for that is true then sets the trigger to 2, and this allows the last series to evaluate (BarsInProgress 2).

            If you don't want actions to happen on every series, then you will need to specify which series you want the action triggered on.

            Below is a publicly available link to the help guide on BarsInProgress.
            Chelsea B.NinjaTrader Customer Service


            Latest Posts


            Topics Statistics Last Post
            Started by refine, Today, 05:02 PM
            0 responses
            1 view
            Last Post refine
            by refine
            Started by White Fork, Today, 01:57 PM
            2 responses
            Last Post NinjaTrader_Jim  
            Started by martyn73, Today, 02:45 PM
            2 responses
            Last Post martyn73  
            Started by ct, Today, 12:48 PM
            4 responses
            Last Post eDanny
            by eDanny
            Started by BrandonT1292, 07-30-2018, 07:32 PM
            11 responses
            Last Post EllisEdi