Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Custom Optimization Fitness Metric NT8

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

    Custom Optimization Fitness Metric NT8

    Is there a way in NT8 to write my own metric that can be optimized on? For example, if I had a new JPRatio that I wanted to update based on every trade a strategy made and then I could optimize on this metric, is that possible?

    I see there is a SampleCumProfit.cs file that might be helpful. I assume OnAddTrade gets called for each trade? I think I can work with that.

    The problem I see is that "SampleCumProfit" does not appear in the list of available Optimization methods to optimize on. I could create my own in the OptimizationFitnesses directory, but how do I give it access to the ratio that I calculate in the PerformanceMetrics cs file?

    Thanks,
    John

    #2
    Hi John, thanks for your note.

    Custom performance metrics do not tie into the SystemPerformance class. I will submit a feature request to track the demand for this feature.

    Thanks in advance for your patience.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply.

      1. Could you explain what the SampleCumProfit.cs file is showing that can be done and how it is tied into the system?

      2. Could you explain what would happen if I create my own .cs file in OptimizationFitnesses? What effect would that have?

      Thanks,
      John

      Comment


        #4
        Hi John, thanks for your patience.

        1. The SampleCumProfit script demonstrated how one could create their own performance metric for the Trade Performance window or strategy analyzer window. If a developer has some performance metric not listed on this page they can create a custom one that gets analyzed as the Trade Performance report is generated.



        2. An OptimizationFitness allows one to create a data point (Value) to optimize a strategy on (min or max). So if there is some formula that a trader wants to optimize on they can calculate it in an OptimizationFitness. After looking through this, though, it raised some questions of my own. Like how the optimizer knows wether to optimize on the minimum or maximum. There does not seem to be anything in the code to tell this to the optimizer. I asked out QA team about it and will relay what they tell me.

        Thanks in advance for your patience.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hello John,

          To elaborate on Chris' message. In an Optimization Fitness Metric, the strategy's SystemPerformance is checked for a statistic and that statistic is applied to the OptimizationFitnessMetric's Value property. The optimization that has the highest Value would be weighed as the top performer.

          If you want to look at a minimum value, you could consider inversing the value so a negative value is assigned, or for the case of percent based metrics, the metric could be subtracted from 1 so that a metric with a value of 92% would actually be 8%.

          We look forward to assisting.
          JimNinjaTrader Customer Service

          Comment


            #6
            Jim,

            Thanks for your reply. What I want to do is create my own metric and then utilize it in the optimization process. My metric will not be based on any of the statistics collected and stored currently in NinjaTrader.Cbi.TradesPerformanceValues (where it seems the OptimizationFitness classes get their data) unless there is a way to include my metric in the NinjaTrader.Cbi.TradesPerformanceValues class via override or other method.

            Is there a way to create my own metric?

            Thanks,
            John

            Comment


              #7
              Hi John, thanks for the follow up.

              You would need to iterate through the SystemPerformance.AllTrades TradeCollection to calculate your metric within the OptimizationFitness script. Currently, a custom PerformanceMetric will not be added to the TradesPerformance class.

              ​​​​​​​
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                This was helpful. Thank you.

                Comment


                  #9
                  Could you provide a sample/example of a OptimizationFitness that iterates over all the trades and calculates something? Perhaps just a running sum of the profit (I know this is already provided in the metrics, but this would be an example).

                  Thanks,
                  John

                  Comment


                    #10
                    Hi John, thanks for your reply.

                    I attached an example that replicates the Max Profit Factor optimization fitness. Place the attached file within Documents\NinjaTrader 8\bin\Custom\OptimizationFitnesses and compile.

                    Kind regards,

                    -ChrisL
                    Attached Files
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi John, thanks for your note.

                      Custom performance metrics do not tie into the SystemPerformance class. I will submit a feature request to track the demand for this feature.

                      Thanks in advance for your patience.
                      Hi linuxguru, thanks for your patience.

                      The feature request tracking id is SFT-4723.

                      Please check the release notes when future updates are released to track added features:
                      https://ninjatrader.com/support/help...ease_notes.htm

                      We appreciate your suggestion.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Any update on this thread?

                        Comment


                          #13
                          Hello mlprice12, thanks for your post.

                          Feature request IDs will be listed in the release notes if they are implemented. This is the best way to check if a feature has been added. SFT-4723 is still an open feature request, I will add a vote to it for you.
                          https://ninjatrader.com/support/help...ease_notes.htm

                          Best regards.
                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi,

                            I tried making a profit fitness script today but the value isn't calculating the same as when I do it manually.

                            Code:
                            protected override void OnCalculatePerformanceValue(StrategyBase strategy)
                            {
                            double profitfactor = 0;
                            double grossprofit = 0;
                            double grossloss = 0;
                            double drawdown = 0;
                            double trades = 0;
                            double days = 0;
                            double tradesperday = 0;
                            double months = 0;
                            double r2 = 0;
                            
                            
                            foreach(var trade in strategy.SystemPerformance.AllTrades)
                            {
                            if(trade.ProfitCurrency <= 0)
                            {
                            grossloss += trade.ProfitCurrency;
                            }
                            else
                            {
                            grossprofit += trade.ProfitCurrency;
                            }
                            
                            
                            }
                            tradesperday = strategy.SystemPerformance.AllTrades.TradesPerform ance.TradesPerDay;
                            trades = strategy.SystemPerformance.AllTrades.TradesPerform ance.TradesCount;
                            days = trades/tradesperday;
                            months = days/30.5;
                            drawdown = strategy.SystemPerformance.AllTrades.TradesPerform ance.Currency.Drawdown;
                            r2 = strategy.SystemPerformance.AllTrades.TradesPerform ance.RSquared;
                            
                            //Print(grossprofit);
                            //Print(grossloss);
                            //Print(grossprofit/grossloss);
                            Value = ( (grossprofit - grossloss)/months/drawdown*(grossprofit/grossloss)* r2 );
                            I would like net profit / months of trading / max drawdown * profit factor * r^2

                            Any idea where my logic interfered?

                            Thanks


                            Update: the "months" calculation is not as I intended. Trades/Avg.trades per day is giving me a much smaller amount of days than I'm evaluating.

                            The Avg. Trades Per Day calculation is a little misleading. I believe it divides total trades by the number of days between the first and last trade. Is this correct? With a strategy that takes fewer trades this statistic doesn't accurately represent the true average trades per day over the evaluated period.
                            Last edited by mlprice12; 04-01-2021, 07:01 PM.

                            Comment


                              #15
                              How could I more accurately access the amount of days the strategy analyzed?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Started by alifarahani, Today, 09:40 AM
                              6 responses
                              41 views
                              0 likes
                              Last Post alifarahani  
                              Working...
                              X