Announcement

Collapse
No announcement yet.

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.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    559 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    546 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X