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

    #16
    Hello frankduc,

    That does not seem to be part of C#, are you omitting other code which you have used in this test? In what you provided the ToConsole method is not present.

    Pleas let me know if I may be of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #17
      Hello,

      It still in the same topic about the RMMA.

      I am trying to reproduce the RMMA logic to create multiple cma's.

      Code:
      int count = 0;
      
          for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
          {
          mybar = barIndex;
      
      
          switch (cma)
          {
           case cma:
            Values[count][0] = cma(mybar)[0];
            break;
          }
          count++;
          }
      I am receiving an error message about switch (cma)

      [COLOR=rgba(0, 0, 0, 0.87)]NinjaScript File Error Code Line Column[/COLOR]
      [COLOR=rgba(0, 0, 0, 0.87)]SimplyDivVol.cs A switch expression or a case label must be of type bool, char, string, integral, enum or Nullable corresponding CS0151 115 13[/COLOR]

      In RMMA (cma) is actually
      (MaToUse). That part refer to
      if (State == State.SetDefaults) in MaToUse = MultiMA1types.EMA;

      Code:
      int count = 0;
         for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
         {
          switch (MaToUse)
          {
           case MultiMA1types.DEMA:
            Values[count][0] = DEMA(i)[0];
            break;
      My question is, if i dont need a bool and i dont need to make it a choice in parameter by what can i replace that part of switch?

      TY

      Comment


        #18
        Hello frankduc,

        The syntax you provided seems to not be correct, or not enough information was provided. The switch (cma) is likely not correct, what type is cma? Is cma an enum? It looks like you use cma like an indicator shortly after the switch(cma), I cant really tell what you are trying to do there.

        If you wanted to make a choice, you would need to create an enum for that and a public property for the enum. Later in your logic, such as the switch you would use the enum property. This is shown in the candlestick patterns indicator (switch (Pattern)).


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

        Comment


          #19
          Hi Jesse,

          The goal is to create multiple CMA. I dont need them to show up in the chart i just need the result.

          Code:
          for(int barIndex = mybar; barIndex <= ChartBars.ToIndex; barIndex++)
              {
              closePrice = Bars.GetClose(barIndex);
              volumes = Bars.GetVolume(barIndex); 
          
              clovol = closePrice * volumes; 
          
          
              sum1 += clovol++;
              sum2 += volumes++;
          
              cma = sum1 / sum2;
          
          
              }

          To produce that i looked to the RMMA. But there is somethings, i can figure out, how to input them into my code to reproduce the idea.

          For switch (cma), according to docs.msn the match expression can be pretty much everything except double and cma returns a double.

          I am not sure what to do with Values, in principle its link to the value returned by other ma's. In my case the cma is created from a for loop that loop through a certain number of bars in the chart.
          All i wanted was to create cma's for all the period in the chart. The way i see it switch case is use to reproduce a same task over and over.

          Cant just replace in cma
          ChartBars.FromIndex by mybar at this stage it wont produce the desired effect. I am not even sure what to do with count++ if its necessary or not.

          Ty

          Comment


            #20
            Hello frankduc,

            I am still not certain what you are trying to do with the switch, as you noted your cma variable will not work with a switch because it returns a double. In what you provided you have used the cma variable in two different ways, one is like an indicator and the other was like a value. Is cma a double type or is this an indicator?

            If this is a double type you have other problems in your code in addition to the switch. If this is an indicator, you would need to just call your indicator more than once with the parameters you needed to get more than one value.

            Here is an example of calling the same indicator twice to get two different values based on different parameters being input:

            Code:
            double sma1 = SMA(12)[0];
            double sma2 = SMA(14)[0];
            if your cma variable is an indicator, you likely could do something similar:

            Code:
            double cma1 = cma(mybar)[0];
            double cma2 = cma(someOtherBar)[0]
            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #21
              This is a variable double type inside my indicator.
              I could create the cma as an indicator and call it in my new indicator. That would mean to create a Period for the cma.

              I was hoping to use switch because i dont need to convert cma into an indicator, cma is like an indicator in the indicator. CMA is a moving average i use for calculation of my indicator.
              Like you can see in RMMA it produce several numbers of moving average for the sma, ema, etc.
              I just wanted to reproduce that in the moving average of my indicator without having to create a new cma indicator.

              Not to mention i need to understand how to apply switch case so i can use it for my other for loops in the indicator i am creating.
              Attached Files

              Comment


                #22
                Hello frankduc,

                If you wanted to make something similar to what the RMMA does, you would need to use an enum just like the RMMA syntax. In the RMMA it uses the enum MultiMA1types to define the options and a variable MaToUse is used with the switch to know what option is selected. The RMMA does not use a double variable in the switch like you did, so to fix what you were trying to do you would have to match what the RMMA did in its syntax.

                Enum and Switch are not specific to NinjaScript, these are concepts you can learn more about for C#.

                You can also see the following example which uses an enum: https://ninjatrader.com/support/help...ned_parame.htm
                The candle stick patterns indicator also uses an enum for selections.

                I can't tell you how to use the cma with the switch as I don't really understand what you are trying to do in the syntax you provided, you can instead use the examples you have available to see a working enum and then apply that type of logic to your own custom script how you would like.





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



                JesseNinjaTrader Customer Service

                Comment


                  #23
                  Lets solve one problem at a time. Even if want to use the RMMA code to plot my cma i need to make it work first.
                  For now my cma is producing a straight horizontal line in the chart.

                  When using AddPlot(Brushes.Blue, "cma"); do i need to put something in the #region Properties?
                  Someone at NT told me it was necessary to add base.OnRender(chartControl, chartScale); somewhere in the OnRender.
                  But my cma is code in OBU. Does it make a difference?

                  ty

                  Comment


                    #24
                    Hello frankduc,

                    If you are seeing the plot, nothing additional is needed. if the plot is flat, that would be dependent on how you are setting the plot. You would need to review how you are setting the plot if that is not working to your liking.


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

                    JesseNinjaTrader Customer Service

                    Comment


                      #25
                      no text reedit rewrite the code
                      Last edited by frankduc; 10-07-2019, 01:26 PM.

                      Comment


                        #26
                        Hello frankduc,

                        In what you provided it looks like you are combining OnRender logic with OnBarUpdate where it is not relevant to be used. This type of loop in OnBarUpdate can also cause large performance impacts/lagging in the ui.

                        The ToIndex will be known on Bar 0 when you begin processing and will not change until realtime data is received and a new bar is generated. For all historical bars you will see the same value being used in your calculations.

                        Lets do a quick example with actual numbers instead of variables. Lets assume your period is 100 as you have it now and the ChartBars.ToIndex is 4000 because there are 4000 bars on the chart.

                        On bar 0 when processing first starts or CurrentBar 0, this is your loop:

                        Code:
                          for(int barIndex = 100; barIndex <= 4000; barIndex++)
                        Now lets assume the historical data has nearly finished processing and CurrentBar is now 3000, here is the loop again at bar 3000:

                        Code:
                         for(int barIndex = 100; barIndex <= 4000; barIndex++)
                        Nothing changed.

                        The value should be identical (straight line) because you didn't do anything different between bar 0 and bar 3000.

                        Keep in mind that ChartBars.ToIndex represents the right most bar of the chart, when you load a chart that will be known ahead of time so using this value for historical processing is not relevant. It would only be relevant to use this value if you needed to know the count of bars on the chart, however you could just use Count for that. ChartBars.ToIndex also shifts as you scroll back or forward in time, this is used specifically for OnRender to draw over the currently visible bars only.


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









                        JesseNinjaTrader Customer Service

                        Comment


                          #27
                          Than why cma return the right answer? If i do a Print(cma); i get the cma i am suppose to have.
                          Why AddPlot wont draw the line from each point? I know you probably gonna repeat that thing you said about bar 0 to bar 3000 and ToIndex but i dont get why AddPlot wont draw my line from the numbers in the Printed(cma);?
                          It goes from index 100 to barIndex 3000. Switching
                          ChartBars.ToIndex to 3000 wont solve the issue.
                          The calculation is right the cma return's a cumulative moving average of bar 100 to current bar 3000.
                          What you are saying is that AddPlot is drawing right now the last value of the cma 2900 times?

                          Its like drawing (Printing) outside of the non-
                          curly brackets. Can we put AddPlot inside of the brackets?

                          Comment


                            #28
                            Got It ! Thanks
                            now i understand even if go to read you 6 times in a row.

                            Comment


                              #29
                              Hello Jesse,

                              Still struggling with RMMA-CMA.

                              I did replace in my for loop:

                              for(int barIndex = (mybar - Period); barIndex <= CurrentBar; barIndex++)

                              mybar get its logic from the for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)

                              The CMA is fine but the RMMA wont display the multiple ma's. When i was only using Period without mybar the RMMA was displaying the multiple cma's but on the wrong side.

                              It returns 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.


                              Its got to be related to the mybar for loop. Still mybar return the number of bars in the chart. How come it only sees 4 bar in the table and what is wrong with bar 11?

                              Your wisdom is welcome.

                              TY

                              Comment


                                #30
                                Hello frankduc,

                                When using AddPlot() this will create an index in the Values collection. The plot will plot what is set to the Values[plot index][barsAgo index] for each bar.



                                Regarding the index error, what is the value of barIndex when the index error occurs?
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by RookieTrader, Today, 09:37 AM
                                3 responses
                                15 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Started by kulwinder73, Today, 10:31 AM
                                0 responses
                                8 views
                                0 likes
                                Last Post kulwinder73  
                                Started by terofs, Yesterday, 04:18 PM
                                1 response
                                24 views
                                0 likes
                                Last Post terofs
                                by terofs
                                 
                                Started by CommonWhale, Today, 09:55 AM
                                1 response
                                5 views
                                0 likes
                                Last Post NinjaTrader_Erick  
                                Started by Gerik, Today, 09:40 AM
                                2 responses
                                8 views
                                0 likes
                                Last Post Gerik
                                by Gerik
                                 
                                Working...
                                X