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,
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;
}
}

Comment