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

call indicator from OBU

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

    call indicator from OBU

    Hello,

    If i call my cma indicator from proprieties panel in indicator window the cma line will appear in the chart.

    If i call it from another indicator in OBU:

    Code:
    double sm = CMA01(100)[0];
    to make use of it i get an error message:

    Indicator 'NEWSDBA': Error on calling 'OnBarUpdate' method on bar 1: 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.

    Of course, the line do not show up in the chart.

    Frank
    Ty

    #2
    Hello frankduc,

    Thanks for your post.

    I suggest ensuring that you are assigning plot values in your CMA indicator's OnBarUpdate method. A plot value should be assigned with each new bar to ensure that a consistent plot line is made.

    I also suggest ensuring that 100 bars have been processed before making an indicator reference that requires 100 bars.



    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Jim,

      Did you mean:

      Code:
      if (CurrentBar < 100)
               return;
      
         double sm = CMA01(100)[0];
      That has already been done in the cma indicator. Its unfortunate it seems that i am facing the same problem i had when i was trying to create multiple cma's Cant figure out where is the problem.

      Frank

      Comment


        #4
        Hello frankduc,

        I would then suggest reducing the strategy to this simple set of code that simply adds the current bar check and makes you indicator reference. We should see the same results in this small test case.

        I would then suggest testing the SMA indicator with a 100 period in place of your CMA indicator. We should then see that the SMA reference works, and this would point to an issue with your CMA indicator. SMA will apply an plot value for each bar that OnBarUpdate is called. I would suggest ensuring that your indicator does the same if you want to reference it from an external script.

        Please let me know if you have any questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi,

          Is this the right way to do this?

          Code:
          protected override void OnBarUpdate()
            {
             double sm = SMA(100)[0];
            }
          Because SMA not showing either in the chart.

          Frank
          ty

          Comment


            #6
            Hello frankduc,

            Referencing an indicator in a script will not add it to a chart. The test would be used to demonstrate that you would not receive an error referencing an indicator that assigns plot values in OnBarUpdate. If you get an error testing your indicator, but do not see an error testing the SMA, it would point to an issue with how your indicator is assigning plot values.

            If you are adding the indicator in a strategy, you can use AddChartIndicator() to add the indicator to the chart. You can use the Strategy Builder to generate syntax using AddChartIndicator by creating a condition with an indicator and checking "Plot on chart" in the indicator's parameters. You can then click View Code to see the resulting syntax.

            If you are referencing the indicator in another indicator, you can add a plot in the hosting indicator and assign that plot value to what is calculated from the child indicator.

            I.E.

            Code:
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "MyCustomIndicator1";
                    AddPlot(Brushes.Orange, "MyPlot");
                }
            }
            
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 100)
                    return;
                Values[0][0] = SMA(100)[0];
            }
            I look forward to being of further assistance.
            JimNinjaTrader Customer Service

            Comment


              #7
              Hi,

              I would like some clarification.

              If i use

              Code:
              [LEFT][COLOR=#252C2F][FONT=Courier] [/FONT][/COLOR][/LEFT]double cma01 = CMA01(100)[0];
              
                 Print("cma" +cma01);
              I end up with this error message. With an SMA it is working.

              cma0
              Indicator 'CALcmaDown': Error on calling 'OnBarUpdate' method on bar 1: 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.

              Why CMA01 work alone but when call into an another indicator it fails is a mystery to me.

              Using
              if (CurrentBar < 100) make things worst because it prevent all codes to load under the if statement included in OBU or OR.

              In barsAgo 0 the first bar on the right. What is the last bar on the left? How do you get it?

              Frank
              TY

              Comment


                #8
                Hello frankduc,

                If you get an error testing your indicator, but do not see an error testing the SMA, it would point to an issue with how your indicator is assigning plot values.

                Please check how your indicator is assigning plot values. Plot values should be assigned in OnBarUpdate. You can also take steps to comment out sections of code in your indicator to identify a problematic part of the code.

                OnBarUpdate will process the first bar on the left first, then the second bar from the left, then the third and so on. CurrentBar increments whenever there is a new bar. Therefore, referencing Close[CurrentBar] will reference the first bar on the chart. Unless you specifically need to reference the first bar on the chart, you would just need to keep in mind what previous bars are needed for the calculation for the current plot value, and reference those bars using BarsAgo references to calculate that plot value.

                Please let us know if we can be of further assistance.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hello,

                  Sorry that's not what i meant. I know that
                  Close[CurrentBar] return the price of the first most left bar. Is there a way to get the total number of bars in chart without for loop?

                  By doing CurrentBar < Period ........Period is always barIndex I cant just CurrentBar < (CurrentBar - Period) to convert Period into barsAgo ? It wont return the right answer.

                  Ty

                  Comment


                    #10
                    Hello frankduc,

                    CurrentBar increments whenever there is a new bar. It could be used to tell you how many bars are on the chart that the script has processed.

                    By doing CurrentBar < Period ........Period is always barIndex I cant just CurrentBar < (CurrentBar - Period) to convert Period into barsAgo ? It wont return the right answer.
                    I'm not sure what you are trying to do here. Please provide context to show how you are using this code, and please also describe what you are expecting it to do.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Jim,

                      I am still investigating why i cant get the answer i seek with my attempt to produce more than one CMA.

                      Code:
                       for(int barsAgo = ChartBars.FromIndex; barsAgo < ChartBars.ToIndex; barsAgo++)
                                               {
                             allbar = barsAgo;
                      
                            }   
                      
                         
                         if (CurrentBar < (allbar-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++;
                      I am trying to eliminate the first for loop. If CurrentBar gives the number of bars in the chart why cant i replace allbar by CurrentBar?
                      If i use only
                      if (CurrentBar < (Period)) i will have to enter the barIndex in the indicator parameter instead of the barsAgo. Which is not a big drama but not solving the issue to how to create more than one cma correctly.

                      The way i see it the if statement determine the number of Periods used to create the cma.

                      Frank
                      ty

                      Comment


                        #12
                        Hello frankduc,

                        I see you are looping over ChartBars.FromIndex and ChartBars.ToIndex. This would be used in OnRender and would not be used in OnBarUpdate. You have not included the full code so this is unclear.

                        Setting plot values in OnRender is not advised. OnRender occurs after the script processes historical data, so the indicator will not have any plot values to be referenced when it is called from another script. This is in line with the symptoms you are seeing.

                        If you are still trying to calculate the indicator plot in OnRender, I suggest to stop doing so because you are creating more issues trying to take an approach to build your indicator than what we advise. We have mentioned this several times from various technicians, and community members have also given this insight. Disregarding this advice will bring you back to the same issues without a resolution.

                        Your indicator should calculate and set plots from within OnBarUpdate if you want it to be properly referenced when called from another script.

                        There is a CMA indicator on the User App Share that calculates in OnBarUpdate. I suggest using this indicator for your CMA, or using it as the basis for your works as it takes an approach we would advise.

                        Cumulative Moving Average. This indicator calculates the average of all prices summed up to the current bar. For more information: https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average


                        The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

                        Regarding allbar, you are setting allbar to a bar index between ChartBars.FromIndex and ChartBars.ToIndex. Logically, your loop will leave allbar as ChartBars.ToIndex-1 when the loop exits. This is not the same thing as CurrentBar which increments with each bar update in OnBarUpdate.

                        If you do not follow our recommendation to use OnBarUpdate to calculate the indicator plot and assign the plot value there, you will continue to run into these issues. We would not be able to provide insight when an approach is taken which we do not advise.

                        Please let me know if you have any questions.
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          Jim,

                          I have been talking about this for awhile now. That code is OnBarUpdate not OnRender.
                          I clearly said i wanted to eliminate the for loop.

                          Comment


                            #14
                            Hello frankduc,

                            As we have mentioned, using ChartBars.FromIndex and ChartBars.ToIndex has no viable purpose in OnBarUpdate. We do not advise using these properties here. Those properties represent what is visible on the chart. OnBarUpdate has no relation to what is currently visible on the chart and only has to do with the data that the script processes.

                            A CurrentBar check does not need to check what bars are visible on your chart. Subtracting Period from "allbar" would not be the proper way to write a current bar check. A proper current bar check would look as follows:

                            if (CurrentBar < Period) return;

                            "allbar" would not be used and the loop that assigns "allbar" a value would not be used.
                            Last edited by NinjaTrader_Jim; 12-13-2019, 07:51 AM.
                            JimNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by timmbbo, 07-05-2023, 10:21 PM
                            3 responses
                            150 views
                            0 likes
                            Last Post grayfrog  
                            Started by Lumbeezl, 01-11-2022, 06:50 PM
                            30 responses
                            804 views
                            1 like
                            Last Post grayfrog  
                            Started by xiinteractive, 04-09-2024, 08:08 AM
                            3 responses
                            11 views
                            0 likes
                            Last Post NinjaTrader_Erick  
                            Started by Johnny Santiago, 10-11-2019, 09:21 AM
                            95 responses
                            6,194 views
                            0 likes
                            Last Post xiinteractive  
                            Started by Irukandji, Today, 09:34 AM
                            1 response
                            3 views
                            0 likes
                            Last Post NinjaTrader_Clayton  
                            Working...
                            X