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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    553 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    100 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    543 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    546 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X