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

Trade Summary/Performance issue

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

    Trade Summary/Performance issue

    hello, I've kn0own this for a long time but I've ignored it b/c I'm usually only looking at my currency equity curve rather than % gains per trade but this is going to cause me issues calculating expectancy for the strategy.

    Anyways; I don't think there can be a fix for this unless you guys make the change, but in the case you guys don't know about this:

    if a trade with same entry has multiple exits.. for example, purchase 100k at X price. And then sell 50k at Y price for a gain of 1% and sell 50k at Z price for a gain of 3%.. the cum return column as it stands now will show a cum gain of 4%. which is wrong b/c it's not weighted by quantity purchased. Then if you have 10 exits... returns look like attached picture. i don't really know how you guys would go about fixing this....


    Any idea how I can calculate avg gain in my scenario? Say I entered in 2 trades... each trade was closed out in 3 increments. NT as of right now is seeing 6 trades and my gains are being divided by 3 rather than 2. I've been able to calculate the correct # of trades in the strategy but I have no idea how to implement this in the OptimizationFitness

    Code:
    protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
    {
        if (test==false)
       {
    		test=true;
       }
          	
        if (position.MarketPosition == MarketPosition.Flat)// && SystemPerformance.AllTrades.Count>2)
        {
    		test=false;
            ++strategy_trade_counts;
    		Trade a = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    		b =SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;
    		Print(string.Format("{0};{1};{2}",Time[0],strategy_trade_counts,b));
        }
      
    }
    The above gives me the correct trade count for my strategy and gives me the CumProfit in time t. The change in CumProfit from time1 to time 2 is my profit.

    I could use the above and calculate my expectancy but I have no idea how I can implement this in the OptimizerFitness as I've never been able to figure out how to access strategy information in OptimizerFitness....
    Attached Files
    Last edited by staycool3_a; 09-17-2018, 01:42 PM.

    #2
    Thank you for your report staycool3_a .

    I wasn't able to see quite what you were seeing in a historical data situation, and so I was hoping to work with you to clarify the situation under controlled conditions. Can you analyze the following and let me know about any changes I need to make so I can see what you are seeing on your end?


    1. Copy the attached strategy code TestHS1973073.cs to your Documents\NinjaTrader 8\bin\Custom\Strategy folder
    2. Start NinjaTrader, which will compile this strategy
    3. Visit Tools → Import → Historical Data. Expand Import, and click the import button. Browse to the attached ES 12-18.Last.txt file . Open.
    4. Visit New → NinjaScript Output Window
    5. In the Strategies tab, right-click, select New Strategy, and run TestHS1973073 on ES 12-18 1 minute bars. OK
    6. Connect to your Simulated Data Feed, and enable the strategy instance you created in step 5

    At this point you can right-click the strategy instance, visit Strategy Performance, and Historical, and you'll observe the following :


    • You enter the market at 2892.25, 10 contracts deep (see the NinjaScript Output Window)
    • You sell 5 of these at 2905.00
    • You sell the other 5 at 2909.75
    • By hand, 5(2909.75-2892.25) + 5(2905.00-2892.25) = 151.25
    • Each point is worth $50, so this is a profit of $50(151.25) = $7 562.50
    • This is what shows in the strategy performance window
    • When you then select the percentage display, you see you made 1.05% of your investment
    • This is the value you get mathematically when you divide 151.25 / 2892.25 and add 1, when you round to two decimal places
      • This proves that we are scaling by quantity, since 2892.25 / (10(2909.75-2892.25)) = 6%, not 5%



    Thanks for your assistance in improving NinjaTrader 8
    Attached Files
    Last edited by NinjaTrader_JessicaP; 09-17-2018, 03:50 PM.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_JessicaP View Post
      Thank you for your report staycool3_a .

      I wasn't able to see quite what you were seeing in a historical data situation, and so I was hoping to work with you to clarify the situation under controlled conditions. Can you analyze the following and let me know about any changes I need to make so I can see what you are seeing on your end?


      1. Copy the attached strategy code TestHS1973073.cs to your Documents\NinjaTrader 8\bin\Custom\Strategy folder
      2. Start NinjaTrader, which will compile this strategy
      3. Visit Tools → Import → Historical Data. Expand Import, and click the import button. Browse to the attached ES 12-18.Last.txt file . Open.
      4. Visit New → NinjaScript Output Window
      5. In the Strategies tab, right-click, select New Strategy, and run TestHS1973073 on ES 12-18 1 minute bars. OK
      6. Connect to your Simulated Data Feed, and enable the strategy instance you created in step 5

      At this point you can right-click the strategy instance, visit Strategy Performance, and Historical, and you'll observe the following :


      • You enter the market at 2892.25, 10 contracts deep (see the NinjaScript Output Window)
      • You sell 5 of these at 2905.00
      • You sell the other 5 at 2909.75
      • By hand, 5(2909.75-2892.25) + 5(2905.00-2892.25) = 151.25
      • Each point is worth $50, so this is a profit of $50(151.25) = $7 562.50
      • This is what shows in the strategy performance window
      • When you then select the percentage display, you see you made 1.05% of your investment
      • This is the value you get mathematically when you divide 151.25 / 2892.25 and add 1, when you round to two decimal places
        • This proves that we are scaling by quantity, since 2892.25 / (10(2909.75-2892.25)) = 6%, not 5%



      Thanks for your assistance in improving NinjaTrader 8
      Hello Jessica,

      Unfortunately, I do not have the time to familiarize myself with the code you sent over and to try to replicate it on my end. However, see attached example which shows exactly what I am talking about. It's a strategy Alan had sent over previously. I amended it to replicate the issue I am talking about. I added a note within the code. Please read it. Then, run the strategy in strategy analyzer, then go to the trade tab and look at the percentage gains. You'll notice what I am talking about.

      Thank you!
      Attached Files

      Comment


        #4
        I appreciate your patience as we work together to improve Ninja.


        When I add some code to print the profit or loss made on each trade - that is scaled by quantity - I end up validating the values used in cumulative net profit and the fact that they appear to be scaling by quantity in accord with their help guide specs.







        I am attaching a modified copy of the code you sent us which does this.

        Using the attached script and the attached historical data, can you identify the trade number that is giving unexpected results? Instructions on importing historical data are in the previous post if needed. If we both use ES 12-18 1 minute bars we will get the trades tab in the attached spreadsheet. No code analysis is needed, just running the scenario with a NinjaScript Output Window open will give us the results on both our ends to talk over.


        Once we know
        • Which trade is giving unexpected values
        • What the expected value should be instead

        It will be easy for our development team to change the behavior of NinjaTrader.


        If this would work better with a remote support session, please contact platformsupport at ninjatrader dot com and reference 1973073 in the subject line and body of your e-mail.
        Attached Files
        Last edited by NinjaTrader_JessicaP; 09-18-2018, 09:59 AM.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_JessicaP View Post
          I appreciate your patience as we work together to improve Ninja.


          When I add some code to print the profit or loss made on each trade - that is scaled by quantity - I end up validating the values used in cumulative net profit and the fact that they appear to be scaling by quantity in accord with their help guide specs.







          I am attaching a modified copy of the code you sent us which does this.

          Using the attached script and the attached historical data, can you identify the trade number that is giving unexpected results? Instructions on importing historical data are in the previous post if needed. If we both use ES 12-18 1 minute bars we will get the trades tab in the attached spreadsheet. No code analysis is needed, just running the scenario with a NinjaScript Output Window open will give us the results on both our ends to talk over.


          Once we know
          • Which trade is giving unexpected values
          • What the expected value should be instead

          It will be easy for our development team to change the behavior of NinjaTrader.


          If this would work better with a remote support session, please contact platformsupport at ninjatrader dot com and reference 1973073 in the subject line and body of your e-mail.
          Hello Jessica,

          Apologies it took me a while. I just had some time to test it in the suggested controlled environment. I did not see any issues on my end for ES.

          However, the same unexpected returns are present for all FX pairs. I don't trade equities at all so most of my usage for NT is FX.

          Test the script you sent over to me on any FX pair. I'm sending you the results i'm seeing on my end on AUDUSD. After you test it for the date range I'm sending over to you, test it in a year worth of date range and then look at the cumulative returns Wouldn't those be nice?

          The cumulative percentage returns are not weighted by quantity. Fortunately, the currency returns are correct.

          I have not had the time to look at your guidelines for display percentage cumulative returns but if it is within your guidelines that the returns are shown as such.. I think it must be changed for FX.

          Problem I am seeing is the same I described in my OP.

          If I purchased 100k worth of AUDUSD with same entry and sold it in 3 increments as shown in the script I sent over to you. The returns being calculated are off the purchase and sold price and are not being weighted by quantity sold at different times. Hence, the returns are insane. I would like to add that I am well aware of data issues that can cause erroneous and outlandish returns, this is not a data issue.. it's how the returns are being calculated.
          Attached Files
          Last edited by staycool3_a; 09-18-2018, 11:37 PM.

          Comment


            #6
            Let's step away from the NinjaScript and just analyze results for a second.


            I'm exporting a trades grid I generated with the attached strategy. The strategy is attached and does just this,
            • Enters 20 000 contracts at 0.7144'4 on the AUDUSD
            • Exits 10 000 of those at 0.7155'6
            • Exits 10 000 of those at 0.7221'4

            It does that on the attached historical data.


            Trusting the above and examining only the results - trade calculations - that we are interested in, my trades tab told me this :


            • The 1st trade made a profit of $11.20 . That's what I get when I multiply 10 000 contracts by the price difference between open (0.71444) and close (0.71556)
            • The cumulative profit so far is just the profit for this trade, $11.20
            • The 2nd trade made a profit of $77.00 which also matches my hand calculations
            • The cumulative profit is now the profit off both trades, $88.20

            Can you analyze the above and tell development what the correct value should be?


            If the above isn't demonstrating what we're after, can you


            1. Run your strategy
            2. Right-click the Trades tab in the Strategy Analyzer
            3. Select Export
            4. Save this as a CSV file (there is a pulldown at the bottom)
            5. Upload that CSV file

            Once you've done that, please double-click your CSV file to open it in excel spreadsheets ( or use a free and publicly available spreadsheet reader like LibreOffice, https://www.libreoffice.org/ ), and let us know these three key pieces of information our development team will need to improve NinjaTrader :



            1. Which row contains an incorrect cumulative net profit
            2. What profit is showing. Letting us know this separately is an added check on item 1 to make sure we are looking at the correct information
            3. What this should be instead, and why

            The spreadsheet will contain all the other information we need, such as what quantity you entered on and what quantity you left on.


            Thanks again for your continued efforts to improve NinjaTrader 8.
            Attached Files
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              @staycool3_a - sorry in advance, not looking to hijack your thread, but I'm looking for a similar feature to what you're asking about.


              @NinjaTrader_JessicaP - two questions:
              1) can transaction logging by-strategy be done for live trading, not just Strategy Analyzer/backtesting?


              2) can the export-to-CSV process be made available as an automatic operation?

              Comment


                #8
                @CB - no worries

                @ Jessica

                The 1st trade made a profit of $11.20 . That's what I get when I multiply 10 000 contracts by the price difference between open (0.71444) and close (0.71556)
                The cumulative profit so far is just the profit for this trade, $11.20
                The 2nd trade made a profit of $77.00 which also matches my hand calculations
                The cumulative profit is now the profit off both trades, $88.20
                Correct. That is correct. As I mentioned previously, the returns that are being calculated in dollar/currency terms are correct. The above calculation is exactly how NT correctly displays the currency returns. This is why I only look at the performance reports in currency terms and not percentage terms.

                Note: Anytime I use "/" in the below, it means I am dividing.

                The problem occurs when cum. profit is being calculated in percentages. The problem occurs when the strategy is buying/selling different quantities. As of right now, the cum. profit are only based on purchased price and sold price. I can only speak for the FX.

                Example 1:

                If you purchased 1m lot of AUDUSD at .70 and sold it at .75. Your net percentage gain based off price is 7.14%. Your net gain for this position in dollar terms is 5 cents*1m, which is 50k. The percentage gain that should be shown in the performance report is 50k/1m, which is 5%. Currently, the percentage returns are only shown as derivatives of price movement alone.

                The reason why you should be showing dollar returns/quantity is because in most strategies the quantity does not stay constant. The purchasing quantity can and does in most cases vary over time.

                Example 2:

                purchased 1m lot of AUDUSD at .7

                sold 200k at .71; $ gain = 2k; percentage gain should be = 2k/1m = .2%
                sold 200k at .72; $ gain = 4k; percentage gain should be = 4k/1m = .4%
                sold 200k at .73; $ gain = 6k; percentage gain should be = 5k/1m = .6%
                sold 400k at .75; $ gain = 20k; percentage gain should be = 20k/1m = 2%

                cumulative return is the some of all percentage gains above. Also, the other thing I think that needs to be thought about is the base currency. AUDUSD is easy to calculate returns but if it were EURJPY, the 1m would need to be converted to USD for clients that use USD as their base currency.

                The way NT is currently calculating the cum % returns in Example 2:

                purchased 1m lot of AUDUSD at .7

                sold 200k at .71; $ gain = 2k; percentage gain = (.71 / .70) -1 = 1%
                sold 200k at .72; $ gain = 4k; percentage gain = (.72 / .70) - 1 = 2%
                sold 200k at .73; $ gain = 6k; percentage gain = (.73 / .70) -1 = 3%
                sold 400k at .75; $ gain = 20k; percentage gain = (.73 / .70) -1 = 4%

                And then it's adding up all the individual trades returns for a cum. return of 11%. Which is clearly not correct way to show returns.

                The reason why the cum. % returns need to be quantity weighted:

                Say you purchased 1m at .5 and sold at 1$ for a 100% gain. Then you purchased 100m at .5 and took a loss and sold at .4 for a loss of 20% price movement. You're broke. Your 100m position far out weighs the 100% gain. Your account is not going to show you a gain of 80%... even though you made a 100% and only lost 20%.

                Run the script I sent you on any fx pair for a period of 2 months in strategy analyzer. Then look at the trade performance report in percentages. You'll see what I'm talking about.
                Last edited by staycool3_a; 09-19-2018, 11:33 PM.

                Comment


                  #9
                  Originally posted by CB001 View Post
                  @staycool3_a - sorry in advance, not looking to hijack your thread, but I'm looking for a similar feature to what you're asking about.


                  @NinjaTrader_JessicaP - two questions:
                  1) can transaction logging by-strategy be done for live trading, not just Strategy Analyzer/backtesting?


                  2) can the export-to-CSV process be made available as an automatic operation?

                  To your 1st question, yes. Right-click your strategy instance in the Strategies tab of the control center, and hover over Strategy Performance. You can view realtime, historical, or both trades from here.


                  To your 2nd question, there are several C# solutions for CSV I/O on the internet. This add-on is a NinjaScript front-end for one of them, and will allow you to automate CSV I/O from NinjaScript.


                  Jessica P.NinjaTrader Customer Service

                  Comment


                    #10
                    staycool3_a I appreciate your patience. I would like to assure you I have run your strategy over forex several times. The reason I keep asking for specifics, and an exported trades grid, is this message in the help guide.





                    Tip: Cumulative profit in % mode will reinvest your profits - as an example let's say you take these 3 trades on a 10K account - 5%, 10%, 7.5% profit or in currency terms you made 500$, 1000$, 750$. The first trade added to the account gives us 10500$, adding the second trade in gives us 10550 (first one of 500$ plus the 10% second trade of 10500$ as base). The third trade of 7.5% of 11550$ gives us 866.25$, so a total of 2386.25$ if we reinvested. Or in % terms we made 23.8625% - while, if we had only summed the individual trades % profits without reinvesting, we would have had 22.5% profit (5% + 10% + 7.5%)
                    Reinvesting profits, and adding percentages, would both cause higher percentage values than currency reports (which do not reinvest), but adding after scaling - as you are reporting - would cause higher values than adding before scaling. I am working to prepare a case, therefore, that shows that we are adding after scaling and not before. I am asking for any assistance you are willing to give while I prepare this case. A trade performance grid from a situation which clearly demonstrates this would be very good evidence, and it would save us time finding market conditions which clearly demonstrate this.


                    While I am continuing to investigate on my end, if it is at all possible to export and attach a trade grid as requested above it would be greatly appreciated and would speed up the investigation process.
                    Last edited by NinjaTrader_JessicaP; 09-20-2018, 08:57 AM.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      Code:
                      Tip: Cumulative profit in % mode will reinvest your profits - as an example let's say you take these 3 trades on a 10K account - 5%, 10%, 7.5% profit or in currency terms you made 500$, 1000$, 750$. The first trade added to the account gives us 10500$, adding the second trade in gives us 10550 (first one of 500$ plus the 10% second trade of 10500$ as base). The third trade of 7.5% of 11550$ gives us 866.25$, so a total of 2386.25$ if we reinvested. Or in % terms we made 23.8625% - while, if we had only summed the individual trades % profits without reinvesting, we would have had 22.5% profit (5% + 10% + 7.5%)
                      Yea example is nullified as soon as you start exiting positions with multiple targets. You can't magically "reinvest" your profits when you exit at first target, second target and so on.

                      I will send you an email Att: Jessica

                      Comment


                        #12
                        This is aside from your report which I am still investigating.


                        I was able to discover some text in the help guide which we will need to change. Thank you for helping us discover this.


                        For your records, the proposed text is the following :

                        Tip : Cumulative profit in % mode will reinvest your profits. As an example, let’s say you take 3 trades on a starting price of $10k. On these trades you made $500, $1000, and $750. The starting percentages for these trades are 5%, 10%, and 7.5%, respectively. On Trade 1, we made 5% of $10k, which is $500. Rolling that into Trade 2, we made 7.5 % of ($10k + $500), or $1050 . Rolling that into trade 3, we made 7.5% of ($10k + $500 + $1050) or $866.25 . This means that after the 3rd trade, we made an extra $866.25. This means our cumulative net profit will show ($500 + $1000 + $750 + $866.25) / $10k or 31% of our investment, and not ($500 + $1000 + $750) / $10k = 22.5% of our investment
                        Note : NT7 was designed to work the same way.


                        All that said, as traders such as yourself will not be expecting percent display to work differently from currency display I have submitted a feature request to change this behavior so that reinvesting is optional, and not limited to percent mode. The text of the feature request is as follows :
                        This customer would like the Cumulative Net Profit's reinvesting behavior to be made optional

                        Disabling this feature would cause percent display mode to display in accord with expectations.
                        Last edited by NinjaTrader_JessicaP; 09-20-2018, 03:44 PM.
                        Jessica P.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by StrongLikeBull, Yesterday, 04:05 PM
                        1 response
                        12 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by Mestor, 03-10-2023, 01:50 AM
                        14 responses
                        375 views
                        0 likes
                        Last Post z.franck  
                        Started by molecool, 10-09-2017, 10:48 AM
                        5 responses
                        1,621 views
                        0 likes
                        Last Post trader-ap  
                        Started by The_Sec, Yesterday, 03:53 PM
                        1 response
                        12 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by mmenigma, Yesterday, 03:25 PM
                        1 response
                        11 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Working...
                        X