Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue calling custom indicator, multi-time frame

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

    Issue calling custom indicator, multi-time frame

    Hello folks! First post. I'm new to NT and have low/moderate coding experience. I've been able to search answers to most roadblocks so far, but am very stuck with one particular issue. Help please?

    I customized a version of the ATR, called PercentATR, and it has four different plots. It runs successfully on standard candlestick charts.

    I want to pass the PercentATR.P_per_atr_avg plot/values to the second indicator called RenkoAnalysis, which has a renko bars array as its primary data series. I've added the candlestick series and was able to successfully read a standard ATR indicator from the candlestick BarsArray[1], so it seems like some aspect of my custom PercentATR indicator is not correct.

    Hope this makes sense, and thank you in advance for any guidance. Both indicators are attached.
    Attached Files

    #2
    Hello ianmackenzi,

    I'm not clear on what the issue is you running into..
    Are you getting an error message?
    Are you adding an indicator to a chart and the values are different than the values printed from a host indicator calling the same hosted indicator on the same chart?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response, I see I left out a lot of useful information.

      I created the PercentATR indicator using the wizard, it is just a copy of ATR but divided by the close price to achieve a percentage value, and then an average taken from that value. When applied directly to a candlestick chart (60 min) both the percent ATR and the average of it work as expected and give correct values.

      The second indicator RenkoAnalysis runs on renko bars, but I've added the 60 minute candlestick data series within it as a second data series. Basically, it will be a summary of multiple indicators, all of which will be on the renko bar series except the PercentATR average which I need from the candlestick data series[1]. Add the second data series was successful, and in OnBarUpdate==1 I can call standard indicators and get values in RenkoAnalysis that match the values if I ran that same indicator directly on the candlestick chart.

      But, when I try to call the PercentATR indicator and its average into RenkoAnalysis, I get the following errors:

      PHP Code:
      Indicator 'PercentATR': Error on calling 'OnBarUpdate' method on bar 20: Object reference not set to an instance of an object.
      0
      Indicator 'RenkoAnalysis': Error on calling 'OnBarUpdate' method on bar 22: 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. 
      
      The default barsrequiredtoplot is 20, but I don't understand why the factory indicators are running but PercentATR is not.

      In terms of multi-series indicators, one concept I don't understand is whether the barsarray[1] calls/calculations should be done in if barsarray==0 or if barsarray==1. It's only important to know what the value is during the primary series bar update, so that makes me think '0', which I believe also provides continuous data points without the gaps that would happen if done in barsarray 1.

      Many thanks!

      Comment


        #4
        Hello ianmackenzi,

        Ok, you are getting an error message.

        You have multiple series, are you checking that CurrentBar is greater than any bars Ago index used for every series?

        int barsBack = 20;

        if (CurrentBars[0] < barsBack || CurrentBars[1] < barsBack)
        return;

        Print(Closes[1][barsBack]);

        Below is a link to a forum post on index errors.
        Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea. Yes, I'm checking BarsRequiredToPlot both in the PercentATR indicator and in RenkoAnalysis indicator that calls it. Here is a screenshot of the Renko Analysis code, the full updated/simplified code is attached.

          Click image for larger version

Name:	RenkoAnalysisv2b screenshot.jpg
Views:	323
Size:	673.5 KB
ID:	1200789

          Print statements of Closes[1][0] or of 'factory' ATR[0] for BarsArray1 in OnBarUpdate==1 are 100% successful, and exactly match values generated on candlestick charts with the same settings and indicator.

          No matter what I do, trying to print/access values from PercentATRv3 returns the same error, and I do not understand what the issue is in it. Applying PercentATRv3 directly to a candlestick chart works perfectly also. The error is the same that I posted before, here are the two lines again:

          Indicator 'RenkoAnalysisv2': Error on calling 'OnBarUpdate' method on bar 20: Object reference not set to an instance of an object.
          Indicator 'RenkoAnalysisv2': Error on calling 'OnBarUpdate' method on bar 22: 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.

          I read through the link you posted, and cannot see how any of the barsago issues described there might be occurring in either file. If I update BarsRequiredToPlot in RenkoAnalysis to a higher value like 50, it simply gives the same two errors with 'on bar 50' and 'on bar 52' instead.

          Does it matter what bar type RenkoAnalysis runs on? It is running on WickedRenkoNT bars that I downloaded if that matters. Any indicators I wish to call/access from OnBarUpdate==0 have worked fine.

          Can you or someone on the team please look at the two files? I have spent many nights trying to solve this single issue and am really frustrated.

          Thank you, Ian

          [ATTACH]n1200788[/ATTACH]

          Comment


            #6
            Hello ianmackenzi,

            Are you checking that CurrentBar is greater than any bars Ago index used for every series in the indicators?

            For example, if you comment those calls to the indicator out and call SMA(BarsArray[1], 14), do you get an error? Or does the error only happen when calling one of you custom indicators that has an index error in it?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I thought this was clearly shown/explained in the last post and screenshot, but I'll point it out again. In the third purple box in the screenshot of the RenkoAnalysis indicator, there are calls to Closes[1][0], testcandleATR[0], and then my indicator PercentATRv3. If I comment out PercentATRv3, calls to testcandleATR are totally successful, even with a higher period setting than I used in PercentATRv3. So, the error only happens when calling my customized version of the ATR. It does not happen with other 'factory' indicators. Please look at the screenshot, or the attached files, or both.

              Is there a way programmatically in RenkoAnalysis to check that CurrentBars is greater than any barsago index used in every indicator called? I'm using periods 14 or less, so the default BarsRequiredToPlot of 20 should be fine, right? Doesn't the fact that PercentATRv3 runs successfully on its own on a candlestick chart using the same default BarsRequiredToPlot (default 20) also confirm that is not the issue?
              Last edited by ianmackenzi; 05-11-2022, 09:46 AM.

              Comment


                #8
                Hello ianmackenzi,

                You may have posted the wrong screenshot. The screenshot in post #5 is of the RenkoAnalysisv2b indicator, the host, and doesn't show any code of the hosted PercentATRv3 code that I am referring to.

                So no, it is not clearly shown at all in your previous post. All we can see is that some indicators are being called. We have no idea why the hosted script is returning a null (which is likely due to hitting an error in the hosted code).

                What specifically are you pointing out in the hosted indicator?

                Try commenting out all code in State.DataLoaded and OnBarUpdate in the hosted script, compile, and reload the host script. Does the error still occur?
                Last edited by NinjaTrader_ChelseaB; 05-11-2022, 09:59 AM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  That was the screenshot I intended to post, to show that regular indicators called from the 'host' indicator run normally but that the hosted custom indicator is the only one experiencing the error.

                  When I comment out all code in State.DataLoaded and OnBarUpdate in the hosted PercentATRv3, the error code disappears when running RenkoAnalysis (host) and lines of zeroes appear in the output window like this:

                  0
                  0
                  0....

                  That confirms your belief that the issue is in the hosted custom indicator PercentATRv3. Now how can we identify what the error actually is? Are screenshots helpful? I expected that looking in the actual code of both indicators would give you the clearest fullest picture, but maybe that is not how you work.

                  I'm still confused that PercentATRv3 runs normally on its own on a chart, but fails when hosted in another indicator. Here is a screenshot of the PercentATRv3 code, exactly as it is commented out right now per your instructions.

                  Click image for larger version

Name:	PercentATRv3 screenshot.jpg
Views:	321
Size:	536.1 KB
ID:	1200900

                  Comment


                    #10
                    Hello ianmackenzi,

                    While I am not able to debug the code on your behalf, I can guide you reducing and debugging so that you are able to discover a solution.

                    However, posting on the forums is nice because the community is able to and does sometimes do debugging and coding work as a convenience to other community members.
                    Further, if you find a solution, posting this publicly on the forum becomes a great resource for anyone following the same path.

                    Yes, if your custom indicator is the only indicator causing the error, then its an issue with the code in the indicator. We would need to see the output from the debugging in the script causing the error. We would need to see the index checking conditions in the script causing the error.

                    Below is a link to a forum post that demonstrates using Print() to understand behavior.
                    https://ninjatrader.com/support/foru...121#post791121

                    If you have commented out all logic and the error continues, it's an issue with a property.
                    If you have commented out all logic and the error stops, it's an issue with the logic.

                    Uncommenting the code until the error returns, shows the last uncommented line as causing the error.
                    Or adding prints above each branching command (if statements), assignment, and method call, will show the line with the error by identifying the last print to appear before the error occurs.


                    You mentioned that you are checking that CurrentBars is greater than BarsRequiredToPlot, which is greater than any periods used.

                    What is the specific line where the error is occuring?
                    Last edited by NinjaTrader_ChelseaB; 05-11-2022, 12:07 PM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I understand drawing the line at debugging code for users and will rely on screenshots to show code and results. I've used print commands for debugging already, but appreciate you making the distinction between logic and properties.

                      Can you please explain more what you mean by this?
                      Yes, if your custom indicator is the only indicator causing the error, then its an issue with the code in the indicator. We would need to see the output from the debugging in the script causing the error. We would need to see the index checking conditions in the script causing the error.
                      In this case, the hosted custom indicator that is causing the issue (PercentATRv3) does not show any errors when compiling, and operates normally when applied to a chart. The errors only occur when it is hosted/called by RenkoAnalysisv2b, in which it is asked to run on a secondary data series. No errors are shown when compiling RenkoAnalysis, only when it runs.

                      This appears to have been cut off:
                      You mentioned that you are checking that CurrentBars

                      Comment


                        #12
                        Hello ianmackenzi,

                        I am meaning, that showing a screenshot of the code for RenkoAnalysisv2b is not helpful if the error is occurring in PercentATRv3.

                        But that said, really we should be focusing on specific lines instead of sharing a screenshot of code at all. If you want to share the full script code, export it (preferably export a simplified version) and attach the export as you did in your first post. Other wise just copy and paste the line of code you have inquiry about.

                        If you are unsure what is causing the error, then reduce first and determine what code when uncommented causes the error.

                        Runtime errors are not compile errors.

                        DateTime myDateTime = null;
                        Print(myDateTime);

                        The above code will compile because there are no syntax errors. However, it will absolutely will have a run time error and cannot be run because this will cause an Object reference not set to instance of an object error as the value is null.
                        Compile errors and run time errors are not the same. Compile errors means the syntax is incorrect and will show in the NinjaScript Editor. Run time errors can be compiled, but will have an error when running the script and will appear in the Log tab of the Control Center.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Success. I progressively uncommented items in PercentATR while reloading RenkoAnalysis which hosts it, and it turns out it was the Draw.TextFixed in lines 90/91 of PercentATR that were the issue. That was surprising because the error messages didn't hint at drawing issues at all, but it does make sense that the hosted indicator doesn't know where to draw.

                          This is the table that PercentATR draws when run directly on a chart:
                          Click image for larger version

Name:	Screenshot_3.jpg
Views:	375
Size:	136.5 KB
ID:	1200984

                          When run as a hosted indicator it gave two errors:
                          Indicator {host indicator}: Error on calling 'OnBarUpdate' method on bar 20: Object reference not set to an instance of an object.
                          Indicator {host indicator}: Error on calling 'OnBarUpdate' method on bar 22: 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.

                          Thank you for all of the help, I've learned better debugging methods and terminology.

                          Comment


                            #14
                            I have the exactly same issue, but I would like to keep showing the content of the "Draw.TextFixed" on the chart.

                            Click image for larger version

Name:	image.png
Views:	213
Size:	5.3 KB
ID:	1257251
                            and is correct, inside of the Draw.TextFixed command the "owner" is "this" (Which is incorrect)
                            but it does make sense that the hosted indicator doesn't know where to draw
                            .

                            Now, the question is:
                            How shall be the syntax of the NinjaScriptBase in order to achieve to Draw the text of the hosted indicator in the chart where the Parent indicator is running??
                            Click image for larger version

Name:	image.png
Views:	202
Size:	11.0 KB
ID:	1257252

                            Thank you very Much!!!
                            FVJ​

                            Comment


                              #15
                              Hello efeuvejota01,

                              Your indicator would use the keyword this when drawing an object, for example Draw.TextFixed(this
                              If you want the objects to remain on the chart you can use DrawOnPricePanel = true

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              563 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              329 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              547 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              547 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X