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

BarsArray question

Collapse
X
 
  • 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.
    Code:
    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) 
            return;  
     
         // 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]) 
              EnterLong(); 
    }
    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?
    Thanks

    #2
    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

    Comment


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

      Comment


        #4
        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, 04:27 PM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          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?

          Comment


            #6
            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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, Today, 06:40 PM
            0 responses
            9 views
            0 likes
            Last Post algospoke  
            Started by maybeimnotrader, Today, 05:46 PM
            0 responses
            7 views
            0 likes
            Last Post maybeimnotrader  
            Started by quantismo, Today, 05:13 PM
            0 responses
            7 views
            0 likes
            Last Post quantismo  
            Started by AttiM, 02-14-2024, 05:20 PM
            8 responses
            168 views
            0 likes
            Last Post jeronymite  
            Started by cre8able, Today, 04:22 PM
            0 responses
            10 views
            0 likes
            Last Post cre8able  
            Working...
            X