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

How to optimize with second data series is added

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

    How to optimize with second data series is added

    I have a strategy that dynamically adds the volumetrics for whatever my primary instrument is. It works, but when i try to optimize non-data series values, i get this error:
    Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

    My second data series is added here, and the values themselves aren't being optimized but i expect this is a limitation of the strategy analyzer?

    Code:
    else if (State == State.Configure)
    {
    string instrumentNameLabel = "";
    ClearOutputWindow();
    int periodValue = BarsPeriod.Value;
    
    if (Instrument.MasterInstrument.InstrumentType == InstrumentType.Future) // we have to add expiry to futures
    {
    instrumentNameLabel = Instrument.MasterInstrument.Name+" "+Instrument.MasterInstrument.GetNextExpiry(DateTi me.Now).ToString("MM-yy");
    
    }
    else if (Instrument.MasterInstrument.InstrumentType == InstrumentType.Forex)
    {
    instrumentNameLabel = Instrument.MasterInstrument.Name; // no expiry on forex
    }
    if(instrumentNameLabel == "")
    {
    Print("BigError");
    return;
    }
    AddVolumetric(instrumentNameLabel, BarsPeriodType.Minute, periodValue, VolumetricDeltaType.BidAsk, 1);


    #2
    Hello Skifree,

    Thank you for your post.

    To narrow down which particular line of code in OnStateChange is causing the error, please add prints every couple of lines for debugging purposes. These could be as simple as printing the number "1" then "2" etc. Review the NinjaScript Output window for which prints appear before the error is thrown. The last print to show in the output prior to the error means that the offending line of code is below that print. This should help to narrow the error down further and then you can even try to print any values you are accessing in OnStateChange. To prevent the error, you may simply need to add a null reference check:


    For more information on using prints to debug:


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      So i know how to debug and all, but this only happens during optimizer, not during backtest. Does optimizer run strategies differently than backtest?

      Comment


        #4
        Hello Skifree,

        Thank you for your reply.

        I am not aware of anything specific to look out for between backtests vs. optimizations other than the following note in the help guide that doesn't seem to apply to this scenario:
        With that said, I am curious about your use of "instrumentNameLabel" for the instrumentName in AddVolumetric(). All arguments supplied to AddVolumetric() should be hardcoded and not dependent on any run-time variables because they may not be obtained reliably in State.Configure. Trying to add a data series dynamically is unsupported and may result in errors. As a test, please try hard-coding a value for instrumentName such as "ES 03-24" in your call to AddVolumetric() and see if it results in errors or if the optimization is successful.

        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Emily,

          It backtests fine but according to this debug:
          Code:
          protected override void OnStateChange()
          {
          Print("OnStateChange Begin: "+State);​
          I get very different results.
          Backtest is fine:
          OnStateChange End: Configure
          OnStateChange Begin: DataLoaded
          OnStateChange End: DataLoaded
          OnStateChange Begin: Historical
          OnStateChange End: Historical
          OnStateChange Begin: SetDefaults
          OnStateChange End: SetDefaults
          OnStateChange Begin: Terminated
          OnStateChange End: Terminated
          OnStateChange Begin: Terminated
          Lowest PNL = -150
          Avergae Total Buying Volume 0
          OnStateChange End: Terminated
          OnStateChange Begin: Terminated
          OnStateChange End: Terminated
          OnStateChange Begin: SetDefaults
          OnStateChange End: SetDefaults
          OnStateChange Begin: SetDefaults
          OnStateChange End: SetDefaults
          OnStateChange Begin: Terminated
          OnStateChange End: Terminated

          Optimizer for single value in OnBarUpdatecode:
          Strategy 'scalpClassV3': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
          OnStateChange Begin: Terminated
          OnStateChange End: Terminated
          Strategy 'scalpClassV3': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
          OnStateChange Begin: Terminated
          OnStateChange End: Terminated
          OnStateChange Begin: SetDefaults
          OnStateChange End: SetDefaults
          OnStateChange Begin: Terminated
          OnStateChange End: Terminated
          OnStateChange Begin: Terminated
          OnStateChange End: Terminated
          OnStateChange Begin: SetDefaults
          OnStateChange End: SetDefaults
          OnStateChange Begin: SetDefaults
          OnStateChange End: SetDefaults
          OnStateChange Begin: Terminated
          OnStateChange End: Terminated

          Notice that in the optimizer, configure isn't being called and that's where data series 2 is. I also tried hard coding but there was no difference​​

          Comment


            #6
            Hello Skifree,

            Thank you for your patience.

            I suspect that you are receiving this error because you are trying to access Instrument properties before State.DataLoaded. As noted in the help guide, "Warning: The properties in this class should NOT be accessed within the OnStateChange() method before the State has reached State.DataLoaded​"


            With that said, AddDataSeries() (including AddVolumetric() in your case) should only be called during State.Configure:



            You must use hard-coded values in your call to AddVolumetric(). I mentioned this previously, and here is the specific note from the help guide as well:
            "Arguments supplied to AddVolumetric() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner.​"

            You mentioned you tried hard coding but there was no difference; what changes did you make for that test? Did you still receive the "Object reference not set to an instance of an object." error and if so, what line of code caused that error?

            Please let us know if we may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              I think this went away when I hard coded the second data series. It seems silly to not be able to have a dynamic value in initialization of the code, because otherwise I you need many derived classes with just an onStateChange override,as you can't override just the configure portion of the code?

              Comment


                #8
                Originally posted by Skifree View Post
                I think this went away when I hard coded the second data series. It seems silly to not be able to have a dynamic value in initialization of the code, because otherwise I you need many derived classes with just an onStateChange override,as you can't override just the configure portion of the code?
                Hello Skifree,

                Thank you for your reply.

                There is an existing feature request to allow and support dynamic handling/loading of data series. I will gladly add your vote. The internal tracking number for this feature request is SFT-882. Please reference this internal tracking number when contacting Platform Support if you ever have questions regarding this feature request.

                When a feature request is implemented, you'll find a description of the new feature in the release notes:Thank you for using NinjaTrader.​
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  That would be really helpful, is there a public roadmap or anything to follow?

                  Comment


                    #10
                    Helli Skifree,

                    Thank you for your reply.

                    Although feature request tracking is not public-facing, you can certainly refer to the tracking ID SFT-882 any time you contact support regarding this request. The only public-facing page to follow would be the release notes in the help guide:


                    When a feature request is added to a new version of NinjaTrader, the tracking ID is shown in the "Issue #" column so you could even Ctrl + F to find "882" and see if it has been added to any new release notes when a new version is released.

                    Thank you for using NinjaTrader.
                    Emily C.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by AaronKoRn, Yesterday, 09:49 PM
                    0 responses
                    11 views
                    0 likes
                    Last Post AaronKoRn  
                    Started by carnitron, Yesterday, 08:42 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post carnitron  
                    Started by strategist007, Yesterday, 07:51 PM
                    0 responses
                    12 views
                    0 likes
                    Last Post strategist007  
                    Started by StockTrader88, 03-06-2021, 08:58 AM
                    44 responses
                    3,982 views
                    3 likes
                    Last Post jhudas88  
                    Started by rbeckmann05, Yesterday, 06:48 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post rbeckmann05  
                    Working...
                    X