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

    #16
    Accurate PnLs for All Trades

    Hi Patrick.

    As mentioned previously, my code is in excess of 20K lines and it will take some considerable effort to reduce it to a repeatable example.

    As I also mentioned, ChelseaB has already confirmed anomalous behaviour in NT7 here: http://ninjatrader.com/support/forum/showthread.php?t=65400

    I would be most grateful for the four definitive working code examples requested so I may use them in situ in my strategy, as is, to see if they work or if there is some other complication.

    Hoping to receive those soon. Many thanks!
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    Comment


      #17
      Hello jeronymite,

      I was able to confirm that Performance.AllTrades.GetTrades does not work for short entries, however, this does not apply to Performance.AllTrades[Performance.AllTrades.Count-1].ProfitCurrency which I am finding works for both long and short trades.

      Attached is an example to demonstrate.
      Attached Files
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Accurate PnLs for All Trades

        Thanks Chelsea! Hopefully this bug will be fixed in the next release of NT7.

        From my perspective though, I still would like NT Support to provide the definitive code I have been requesting:

        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
        I further note that the values I need returned are currency ($xxx.xx). I hope this can be provided easily. Many thanks!
        Multi-Dimensional Managed Trading
        jeronymite
        NinjaTrader Ecosystem Vendor - Mizpah Software

        Comment


          #19
          Hello jeronymite,

          Tracking the unrealized PnL for a position is easy using Position.GetProfitLoss().

          However, tracking the unrealized PnL for individual trades requires that you calculate these values with custom logic.

          Attached is an example that demonstrates calculating the unrealized PnL for individual trades and also shows the realized PnL for the trade once it is exited.
          Attached Files
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            Accurate PnLs for All Trades

            Thanks, Chelsea.

            I have attached the output from running your example, and I must confess I am not sure what to make of it. It doesn't seem to make sense to me. More than that, there are no trace or log file records of any trades as a result of running the script. Trace and log files attached also.

            Your further advice eagerly awaited. Thanks!
            Attached Files
            Multi-Dimensional Managed Trading
            jeronymite
            NinjaTrader Ecosystem Vendor - Mizpah Software

            Comment


              #21
              Accurate PnLs for All Trades

              Hi Chelsea. I made a one line change to your example NinjaScript: I added if (Historical) return; as the first line of OnBarUpdate(). Since I am only interested in realtime trades, this makes sense.

              The output is obviously much shorter, but is still not making complete sense. The realized PnLs seem correct. The unrealized PnLs seem to be almost random and do not reflect any obvious interim PnL.

              I have attached output, trace, log and Account Performance grid dump for the trades.

              Looking forward to your further advice and an updated example, hopefully, that shows correct $xxx.xx values for all four circumstances requested.

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

              Comment


                #22
                Accurate PnLs for All Trades

                Hi Chelsea. Wondering if you have been able to recreate the issues I saw with this example and have reworked code to address them? Thanks!
                Multi-Dimensional Managed Trading
                jeronymite
                NinjaTrader Ecosystem Vendor - Mizpah Software

                Comment


                  #23
                  Hello jeronymite,

                  This script was made using a future instrument.

                  Forex is calculated different, as the point value and tick size are applied to a full lot not a single contract.

                  Attached is an updated version that has a separate calculation for calculating forex PnL.
                  Attached Files
                  Last edited by NinjaTrader_ChelseaB; 05-21-2017, 05:22 PM.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Accurate PnLs for All Trades

                    Thanks, Chelsea.

                    Some comments:
                    • I presume an accurate PnL needs to be calculated from the appropriate DataSeries: Close[0] must be the Close of the Ask DataSeries for Sell Short trades and the Bid DataSeries for Long trades.
                    • For all trades, the Close and AvgFillPrice values appear to be rounded in accordance with TickSize -- this significantly compromises the accuracy of manual PnL calculations as TickSize increases, and even more so as Quantity also increases.
                    • For open trades, Position[n].GetProfitLoss calculates the position PnL correctly for Long and Short trades, but not a per trade PnL (unless, of course, the position is comprised of only one trade)
                    • For open trades, (Order.AvgFillPrice-Close[0])*PointValue*Quantity*TickSize*0.1 calculates values wrongly for Short trades and especially so when TickSize is TenthPip. The same applies to Long trades with the formula changed to (Close[0]-Order.AvgFillPrice)*PointValue*Quantity*TickSize*0 .1
                    • For closed trades, Performance.AllTrades[n].ProfitCurrency*Performance.AllTrades[n].Quantity correctly calculates the per trade PnL for Long and Short trades
                    My output from various runs of the script, modified to include the PnL in $xxx.xx format, attached. I have added commission data for NT FXCM to my configuration, and the account is Sim101.

                    Most grateful for your further investigation and, hopefully, truly definitive and accurate solution. Thanks!
                    Attached Files
                    Last edited by jeronymite; 07-08-2015, 01:46 AM.
                    Multi-Dimensional Managed Trading
                    jeronymite
                    NinjaTrader Ecosystem Vendor - Mizpah Software

                    Comment


                      #25
                      Hi jeronymite,

                      Yes, that would be best for accurate fills. When using forex data, the bid data is used as the last price and ask data is not included.

                      This means that a live buy order can fill at the ask in realtime data but cannot in historical. So yes, I would recommend you include an ask series and both use this place buy orders to that BarsInProgress as well as use this price to get an accurate price for calculating PnL.

                      Each trade does have to made at an exact tick size (an exchange will only fill an order at the tick size). The AvgFill price, however, is not rounded to the tick size. I made a quick video to confirm this. In the video I print the avg fill price of the order as it fills and the amount digits to the right of the decimal are constantly changing as we get fills at different prices.

                      http://screencast.com/t/xvCDheJ06

                      You are correct, Positions[n].GetProfitLoss() and Position.GetProfitLoss() will return the pnl for the position not individual trades.

                      Also, I was not applying the conversion rate for forex. I've updated the file to account for this using an unsupported method to get the conversion rate.

                      However, I am not finding any differences between short and long trades. I am seeing both these calculated the same.

                      Last, I have your output, but I have no idea what code made this output.
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #26
                        Since I've been through in those requests, I'd suggest to test for general purpose and reliable real-time info, this variable:

                        - Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit

                        Test to substitute AllTrades by RealtimeTrades, it's possible to see some differences, cause RealtimeTrades is for that session.

                        Regards

                        Comment


                          #27
                          Accurate PnLs for All Trades

                          Thanks, pstrusi.

                          Yes, that can provide a summary of cumulative profit, but what I need is a reliable per trade (open/closed, long/short) PnL on demand. In other words, I need to know my PnL "right now" for any individual trade -- the trade may be open or closed, long or short. This PnL information will be generated by an event handler that may be triggered on demand during an open trade and on trade close.

                          Unfortunately, there are some known bugs using the documented approach in the current version of NT7 when calculating this for short trades. Hence my persistence with requesting definitive reliable accurate code to do this for any individual trade, long or short, at any time on demand in the trade's lifecycle.
                          Multi-Dimensional Managed Trading
                          jeronymite
                          NinjaTrader Ecosystem Vendor - Mizpah Software

                          Comment


                            #28
                            I understand. Well, for that specific request, it seems that you should a snippet, working with matrix that you can feed on and extract info on demand. It doesn't seem too complex but certainly it could be buggy at first.

                            Comment


                              #29
                              Accurate PnLs for All Trades

                              Thanks for the updated code, Chelsea.

                              I had already adapted your original code to provide a more detailed view and I integrated the changes in your new code into my adaptation. I attach that updated adapted code. I ran the code and so I also attach the raw Output and an Excel spreadsheet with some minor processing of the data.

                              The data are still very problematic:
                              • Commissions also play into this and I have included them in the code
                              • NT7 TickSize (Pip, HalfPip and TenthPip) dramatically affect the data
                              • My broker is NT FXCM and FXCM uses microlots, which also needs to be taken into account, given that it is not a selectable tick size (it would presumably be called HundredthPip if it were)
                              • The only TickSize that seems to work as one would want is Pip -- everything else, including a manually forced use of microlots as the TickSize, seems to fail to produce accurate data
                              • So, although the highly relevant introduction of Rate is essential and gets us closer to a universally accurate and reliable solution, there are still major gaps to be overcome
                              To my mind, there are two aspects to this:
                              1. Provide a universally accurate and reliable way to obtain per trade PnL and Commission data for open Long and Short trades.
                              2. Fix the bugs in the documented methods so the supported interfaces actually work as documented.
                              Still hoping for a working solution, Chelsea, and still most grateful for your kind attention to this. Thanks!
                              Attached Files
                              Multi-Dimensional Managed Trading
                              jeronymite
                              NinjaTrader Ecosystem Vendor - Mizpah Software

                              Comment


                                #30
                                Hello jeronymite,

                                The calculation in my script does not account for commissions or other pip sizes.

                                However, I was wanting to create a basic demonstration of how calculating unrealized PnL per trade could be done (mostly to show that its possible). As far as coming up with all of the logic and calculations you are requesting, I can provide you with a list of consultants who would be happy to assist you in creating this logic.

                                Would you like this list?

                                Further, I would also be happy to submit a feature request in your behalf for a NinjaScript method that returns the unrealized PnL for an individual trade as part of the IOrder / Order classes.

                                Would you like me to do this?

                                Which bugs are you referring to that need to be corrected? (If you are referring to long and short differences, I am not able to reproduce this. Are you running the script that I provided for you, or your own script? Is the script that I created for you not working for shorts? I am finding on my end it works the same for shorts as for longs.)
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Spiderbird, Today, 12:15 PM
                                2 responses
                                8 views
                                0 likes
                                Last Post Spiderbird  
                                Started by WHICKED, Today, 12:45 PM
                                0 responses
                                3 views
                                0 likes
                                Last Post WHICKED
                                by WHICKED
                                 
                                Started by FrazMann, Today, 11:21 AM
                                2 responses
                                6 views
                                0 likes
                                Last Post NinjaTrader_ChristopherJ  
                                Started by rjbtrade1, 11-30-2023, 04:38 PM
                                2 responses
                                80 views
                                0 likes
                                Last Post DavidHP
                                by DavidHP
                                 
                                Started by lorem, Yesterday, 09:18 AM
                                5 responses
                                18 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Working...
                                X