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

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.

            A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.


            The List is a collection of strongly typed objects that can be accessed by index and having methods for sorting, searching, and modifying list. It is the generic version of the ArrayList that comes under System.Collection.Generic namespace.


            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 CortexZenUSA, Today, 12:53 AM
              0 responses
              1 view
              0 likes
              Last Post CortexZenUSA  
              Started by CortexZenUSA, Today, 12:46 AM
              0 responses
              1 view
              0 likes
              Last Post CortexZenUSA  
              Started by usazencortex, Today, 12:43 AM
              0 responses
              5 views
              0 likes
              Last Post usazencortex  
              Started by sidlercom80, 10-28-2023, 08:49 AM
              168 responses
              2,266 views
              0 likes
              Last Post sidlercom80  
              Started by Barry Milan, Yesterday, 10:35 PM
              3 responses
              13 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Working...
              X