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

loop through all the bars in OBU

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

    loop through all the bars in OBU

    Hello,

    I want to loop through all the bars in OBU to produce multiple CMA's.

    Code:
    int allbar = 0;
         for(int i = ChartBars.FromIndex; i < ChartBars.ToIndex; i++)
                             {
           allbar = i;
    
          }  
    
    
    
         if (CurrentBar < (allbar-Period))
          return;
    The way i see it in the OW the allbar for loop, loop till the first bar of the right that is not in progress.

    That would be the reason why i get this error message:

    Indicator 'CMA01': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.
    Indicator 'RMMA': Error on calling 'OnBarUpdate' method on bar 11: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    Any pointers?
    TY

    #2
    Hi frankduc thanks for your post.

    There are two different errors coming from two different indicators. The second error means your accessing too far back in an array when there are not enough bars in the array to begin with. The first one looks like the strategy is using an object that is not initialized yet. The for loop posted works for me, so there is something else in the code that is wrong other than this loop.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      This is the code for cma and it works fine for me:

      Code:
           {
          int allbar = 0;
           for(int i = ChartBars.FromIndex; i < ChartBars.ToIndex; i++)
                               {
             allbar = i;
      
            }  
      
      
      
           if (CurrentBar < (allbar-Period))
            return;
      
      
           double volus = Volume[0];
      
            double price = Input[0];
      
      
         double privol = volus * price;
      
          sum01 += privol;
          sum02 += volus;
      
          cma0 = sum01/sum02;
      
      
           Value[0] = cma0;
      I also use this indicator to create multiple CMA's and i am not sure which one is bugging. From what you are saying there is a problem coming from both.

      Code:
      if (CurrentBar < 0)
                      return;
      
      
         int count = 0;
         for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
         {
      
      
          Values[count][0] = CMA01(i)[0];
      
          Print(i);
      
          count++;
      
         }
      I tried with the RMMA an indicator that allow you to create multiple moving average and it return the same errors.

      Comment


        #4
        Hi, thanks for your reply.

        Did you initialize CMA01 in State.DataLoaded? I would guess the problem goes away if you comment out this line:
        Values[count][0] = CMA01(i)[0];
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Chris,

          No i did not
          initialize CMA01 in State.DataLoaded. How to do that? In the RMMA logic there is no State.DataLoaded and i am NOT trying to draw the cma in the chart. I just want the result in the output window and use them in my indicator.

          // Values[count][0] = CMA01(i)[0];
          I reproduced the logic from RMMA indicator where Values represent each moving average created by the cma. count is counting the number of intervals between each cma's. So the (i) in CMA01(i) create a moving average of 2 periods to 160 periods with an interval of 10, that means about 9 cma Values in the chart. That would translate into one cma of 2 periods, another cma of 12 periods, 22 periods , etc //
          Last edited by frankduc; 11-15-2019, 11:40 AM. Reason: for NOT

          Comment


            #6
            Hi, thanks for your reply.

            I can not tell what is going wrong from the info given. Do you have a test script I can test out on my PC?
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Yes, should i sent you the file using email support to your attn?

              Comment


                #8
                Chris i just sent you an email support attn.

                Forgot to tell you i did modify the RMMA here's the file attach.

                ty
                Attached Files
                Last edited by frankduc; 11-15-2019, 02:31 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by andrewtrades, Today, 04:57 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by chbruno, Today, 04:10 PM
                0 responses
                5 views
                0 likes
                Last Post chbruno
                by chbruno
                 
                Started by josh18955, 03-25-2023, 11:16 AM
                6 responses
                436 views
                0 likes
                Last Post Delerium  
                Started by FAQtrader, Today, 03:35 PM
                0 responses
                7 views
                0 likes
                Last Post FAQtrader  
                Started by rocketman7, Today, 09:41 AM
                5 responses
                19 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X