• 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.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

nested for loop of a barIndex

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

    #31
    The value of barIndex? I dont know, how do i get that?

    If i print Values ill get : 0

    Code:
    case MultiMA1types.MTYCMA:
          Values[count][0] = MTYCMA(i)[0];
          Print(Values[count][0]);
          break;
    Printing (i) will return the periods set to 160, so 3 to 158

    Code:
    if (CurrentBar < 10)
        return;
    
       int count = 0;
       for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
       {
        Print(i);
        switch (MaToUse)
        {
    My cma works fine and draw the line but with RMMA the cma dont show any lines in the chart.

    Code:
    for(int barIndex = (mybar - Period); barIndex <= CurrentBar; barIndex++)
        {
        double closePrice = Bars.GetClose(barIndex);
        double volumes = Bars.GetVolume(barIndex); 
    
        double clovol = closePrice * volumes; 
    
    
        sum1 += clovol++;
        sum2 += volumes++;
    
        cma = sum1 / sum2;
    
    
         Value[0] = cma;
        Print(Value[0]);
    TY

    Comment


      #32
      Hello frankduc,

      In the loop print barIndex.

      Print(barIndex);

      Below is a link to a forum post that demonstrates how to use prints to understand behavior (and errors).
      https://ninjatrader.com/support/foru...121#post791121
      Chelsea B.NinjaTrader Customer Service

      Comment


        #33
        CMA barIndex return in attachment.
        Notice how the numbers begin by 647, 647, and after that repeating 647 for a while. I would of expect 647, 648, 649, to 808. 809 would be the bar in progress not show in the Print.

        There is no barIndex in RMMA.
        Attached Files

        Comment


          #34
          Also i like to add that Printing inside or outside the brackets of the for loop return the same answer.
          Usually OR if i Print() inside the brackets i get all the values of the CMA and outside i only get the value of the last CurrentBar.
          But OBU it seems outside or inside the brackets it returns almost the same values. OBU outside the brackets i get a series of 0's than the values from cma comes in the OW.
          That explain why i cant use CMA(0)[0] in my indicator it returns the same error message as using the RMMA.CMA.

          TY

          Comment


            #35
            Hello frankduc,

            To understand why you are getting a repeating value in the print would require that you look through your logic to see why that is happening. From just the print I would be unsure why however this would relate to the logic you are using.

            Also i like to add that Printing inside or outside the brackets of the for loop return the same answer.
            Inside the for loop and outside the loop are two different contexts. When you are inside the loop you are working toward calculating the value, outside the loop you have already calculated the value. The two locations for the print would not be the same so you really cant expect the two prints to be the same. The only reason the prints would be the same would be if you are printing a variable which is not being set from within the loop. In that case the variable wont change and could be printed the same inside or outside the loop. If you are changing the variable during the loop, it should not be the same as when you print at the end of the loop.

            Usually OR if i Print() inside the brackets i get all the values of the CMA and outside i only get the value of the last CurrentBar.
            Correct, a loop iterates multiple times. The print outside your loop is not iterating so it happens one time on the bar when it was called.

            But OBU it seems outside or inside the brackets it returns almost the same values. OBU outside the brackets i get a series of 0's than the values from cma comes in the OW.
            I can't really comment on the calculation you are doing, if the value is different that would be my expectation even if it is a small difference. This will strictly depend on what you do in your logic as to what values would be printed inside or outside the loop.


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

            Comment


              #36
              Hi,

              I can't really comment on the calculation you are doing, if the value is different that would be my expectation even if it is a small difference. This will strictly depend on what you do in your logic as to what values would be printed inside or outside the loop.
              But is there a different impact between Print() inside or outside if its OR or OBU?

              Code:
              for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
                  {
                  mybar = barIndex;
                  }
              
              
              
                  for(int barIndex = (mybar - Period); barIndex <= CurrentBar; barIndex++)
                  {
                  double closePrice = Bars.GetClose(barIndex);
                  double volumes = Bars.GetVolume(barIndex); 
              
                  clovol = closePrice * volumes; 
              
              
                  sum1 += clovol++;
                  sum2 += volumes++;
              
                  cma = sum1 / sum2;
              
                   Value[0] = cma;
              
              
              Print(Value[0]); // inside and outside see attachement//
              } Print(Value[0]);
              That is the only reason why i get those errors:


              "CMAtest" flag: error on the application of the "OnBarUpdate" method on bar 0: The reference of the object is not passed to an instance of an object.
              "RMMA" indicator: error on the application of the "OnBarUpdate" method on bar 11: you can display an index that is not valid, but it is out of range. That is, accessing a series of [barsAgo] with a value of 5 when there are only 4 bars in the table.


              You spoke in earlier post :
              This type of loop in OnBarUpdate can also cause large performance impacts/lagging in the ui.
              Is there another way to get all the closes in a range of bars without using for loop? Because in the sma they dont use for loop. Also the RMMA has been built in the perspective to include ma's that have a different way of calculation.
              Attached Files

              Comment


                #37
                Hello

                But is there a different impact between Print() inside or outside if its OR or OBU?
                In contrast to your loop, not really you are still asking about the scope of where the print is. If the print is within the loop, it will happen for each iteration of the loop, the loop will happen once for the parent scope. If you don't do the print in the loop, it happens for the parent scope which would be OBU or OR in your example, it would happen at the frequency OBU or OR is called.

                If you are not certain with this question, just try printing from those two locations and observe how the print is between the tests.


                "CMAtest" flag: error on the application of the "OnBarUpdate" method on bar 0: The reference of the object is not passed to an instance of an object.
                This error means that something you used was null, from the provided syntax I cant tell what that was. You could reduce your code and use Prints to isolate the line having the error.

                "RMMA" indicator: error on the application of the "OnBarUpdate" method on bar 11: you can display an index that is not valid, but it is out of range. That is, accessing a series of [barsAgo] with a value of 5 when there are only 4 bars in the table.
                Again from the sample I cant tell what may have caused this, but you can track the error down by using prints to find the line in question and then review what you are doing. The error is noting that you are trying to use data which is not present, if you are looping over a series or using BarsAgo this can happen if you try to access data too early.

                Is there another way to get all the closes in a range of bars without using for loop? Because in the sma they dont use for loop. Also the RMMA has been built in the perspective to include ma's that have a different way of calculation.
                I am not certain why the loop is being used but if that is your requirement than you can certainly use it, loops are just in general a slow way to do logic in contrast to how the bars are processed. The SMA does not require a loop because it uses OnBarUpdate, OnbarUpdate is your loop. That is called for every bar so your calculation logic should start on bar 0 and work its way toward realtime one bar at a time.
                Here is an example, assume you using the SMA and have 10 bars on the chart. If you wanted to know the value of the 5 SMA, you would just use SMA(5)[0]. The [0] at the end denotes we want the current value of the indicator using the last 5 bars as its period. This forward processing allows you to calculate without a loop because you just move to the next bar once a new bar is received and do the same calculation using 5 bars as a period.

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

                Comment


                  #38
                  Sorry to ask more but that approach is new to me.

                  To get the volume in sma or in volume indicator they use:

                  Code:
                  Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
                  In SMA Value[0] = Input[0]; return all the close of each visible bars.

                  So i tried to replace Input[0] by Input[Period] Period = 160.
                  Strangely it is not returning what i was expecting.

                  I tried this but as you can see on the chart the yellow line is not at the right place.

                  Code:
                  for(int barIndex = (mybar - Period); barIndex <= CurrentBar; barIndex++)
                      {
                      double closePrice = Bars.GetClose(barIndex);
                      double volumes = Bars.GetVolume(barIndex); 
                      vols = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
                  
                      clovol = Input[Period] * vols; 
                  
                  
                      sum1 += clovol++;
                      sum2 += vols++;
                  
                      cma = sum1 / sum2;
                  
                  
                      Value[0] = cma;
                  
                       Print(
                  Value[0]
                  ); }
                  I also tried this:

                  Code:
                  {
                  
                  vols = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
                  clovol = Input[Period] * vols;
                  sum1 += clovol++;
                  sum2 += vols++;
                  cma = sum1 / sum2;
                  Value[0] = cma;
                  Print( Value[0]) ;
                  }

                  But line wont appear in the chart. Its like it is still needing the for loop?
                  I was trying this new approach hoping to solve the mystery of the errors i get.
                  Attached Files

                  Comment


                    #39
                    Hello frankduc,

                    To get the volume in sma or in volume indicator they use:

                    Code:
                    Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
                    Most of the internal indicators have this type of logic to account for crypto currencies, if you are not using crypto you can just use Volume[0]. Otherwise you could just copy this.

                    In SMA Value[0] = Input[0]; return all the close of each visible bars.
                    In some indicators you will see Input used, this means the indicator supports supplying a series to it as an input. This could represent the close or whatever was passed to the indicator to calculate, it doesn't have to be the close.


                    As for the plot, I am not sure what you are expecting the value to be to say this is wrong or right. I see you are multiplying the volume against an input and doing other calculations before plotting the value. If the plot is not where you expected it you would need to revise your calculation.


                    So i tried to replace Input[0] by Input[Period] Period = 160.
                    Strangely it is not returning what i was expecting.
                    What relevance does 160 have? You are asking for the input value of 160 bars ago, it wont match what you are currently looking at.


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

                    Comment


                      #40
                      Hello Jesse,

                      What produces the multiple moving averages in the RMMA is that part:

                      Code:
                      int LowPeriod = 2;
                      int HighPeriod = 160;
                      int StepAverage = 3;
                      int count = 0;
                      
                         for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
                         {
                      
                          Values[count][0] = MTYCMA(i)[0];
                      
                         count++;
                      
                         }
                      Not the Switch case statement, right? If i want to produce multiple cma's i dont need the Switch case.

                      Now is it possible to call
                      Code:
                       Values[count][0] = MTYCMA(i)[0];
                      within the CMA indicator to create multiple cma's?

                      Because i still get error message saying:

                      "CMAtest" flag: error on the application of the "OnBarUpdate" method on bar 0: The reference of the object is not passed to an instance of an object.

                      "CMAtest" indicator: error on the application of the "OnBarUpdate" method on bar 0: The index is outside the limits of the table.



                      Now in the RMMA AddPlot end up in State == State.Configure. I dont know why he has done that.

                      Cant see why it wont work.

                      Code:
                      protected override void OnBarUpdate()
                        {
                      
                         for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
                          {
                          mybar = barIndex;
                          }
                      
                      
                          for(int barIndex = (mybar - Period); barIndex <= CurrentBar; barIndex++)
                          {
                          double closePrice = Bars.GetClose(barIndex);
                          double volumes = Bars.GetVolume(barIndex); 
                      
                          clovol = closePrice * volumes; 
                      
                      
                          sum1 += clovol++;
                          sum2 += volumes++;
                      
                          cma = sum1 / sum2;
                      
                      
                          }
                      
                      
                         int LowPeriod = 2;
                         int HighPeriod = 160;
                         int StepAverage = 3;
                         int count = 0;
                         for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
                         {
                      
                      
                      
                          Values[count][0] = MTYCMA(i)[0];
                      
                      
                          count++;
                      
                         } 
                      
                        }

                      Comment


                        #41

                        Not the Switch case statement, right? If i want to produce multiple cma's i dont need the Switch case.
                        The RMMA specifically uses the switch to control what indicator is setting the plot. Only one of the indicators is used at once.


                        Now is it possible to call
                        Code:
                        Values[count][0] = MTYCMA(i)[0];
                        within the CMA indicator to create multiple cma's?
                        I couldn't say, this depends on your scripts configuration. You are calling MTYCMA with the i variable, and assigning it to an unknown plot. What is count and what value does it represent? That should be a index for the plot you want to set.


                        within the CMA indicator to create multiple cma's?
                        I am not sure what you mean by multiple cmas, if you mean to get the CMA value while changing its period, yes you are doing that by passing i so long as your indicator uses that input.

                        "CMAtest" flag: error on the application of the "OnBarUpdate" method on bar 0: The reference of the object is not passed to an instance of an object.

                        "CMAtest" indicator: error on the application of the "OnBarUpdate" method on bar 0: The index is outside the limits of the table..
                        These errors are for specific reasons but I couldn't tell what that is from the provided syntax. This is something you would need to debug to find what specific syntax throws the error.
                        The second error means you are using something that is null, that may be where you are trying to set the plot using count as the plot index, I couldn't say you would need to debug the code further to find the answer.

                        The second error relates to using an index incorrectly, this could also be related to the plot, from the sample I cannot tell. this is another situation you would need to find the specific line which causes the error and then view what values you are using in contrast to what data is available at that time.


                        Now in the RMMA AddPlot end up in State == State.Configure. I dont know why he has done that.

                        Cant see why it wont work.

                        I don't understand what you saying, are you saying that you placed the AddPlot in State.Configure? AddPlot can go in State.SetDefaults or Configure.

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

                        Comment


                          #42
                          Hello Jesse,

                          I am trying to use the RMMA in a if statement but i get an error once again. is it possible to use the RMMA in this context? It works with an SMA. The RMMA has a MultiMA1types.EMA; // default MA. So i suppose in theory it should throw multiple EMA.

                          Code:
                          if(RMMA(160)[0] < highPrice && RMMA(160)[0] > lowPrice)
                          NinjaScript File Error Code Line Column
                          SampleDisplayBarsAgo.cs No overload for 'RMMA' method takes no arguments 1 CS1501 329 10

                          Comment


                            #43
                            Hello frankduc,

                            Are you using a different RMMA than the one you previously uploaded to this thread? From what I can see the RMMA does not take just 1 parameter, I have attached an image of the file you provided and what it is asking for.

                            Code:
                            RMMA(MultiMA1types maToUse, int lowPeriod, int highPeriod, int stepAverage, int plotWidth, byte opacity, double saturation, double luminosity)

                            The error you are seeing seems to indicate you have used no parameters somewhere in your code or:

                            Code:
                            RMMA()[0]
                            Please let me know if I may be of additional assistance.
                            JesseNinjaTrader Customer Service

                            Comment


                              #44
                              Jesse,

                              I did

                              Code:
                              if(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]< highPrice && RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5)[0] > lowPrice)
                              I get no error message but its like i never did any change. Tried a Print( of the RMMA); nothing come's out of the OW.

                              Is it possible the RMMA.EMA is not applied to every high and low price it cross in the statement? It sees as a whole RMMA.EMA and if statement dont consider the RMMA as 15 EMA to test in the if statement?

                              Wow, if its that, when its not one thing its another. How do i get to separate them.

                              Comment


                                #45

                                Hello frankduc,

                                I tried printing the RMMA using the settings you provided and it outputs data for me, the problem likely resides in your logic. If your print is not being called you would see no output, try a more simple test like the following. Note the placement is the first line of OnBarUpdate and there is no other logic being used to return or prevent this from being called:

                                Code:
                                protected override void OnBarUpdate()
                                {
                                     Print(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]); //before any other logic
                                }
                                2998.42394724058
                                2997.97464908019
                                2997.4915496934
                                2997.83051656447
                                If you now see output, you will need to figure out at what point in your logic it is stopping short of reaching the other print.




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

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by GMiller64, Today, 01:13 PM
                                3 responses
                                5 views
                                0 likes
                                Last Post GMiller64  
                                Started by itrader46, Today, 12:51 PM
                                4 responses
                                30 views
                                0 likes
                                Last Post itrader46  
                                Started by hemlock, Today, 11:45 AM
                                3 responses
                                9 views
                                0 likes
                                Last Post NinjaTrader_PatrickG  
                                Started by gbulle, Today, 11:31 AM
                                1 response
                                6 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Started by JPPoulin, Today, 10:43 AM
                                6 responses
                                20 views
                                0 likes
                                Last Post JPPoulin  
                                Working...
                                X