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

collect stats on optimization run

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

    collect stats on optimization run

    Hi,
    I want to run an optimization, and print statistics on each trade to the output window. Do you have an example of how to do that without getting duplicate stats every bar update? I want to be able to export the data for further analysis in a table format. The stats that NT gives in the analyzer window aren't calculated the way I want.
    thanks,
    David

    #2
    Have you considered using a custom optimization fitness function? Or, you could write out the data you want to a file at the end of each iteration.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Is the ability to write a custom optimization fitness function a feature in NT?

      Comment


        #4
        Sure. https://ninjatrader.com/support/help...on_fitness.htm

        Look in Documents -> NinjaTrader 8 -> bin -> Custom -> OptimizationFitnesses to see how the inbuilt ones work to get an idea.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Hello, thanks for writing in. An optimization fitness would not be the best place to do this. Use the Terminal state in the strategy itself within OnStateChanged:

          else if (State == State.Terminated)
          {
          if(SystemPerformance.AllTrades.Count > 0)
          Print("TERMINAL " + SystemPerformance.AllTrades.TradesPerformance.NetP rofit);
          }​
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Hi,
            I like both ideas for different reasons.
            Is there a way to view the code for the various PerfromanceMetrics? I want to write my own and call it when I like.
            Thanks Bruce and Chris!
            Last edited by trader3000a; 05-17-2023, 10:26 AM.

            Comment


              #7
              Hi, the code for the performance metrics is not open, but you can create your own. See the "Performance Metrics" folder in the NinjaScript Editor for an example.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Hi ChrisL,
                The suggestion you gave me only prints data on or for for the very last trade of the series.For example (within on state change) :

                else if (State == State.Terminated)
                {

                if (SystemPerformance.AllTrades.Count > 1 )
                {

                Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
                if(SystemPerformance.AllTrades.Count > 1)
                Print("TERMINAL " + SystemPerformance.AllTrades.TradesPerformance.NetP rofit);
                Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
                double lastProfitCurrency = lastTrade.ProfitCurrency;
                Print("lastProfitCurrency = " + lastTrade.ProfitCurrency);
                }
                }

                Only prints:

                a value representing the aggregate net profit of all trades
                a value representing the net profit of the very last trade in the series


                I'm looking for a way to print data for each trade in the series. How do I print data for each trade?
                thanks,
                David
                Last edited by trader3000a; 05-18-2023, 03:54 AM.

                Comment


                  #9
                  Hi David, You wil need to iterate through the entire collection. Please first read the documentation on the SystemPerformance class:



                  This is a handful of collections that contain Trade objects.

                  foreach(Trade T in SystemPerformance.AllTrades)
                  {
                  //get information about Trade T
                  }
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chris,
                    That method still prints duplicates of the last trade stats on every bar update. I only want 1 output line of stats per trade (like the analyzer does it).
                    thanks,
                    David

                    Comment


                      #11
                      Hi, Iterating through this array in State.Terminated will iterate for each optimization iteration. Your code will need to be set up such that it will only print once. You can do this using C# logic such as boolean flags.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        This snippet is not printing out anything twice:

                        Code:
                        else if (State == State.Terminated)
                                    {
                                        if(SystemPerformance.AllTrades.Count > 0)
                                        {
                                            Print("Optimization Iteration Complete");
                                            foreach(Trade T in SystemPerformance.AllTrades)
                                            {
                                                Print("Trade info " + T.ProfitCurrency);
                                            }
                                        }
                                    }​
                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          cool, I'll try it, thx

                          Comment


                            #14
                            amazing. you are the g.o.a.t.

                            Comment


                              #15
                              Hi Chris,
                              1 last issue. I've uploaded the script which has a basic entry and a few prints.
                              Test date is 05/04/2020
                              Data series is ES JUN23, 5 minute bars


                              If I run a backtest with "BarsToHold" set at 100, I get the result from the output window we expect:

                              Optimization Iteration Complete
                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 1212.5
                              MFE 1912.5
                              MAE 262.5
                              Basic Excursion Ratio 7.31

                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 587.5
                              MFE 1062.5
                              MAE 650
                              Basic Excursion Ratio 1.61

                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 1000
                              MFE 1412.5
                              MAE 450
                              Basic Excursion Ratio 3.11



                              However, if I run an optimization with "BarsToHold" set for a range of 100 - 200 and increment of 100 (expecting 2 sets of trades), we get 4 sets of trades from the output window, the final 2 sets being a duplicate of the first 2 sets

                              Optimization Iteration Complete
                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 1212.5
                              MFE 1912.5
                              MAE 262.5
                              Basic Excursion Ratio 7.31

                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 587.5
                              MFE 1062.5
                              MAE 650
                              Basic Excursion Ratio 1.61

                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 1000
                              MFE 1412.5
                              MAE 450
                              Basic Excursion Ratio 3.11


                              Optimization Iteration Complete
                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 1812.5
                              MFE 2075
                              MAE 262.5
                              Basic Excursion Ratio 7.91

                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 887.5
                              MFE 1300
                              MAE 562.5
                              Basic Excursion Ratio 2.31


                              Optimization Iteration Complete
                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 1212.5
                              MFE 1912.5
                              MAE 262.5
                              Basic Excursion Ratio 7.31

                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 587.5
                              MFE 1062.5
                              MAE 650
                              Basic Excursion Ratio 1.61

                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 1000
                              MFE 1412.5
                              MAE 450
                              Basic Excursion Ratio 3.11


                              Optimization Iteration Complete
                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 1812.5
                              MFE 2075
                              MAE 262.5
                              Basic Excursion Ratio 7.91

                              __________________
                              5/3/2020 6:05:00 PM
                              Trade info 887.5
                              MFE 1300
                              MAE 562.5
                              Basic Excursion Ratio 2.31


                              How would we eliminate the duplicates of the 2 optimization runs?
                              thanks,
                              David
                              Attached Files
                              Last edited by trader3000a; 05-18-2023, 09:47 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,403 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              94 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X