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

Reporting Incorrect Profit Loss

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

    Reporting Incorrect Profit Loss

    Hello,

    I am attempting to put logic into some scripts to monitor and react to individual strategy profit/loss or account profit/loss. I am running into issues where, seemingly randomly, I will get an inaccurate number for, I believe the account and strategy unrealized P/L. I think it is only for the unrealized P/L as I can not accurately reproduce when this happens and I have been having a hard time tracking down exactly where the issue is.

    A simplified version of the code is below. Can anyone tell me if this is generally the correct or recommended way to be getting account/strategy PL updates?

    Thank you,


    Code:
    private Account _account;
    private double _strategyPl = 0;
    private double _accountPl = 0;
    private double _strategyPeakPl = 0;
    private double _accountPeakPl = 0;
    ​
    protected override void OnAccountItemUpdate(Cbi.Account account, Cbi.AccountItem accountItem, double value)
    {
         //initialize account object
         if (_account == null)
         {
              _account = account;
         }
    }
    
    
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
         //make sure account object is initialized
         if (_account == null)
         {
              return;
         }
    
         // get Account P&L
         var accountRealizedPl = _account.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar);
         var accountUnrealizedPl = _account.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);
    
         //get strategy P&L
         var strategyUnrealizedPl = PositionAccount.GetUnrealizedProfitLoss(Performanc eUnit.Currency);
         var strategyTotalPl = SystemPerformance.RealTimeTrades.TradesPerformance .NetProfit + strategyUnrealizedPl;
    
         //update globals
         _strategyPl = strategyTotalPl;
         _accountPl = accountRealizedPl + accountUnrealizedPl;
    
         //update peak strategy PL
         if (_strategyPl > _strategyPeakPl)
         {
              _strategyPeakPl = _strategyPl;
         }
    
         //update peak account PL
         if (_accountPl > _accountPeakPl)
         {
         _accountPeakPl = _accountPl;
         }
    }​

    #2
    Hello HypoXic5665,

    Thank you for your post.

    Rather than subscribing to OnMarketData(), especially if it is not necessary because it can be CPU intensive, you should be able to get the desired information from OnAccountItemUpdate() and/or OnAccountStatusUpdate().

    You may utilize the account class from a script to subscribe to account related events and access account related information. For an overview of the methods and properties available from the Account class:You mentioned both individual strategy profit/loss and account profit/loss overall. To get account information overall, you could subscribe to AccountItemUpdate for each account you would like to check:Then, you could check for updates to account items such as AcccountItem.GrossRealizedProfitLoss or AccountItem.UnrealizedProfitLoss. A full list of items that are updated via OnAcountItemUpdate() may be found here:You could also subscribe to status updates from all accounts rather than just individual accounts using AccoutnStatusUpdate:In order to get the PnL from a specific strategy, you could do something like the loop shown in the example on the following page combined with GetUnrealizedProfitLoss():Here is a modified version of that example:
    Code:
    private Account myAccount;
    
    protected override void OnStateChange()
    {
      if (State == State.SetDefaults)
      {
          // Initialize myAccount
      }
    
    }
    
    private void OnAccountStatusUpdate(object sender, AccountStatusEventArgs e)
    {
      foreach (StrategyBase strategy in myAccount.Strategies)
      {
          Print(String.Format("Account status updated. {0} strategy applied with position {1}", strategy.Name, strategy.Position));
          // print out the unrealized PnL for the strategy position
          Print("Unrealized PnL for strategy position: " + strategy.Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency);
      }
    }
    ​
    Using these ideas should help you to get accurate account and strategy PnL information. Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by SaltyCoffee, Today, 01:13 AM
    0 responses
    3 views
    0 likes
    Last Post SaltyCoffee  
    Started by FishTrade, 05-13-2024, 11:11 PM
    3 responses
    13 views
    0 likes
    Last Post FishTrade  
    Started by Graci117, Yesterday, 09:02 PM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by ETFVoyageur, Yesterday, 07:55 PM
    0 responses
    9 views
    0 likes
    Last Post ETFVoyageur  
    Started by janio973, Yesterday, 07:24 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X