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

Performance Metric Development Help

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

    Performance Metric Development Help

    Hey guys!

    so for the past day i have been trying to learn how performance metric code works and cant seem to figure it out... is there any material out there i can read up on (ive read the help guides for custom performance metrics.... not much help) or even example codes for the built in metrics like net profit? i feel like its very easy but i'm getting nothing but errors all day.

    #2
    Hello travelingsalesman,

    Thank you for the post.

    The only documentation on custom metrics is in the help guide and as an example named "SampleCumProfit". Could you please share your script file and point out the specific errors you are getting?

    I look forward to your reply.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      ya i can share it but its really nothing special. ill post my latest attempt to try to get the total profit to show up. i am trying to learn based off of 1 help guide so dont laugh

      here it is. but its basically the help guide up until protected override void OnMergePerformanceMetric(PerformanceMetricBase target). the thing is i dont know how to access the performance numbers like net profit, sharpe, win/loss, etc...


      namespace NinjaTrader.NinjaScript.PerformanceMetrics
      {
      public class NetProfitPerTrade : PerformanceMetric
      {

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      Name = "NetProfitPerTrade";
      else if (State == State.Configure)
      Values = new double[5]; // There needed to be one value per Cbi.PerformanceUnit, which is why the values are managed in an array of length "ValueArrayLength"
      else if (State == State.Active)
      Array.Clear(Values, 0, Values.Length); // Needed to be reset before every backtest iteration.
      }

      // This is called as the values of a trade metric are saved, which occurs e.g. in the strategy analyzer on optimizer runs
      protected override void OnCopyTo(PerformanceMetricBase target)
      {
      // You need to cast, in order to access the right type
      NetProfitPerTrade targetMetrics = (target as NetProfitPerTrade);

      if (targetMetrics != null)
      Array.Copy(Values, targetMetrics.Values, Values.Length);
      }

      // This is called as the trade metric is merged, which occurs e.g. in the strategy analyzer for the total row and for aggregate
      protected override void OnMergePerformanceMetric(PerformanceMetricBase target)
      {
      // You need to cast, in order to access the right type
      NetProfitPerTrade targetMetrics = (target as NetProfitPerTrade);

      // This is just a simple weighted average sample
      if (targetMetrics != null && TradesPerformance.TradesCount + targetMetrics.TradesPerformance.TradesCount > 0)
      for (int i = 0; i < Values.Length; i++)
      targetMetrics.Values[i] = TradePerformance.AllTrades.TradesPerformance.NetPr ofit;

      }

      // The attribute determines the name of the performance value on the grid (the actual property name below is irrelevant for that matter)
      [Display(Name = "Net Profit Per Trade", Order = 0)]
      public double[] Values
      { get; private set; }

      Comment


        #4
        Hello travelingsalesman,

        Thank you for the reply.

        I fixed up your file so that it added the denomination variable at the class level:

        private Cbi.Currency denomination = (Cbi.Currency) (-1);

        I also added the OnAddTrade override. This method is a critical part of any performance metric. That is where you will define the value of your metric per performance unit type.

        Please see the attached file. If you want to compile the script, copy the file to ..\NinjaTrader 8\bin\Custom\PerformanceMetrics and compile within the NinjaScript editor.

        Please let me know if you have any questions.
        Attached Files
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Ahhhhh.... i see..... i was off. the on add trade override is where the calculation occurs and you the merge performance metric is just for accessing that information and casting it to the summary page.

          i removed all those sections thinking that the calculation and casting was all done in the merge override.

          thanks! ill give it a shot and see what i get.

          Comment


            #6
            Hey Chris,

            what about keeping the performance metric on screen even if i change the performance unit from currency? so for instance i want net profit to be displayed in currency even if i change the strategy analyzer window to points or pips?

            i do this but it just ends up changing the net profit to points or pips (I.E. $1,000 Net Profit >> 1000 points or 1000pips)

            Values[(int)Cbi.PerformanceUnit.Currency] = (TradesPerformance.NetProfit);
            Values[(int)Cbi.PerformanceUnit.Percent] = (TradesPerformance.NetProfit);
            Values[(int)Cbi.PerformanceUnit.Pips] = (TradesPerformance.NetProfit);
            Values[(int)Cbi.PerformanceUnit.Points] = (TradesPerformance.NetProfit);

            Or for instance what if i wanted to show the TradesPerformance.MonthlyStdDev as a percent in all strategy analyzer windows denominations?
            Last edited by travelingsalesman; 02-19-2019, 03:29 PM. Reason: forgot to mention the MonthlyStdDev

            Comment


              #7
              I found it! the format section was in the mic region

              Comment


                #8
                Hey Chris,

                sorry to keep bugging you but i ran into another bump in the road and was wondering if you could help me out.

                after getting the custom performance metric to show up on strategy analyzer window i wanted to see if i can get it to be a custom optimization fitness. whenever i put the "Value = NinjaTrader.NinjaScript.PerformanceMetrics.SampleC umProfit;" under the on calculateperformanceValue to try to use the custom performance metric as the value for optimization fitness i get an error:
                'NinjaTrader.NinjaScript.PerformanceMetrics.Sample CumProfit' is a 'type', which is not valid in the given context

                Double clicking the code takes me to ninjatraders help documents section and says there is no help docs availble on this topic.

                do you know how i can set the optimization fitness to the custom performance metric?


                for reference here is the full code for the custom optimization fitness

                #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

                //This namespace holds Optimization fitnesses in this folder and is required. Do not change it.
                namespace NinjaTrader.NinjaScript.OptimizationFitnesses
                {
                public class SampleCumProfit : OptimizationFitness
                {
                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = @"Enter the description for your new custom Optimization Fitness here.";
                Name = "SampleCumProfit";
                }
                else if (State == State.Configure)
                {
                }
                }

                protected override void OnCalculatePerformanceValue(StrategyBase strategy)
                {
                Value = NinjaTrader.NinjaScript.PerformanceMetrics.SampleC umProfit;
                }

                }
                }

                Comment


                  #9
                  Hello travelingsalesman,

                  Thanks for the reply, Im happy to assist.

                  You must access the Cumulative Profit metric in terms of a TradesPerformance value like so:

                  Code:
                  Value = strategy.SystemPerformance.AllTrades.TradesPerform ance.Percent.CumProfit;
                  Please let me know if I can assist further.
                  Last edited by NinjaTrader_ChrisL; 02-22-2019, 08:53 AM.
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    yes that would work with any builtin metrics yes. but the whole premise here is how to access a custom performance metric. lets say i had my own metric that i wanted to optimize on how would i do it?

                    Comment


                      #11
                      Hello travelingsalesman,

                      Thanks for the reply.

                      You will need to select and cast the custom performance metric like in the example I have attached.

                      Please let me know if I can assist further.
                      Attached Files
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Awesome! thanks so much that worked! jezz i was way off, i would have never figured that out, much appreciated.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by inanazsocial, Today, 01:15 AM
                        1 response
                        5 views
                        0 likes
                        Last Post NinjaTrader_Jason  
                        Started by rocketman7, Today, 02:12 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post rocketman7  
                        Started by dustydbayer, Today, 01:59 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post dustydbayer  
                        Started by trilliantrader, 04-18-2024, 08:16 AM
                        5 responses
                        22 views
                        0 likes
                        Last Post trilliantrader  
                        Started by Davidtowleii, Today, 12:15 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post Davidtowleii  
                        Working...
                        X