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

nested for loop of a barIndex

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

    #46
    You are right it is working OBU but not in OR.
    The
    Print(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]); return 0 in OnRender
    Is it suppose to do that or will i have to move everything in OBU?

    Comment


      #47
      I created a rmma in OBU and send the result using a variable in OR. Seems to hold for now. Not sure about the result, will test that.

      thanks

      Comment


        #48
        Hello frankduc,

        You are right it is working OBU but not in OR.
        Just for clarification this should not directly work in OnRender.

        This is a good opportunity to explain this difference between OBU and OR as you have been able to see it directly.

        When working in OBU the code you write is aware of what bar is being processed. For example Close[0] or SMA(13)[0], these have context and the [0] represents the value of now. When working in OnRender you loose that context, [0] BarsAgo means nothing there. With that being said, calling an Indicator or Price series with a BarsAgo will not work, you could however use the GetValueAt method with a specific bars index:

        Code:
        Print(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5).[B]GetValueAt[/B]([B]SomeBarsIndex[/B]));
        The purpose of OnRender is to only render over the visible bars, this is why we use an Index in OnRender because you are looping over the bars indexes. Special methods are used in OnRender to gather data because of that context change.

        I created a rmma in OBU and send the result using a variable in OR. Seems to hold for now. Not sure about the result, will test that.
        This is the right way to approach rendering values because OnBarUpdate has the context you need and OnRender can be used to display that data in some custom way. You will see this used in other indicators like the ZigZag.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #49
          Jesse,

          Maybe creating a variable of RMMA and sending it to OR is the right way. But it will only return one average in the my if statement.

          If i
          Print(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]);

          in OBU i get all the last value of each RMMA.VWMA but if i make a variable called double myramma =
          RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]);

          I end up with only one value and not the 9 of them.

          Is it how it suppose to look or there's something i dont get.

          Also what impact those it have if we change [0] for [1] or [2]? What do you mean by the value of now?

          Thanks

          Comment


            #50
            Hello frankduc,

            If you need 9 values you likely need 9 variables, the syntax you are showing is only going to return one value at a time. I am also not sure what you mean by the 9 of them, do you mean the last 9 bars or are you using 9 different periods to create 9 different RMMA values?

            The brackets after the indicator/plot name is the BarsAgo. When you are using OnBarUpdate everything is relative to "now" or the bar being processed at that time. If you wanted the current bars data, that is zero BarsAgo or [0]. If you needed father back data, you use a higher number of BarsAgo from now such as [1] which would be the previous bar.

            A quick example of the BarsAgo would be on bar 5 (5 bars have processed, CurrentBar == 5) we want the value of the last bar or CurrentBar == 4, that would equate to 1 BarsAgo or [1]. We are now processing bar 5, and 1 bar ago would be bar 4.








            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #51
              Jesse,

              That's the principle of the RMMA. You choose your moving average in this case the VWMA (testing) or the CMA and the RMMA will return the number of moving average according to settings.

              In
              RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]);

              It will create VWMA from period 2 to 160 with an interval of 10. That will produce 9 VWMA in the chart. In the output window it returns the last value at the right side of the chart of each VWMA.

              2991.58
              2991.13
              2990.8
              etc shown in data box

              My if statement should test each value 2991.58, 2991.13 etc.

              If i
              Print("myrmma"+myrmma); it only return close price and not the 9 value of the VWMA. Actually Printing the RMMA on OBU will only return the last value of each 9 VWMA and not all the values of each VWMA. To get all the values of each 9 VWMA you have to Print() that part in the RMMA Values[count][0] = VWMA(i)[0];

              I need the last value of each 9 VWMA to include in the if statement and later i will need all the values of each of the 9 VWMA as a variable. My question is why when i make a variable of the
              RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]); its not returning the last value of each 9 variables?

              Is it possible to import as a variable the result of
              Values[count][0] = VWMA(i)[0]; in RMMA to my indicator?

              Frank
              thanks
              Attached Files

              Comment


                #52
                Hello frankduc,

                If you want to access the other plots you would need to be specific and use the plot names/plot index. Using just the indicator syntax will return only the first default plots value. If you mean to access the other plots the indicator has that would mean you will need 9 variables and to collect those values from the indicator 9 times.

                This is no different than using the stock Bollinger indicator, if you want the Upper and Lower plot values you would have to call the indicator two times to get those values:
                Code:
                double upperValue = Bollinger(Low, 2, 20).Upper[0];
                double lowerValue = Bollinger(Low, 2, 20).Lower[0];


                The RMMA was not programmed to make the plots have names so you could instead use the Values collection just like they do inside of the RMMA:


                Code:
                double fistPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[B][0][/B][0];
                double secondPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[B][1][/B][0];
                The first index [0] or [1] which I bolded is the index of the plot, not a BarsAgo. The second index which are not bolded and are both [0] are BarsAgo representing the Current Value.


                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #53
                  Nice solution.

                  But what if i need the 160 period or more, i cant create 160 variables?
                  Is there a fast way to create 160 variables or a variable that could include the 160 period values?
                  A magic trick like :

                  double AllPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values
                  [0,1,2,3.....160]
                  [0];

                  Of course that would be to easy.
                  Cause this project is going to hit a wall otherwise.

                  Thanks

                  Comment


                    #54
                    Hello frankduc,

                    But what if i need the 160 period or more, i cant create 160 variables?
                    You are talking about two different things here. The period is not the same as your plots, you need 9 variables because you have 9 plots and you want to use the value in OnRender. You can use any period you want and re calculate, the 9 plots would not change only the calculated value which is being plotted changes. The period is a parameter you are providing to the indicator.

                    If you actually mean you need 9 plots, plus 160 variations of 9 plots, yes that will be a lot of variables because that is 160 X 9 if you wanted to plot that. In that situation you wouldn't use variables and would use a collection.


                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #55
                      Jesse,

                      Technically, if it was a CMA it would be only one moving average (one plot). But RMMA throw 160 averages of one kind of ma with a stepaverage of 10 producing 9 plots or (9 VWMA's) in this case.
                      The more ma's the more precise, so i will probably lower stepaverage to 1, so 1 ma by 160. But 160 is an exemple it could be actually many more.

                      About the collection the only way i see, it would be possible, is if i create one variable per plot? That makes to many variable to create to put in a collection. For exemple:

                      Code:
                      double fistPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[15][0];
                      double secondPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[14][0];
                      
                         var moavrs = new List<double> { fistPlot, secondPlot};
                      
                         foreach (var moavr in moavrs)
                                    {
                                     myavo = moavr;
                      
                                    }
                      Than i include myavo in the if statement. But i have to create as many variables as need it to install them into a collection.

                      Code:
                      if(myavo < highPrice && myavo > lowPrice)
                      Was this the way you were seeing this? Cause for now with my knowledge that is the best i can think of. Is there another way we can include in the collection all the 9 plots or more plots automatically without having to create a variable for each?

                      Something is strange, when Print(myavo); it flash the result of plot 14 and 15 but its often flashing more than 2 answers. Sometimes 4 or 6, i was expecting to return only 2 answers?

                      Frank
                      Thanks

                      Comment


                        #56
                        Hello frankduc,

                        From the given information it is still not clear what you are trying to accomplish.

                        A collection is not likely the correct solution for the goal, this was only mentioned based on your note:
                        But what if i need the 160 period or more, i cant create 160 variables?
                        If you had 160 variables defined a collection may be good for that use case to avoid using that many variables. The syntax you have shown you are only using two specific plots and the collection is likely not being used correctly. Additionally not all the relevant code was provided with your sample for me to understand what you were attempting to do here.

                        If you think you may need a collection, start by only using variables to accomplish the goal. Make the code work using only variables even if it takes 190 variables. Once you have it working, if you feel that you have used too many variables we could look at how to reduce the code. Because you are not certain right now if a collection should be used, or how it should be used I would likely suggest to remove that entirely and start with a more simple test. Collections are also not something our support will assist with, you can learn about C# collections and how to use them in external learning resources.

                        Lets step back to your last question again and try to clarify your needs further:

                        But what if i need the 160 period or more, i cant create 160 variables?
                        The RMMA produces 9 plots and takes an input period which you supplied as 160. The internal logic of that indicator uses the 160 to produce the 9 plots. If you needed a 160 period RMMA you would supply 160 as its period and call it like you have shown. If you instead wanted a different period like 140 you would enter 140 instead of 160. In the case you needed both the 160 RMMA and 140 RMMA at the same time, you will need to call the RMMA two times so you can provide the period each time.

                        For your question are you asking how to call the RMMA more than one time within a bar to get more than 9 values at once? What specifically are you trying to do with the RMMA?




                        Please let me know if i may be of further assistance.








                        JesseNinjaTrader Customer Service

                        Comment


                          #57
                          Hi Jesse,

                          You are right on one thing i am not sure what it takes to solve that issue.

                          In my indicator a for loop returns a variable call foundIndex which is equal to a certain barIndex.
                          foundIndex can return bar 1020, 500, 600, etc.
                          From foundIndex i create my moving average, so 160 is an exemple because foundIndex change constantly. foundIndex is moving in time and i choose 160 because there is 160 periods between foundIndex and CurrentBar for the exemple.

                          Now i need to plot all the moving averages between foundIndex and CurrentBar. Meaning one moving average from bar 160 to CB, bar 159 to CB, 158 to CB, etc.

                          RMMA allow me to do that. If i input 160, 2, 1 it will create 160 plots of VWMA.

                          Is there a way i can enter in the same if statement the two variables or all the plots and test them in one shot?

                          Code:
                          double fistPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[15][0];
                          double secondPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[14][0];
                          Code:
                          if([COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]fistPlot[/SIZE][/FONT][/COLOR]< highPrice &&[COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]fistPlot[/SIZE][/FONT][/COLOR]> lowPrice)
                          [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]if([/SIZE][/FONT][/COLOR][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]secondPlot[/SIZE][/FONT][/COLOR][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]< highPrice &&[/SIZE][/FONT][/COLOR][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]secondPlot[/SIZE][/FONT][/COLOR][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]> lowPrice)[/SIZE][/FONT][/COLOR][/LEFT]
                          I cannot repeat the if statement for each 160 plots, it will never end. If i do that i will also have to repeat all the structure of the formulas. There must be a more productive way to do that.

                          That's not my only problem. After the if statement the result is tested by passing throughout many formulas. At the end there is a lambda expression that chose one result i call the nearclose.
                          The closest value from close.
                          I need nearclose to return a value for each plot. Meaning 160 plots, 160 value returned by nearclose.

                          My collection attempt like you said is not made correctly because at the end nearclose is returning one answer.

                          I am not blaming you if you are not follow me, without the code and all structure or logic behind the project its difficult to grasp.

                          Thank you for doing the best that you can.
                          Last edited by frankduc; 10-21-2019, 12:34 PM.

                          Comment


                            #58
                            Hello,
                            I have been able to create a variable with all the plots from the RMMA.

                            Code:
                            NinjaTrader.NinjaScript.Series<double>[] series = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values;
                            
                                            IEnumerable<double> firstValues = series.Select(s => s[0]);
                            
                                foreach (double firstValue in firstValues)
                                {
                                   Print(firstValue);
                                }
                            For some reason if i try to install firstValue into the if statement it returns nothing.

                            Code:
                            if([LEFT][COLOR=#4D4D4D][FONT=Helvetica]firstValue[/FONT][/COLOR][/LEFT] < highPrice && [LEFT][COLOR=#4D4D4D][FONT=Helvetica]firstValue[/FONT][/COLOR][/LEFT] > lowPrice)
                            
                            //Print([LEFT][COLOR=#4D4D4D][FONT=Helvetica]firstValue); ////return none//[/FONT][/COLOR][/LEFT]
                            Any link to ninjatrader?

                            Thanks

                            Comment


                              #59
                              Hello frankduc,

                              If you are trying to loop over the plots you can just loop over the Values collection directly. The Select is not needed to loop over the existing plots, I would likely suggest using just a for loop for simplicity here.

                              This would be a basic loop over an indicators Values collection:

                              Code:
                              for(int i = 0; i < Values.Count(); i++)
                              {
                                     double value = Values[i][0];
                              }
                              This would give you access to the whole series and BarsAgo from within the loop to do any calculations.





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

                              Comment


                                #60
                                Hello,

                                Ok but how do we use it?

                                I tried

                                Code:
                                [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]myrmma =  RMMA(MultiMA1types.VWMA, 1, 160, 1, 2, 128, 0.75, 0.5) [0];
                                
                                   for(int i = 0; i < Values.Count(RMMA); i++)  // or [/SIZE][/FONT][/COLOR][/LEFT][LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]Values.Count(myrmma)[/SIZE][/FONT][/COLOR][/LEFT]
                                [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]     {
                                            double value = Values[i][0];
                                
                                      Print(value);
                                     }[/SIZE][/FONT][/COLOR][/LEFT]
                                [LEFT][COLOR=#4D4D4D][FONT=Helvetica][/FONT][/COLOR][/LEFT]

                                I get errors

                                NinjaScript File Error Code Line Column
                                SampleDisplayBarsAgo.cs' NinjaTrader.NinjaScript.Series <double> [] 'does not contain a definition for' Count 'and the best overload of the extension method' System.Linq.Enumerable.Count <TSource> (System.Collections. Generic.IEnumerable <TSource>, System.Func <TSource, bool>) 'contains invalid arguments CS1928 105 23


                                NinjaScript File Error Code Line Column
                                SampleDisplayBarsAgo.cs Argument 2: Can not convert group methods to System.Func <NinjaTrader.NinjaScript.Series <double>, bool> 'CS1503 105 36


                                This is returning a bunch of zero's

                                Code:
                                for(int i = 0; i < RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values.Count(); i++)
                                     {
                                            double value = Values[i][0];
                                
                                           Print(value);
                                     }


                                Frank
                                TY



                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by ScottWalsh, Today, 04:29 PM
                                0 responses
                                0 views
                                0 likes
                                Last Post ScottWalsh  
                                Started by rtwave, 04-12-2024, 09:30 AM
                                2 responses
                                20 views
                                0 likes
                                Last Post rtwave
                                by rtwave
                                 
                                Started by tsantospinto, 04-12-2024, 07:04 PM
                                5 responses
                                68 views
                                0 likes
                                Last Post tsantospinto  
                                Started by cre8able, Today, 03:20 PM
                                0 responses
                                7 views
                                0 likes
                                Last Post cre8able  
                                Started by Fran888, 02-16-2024, 10:48 AM
                                3 responses
                                49 views
                                0 likes
                                Last Post Sam2515
                                by Sam2515
                                 
                                Working...
                                X