Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing Strategy Variables in Custom Optimization Fitness

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

    Accessing Strategy Variables in Custom Optimization Fitness

    Hello NinjaTrader Community!

    I'm struggling to pass variable values from a strategy to a custom optimization fitness. I want to access public custom variables defined in my strategy within the fitness function, rather than relying solely on SystemPerformance values.


    Current Optimization Fitness Code Snippet:

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

    protected override void OnCalculatePerformanceValue(StrategyBase strategy)
    {
    Value = (double) strategy.SystemPerformance.LongTrades.Count/strategy.SystemPerformance.AllTrades.Count;
    }
    }
    }

    Goal:
    1. Define public custom variables in my strategy (e.g., myVariable)
    2. Access these variables in a custom optimization fitness function​
    3. In the code above, the idea is to use custom trade collections (e.g, fpTrades or tpTrades) from strategy and count the number of trades within the optimization fitness logic.

    Questions:
    1. How can I pass strategy variables to the custom fitness function?
    2. Is there a specific interface or base class I need to implement?

    Any guidance or examples would be greatly appreciated!

    #2
    Hello sketchicos,

    Welcome to the NinjaTrader forums!

    Cast the strategy as the actual class type.

    Then you can access the Strategy values.

    NinjaTrader.NinjaScript.Strategies.MyStrategyName myStrategyName = strategy as NinjaTrader.NinjaScript.Strategies.MyStrategyName;
    Code.Output.Process(myStrategyName.MyPropertyName. ToString(), PrintTo.OutputTab1);
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    85 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    143 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    83 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    256 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    334 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X