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

Varible as input value in a custom Optimization Fitnesses

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

    Varible as input value in a custom Optimization Fitnesses

    Hello

    I’m working with a custom Optimization Fitnesses and I’m trying to add the way to specify an int variable value of the Optimization Fitnesses internal calculation as an input from the Strategy Analyzer, i.e. in a similar way as with a strategy you could insert a variable value as an input from its Properties from the Strategy Analyzer.

    I was looking for an example about a similar situation but haven’t found a solution yet.


    Here is a code with the structure of some things I’m trying with the part to add the input, please check the sections `Properties` and `State.SetDefaults`, but none of them make the input to be shown in the Strategy Analyzer UI to be an user inserted value.

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    #endregion​
    
    namespace NinjaTrader.NinjaScript.OptimizationFitnesses
    {
        public class MaxProfitFactorCustom: OptimizationFitness
        {
    
            protected override void OnStateChange()
            {              
                if (State == State.SetDefaults)
                {
                    Name = "MaxProfitFactorCustom";
    
                    inputVar = 10;
                 }
            }​
    
    
            protected override void OnCalculatePerformanceValue(StrategyBase strategy)
            {
                Value = strategy.SystemPerformance.AllTrades.TradesPerformance.ProfitFactor;
    
                 // extra calculation here with `inputVar`.
            }
    
    
    
            #region Properties
    
            [Display(ResourceType = typeof(Custom.Resource), Name = "NinjaScriptOptimizationFitness")]
            [Range(1, Int32.MaxValue)]
            public int inputVar
            { get; set; }​
    
    
            // some of other alternatives tried.
    
            /*
            [NinjaScriptProperty]
            [Display(Name="variableInput", Description="...", Order=0, GroupName="NinjaScriptOptimizationFitness")]
            [Range(0, int.MaxValue)]
            public int inputVar
            { get; set; }
            */
    
            /*
            [Display(ResourceType = typeof(Custom.Resource), Description = "...", Name = "Properties", Order = 0)]
            [Range(0, int.MaxValue)]
            public int inputVar
            { get; private set; }​
            */
    
            #endregion​
    
        }
    }​


    What would be solution for these cases?

    Thank you
    Last edited by futurenow; 02-21-2023, 07:05 PM.

    #2
    Hello futurenow,

    OptimizationFitness, Optimizers, PerformanceMetrics, do not have input controls.

    You would need to add the inputs to the strategy itself, then you could reference the value from the strategy object provided from OnCalculatePerformanceValue().

    strategy.MyCustomProperty
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello futurenow,

      OptimizationFitness, Optimizers, PerformanceMetrics, do not have input controls.

      You would need to add the inputs to the strategy itself, then you could reference the value from the strategy object provided from OnCalculatePerformanceValue().

      strategy.MyCustomProperty

      Thank you Chelsea
      Last edited by futurenow; 02-23-2023, 01:23 PM.

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post

        OptimizationFitness, Optimizers, PerformanceMetrics, do not have input controls.

        You would need to add the inputs to the strategy itself, then you could reference the value from the strategy object provided from OnCalculatePerformanceValue().

        strategy.MyCustomProperty
        I've tried with multiple ways with the next strategy sample.

        - First of all, I had to add 2 inputs because I see the way to access to the input from the OptimizationFitness is when the variable is a `static`, but then I see that making the input varible `static` causes that the input doesn't show up in the Strategy Analyzer, that is exactly what it is needed here.

        - Second, from the OptimizationFitness I can access to the varible `inputForOptimizationFitnesses_part2` but I see that if for an Optimization I change the input value (inputForOptimizationFitnesses_part1) from the Strategy Analyzer, that then the new value inserted from the Strategy Analyzer is not updated in the OptimizationFitness, so it is like if in the OptimizationFitness the input value would be read from the value in the strategy code instead to take the user-modified value from the Strategy Analyzer.


        To access to the strategy input value from the OptimizationFitness and print it, I'm using the next:
        `Print ("strategy input value = " + NinjaTrader.NinjaScript.Strategies.SampleMaTest.in putForOptimizationFitnesses_part2);`

        Code:
        #region Using declarations
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Gui;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Gui.SuperDom;
        using NinjaTrader.Data;
        using NinjaTrader.NinjaScript;
        using NinjaTrader.Core.FloatingPoint;
        using NinjaTrader.NinjaScript.Indicators;
        using NinjaTrader.NinjaScript.DrawingTools;
        #endregion
        
        //This namespace holds strategies in this folder and is required. Do not change it.
        namespace NinjaTrader.NinjaScript.Strategies
        {
            public class SampleMaTest : Strategy
            {
                private SMA smaFast;
                private SMA smaSlow;
        
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description                         = "SampleMaTest";
                        Name                                = "SampleMaTest";
                        // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
                        // See the Help Guide for additional information
                        IsInstantiatedOnEachOptimizationIteration = false;
        
                        Fast                                = 10;
                        Slow                                = 25;
        
        
                        inputForOptimizationFitnesses_part1 = 10;
                        inputForOptimizationFitnesses_part2 = 20 + inputForOptimizationFitnesses_part1;
                    }
                    else if (State == State.DataLoaded)
                    {
                        smaFast = SMA(Fast);
                        smaSlow = SMA(Slow);
        
                        smaFast.Plots[0].Brush = Brushes.Goldenrod;
                        smaSlow.Plots[0].Brush = Brushes.SeaGreen;
        
                        AddChartIndicator(smaFast);
                        AddChartIndicator(smaSlow);
                    }
                }
        
                protected override void OnBarUpdate()
                {
                    if (CurrentBar < BarsRequiredToTrade)
                        return;
        
                    if (CrossAbove(smaFast, smaSlow, 1))
                        EnterLong();
                    else if (CrossBelow(smaFast, smaSlow, 1))
                        EnterShort();
                }
        
                #region Properties
                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
                public int Fast
                { get; set; }
        
                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
                public int Slow
                { get; set; }
        
        
        
                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(Name = "inputForOptimizationFitnesses_part1", Description = "inputForOptimizationFitnesses_part1", GroupName = "InputForOptimizationFitnesses", Order = 1)]
                public int inputForOptimizationFitnesses_part1
                { get; set; }
        
                [Range(1, int.MaxValue), NinjaScriptProperty]
                [Display(Name = "inputForOptimizationFitnesses_part2", Description = "inputForOptimizationFitnesses_part2", GroupName = "InputForOptimizationFitnesses", Order = 0)]
                public static int inputForOptimizationFitnesses_part2
                { get; set; }​
                #endregion
            }
        }
        
        ​
        Last edited by futurenow; 02-23-2023, 03:58 PM.

        Comment


          #5
          Hello futurenow,

          Static is not something we suggest to use with NinjaScript, that is also not the correct way to make an input. Static is a powerful feature of C# and is often conflicting with how NinjaScript works. Static exists on the type and not an instance. The analyzer works based on the instance of the strategy and not the type so these types of coding conflict.

          I would highly suggest looking at how the existing optimization fitness work with the strategy instance before trying to make your own. You need to use the passed in strategy object to retrieve its values. For example:

          Code:
          Value = strategy.SystemPerformance.AllTrades.TradesPerform ance.Percent.AverageMfe;
          strategy is the instance of the strategy that is being tested.

          The fitness passes in the strategy as its base type: (StrategyBase strategy)

          If you need a custom input you need to cast this object to the correct type:

          Code:
          protected override void OnCalculatePerformanceValue(StrategyBase strategy)
          {
              NinjaTrader.NinjaScript.Strategies.SampleMACrossOver strat = (NinjaTrader.NinjaScript.Strategies.SampleMACrossOver)strategy;
              int period = strat.Period;
          }
          JesseNinjaTrader Customer Service

          Comment


            #6
            I just tried with the provided syntax and now I see the Optimization Fitness is getting the input variable value in the correct way.


            Thank you Jesse and ChelseaB for your time

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by llanqui, Today, 03:53 AM
            0 responses
            2 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Started by burtoninlondon, Today, 12:38 AM
            0 responses
            10 views
            0 likes
            Last Post burtoninlondon  
            Started by AaronKoRn, Yesterday, 09:49 PM
            0 responses
            14 views
            0 likes
            Last Post AaronKoRn  
            Started by carnitron, Yesterday, 08:42 PM
            0 responses
            11 views
            0 likes
            Last Post carnitron  
            Started by strategist007, Yesterday, 07:51 PM
            0 responses
            14 views
            0 likes
            Last Post strategist007  
            Working...
            X