Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Pass the value of a variable to an optimization fitness

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

    Pass the value of a variable to an optimization fitness

    Hello,

    is there a way to pass the value of a proprietary variable from a strategy to an optimization fitness? I need to calculate the optimization with my own metrics.

    #2
    Hello guillembm,

    Make the variable public and this will be available from the strategy provided from the OnCalculatePerformanceValue() override.


    Print(strategy.MyCustomPublicVariableName);
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      This works fine. But, I am getting a problem.

      When optimizing, if "aggregated" is checked it doesn't display the Combined result value in the Performance column. Instead, if I use any NT default optimizer, the value appears correctly. For each symbol the value is correct.

      I use the code: Value = MyAcumProfit;

      Comment


        #4
        Hello guillembm,

        Do you have that code in the OnMergePerformanceMetric() override method?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Where does this code need to be placed? In the optimization fitness or in the strategy? Where it has to be called from?. Do you have an complete example of an optimization fitness with this code?

          Thanks

          Comment


            #6
            Hello guillembm,

            This override is used in a OptimizationFitness script for the aggregate values, which might be causing the issue.

            I don't have an example of using this override, but I have an example of a custom optimizationfitness script I can point you to.


            Does this also exhibit the behavior?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              When using the code:

              namespace NinjaTrader.NinjaScript.OptimizationFitnesses
              {
              public class MyOptimizer : OptimizationFitness
              {
              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Enter the description for your new custom Optimization Fitness here.";
              Name = "MyOptimizer";
              }
              }

              protected override void OnCalculatePerformanceValue(StrategyBase strategy)
              {
              var myStrategy = strategy as TestStrategy;
              if (myStrategy != null)
              {
              double AcumProfit = myStrategy.MyAcumProfit;
              Value = AcumProfit;
              }
              }

              protected override void OnMergePerformanceMetric(PerformanceMetricBase merge)
              {
              // Cast the merge parameter to the specific type of your optimizer
              MyOptimizer targetMetrics = merge as MyOptimizer;

              if (targetMetrics != null && TradesPerformance.TradesCount + targetMetrics.TradesPerformance.TradesCount > 0)
              {
              // Aggregate the values using a weighted average
              targetMetrics.Value = (targetMetrics.Value * targetMetrics.TradesPerformance.TradesCount + Value * TradesPerformance.TradesCount) / (TradesPerformance.TradesCount + targetMetrics.TradesPerformance.TradesCount);
              }
              }
              }
              }


              I get the error: 'NinjaTrader.NinjaScript.OptimizationFitnesses.MyO ptimizer.OnMergePerformanceMetric(NinjaTrader.Ninj aScript.PerformanceMetricBase)': no suitable method found to override

              What could be the cause?

              Comment


                #8
                guillembm,

                Apologies, that was my confusion when glancing at the documentation, this method is for a PerformanceMetric not an OptimizationFitness script.

                The PerformanceMetric would be the value that appears in the summary. The OptimizationFitness is used for ranking the optimization results by a metric.

                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Then, what is the solution to display the aggregated value of the performance column?

                  Comment


                    #10
                    Hello guillembm,

                    This would be in a PerformanceMetric script with the OnMergePerformanceMetric() override method.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    69 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    42 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    24 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    27 views
                    0 likes
                    Last Post TheRealMorford  
                    Started by Mindset, 02-28-2026, 06:16 AM
                    0 responses
                    54 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Working...
                    X