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

Accurate PnLs for All Trades

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

    Accurate PnLs for All Trades

    There are some known issues in NT7 with obtaining PnLs for individual SHORT and sometimes LONG trades. For example, the following pseudo-code examples illustrate the matter:

    Code:
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Performance.RealtimeTrades.GetTrades(<Instrument>,<EntrySignal>,[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]).TradesPerformance.Currency.AvgProfit ;[/SIZE][/FONT]
    [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Works for LONG during trade, but not for SHORT. Does not always work after trade ends.[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
     
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Performance.RealtimeTrades[n].Commission ;[/SIZE][/FONT]
    [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Works for both after trade ends.[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
     
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Performance.RealtimeTrades[n].ProfitCurrency ;[/SIZE][/FONT]
    [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Does not work for LONG or SHORT after trade ends.[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
     
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Performance.RealtimeTrades[n].ProfitPercent ;[/SIZE][/FONT]
    [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Does not work for LONG or SHORT after trade ends.[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
     
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit ;[/SIZE][/FONT]
    [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Works for both after trade ends.[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
     
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Positions[n].GetProfitLoss(<MarketPrice>,PerformanceUnit.Currency) ;[/SIZE][/FONT]
    [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Works for both LONG and SHORT during trades, but not after trade ends.[/COLOR][/SIZE][/FONT]
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
    "Does not work" means returns zero values.

    How can one absolutely reliably, and as simply as possible, obtain realtime trade PnLs as Currency for individual trades during the trades and after the end of trades for both LONG and SHORT?

    Thanks!
    Last edited by jeronymite; 06-19-2015, 01:51 AM.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    #2
    Hello jeronymite,

    Thank you for your post.

    During the trade the PnL is considered Unrealized and would require pulling Position.GetProfitLoss(): http://www.ninjatrader.com/support/h...profitloss.htm
    When the trade ends the PnL is considered realized and would require pulling Performance.AllTrades.TradesPerformance.Currency.C umProfit: http://www.ninjatrader.com/support/h.../cumprofit.htm

    Performance.RealtimeTrades.GetTrades(<Instrument>, <EntrySignal>,1).TradesPerformance.Currency.AvgPro fit ;
    // Works for LONG during trade, but not for SHORT. Does not always work after trade ends.
    This returns the average profit of the real-time trades in the trades collection, this is not Realized not Unrealized profit but an average of the profit for the real-time trades.

    Performance.RealtimeTrades[n].ProfitCurrency ;
    // Does not work for LONG or SHORT after trade ends.

    Performance.RealtimeTrades[n].ProfitPercent ;
    // Does not work for LONG or SHORT after trade ends.
    This will pull the profit in currency or percent for the trade 'n' in the collection. You may wish to look into 'n'. Is it a valid value for the collection for example?

    Positions[n].GetProfitLoss(<MarketPrice>,PerformanceUnit.Curre ncy) ;
    // Works for both LONG and SHORT during trades, but not after trade ends.
    Correct, this is Unrealized PnL. So after the trade ends there is no Unrealized PnL.

    Please let me know if you have any questions.

    Comment


      #3
      Accurate PnLs for All Trades

      Thanks, Patrick.

      Please reference this thread http://www.ninjatrader.com/support/f...ad.php?t=65400 where I have explored this previously. As you will see from this thread, it has previously been confirmed that there are problems with getting information for Short trades.

      Simply, can you provide tested and proven code that always and reliably:
      • Provides Unrealized PnL of a realtime trade (not position) during the trade by specifying Entry Signal
      • Provides Realized PnL of a realtime trade (not position) after the trade closes by specifying Entry Signal
      • Provides Commission of a realtime trade (not position) after the trade closes by specifying Entry Signal
      • Works for both Long and Short realtime trades
      Many thanks!
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Hello jeronymite,

        You are going to need to explain what a "realtime trade not a position" is.

        Comment


          #5
          Accurate PnLs for All Trades

          Sorry, Patrick. Please correct me if I have misunderstood anything.

          By "realtime trade (not position)", I mean a single order submission on a single instrument. Now that may have been filled by multiple part fills, but ultimately the submission constitutes a trade on an instrument. In my situation, one such submission is the limit of the open order submissions for any one instrument at any time; although obviously, in other environments, there may be multiple independent order submissions for any one instrument that all coexist.

          I distinguish these "trades" from the "position", even though in practice, in this case, for any one instrument, there is no practical difference. But as a general principle, the distinction allows one to manage individual orders submissions within a position individually.

          I also distinguish an instrument position from an overall strategy position; and hence, my emphasis on the "trade" for any one instrument, rather than the "position", which may be interpreted as that for an instrument or the whole strategy.

          Again, perhaps I am splitting hairs (or just plain confused), but in any case, what I want to be able to do is the list of things previously provided for a "trade" as I have described it, or if you prefer, the position of any one instrument.

          Since NinjaTrader speaks of both "RealtimeTrades" and "Position", perhaps it may be simpler to think of it as a RealtimeTrade (be it Long or Short, or open or closed)?

          Your comments/advice/input/code most appreciated.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment


            #6
            Hello jeronymite,

            Thank you for your response.

            NinjaTrader treats trades as an opening order and closing order. The trade is not marked until the closing order is filled.

            Once an order fills that places the strategy into a position you can then track that PnL as Unrealized PnL. Realized PnL is not tracked until the closing order fills to complete the trade.

            If you need information differently then how NinjaTrader tracks it you would need to develop your own logic to do so.

            Although, I do not fully understand your version of a 'trade' or 'position' from an order waiting to fill or and order that has filled and thus created a position. Perhaps further details are needed.

            Comment


              #7
              Accurate PnLs for All Trades

              Thanks, Patrick. Apologies if my descriptions obscure the obvious.

              In NinjaScript terms, I need the following:
              • Do EnterLong OR EnterShort with unique EntrySignal
              • Get accurate unrealized PnL on active trade, Long or Short
              • Do ExitLong OR ExitShort
              • Get accurate realized PnL on closed trade, Long or Short
              Hopefully, that is a clearer explanation.

              Thanks again for tested and proven working code to get these data!
              Multi-Dimensional Managed Trading
              jeronymite
              NinjaTrader Ecosystem Vendor - Mizpah Software

              Comment


                #8
                Hello,

                For the Unique entries, you can use the CurrentBar or other variable that is unique such as a DateTime.Now in the SignalName to define a unique signal. Something like the following should work for unique:

                EnterLong("tag" + CurrentBar)


                For the Pnl related questions, these look to have been answered in a prior post by Patrick. The Unrealized can be tracked using the built in methods but for the Realized, if this is not after the trade closes, you would likely need to create logic that calculates this from information you supply it. if this will be when the trade closes you could potentially store the value of the RealizedPnL when the trade enters and then check the difference after it has closed using the CumProfit in TradesPerformance.



                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Accurate PnLs for All Trades

                  ... ... it must be my Australian English that's the problem.

                  When I said:

                  In NinjaScript terms, I need the following:
                  • Do EnterLong OR EnterShort with unique EntrySignal
                  • Get accurate unrealized PnL on active trade, Long or Short
                  • Do ExitLong OR ExitShort
                  • Get accurate realized PnL on closed trade, Long or Short
                  Hopefully, that is a clearer explanation.

                  Thanks again for tested and proven working code to get these data!
                  I was really saying "this is what I am doing and the bits I want are the second and fourth bullets".

                  So, let me try once more:
                  • As quoted above, that is what I am currently doing -- I am entering a trade with a unique EntrySignal and I am exiting it -- absolutely no problem there.
                  • When I am in the trade, be it Long or Short, I need to reliably know the unrealized PnL
                  • When I exit the trade, I need to reliably know the realized PnL
                  • There is a known bug in NT7 whereby unrealized PnL data for Short trades is zero; for Long trades it appears to work
                  So, by way of a definitive complete solution (four situations: for each of Long and Short, both in a trade and after a trade has ended) I am asking for you to provide (or re-provide, if you would be so kind) tested and proven code for each of those situations, taking into account the known bug for Short trade performance data.

                  Many thanks!
                  Multi-Dimensional Managed Trading
                  jeronymite
                  NinjaTrader Ecosystem Vendor - Mizpah Software

                  Comment


                    #10
                    Accurate PnLs for All Trades

                    Further to this, by way of identifying other potential bugs and issues with obtaining accurate PnL information, here are the data of 5 consecutive trades obtained using Performance.RealtimeTrades[n] after trade completion:
                    Code:
                    [FONT=Courier New]TRADE # 4[/FONT]
                    [FONT=Courier New]Instrument = $AUDUSD[/FONT]
                    [FONT=Courier New]Entry.MarketPosition = Long[/FONT]
                    [FONT=Courier New]Quantity = 227000[/FONT]
                    [FONT=Courier New]Entry.Price = 0.770540[/FONT]
                    [FONT=Courier New]Exit.Price = 0.770580[/FONT]
                    [FONT=Courier New]ProfitCurrency = $0.00[/FONT]
                    [FONT=Courier New]ProfitPercent = -0.000052[/FONT]
                    [FONT=Courier New]ProfitPoints = -0.000040[/FONT]
                    [FONT=Courier New]Commission = $18.16[/FONT]
                     
                    [FONT=Courier New]TRADE # 3[/FONT]
                    [FONT=Courier New]Instrument = $AUDUSD[/FONT]
                    [FONT=Courier New]Entry.MarketPosition = Long[/FONT]
                    [FONT=Courier New]Quantity = 287000[/FONT]
                    [FONT=Courier New]Entry.Price = 0.770560[/FONT]
                    [FONT=Courier New]Exit.Price = 0.770520[/FONT]
                    [FONT=Courier New]ProfitCurrency = $0.00[/FONT]
                    [FONT=Courier New]ProfitPercent = -0.000156[/FONT]
                    [FONT=Courier New]ProfitPoints = -0.000120[/FONT]
                    [FONT=Courier New]Commission = $22.96[/FONT]
                     
                    [FONT=Courier New]TRADE # 2[/FONT]
                    [FONT=Courier New]Instrument = $NZDJPY[/FONT]
                    [FONT=Courier New]Entry.MarketPosition = Long[/FONT]
                    [FONT=Courier New]Quantity = 442000[/FONT]
                    [FONT=Courier New]Entry.Price = 85.309000[/FONT]
                    [FONT=Courier New]Exit.Price = 85.291000[/FONT]
                    [FONT=Courier New]ProfitCurrency = $0.00[/FONT]
                    [FONT=Courier New]ProfitPercent = -0.000385[/FONT]
                    [FONT=Courier New]ProfitPoints = -0.032865[/FONT]
                    [FONT=Courier New]Commission = $53.04[/FONT]
                     
                    [FONT=Courier New]TRADE # 1[/FONT]
                    [FONT=Courier New]Instrument = $NZDJPY[/FONT]
                    [FONT=Courier New]Entry.MarketPosition = Long[/FONT]
                    [FONT=Courier New]Quantity = 352000[/FONT]
                    [FONT=Courier New]Entry.Price = 85.319000[/FONT]
                    [FONT=Courier New]Exit.Price = 85.305000[/FONT]
                    [FONT=Courier New]ProfitCurrency = $0.00[/FONT]
                    [FONT=Courier New]ProfitPercent = -0.000338[/FONT]
                    [FONT=Courier New]ProfitPoints = -0.028866[/FONT]
                    [FONT=Courier New]Commission = $42.24[/FONT]
                     
                    [FONT=Courier New]TRADE # 0[/FONT]
                    [FONT=Courier New]Instrument = $AUDUSD[/FONT]
                    [FONT=Courier New]Entry.MarketPosition = Long[/FONT]
                    [FONT=Courier New]Quantity = 421000[/FONT]
                    [FONT=Courier New]Entry.Price = 0.770390[/FONT]
                    [FONT=Courier New]Exit.Price = 0.770360[/FONT]
                    [FONT=Courier New]ProfitCurrency = $0.00[/FONT]
                    [FONT=Courier New]ProfitPercent = -0.000143[/FONT]
                    [FONT=Courier New]ProfitPoints = -0.000110[/FONT]
                    [FONT=Courier New]Commission = $33.68[/FONT]
                    Looking at the data, it is obvious that there are some anomalies:
                    • ProfitCurrency is zero in all cases
                    • Assuming that ProfitPoints is meant to be the difference between Entry.Price and Exit.Price the calculation seems wrong in most cases
                    Most grateful for your continued attention to this overall matter and this additional information.

                    Many thanks!
                    Multi-Dimensional Managed Trading
                    jeronymite
                    NinjaTrader Ecosystem Vendor - Mizpah Software

                    Comment


                      #11
                      Hello jeronymite,

                      Thank you for your response.

                      I am not seeing the same behavior on my end. Do you have a test script we could use to recreate this?

                      Comment


                        #12
                        Accurate PnLs for All Trades

                        Thanks for testing this, Patrick -- much appreciated.

                        I'll see if I can reduce more than 20,000 lines of code into a workable and repeatable example for you to play with -- it may take me a little while, I'm sorry.

                        In the meantime, may I trouble you to provide the tested code for the 4 cases I've listed below, so that I can ensure I'm following the definitive recommended practice, in light of known issues in NT7 with Short trades.

                        Many thanks!
                        Multi-Dimensional Managed Trading
                        jeronymite
                        NinjaTrader Ecosystem Vendor - Mizpah Software

                        Comment


                          #13
                          Hello jeronymite,

                          My test script is attached.
                          Attached Files

                          Comment


                            #14
                            Accurate PnLs for All Trades

                            Many thanks again for your kind support, Patrick.

                            Just to reiterate, I need a working example containing all of the following four situations, where all trades are submitted with unique EntrySignals:
                            • Current unrealized PnL of any particular open Long trade
                            • Current unrealized PnL of any particular open Short trade
                            • Realized PnL of any particular closed Long trade
                            • Realized PnL of any particular closed Short trade
                            Looking forward to your definitive code solution.

                            Many thanks!
                            Multi-Dimensional Managed Trading
                            jeronymite
                            NinjaTrader Ecosystem Vendor - Mizpah Software

                            Comment


                              #15
                              Hello jeronymite,

                              Do you have a test script that produces your irrregularities?

                              Originally posted by NinjaTrader_PatrickH View Post
                              Hello jeronymite,

                              Thank you for your response.

                              I am not seeing the same behavior on my end. Do you have a test script we could use to recreate this?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by funk10101, Today, 08:14 AM
                              1 response
                              2 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by trilliantrader, Today, 08:16 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by bill2023, Yesterday, 08:51 AM
                              3 responses
                              20 views
                              0 likes
                              Last Post bltdavid  
                              Started by yertle, Today, 08:38 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post yertle
                              by yertle
                               
                              Started by Mestor, 03-10-2023, 01:50 AM
                              15 responses
                              379 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X