Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Proposed Unrealized PnL Change

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

    Proposed Unrealized PnL Change

    I have written "watchdog" code in an unmanaged strategy that compares internal accounting of the quantity long or short, Realized PnL, and Unrealized PnL with values calculated by the NinjaTrader platform to guard against accidentally "winning the lottery" or "losing the farm" if an error causes something wild to happen, such as overfills, unintentional pyramiding, or whatever. That has caused me to realize that platform Unrealized PnL calculations seem to be based on last trading prices rather than current bid or ask prices. Of course, true Unrealized PnL values are current liquidation values, rather values based on previous trading.

    If platform Unrealized PnL values are in fact not current liquidation values, I propose a change to make them so.

    #2
    Hello caveat_lector,

    Are you using Position.GetUnrealizedProfitLoss(PerformanceUnit unit, [double price])?

    Have you supplied the GetCurrentAsk() or GetCurrentBid() to this method and you are finding the last price is used and not the price you have submitted?
    https://ninjatrader.com/support/help...profitloss.htm

    Position.GetUnrealizedProfitLoss is for the strategy position, not the account.

    If you want the account unrealized you will need to use the Account.Get().
    https://ninjatrader.com/support/helpGuides/nt8/get.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I am using 15-second bars as Primary and the associated tick series as Secondary.

      This code is at beginning of OnBarUpdate():
      Code:
              protected override void OnBarUpdate() {
      
                  if (State == State.Historical)
                      return;
      
                  if (BarsInProgress == 0) {
      
                      // Wait until there are enough bars for reliable indicator signals.
                      if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
                          return;
      
                      if (CurrentPosition > 0)
                          UnRealizedPnL = PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, GetCurrentBid(1));
                      else if (CurrentPosition < 0)
                          UnRealizedPnL = PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, GetCurrentAsk(1));
                      else
                          UnRealizedPnL = 0;
      
                      TotalPnL = RealizedPnL + UnRealizedPnL;
      
                      if (DisplayCurrentPosition)
                          Draw.TextFixed(this,
                              "CurrentPosition", "CurrentPosition = " + CurrentPosition
                              + "  RealizedPnL = " + RealizedPnL.ToString("0.00")
                              + "  UnRealizedPnL = " + UnRealizedPnL.ToString("0.00")
                              + "  TotalPnL = " + TotalPnL.ToString("0.00"),
                              TextPosition.BottomLeft, ChartControl.Properties.ChartText,
                              ChartControl.Properties.LabelFont, (TotalPnL >= 0)?Brushes.Green:Brushes.Red, Brushes.Transparent, 0);
      This is the OnPositionUpdate code:
      Code:
              // This is called whenever the strategy position state changes.
              protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition) {
      
                  if (position.MarketPosition == MarketPosition.Long) {
                      CurrentPosition = quantity;
                  } else if (position.MarketPosition == MarketPosition.Short) {
                      CurrentPosition = -quantity;
                  } else {
                      CurrentPosition = 0;
                      UnRealizedPnL = 0;
                      RealizedPnL += SystemPerformance.AllTrades[SystemPerformance.AllTrades.TradesCount-1].ProfitCurrency;
                  }
              }
      Last edited by caveat_lector; 04-09-2020, 10:28 AM.

      Comment


        #4
        Hello caveat_lector,

        So you are using the ask and bid of the previous bar and not the current snap shot.

        Are the ask and bid of the previous bar different than each other?

        If yes, does the PositionAccount.GetUnrealizedProfitLoss() return the same value as last for both of these calls?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          The 1 in GetCurrentBid(1) references the Secondary tick series, not the previous bar.

          The ask and bid values that I retrieve are different than each other.

          Comment


            #6
            Hello caveat_lector,

            Apologies, over-looked that.

            The values of the bid and ask are different, however the GetUnrealizedProfitLoss() is returning the same value for both calls with ask vs bid?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              No, the dollar values returned by GetUnrealizedProfitLoss() usually differ depending on whether Close[0], GetCurrentBid(1) or GetCurrentAsk(1) is used. Of course, there are occasions where the last 15-second bar close happens to be the same as a current bid or ask price, but usually they are different. If I use Close[0], the UnrealizedPnL value that I display is usually identical to what NinjaTrader shows in its Accounts tab. There are sometimes instantaneous differences that are probably due to processing delay differences, but usually they are identical. However, if I use bid and ask prices the two displayed values are only occasionally identical.

              To more specifically answer your question as asked; No, GetUnrealizedProfitLoss() always returns different values if requests based on bid and ask are made consecutively.
              Last edited by caveat_lector; 04-09-2020, 12:09 PM.

              Comment


                #8
                Hello caveat_lector,

                Ah, you are wanting the Accounts tab of the Control Center UnrealizedPnL to use the bid and ask and not the custom strategies position unrealized pnl.

                I will submit a feature request.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  UnrealizedPnL always should be based on bid and ask prices regardless of whether the calculation is for a single custom strategy position or an entire account.

                  I started this thread in Platform Technical Support, because it looked to me like a platform miscalculation issue, rather than a strategy-related issue.
                  Last edited by caveat_lector; 04-09-2020, 12:32 PM.

                  Comment


                    #10
                    Hello caveat_lector,

                    I was going off post #1 which is specifically mentioning a ninjascript strategy and makes no mention of the control center or the account.

                    I will move this back to Platform Technical.
                    Last edited by NinjaTrader_ChelseaB; 04-09-2020, 03:09 PM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello caveat_lector,

                      Our development has pointed to the Tools > Options > Trading > Use last price for PnL. When unchecked the ask and bid will be used for PnL calculations.

                      https://ninjatrader.com/support/help...ns_trading.htm
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for checking with the development team and reporting back. That option provides a way to correct the problem. However, it seems to me that the default should be to display PnL values based on current liquidation values with the option being to misleadingly base them on last trade prices. I understand that trading novices may not realize the difference and expect PnL values to be based on last trade prices, but they should be educated rather than provided with a trading system that validates their false assumption.
                        Last edited by caveat_lector; 04-30-2020, 07:46 AM.

                        Comment


                          #13
                          Hello caveat_lector,

                          I will submit your request for Use last price for PnL to be unchecked by default.

                          Once I have a tracking ID for this request I will post in this thread for future reference.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello caveat_lector,

                            I've received tracking ID# SFT-4811 for this request have Use last price for PnL unchecked.

                            As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted in the Release Notes page of the Help Guide.

                            Release Notes — https://ninjatrader.com/support/help...ease_notes.htm
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by alrafer, Today, 05:39 AM
                            0 responses
                            1 view
                            0 likes
                            Last Post alrafer
                            by alrafer
                             
                            Started by varun12343, Today, 03:09 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Marco  
                            Started by Lance El Camino, Yesterday, 04:14 PM
                            2 responses
                            27 views
                            0 likes
                            Last Post NT-Roland  
                            Started by MatthewLesko, Yesterday, 11:13 PM
                            1 response
                            15 views
                            0 likes
                            Last Post NT-Roland  
                            Started by kevinenergy, Today, 01:44 AM
                            0 responses
                            8 views
                            0 likes
                            Last Post kevinenergy  
                            Working...
                            X