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

Object Ref error using indicator with embedded indicators in a strategy

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

    Object Ref error using indicator with embedded indicators in a strategy

    Hello Strategy Development Forum,

    I've got an indicator that embeds other indicators for internal use. I'm using that indicator in a strategy assigned to a local variable. The indicator assigns the embedded indicators to local variables in the OnStartUp event.

    The indicator works perfectly on a chart. It even works great on the Strategy Analyzer chart when I assign it to the Strategy local variable in the Initialize event.

    However, when the strategy uses it to make trade decisions the indicator throws an Object reference not set to an instance of an object error when it attempts to access the indicators embedded in the indicator.

    I've tried using two instances of the indicator (one for the graph and one for trade decisions) and tried putting the trade decision instance in both Initialize and OnStartUp but it still throws causes the indicator to throw the Obj ref error.

    Any ideas how to fix this?

    Thanks!
    daqu40
    NinjaTrader Ecosystem Vendor - QTradez

    #2
    Hello daqu40, and thank you for your question.

    It sounds like you have a variable that is not quite set up before it is ready to be used. It is difficult to track down this kind of thing without reviewing the source code. If you would like, if you could provide a code sample to platformsupport[at]ninjatrader[dot]com , referencing the unique ID 1539803 in the body of your e-mail, I may be able to take a deeper look. Without a complete picture, I can not determine exactly is happening.

    Generally, though, I believe the strategy I would follow is this :

    • Create a new strategy, which only uses the embedded indicator directly to make trade decisions
    • Once this works, create the outer indicator in this new strategy, and attach a reference to the embedded indicator to this strategy from the outside (e.g. through a public member)
    • If your outer indicator is intended to create an instance of the inner indicator itself internally :
      • Create a new indicator
      • Create an instance of your inner indicator from within this indicator
      • Make sure the new indicator can read the embedded indicator's values
      • Copy this working code to your outer indicator's code
    • Copy your working code from the new strategy back into your production strategy

    If you can follow the above procedure, and let us know which if any step stops things up, this information will help us as well. If your strategy and indicator code are proprietary, the above procedure should give you slimmed down copies that can more freely be shared.


    Please let us know if there is any other way we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Jessica,

      I've attempted the first step (only use the indicator directly in the strategy to make a decision) and can't make it work. It just continues to throw the error.

      The error clearly is being thrown by the indicator itself. And this only happens when being used the by the strategy. When the indicator is applied to a chart it works just fine.

      I'll send you the source code for the indicator and strategy along with settings templates so you can take a look yourself.

      Thanks,
      daqu40
      NinjaTrader Ecosystem Vendor - QTradez

      Comment


        #4
        Originally posted by daqu40 View Post
        Thanks Jessica,

        I've attempted the first step (only use the indicator directly in the strategy to make a decision) and can't make it work. It just continues to throw the error.

        The error clearly is being thrown by the indicator itself. And this only happens when being used the by the strategy. When the indicator is applied to a chart it works just fine.

        I'll send you the source code for the indicator and strategy along with settings templates so you can take a look yourself.

        Thanks,
        It sounds then very much as if you are making a call to ChartControl, without first that it is not null.

        Comment


          #5
          Thanks for the input Osikani,

          Not sure I'm following you on this. The attempt I made to use the indicator in question was only for its data. I'm not using it on the chart the strategy is running on.

          And when I do use it on the chart it paints perfectly. It's only when trying to access the indicator's data for the strategy to make a trade decision that the indicator throws the Object reference error.
          daqu40
          NinjaTrader Ecosystem Vendor - QTradez

          Comment


            #6
            Originally posted by daqu40 View Post
            Thanks for the input Osikani,

            Not sure I'm following you on this. The attempt I made to use the indicator in question was only for its data. I'm not using it on the chart the strategy is running on.

            And when I do use it on the chart it paints perfectly. It's only when trying to access the indicator's data for the strategy to make a trade decision that the indicator throws the Object reference error.
            That is exactly what I mean. Check your classes for an unconditional call to the ChartControl in any of the classes involved.

            Comment


              #7
              Hello daqu40,

              I am glad we were further able to isolate what is happening. What you will need to do now, is to enter your indicator's source code, and look for any variables with the same name as anything you see in the Indicator's property window, that appears anywhere but on the left side of an equals sign, in either your OnStateChange or OnBarUpdate methods. Try using the Print() method to print the values of these elements to an output window. You can bring up an output window from the NT8 control center by visiting New -> NinjaScript Output . You should be able to quickly isolate the value that is being set by the chart properties screen, that is not being set properly by your indicator's constructor. You can then give this value a default in State == State.SetDefaults in OnStateChange to allow your indicator to function again when created programmatically.
              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                Oh now I understand. I'm asking something that needs to work with the ChartControl to just work without a ChartControl.

                Looking at the code I have an overridden Plot method, but it does have a condition to check before proceeding and the whole method is error trapped with a try/catch:

                if (idx - Displacement < 0 || idx - Displacement >= BarsArray[0].Count || (!ChartControl.ShowBarsRequired && idx - Displacement < BarsRequired))
                continue;


                Also, the code that is throwing the error isn't in the overridden Plot method. The error is thrown when the indicator tries to access a local instance of the RegressionChannel indicator that I assigned to a local private variable at my indicator's level. This indicator is also not plotted in my overridden Plot method.

                Thanks again for the ideas.
                daqu40
                NinjaTrader Ecosystem Vendor - QTradez

                Comment


                  #9
                  Will work on that Jessica. By the way I'm working this in NT7.

                  Also I sent you all the necessary source code last night.
                  daqu40
                  NinjaTrader Ecosystem Vendor - QTradez

                  Comment


                    #10
                    Hello daqu40,

                    Could I ask you to re-send the code sample you sent to us last night? Please include this number in the body of your e-mail, so that I can be sure to receive it : 1539860 . You can send it to platformsupport[at]ninjatrader[dot]com , replacing [at] and [dot] appropriately.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      will resend right away Jessica
                      daqu40
                      NinjaTrader Ecosystem Vendor - QTradez

                      Comment


                        #12
                        I've done this and do have examples:

                        RCPeriod is a parameter in the indicator's parameters. It is used as follows:

                        IndRCS = RegressionChannelSlope(RCPeriod, 3);
                        IndaDQRC = anaDQRegressionChannel(RCPeriod, 3);

                        The above lines assign the indicators RegressionChannelSlope and anaDQRegressionChannel to variables in the indicator. Both of these statements appear in the onStartUp event.

                        Each of the indicators I use in my indicator are assigned in this manner. There are three that get parameters from the user and two others that have parameters I've set the value for and not given the user access to those parameters.

                        Using Print to see values of user accessible parameters I get:

                        Params at Initialize event: RCPeriod 13, qSParams 19

                        If I run the strategy that contains this indicator I get:

                        Strategy Initialization time is 00:00:00
                        Strategy time to get to StartUp is 00:00:01.8460186
                        Strategy Startup time is 00:00:03.1770283 BackTestMode BackTest
                        From first bar of OnBarUpdate - Account is Backtest, value is 0
                        6/26/2016 8:07:39 PM WaveRider PivotSignalFilter error: CL 08-16 Object reference not set to an instance of an object.
                        6/26/2016 8:27:37 PM WaveRider PivotSignalFilter error: CL 08-16 Object reference not set to an instance of an object.

                        a bunch more lines of errors....


                        No trace my of indicator's initialization and the parameters at that time.

                        What do we try next?
                        daqu40
                        NinjaTrader Ecosystem Vendor - QTradez

                        Comment


                          #13
                          Originally posted by daqu40 View Post
                          Oh now I understand. I'm asking something that needs to work with the ChartControl to just work without a ChartControl.

                          Looking at the code I have an overridden Plot method, but it does have a condition to check before proceeding and the whole method is error trapped with a try/catch:

                          if (idx - Displacement < 0 || idx - Displacement >= BarsArray[0].Count || (!ChartControl.ShowBarsRequired && idx - Displacement < BarsRequired))
                          continue;


                          Also, the code that is throwing the error isn't in the overridden Plot method. The error is thrown when the indicator tries to access a local instance of the RegressionChannel indicator that I assigned to a local private variable at my indicator's level. This indicator is also not plotted in my overridden Plot method.

                          Thanks again for the ideas.
                          That error is coming from the PivotSignalFilter class. Look there for the problem. It can be in any of the methods or event handlers, not just a Plot override. If it is only happening when the indicator is not itself primarily on a chart, that is the usual reason. Heck, I once had to make a hasty update to one of the indicators that we sold, for that very reason. We had never tested that indicator in a strategy, so we had overlooked that problem. The indicator had no problem working on a chart, naturally, but threw that error in any strategy.

                          ref: http://ninjatrader.com/support/forum...ad.php?t=76551

                          One other possibility is that you are using static variables which are being accessed too early from elsewhere.

                          Comment


                            #14
                            Thank you, daqu40, we have received your code.

                            Expanding on what koganam said, with the benefit of being able to review your code, I noticed you had a method, which contains a try / catch block which covers the entire method, and which is the only place "PivotSignalFilter" appears in an output string. Therefore, whatever is occurring in your code is definitely happening between lines 605 and 715 .

                            As a debugging step, I would like to ask for you to comment out this try catch block. That is, I would like you to change lines 606 and 607 from

                            Code:
                            [FONT=Courier New]            try
                                        {[/FONT]
                            to

                            Code:
                            [FONT=Courier New]            /* try
                                        { */[/FONT]
                            and lines 713 to 714 (code obfuscation mine) from

                            Code:
                            [FONT=Courier New]}
                            catch (Exception exceptionName) {DoStuff();}[/FONT]
                            to

                            Code:
                            [FONT=Courier New]/* }
                            catch (Exception exceptionName) {DoStuff();} */[/FONT]
                            This will give us more verbose output to work with. Could you run this, and then let us know what additional log / trace output you receive?
                            Jessica P.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Jessica,

                              The lines that throw the errors are 645, 647, 682 and 684.

                              Disabling the try/catch just moves the error up to the OnBarUpdate section that calls the PivotSignalFilter. Output is:

                              2ndWave OnBarUpdate Signal calculation and alerting: CL 08-16 Object reference not set to an instance of an object.

                              There is nothing in the Control Center Log.

                              Those lines that error call my local instances of the indicators RegressionChannelSlope and QSqueeze.

                              In another version of this program, I've put a fully qualified call to these indicators and it doesn't throw the error, but I'm getting spurious results that seem to indicate the indicator is not providing values to the indicator that calls it.

                              What next?
                              Last edited by daqu40; 07-08-2016, 12:00 PM.
                              daqu40
                              NinjaTrader Ecosystem Vendor - QTradez

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kujista, Today, 06:23 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post kujista
                              by kujista
                               
                              Started by traderqz, Yesterday, 04:32 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by f.saeidi, Today, 05:56 AM
                              1 response
                              4 views
                              0 likes
                              Last Post Jltarrau  
                              Started by Jltarrau, Today, 05:57 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post Jltarrau  
                              Started by Stanfillirenfro, Yesterday, 09:19 AM
                              7 responses
                              53 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X