Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Question about using the optimizer?

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

    Question about using the optimizer?

    Does anything need to be done with the code to allow optimization on various user inputs to be done?

    I have a strat, I have run through the backtester, and works fine. Changing each variable also works fine. But when I try to use the optimizer, I get the error of:
    Code:
    Strategy 'SessionMAstrat': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    So before I dig deeper into problems that may be specific to MY script, I am wondering if there is something obvious I'm missing?

    I think I may have used the optimizer a long time ago, and I dont think I had to do anything special?

    #2
    Hello forrestang,

    Thanks for your post.

    The following are able to be optimized over when using the NinjaScriptProperty attribute.
    • Enum
    • Int
    • Bool
    • Double
    See this help guide page for more information about the NinjaScriptProperty attribute: https://ninjatrader.com/support/help...yattribute.htm

    In regard to the error message, it is advising that something you are accessing has not been created at the time you are accessing it. To determine what the source of the error is you will need to debug your strategy. This is typically done with print statements added to your code to send a print output to the New > Ninjascript Output window.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      So I found the issue that is making my code break IN THE OPTIMIZER.

      One part of my code is position sizing, to set position size for forex instruments. To get position size, I of course need various quotes depending on the pair. To do this, I add in the State.Configure block the following:
      Code:
      else if (State == State.Configure)
      {
         #region CODE Load Instruments
         if (Instrument.MasterInstrument.InstrumentType == InstrumentType.Forex)
         {
            string pair1 = Instrument.MasterInstrument.Name.ToString( ).Substring(0, 3);
            string pair2 = Instrument.MasterInstrument.Name.ToString( ).Substring(3, 3);
      
             //Assuming USD is NEITHER pair1 or pair2
            if(pair1 != "USD" && pair2 != "USD" )
            {
               if(pair2 == "AUD" ) AddDataSeries("AUDUSD"); //
               if(pair2 == "CAD" ) AddDataSeries("USDCAD"); //
               if(pair2 == "CHF" ) AddDataSeries("USDCHF"); //
               if(pair2 == "EUR" ) AddDataSeries("EURUSD"); //
               if(pair2 == "GBP" ) AddDataSeries("GBPUSD"); //
               if(pair2 == "JPY" ) AddDataSeries("USDJPY"); //
               if(pair2 == "NZD" ) AddDataSeries("NZDUSD"); //
            }
         }
      #endregion
      }
      The above is simply used to add up to one additional data series depending on what the pair on chart is. THis is later used to calculate position size. This works fine for on-chart indicators, in strategies, and in the backtester with no problems or errors.

      It just causes a problem when I try to run it in a strategy within the optimizer. Any ideas on what the problem may be that is specific to the way the optimizer works?

      Comment


        #4
        Hello forrestang,

        Thanks for your note.

        When it comes to Optimizing the strategy in the Strategy Analyzer, AddDataSeries() cannot be called dynamically, and use conditions would be considered dynamic. See the 'Warning' section of the AddDataSeries help guide page linked below.

        For a strategy to be optimized in the Strategy Analyzer, AddDataSeries() cannot be used in a condition in the strategy.

        AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm

        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Originally posted by NinjaTrader_BrandonH View Post
          When it comes to Optimizing the strategy in the Strategy Analyzer, AddDataSeries() cannot be called dynamically, and use conditions would be considered dynamic. See the 'Warning' section of the AddDataSeries help guide page linked below.
          I was aware of this, but I didn't think it would rear its head this way(being specific to the optimizer and not strategies or indicators)?

          But it is in fact the issue, as if I just flat out add the series(w/o conditional logic), it will allow optimization.

          Not only that, ANY type of conditional logic in the State.Configure seems to be an issue, only for the optimization.

          Thanks.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          62 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          134 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X