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

Error on running optimizer: Value cannot be null. Parameter name: value

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

    Error on running optimizer: Value cannot be null. Parameter name: value

    My strategy works fine when backtesting. Yet when I run the optimizer, it throws this error:

    Error on running optimizer: Value cannot be null. Parameter name: value

    Code:
            private double risk = 0.0325;
            private int sL = 100; 
            private int tP = 100; 
            private int fileNumber = 1;
    I read a previous thread on this issue. As you can see from my data types, they are all int and double.

    Code:
            [Description("")]
            [GridCategory("Parameters")]
            public int FileNumber
            {
                get { return fileNumber; }
                set { fileNumber = [B]value[/B]; }
            }
    None of my inputs are named value. I checked properties. The only property I have that explicitly uses value is FileNumber.

    The log file only contains one error. It says "Error on running optimizer: Value cannot be null." It's highlighted in green.
    Attached Files

    #2
    Hello TexasNomad,

    I wasn't able to see the issue using that one property. Value could refer to anything that is accepted as input here, and is not restricted to properties where you assign variableName = value.

    You are running against a bid series. Do you have historical bid data available? Do you get the same error when running against last?

    It may relate to how these variables are used later in code rather than the declarations and property usage. If this doesn't cover it can you share a stripped down strategy that we can run on our side to see what you're seeing?
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      Hello TexasNomad,

      I wasn't able to see the issue using that one property. Value could refer to anything that is accepted as input here, and is not restricted to properties where you assign variableName = value.

      You are running against a bid series. Do you have historical bid data available? Do you get the same error when running against last?

      It may relate to how these variables are used later in code rather than the declarations and property usage. If this doesn't cover it can you share a stripped down strategy that we can run on our side to see what you're seeing?
      I do have the historical data available. Like I said, it works fine on the backtester when it runs on the same period on Bid data.

      I'm trying to get the optimizer to do some cycling for me using different sets of random numbers (read from FileNumber). I write information to a csv file, but occasionally the strategy doesn't want to run b/c balances.csv is locked. When I comment out the StreamWriter object balanceWriter, the optimizer runs ok.

      Is the problem with balanceWriter in Initialize() or OnTermination()?

      Here's the stripped down version, only pasting sections that read/write to files.

      Code:
              StreamReader reader;
              StreamWriter balanceWriter;
      
              protected override void Initialize()
              {            
                  string name = "C:\\...\\" + FileNumber.ToString() + ".txt";
                  
                  reader = new StreamReader(name);
                  
                  balanceWriter = File.AppendText("C:\\....\\balances.csv");            
              }
      
              protected override void OnBarUpdate()
              {
                  if( Position.MarketPosition == MarketPosition.Flat && newTradeSetup) 
                  {
                      StringBuilder output = new StringBuilder();             
                      
                      output.Append(FileNumber);
                      output.Append(String.Format(", Balance: {0:c}, Dollar risk: {1:c}, Risk of 1 lot: {2:c}, Base lot size: {3}, Entries Per Direction: {4}",accountCurrentBalance, desiredRisk, riskOfOneLot, LOT_SIZE, EntriesPerDirection));
                      output.Append(String.Format(", Units per entry: {0}", units));
                      Print(output.ToString());
                      balanceWriter.WriteLine(output.ToString());
                  }
              }
      
              protected override void OnTermination()
              {
                  reader.Close();
                  balanceWriter.Close();
              }

      Comment


        #4
        I see. Thanks for the additional information. Unfortunately this is more C# related and I'm not sure if you are able to optimize from a file selection like this. I would remove everything not related to this process and only add logic once you've verified it's working the way you expect.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Is OnTermination() called at the end of each individual run during an optimization or only when the entire optimization ends?

          What about Initialize()? On every run or only at the very beginning of the whole sequence?

          Comment


            #6
            OnTermination is called for every test within an optimization run.

            Initialize() can be called more than once per test. If there's anything critical you need to run only once, can use OnStartUp() for this - It will also be called for every test done within an optimization run.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks. I got everything working.

              Comment


                #8
                Thanks for the follow up, texasnomad. Glad to hear it's sorted.
                Ryan M.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by ghoul, Today, 06:02 PM
                0 responses
                7 views
                0 likes
                Last Post ghoul
                by ghoul
                 
                Started by Barry Milan, Yesterday, 10:35 PM
                6 responses
                18 views
                0 likes
                Last Post Barry Milan  
                Started by DanielSanMartin, Yesterday, 02:37 PM
                2 responses
                13 views
                0 likes
                Last Post DanielSanMartin  
                Started by DJ888, 04-16-2024, 06:09 PM
                4 responses
                13 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by terofs, Today, 04:18 PM
                0 responses
                12 views
                0 likes
                Last Post terofs
                by terofs
                 
                Working...
                X