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

indexing CurrentBar

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

    indexing CurrentBar

    Hi,

    Is it possible to Index CurrentBar?

    I would like to index CurrentBar or Period inside an if statement using a for loop, if i try to [index] one or the other it returns an error message about impossible to index an int:

    Code:
    if (CurrentBar < (allbar-Period))
        return;
    ty

    #2
    Hello frankduc,

    Current bar is of type int you cannot index an int.

    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,

      In RMMA the
      DEMA(i)[0] is indexed. i represent the Period which is an int? Period is indexed in this case.


      Code:
      protected override void OnBarUpdate()
        {
         if (CurrentBar < 10)
          return;
      
         int count = 0;
         for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
         {
          switch (MaToUse)
          {
           case MultiMA1types.DEMA:
            Values[count][0] = DEMA(i)[0];
            break;

      Comment


        #4
        Hello frankduc,

        The period is not indexed in what you provided nor is the DEMA.

        DEMA is an indicator which returns a Series<double>, a Series double cannot be indexed but does have a BarsAgo field:

        Code:
        DEMA(i)[[B]0[/B]]
        DEMA(i)[[B]BarsAgo[/B]]
        The (i) in this case is the loops initializer, this means the loop starts with this value and increments this variable. i is coming from the loop you provided:

        Code:
        for ([B]int i =[/B] LowPeriod;
        Because i is of type int it can be passed to the DEMA which expects an int for its period parameter.



        To learn more about for loops you can use C# educational resources:
        https://docs.microsoft.com/en-us/dot...e/keywords/for

        To learn more about an "Index" and how that can be used with a C# Collection you can use C# education resources:
        https://docs.microsoft.com/en-us/dot...lection-1.item
        Collections and Indexes are not the same as the Series<T>/BarsAgo system used in NinjaScript



        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Frank, try describing what result you want, instead of what code you think you want.

          Example: "I want to exit the code block if the currentbar is not inside the lookback period."

          Code:
           
           if (CurrentBar < (allbar-Period))     return;
          What does the var "allbar" represent? If it is supposed to represent the total number of bars in the dataset, and you are inside OnBarUpdate, you can use Count to get the total number of bars in the dataset. or try Bars.Count .

          Comment


            #6
            Thanks for the clarification.
            Still not explaining those messages:

            Value of property 'Period' of NinjaScript 'CMA01' is 0 and not in valid range between 1 and 2147483647.
            Indicator 'CMAtest1': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.
            Indicator 'CMA01': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.

            Thanks for the info balltrader i replaced allbar by ChartBars.Count, and eleminated the a loop in the process. Too bad its not the reason why i cant create my multiple moving average.

            I thought my problem was link to the fact i needed to index currentbar. In my mind
            Code:
            if (CurrentBar < (ChartBars.Count-Period))
                  return;
            is used as a starting point to calculate my data series. If i input Period = 100 the calculation starts at bar 2098 and the result draw a line.

            Now to draw 100 lines:

            Code:
            protected override void OnBarUpdate()
              {
            
               int count = 0;
                for (int i = Period-1; i >= 0; i--)
               {
            
                Values[count][0] = CMA01(i)[0];
            
                Print(i);
            
                count++;
            
               }   
            
              }
            but its not working.

            Comment


              #7
              Values[count] is wrong. use the index of the AddPlot , ie first plot object will be 0, on bar 10 ti will be Values[0][10] = 10;

              i must go to a holiday party now, but maybe I can look at this tomorrow

              Comment


                #8
                Hello frankduc,

                Value of property 'Period' of NinjaScript 'CMA01' is 0 and not in valid range between 1 and 2147483647.
                This message is saying the value you used is not within the range you set. a minimum of 1 needs to be used. If you are trying to supply 0 to the CMA and have a property that has a range with a minimum of 1 you would see this error.

                Indicator 'CMAtest1': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.
                Indicator 'CMA01': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.
                Object reference not set to an instance of an object is a C# error which means the object being used is null. Something you are using on bar 0 is null.



                Please let me know if I may be of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Have a nice holiday to both of you.

                  Not sure what you mean by the index of AddPlot.

                  Code:
                  LowPeriod     = 100;    
                     }
                     else if (State == State.Configure)
                     {
                       for (int i = LowPeriod-1; i >= 0; i--)
                  
                  
                      {
                      AddPlot(Brushes.Aqua, "MyPlot"+i);
                  
                      }
                     }
                    }
                  
                    protected override void OnBarUpdate()
                    {
                  
                     int count = 0;
                      for (int i = LowPeriod-1; i >= 0; i--)
                     {
                  
                      Values[i][0] = CMA01(i)[0];
                  
                      Print(i);
                  
                      count++;
                  
                     }   
                  
                    }
                  I tried this approach and no luck either.

                  Code:
                  else if (State == State.Configure)
                     {
                  
                      for (int i = 0; i <= Period-1; i++)
                      {
                       AddPlot(Brushes.Aqua, "MyPlot"+i);
                      } 
                     }
                  
                    }
                  
                    protected override void OnBarUpdate()
                    {
                  
                  
                  
                     if (CurrentBar < (ChartBars.Count-Period))
                      return;
                  
                     int count = 0;
                     double sum01 = 0;
                     double sum02 = 0;
                     double cma = 0;
                     Print("***** Bar " + CurrentBar + " CMA Values: *****");
                      for (int i = 0; i <= Period-1; i++)
                     {
                  
                      
                      double privol = Volume[i] * Input[i];
                  
                      sum01 += privol;
                      sum02 += Volume[i];
                  
                      cma = sum01 / sum02;
                  
                      Values[count][0] = cma;
                      Print("Bar " + count + " of Period CMA value: " + cma);
                      count++;
                  
                  
                  
                     }  
                    }
                  Any insights is appreciated.

                  Happy holidays

                  Comment


                    #10
                    Frank Dude! You have been a member here since Sept 2009, but you don't know about plot objects? LOL I'll relax.
                    When you add a plot in the state=setdefaults, each plot can be "set" using the series named "Values", so first plot object can be set using statement "Values[0][0] = 100; "
                    The first bracket [0] is referencing the first plot object (it uses a zero based index, so 0 means 1st object. If you have a second plot object, you would reference it as Values[1][0].
                    In your code sample where you used Values[count] you would be trying to access plot objects which don't exist, and you get the error "Object reference not set to an instance of an object"

                    if you want to set the 1st plot object values for each bar on the chart, you use the second bracket set
                    Values[0][0] = current bar
                    Values[0][1] = previous bar
                    etc

                    maybe at this point you should attach your code file, and describe what each section is trying to output

                    Comment


                      #11
                      Hi ballTrader,

                      Thanks for the info i feel i am getting closer to the goal.
                      Values[count] = 100 as Period = 100. So its counting 1,2,3,4 …100

                      It should create a cma for each object (candle bars) in the chart. It should look like in the attach chart the yellow lines. Yellow lines represent a cma at every 10 periods. Now i want the blue lines to do the same with 100 periods. As you can see the lines dont start at each bar but all at the same bar on the left. The end of the lines at the right side are ok and return the right answers.

                      I came with that solution by replacing the i's with count.

                      Code:
                      else if (State == State.Configure)
                         {
                      
                          for (int i = Period-1; i >= 0; i--)
                          {
                           AddPlot(Brushes.Aqua, "MyPlot"+i);
                          } 
                         }
                      
                        }
                      
                        protected override void OnBarUpdate()
                        {
                      
                      
                      
                         if (CurrentBar < (ChartBars.Count-Period))
                          return;
                      
                         int count = 0;
                         double sum01 = 0;
                         double sum02 = 0;
                         double cma = 0;
                         Print("***** Bar " + CurrentBar + " CMA Values: *****");
                          for (int i = Period-1; i >= 0; i--)
                         {
                      
                          
                          double privol = Volume[count] * Input[count];
                      
                          sum01 += privol;
                          sum02 += Volume[count];
                      
                          cma = sum01 / sum02;
                      
                          Values[count][0] = cma;
                          Print("Bar " + count + " of Period CMA value: " + cma);
                          count++;
                      
                      
                      
                         }  
                        }
                      There is something i dont get yet to solve that issue and i dont know what.

                      Frank
                      Attached Files

                      Comment


                        #12
                        Hello frankduc,

                        We had advised previously to set up CurrentBar checks as follows:

                        Code:
                        if (CurrentBar < Period)     return;
                        We do this because OnBarUpdate starts counting from the first bar on the chart, Bar Index 0, and then with each new OnBarUpdate iteration, we will process the next bar, Bar Index 1, the next bar, Bar Index 2, and then the next bar, Bar Index 3, up until the last bar on the chart. When a new bar closes, OnBarUpdate then processes that bar. CurrentBar keeps track of the bar that is currently being processed in OnBarUpdate.

                        Let's say you have a Period of 10. I.E. you want a moving average to include 10 bars in the calculation.

                        Code:
                                private int Period = 10;
                                protected override void OnBarUpdate()
                                {
                                    if (CurrentBar < Period)
                                        return;
                        
                                    double sum = 0;
                                    for (int i = 0; i < Period; i++)
                                        sum += Close[i];
                                    Print(sum/Period);
                                }
                        We do the current bar check because if you want to reference the last 10 bars, the script needs to process that many bars first. If CurrentBar is less than 10, do not process further.

                        You have written If CurrentBar is less than ChartBars.Count-Period, do not process further. Please debug your code and add prints for this check to observe the number of bars you are looking for before you want your code to execute further. I.E. Print CurrentBar and (ChartBars.Count-Period) before the CurrentBar check to see what the condition is checking. This is preventing your logic from reaching the code where you assign plots until X number of bars have been processed.

                        All in all, you should abandon that approach and simply follow as is advised in the Help Guide for CurrentBar checks. Understanding your mistakes is important though, so please take the debugging steps to understand what you are attempting.

                        https://ninjatrader.com/support/help...nough_bars.htm

                        It should create a cma for each object (candle bars) in the chart. It should look like in the attach chart the yellow lines. Yellow lines represent a cma at every 10 periods. Now i want the blue lines to do the same with 100 periods. As you can see the lines dont start at each bar but all at the same bar on the left. The end of the lines at the right side are ok and return the right answers.
                        We have addressed many threads where you are still trying to tackle the same concept but are still coming back to the same troubles with looping and plotting.

                        It is critical to understand how plots are added and how BarsAgo references work in order to proceed further. Further experience with looping will be needed, as well. We had advised to start with the Indicator tutorials from NinjaTrader 7 so you can grasp these fundamental plotting concepts. You are in very deep and it is difficult for others to direct you in a forward direction because the underlying concepts are not yet mastered. Because of which, we cannot advise corrections to your code at this point, and would suggest starting small. Start with the NinjaTrader 7 tutorials. Implement one CMA line so it does what you want. Implement a second line after that. Once you have a few CMA lines working and enough code working to achieve your goal without complex loops, start working with more complex loops to condense your code.

                        We can also have an EcoSystem representative share more information on NinjaScript Consultants that can build an indicator for you at your request.

                        We look forward to being of further assistance.
                        JimNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by rayyyu12, Today, 12:47 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post rayyyu12  
                        Started by ETFVoyageur, 05-07-2024, 07:05 PM
                        17 responses
                        135 views
                        0 likes
                        Last Post ETFVoyageur  
                        Started by ETFVoyageur, Yesterday, 10:13 PM
                        1 response
                        10 views
                        0 likes
                        Last Post ETFVoyageur  
                        Started by somethingcomplex, Yesterday, 10:36 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post somethingcomplex  
                        Started by sofortune, 05-10-2024, 10:28 AM
                        5 responses
                        22 views
                        0 likes
                        Last Post sofortune  
                        Working...
                        X