Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What is the correct way to call a custom Performance Metric for use in a Strategy?

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

    What is the correct way to call a custom Performance Metric for use in a Strategy?

    I wrote a custom Performance Metric that I would like to call/reference within a specific strategy. The overall intent is to use the custom Performance Metric as a parameter to halt the strategy (similar to stop strategy code in SampleHaltBasicStrategy or SampleHaltAdvancedStrategy).

    I'm attempting to call the Performance Metric in the same fashion one would call a custom indicator in a strategy. I can get the code to compile, however when I run the strategy I get the "object reference not set to an instance of an object" error. I suspect this is due to how I am calling and/or defining the custom Performance metric within the strategy.

    I'm fairly experienced at coding indicators and strategies in NinjaTrader8, but lack a firm coding foundation in general for C#. That bites me in instances like this.

    If it is not possible to call a custom performance metric from a strategy, that would be helpful to know.

    Thanks in advance for your help.

    Here are the relevant parts of the code for the custom performance metric:

    public class ExampleStrategy : Strategy
    {
    ...
    private DTDeltaRRV0 DeltaRR; //Call DeltaRR custom trade performance parameter like I would a custom indicator

    protected override void OnStateChange()
    {
    else if (State == State.DataLoaded)
    {...
    double[] DeltaRRVal = DeltaRR.Values; //Define custom trade performance parameter like I would a custom indicator
    ...}

    protected override void OnBarUpdate()
    {​...
    double[] DeltaRRVal = DeltaRR.Values; //Set a variable that contains the custom Performance Metric Values
    ...
    if ((DeltaRRVal[0] <= DeltaRRlim
    && this.SystemPerformance.AllTrades.TradesCount >= MinTrades)
    || (this.SystemPerformance.AllTrades.TradesPerformanc e.Currency.CumProfit < MaxLoss
    && this.SystemPerformance.AllTrades.TradesCount < MinTrades))
    {
    //A custom method designed to close all open positions and cancel all working orders will be called.
    //This will ensure we do not have an unmanaged position left after we halt our strategy.

    //StopStrategy();

    // Halt further processing of our strategy
    return;
    }​.​​​

    #2
    Hello whizkpm,

    The help guide does provide sample code.



    Are you looping through the PerformanceMetrics collection?

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea,

      Somehow I had not found those code examples. I think I can figure out my intent from those examples.

      Thanks so much for the help!

      Kevin

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      137 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      120 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      71 views
      0 likes
      Last Post PaulMohn  
      Working...
      X