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

Reading Strategy's Parameters when running the optimizer

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

    Reading Strategy's Parameters when running the optimizer

    I have working on a strategy that runs without any issue when the strategy I run through a chart.
    Now I would like to begin using the Strategy Analyzer, using Backtest Type = Optimization.

    When running the Optimizer, I am getting the following error:
    Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.


    I have been able to identify the statement which is creating the error. Inside the (State == State.Configure) statement I have some code which validates the parameters that a user enters which can alter the strategy's behavior. One of the parameters that I want to check is the "Break at EOD" parameter which resides in the chart's Data Series. When running the strategy in the optimizer, the Break at EOD parameter is part of the strategy's parameters. As such, the line if (!ChartBars.Properties.IsStableSession) creates the error since I don't CharBars object.


    Code:
    Print ("Here Before");
    
                    if (Category == Category.Optimize)
    
                    {
    
                        Print("Strategy is running an optimization ");
    
    
    
    
                    }
    
                    if (!ChartBars.Properties.IsStableSession)
    
                    {
    
                        Log("Make sure \"Break at EOD\" Data Series parameter  is enabled/checked.  Then re-enable the strategy again.", LogLevel.Alert);
    
                        return;    
    
                    }
    
                    Print ("Here after");

    I am trying to read Break at EOD that is found inside the strategy's parameters when running the Optimizer. How do I get to (read) Strategy's parameters when running in the Optimizer ?

    #2
    Hello GARZONJ,

    The Category property is not documented and is not officially supported. Use of this may be unpredictable. Use at your discretion.

    The ChartBars.Properties are available after the State reaches State.DataLoaded, only when the strategy is applied directly to a chart and the ChartControl is not null.
    There will need to be a check for null to prevent the error.

    I'm uncertain if there is a way to get these properties in the Strategy Analyzer (or when the strategy is applied to the Strategies tab of the Control Center) when there is not ChartControl.
    I will research and I will let you know what I find.

    I appreciate your patience.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea,

      For the time being, I am not checking "Beak at EOD" strategy parameter when using the optimizer.

      Now in the State == State.Configure, I also have the following data series as I am using intraday granularity fill base on the code found in this link https://ninjatrader.com/support/foru...ularity?t=6652


      AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Tick, 1, MarketDataType.Bid);
      AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Tick, 1, MarketDataType.Ask);
      AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Tick, 1, MarketDataType.Last);

      In order to obtain the ASK, BID or LAST data series I am force to use an AddDataSeries syntax which requires instrument. When using the above statements, I am getting again the Error on calling 'OnStateChange' method: Object reference not set to an instance of an object error message.

      Base on the documentation, it is recommended to hardcode the instrument name. This is what I now have:

      AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Bid);
      AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Ask);
      AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Last);

      This eliminates the error.

      Based on your previous answer, I am assuming that we don't know how to access the Startegy's Instrument when using the Optimizer. How should I get the Instrument name to use it in the AddDataSeries statement? Should I create another property just so that I can capture the Instrument's Name? Please advice.

      Thanks
      Last edited by GARZONJ; 12-11-2019, 03:45 PM.

      Comment


        #4
        Hello GARZONJ,

        Unfortunately, NinjaTrader does not support using the dynamic property Instrument.FullName or any dynamic input property set by the user or any other dynamic property for the instrument symbol or any other parameter in the AddDataSeries() method call.

        From the help guide:
        "Arguments supplied to AddDataSeries() 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."



        It is acceptable to use null for the instrument symbol.

        From the help guide:
        "4. For the instrument name parameter null could be passed in, resulting in the primary data series instrument being used."
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello GARZONJ,

          So I wasn't able to find a way to get ChartBars properties without an existing ChartControl.

          However, the break at end of day can be detected from the Bars object which exists without a ChartControl with the IsResetOnNewTradingDay property.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by GARZONJ View Post
            Chelsea,

            For the time being, I am not checking "Beak at EOD" strategy parameter when using the optimizer.

            Now in the State == State.Configure, I also have the following data series as I am using intraday granularity fill base on the code found in this link https://ninjatrader.com/support/foru...ularity?t=6652


            AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Tick, 1, MarketDataType.Bid);
            AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Tick, 1, MarketDataType.Ask);
            AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Tick, 1, MarketDataType.Last);

            In order to obtain the ASK, BID or LAST data series I am force to use an AddDataSeries syntax which requires instrument. When using the above statements, I am getting again the Error on calling 'OnStateChange' method: Object reference not set to an instance of an object error message.

            Base on the documentation, it is recommended to hardcode the instrument name. This is what I now have:

            AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Bid);
            AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Ask);
            AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Last);

            This eliminates the error.

            Based on your previous answer, I am assuming that we don't know how to access the Startegy's Instrument when using the Optimizer. How should I get the Instrument name to use it in the AddDataSeries statement? Should I create another property just so that I can capture the Instrument's Name? Please advice.

            Thanks
            Modifying the data deries to include the Instrument name fixes the "Error on calling 'OnStateChange' method: Object reference not set to an instance of an object." error perfectly, now you are just stuck with using that specific instrument to run on Strategy Analyzer - I have been struggling with this for MONTHS and finally found this post - so I am grateful GARZONJ:

            AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Bid);
            AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Ask);
            AddDataSeries("ES 12-19", Data.BarsPeriodType.Tick, 1, MarketDataType.Last);

            Now it will be wonderful if the "brains" at ninjatrader can come up with a propper fix for this, especially if I want to send my subscribers Agiled code of the strategy and now have to make a seperate strategy for every instrument to include the Instrument name in the AddDataSeries.......and then send them new versions when the rollover happens............bet there are hundreds of individuals stuck in the same position with this!!!!

            Comment


              #7
              Hello TrendFollowingCapital, thanks for posting.

              We have an open feature request to track users that would like to allow Dynamic handling / loading of additional Data Series ID SFT-882

              I will add a vote to this for you.

              Best regards,
              -ChrisL
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Hi! Was an update ever made to solve or get around this! Extremely important to run analyzer and optimizer without being forced to hard code instrument name for multi data series.

                Where can we vote for this? Many users requesting it and the idea of using static hard coded value for each strategy is not realistic for optimization.

                Thanks,

                Chad
                chadnash
                NinjaTrader Ecosystem Vendor - Nash Technologies

                Comment


                  #9
                  Hello Chad, thanks for your post.

                  We accept votes for feature requests through the forum, so I will add a vote to this feature for you. This is still an open feature request.

                  Best regards.
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chris,

                    Just a heads up / question... I noticed that on Dec 4th the link referenced above was updated with notes about updated help info:
                    You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


                    So... I checked it out and noticed a tip to use "null"... In the help section of AddDataSeries.

                    "4. For the instrument name parameter null could be passed in, resulting in the primary data series instrument being used."

                    So I tested this from one of the samples and its working...

                    This fails for multi time series optimization:

                    AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Minute, 5); // BarsArray[1]

                    // Add a 15 minute Bars object to the strategy
                    AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Minute, 15); // BarsArray[2]

                    But this works...
                    // Add a 5 minute Bars object to the strategy
                    AddDataSeries(null, Data.BarsPeriodType.Minute, 5); // BarsArray[1]

                    // Add a 15 minute Bars object to the strategy
                    AddDataSeries(null, Data.BarsPeriodType.Minute, 15); // BarsArray[2]

                    So... it does then give me results and not the error for optimization and backtest.

                    Maybe this helps someone else... Or for users wanting to use the default this works?

                    -Chad
                    chadnash
                    NinjaTrader Ecosystem Vendor - Nash Technologies

                    Comment


                      #11
                      Jumping in here a bit late, but maybe this information can help others save time in the future..

                      The problem at the beginning of this this thread (the original problem posted) is that there is a null reference exception being thrown when you call any kind of chart functionality like ChartControl from an indicator running in a Strategy. This is because these charting objects only get instantiated when the indicator is instantiated from a chart. When you instantiate the indicator from a Strategy these references do not get set with a pointer. Therefore, they are null references. This makes total sense to me that Ninjatrader would operate this way.

                      The solution is very simple. Put a try-catch (NullReferenceException) block around any portions of code where there is a reference a class for charting functionality, like ChartControl. This allows it to work properly when called from a strategy and when called from a chart.

                      The root cause of this problem, and why countless thousands of hours have probably been spent chasing this issue, is actually in Visual Studio (or NinjaScript Editor). Most professional IDE will flag unhandled exceptions as you write the code. Then you can make sure they get handled correctly with a try-catch (cleanest in my view). Visual Studio does not flag these pre-build or post-build and you are left to your own devices to figure it out.

                      Hope this helps save someone tons of time in the future.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by inanazsocial, Today, 01:15 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post inanazsocial  
                      Started by trilliantrader, 04-18-2024, 08:16 AM
                      5 responses
                      22 views
                      0 likes
                      Last Post trilliantrader  
                      Started by Davidtowleii, Today, 12:15 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post Davidtowleii  
                      Started by guillembm, Yesterday, 11:25 AM
                      2 responses
                      9 views
                      0 likes
                      Last Post guillembm  
                      Started by junkone, 04-21-2024, 07:17 AM
                      9 responses
                      70 views
                      0 likes
                      Last Post jeronymite  
                      Working...
                      X