Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Derive calculated values for OptimizationFitnesses

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

    Derive calculated values for OptimizationFitnesses

    Hello,

    I need help getting derived values for OptimizationFitnesses:

    Max Cum. Net profit [Maximum value of Cum. net profit from Trades tab]
    Lrg. Win day [Maximum value of Net profit from Analysis tab, Period: Daily]
    Lrg. Loss day [Minimum value of Net profit from Analysis tab, Period: Daily]
    Avg. day [Average value of Net profit from Analysis tab, Period: Daily]
    Tot. Win Days [Count of values of Net profit, if Net profit > 0, from Analysis tab, Period: Daily]
    Tot. Loss Days [Count of values of Net profit, if Net profit < 0, from Analysis tab, Period: Daily]
    Max MAE [Maximum value of MAE from Trades tab]
    Max MFE [Maximum value of MFE from Trades tab]
    Max ETD [Maximum value of ETD from Trades tab]

    Appreciate support team for help in advance.

    #2
    Hello UltraNIX,

    Thanks for your post.

    From an Optimization Fitness Metric, you will be able to access the strategy object that is being optimized, and from there you can access SystemPerformance, TradesCollections, TradePerformance reports from the Trade Collections, and if you reference individual Trades in the Trades Collection, you can get Trade specific metrics.

    SystemPerformance - https://ninjatrader.com/support/help...erformance.htm

    TradesCollections - https://ninjatrader.com/support/help...collection.htm

    TradePerformance reports - https://ninjatrader.com/support/help...erformance.htm

    Trade objects - https://ninjatrader.com/support/help...nt8/?trade.htm

    You may find the existing Optimization Fitness metrics helpful as they can show how various metrics are calculated. You could consider using MaxNetProfit to satisfy the first requirement, or come up with your own metric if you with to customize it.

    Metrics based on the day would involve looping through the AllTrades TradesCollection and to differentiate trades per day, and calculate your "Per-Day" metrics. The "Per-Day" metrics would then need to be weighed to determine which would be the winner, and you can set that value to the Value property of the Optimization Fitness Metric.

    The maximum MAE/MFE of individual trades could be done by looking at each Trade in the AllTrades collection, and keeping note of which trade had the biggest MaeTicks/MaePercent, etc.

    ETD per trade is not provided but you could subtract the trade's profit from the trade's MFE to get this.

    Documentation on statistics definitions and documentation on Optimization Fitness Metrics may also be helpful to achieve your goals.

    Statistics Definitions - https://ninjatrader.com/support/help...efinitions.htm

    Optimization Fitness Metrics - https://ninjatrader.com/support/help...on_fitness.htm

    We look forward to assisting.



    JimNinjaTrader Customer Service

    Comment


      #3
      NinjaTrader_Jim, thank you for your reply.

      Metrics based on the day would involve looping through the AllTrades TradesCollection and to differentiate trades per day, and calculate your "Per-Day" metrics. The "Per-Day" metrics would then need to be weighed to determine which would be the winner, and you can set that value to the Value property of the Optimization Fitness Metric.
      How would I do that?

      The maximum MAE/MFE of individual trades could be done by looking at each Trade in the AllTrades collection, and keeping note of which trade had the biggest MaeTicks/MaePercent, etc.
      Well, I need to come up with a calculation/script that would do all the "looking at each trade in the AllTrades collection" behind the scenes and just give me required value.

      Comment


        #4
        Hello UltraNIX,

        You can loop through each Trade in AllTrades and from there you can access the Entry Execution and/or the Exit Execution of the Trade. From there, you can get the DateTime of the Execution to find the day it was made.

        Execution objects - https://ninjatrader.com/support/help...?execution.htm

        Code:
        foreach (NinjaTrader.Cbi.Trade t in strategy.SystemPerformance.AllTrades)
            Print(t.Entry.Time);
        You would then want to to sort those trades by day, for example, use Lists/Dictionaries to store trades with each day that was backtested.

        You could then calculate metrics based on those trades, and you could also store them in another dictionary that has the metrics sorted by day.

        You could then loop over that dictionary to find your "winning value" and assign it to Value.

        There would be numerous ways to accomplish, this would be one way that you could brainstorm. The key would be looping through the individual trades and extracting the information you need to calculate your metrics.

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          ok, I solved Avg. day.
          Code:
          strategy.SystemPerformance.AllTrades.TradesCount == 0 ? 0 : strategy.SystemPerformance.AllTrades.TradesPerformance.NetProfit / strategy.SystemPerformance.AllTrades.ByDay.Count
          Code:
          foreach (NinjaTrader.Cbi.Trade t in strategy.SystemPerformance.AllTrades)
          Print(t.Entry.Time);
          I also tried other variations with your code and came up that there are other parts of the calculations I need, like t.MaeCurrency, t.MfeCurrency, t.ProfitCurrency, so I just need a method to get those in list so I could calcuate maximum of those.

          I tried
          Code:
          Print(t.ProfitCurrency.Max());
          but I got errors:
          'double' does not contain a definition for 'Max' and the best extension method overload 'System.Linq.Enumerable.Max(System.Collections.Gen eric.IEnumerable<int>)' has some invalid arguments
          Instance argument: cannot convert from 'double' to 'System.Collections.Generic.IEnumerable<int>'
          So a way to 1) compile a list of all i.e. t.MaeCurrency then 2) get maximum value is needed.

          You would then want to to sort those trades by day, for example, use Lists/Dictionaries to store trades with each day that was backtested.

          You could then calculate metrics based on those trades, and you could also store them in another dictionary that has the metrics sorted by day.

          You could then loop over that dictionary to find your "winning value" and assign it to Value.
          As for daily calculations, your throught process sounds spot on, but since I'm not a coder, yet I do my best adapting examples from other codes, I would appreciate if you could help me with a code.

          Comment


            #6
            Hello UltraNIX,

            Lists and Dictionaries are standard C# concepts. You can read more about using them when referencing C# educational resources external to NinjaTrader. Two publicly available resources are linked below.

            https://www.geeksforgeeks.org/c-shar...with-examples/

            https://www.tutorialsteacher.com/csharp/csharp-list

            Trade.ProfitCurrency is a double variable. It does not contain any child methods so t.ProfitCurrency.Max() would not be valid. Math.Max() can help to compare two values and get the highest value of the values that are compared.

            Math.Max() - https://docs.microsoft.com/en-us/dot...x?view=net-4.8

            As for daily calculations, your throught process sounds spot on, but since I'm not a coder, yet I do my best adapting examples from other codes, I would appreciate if you could help me with a code.
            I don't have any ready built examples to demonstrate that idea. I would end up having to write the whole script to create such a demonstration and this would not be within our scope of services for support.

            If you are looking for services to have code written for you, we would suggest enlisting the services of a NinjaScript Consultant to have your desired scripts written for you by request. If you would like more information, I can have a colleague reach out with further details on how you can find such services.
            JimNinjaTrader Customer Service

            Comment


              #7
              I checked several methods, but none of them worked. Then I referenced NinjaCoding course and adapted their idea to fit my needs. All in all, after like 6 hours of testing, I am finally happy with a result.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by aligator, Today, 02:38 PM
              0 responses
              5 views
              0 likes
              Last Post aligator  
              Started by Stett0002, Yesterday, 11:55 PM
              1 response
              11 views
              0 likes
              Last Post NinjaTrader_ShawnB  
              Started by KasLB, Today, 05:12 AM
              2 responses
              17 views
              1 like
              Last Post KasLB
              by KasLB
               
              Started by LeitoGTR, Yesterday, 06:41 PM
              2 responses
              19 views
              0 likes
              Last Post LeitoGTR  
              Started by Prisalice, Today, 07:07 AM
              1 response
              10 views
              0 likes
              Last Post NT-Roland  
              Working...
              X