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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      47 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      23 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      33 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      50 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X