public void SettlementDateEvent(PostingEngineEnvironment env, Transaction element) { if (env.TaxLotStatus.ContainsKey(element.LpOrderId)) { var taxlot = env.TaxLotStatus[element.LpOrderId]; var tradeCurrency = element.TradeCurrency; var settleCurrency = element.SettleCurrency; var allocations = env.FindTradeAllocations(element); var buy = allocations.Where(i => i.SecurityType.Equals("SPOT") && i.Side.Equals("BUY")).FirstOrDefault(); var sell = allocations.Where(i => i.SecurityType.Equals("SPOT") && i.Side.Equals("SELL")).FirstOrDefault(); if (buy == null || sell == null) { Logger.Error($"Unable to process {element.SecurityType}::{element.LpOrderId}"); return; } var accountSell = new AccountUtils().CreateAccount(atSettledCash, listOfTradeTags, sell); var accountBuy = new AccountUtils().CreateAccount(atSettledCash, listOfTradeTags, buy); new AccountUtils().SaveAccountDetails(env, accountSell); new AccountUtils().SaveAccountDetails(env, accountBuy); var sellFx = FxRates.Find(env.ValueDate, sell.TradeCurrency); var buyFx = FxRates.Find(env.ValueDate, buy.TradeCurrency); var sellValue = sell.Quantity * sellFx.Rate; var buyValue = buy.Quantity * buyFx.Rate; if (element.IsBuy()) // BUY { var realizedPnl = buyValue + sellValue; var debit = new Journal(accountBuy, Event.SETTLED_CASH, env.ValueDate) { Source = element.LpOrderId, Fund = env.GetFund(element), FxCurrency = element.TradeCurrency, Symbol = element.Symbol, SecurityId = element.SecurityId, Quantity = Convert.ToDouble(buy.Quantity), FxRate = buyFx.Rate, StartPrice = 0, EndPrice = 0, Value = env.SignedValue(accountBuy, accountSell, true, buyValue), CreditDebit = env.DebitOrCredit(accountBuy, buy.Quantity), }; var credit = new Journal(accountSell, Event.SETTLED_CASH, env.ValueDate) { Source = element.LpOrderId, Fund = env.GetFund(element), FxCurrency = element.SettleCurrency, Symbol = element.Symbol, SecurityId = element.SecurityId, Quantity = Convert.ToDouble(sell.Quantity), FxRate = sellFx.Rate, StartPrice = 0, EndPrice = 0, Value = env.SignedValue(accountBuy, accountSell, true, sellValue), CreditDebit = env.DebitOrCredit(accountSell, sell.Quantity), }; env.Journals.AddRange(new[] { credit, debit }); var originalAccount = AccountUtils.GetDerivativeAccountType(realizedPnl); // Realized Pnl to go along with the Settled Cash CommonRules.GenerateJournalEntry(env, element, listOfTags, realizedAccountType, Event.REALIZED_PNL, realizedPnl); CommonRules.GenerateJournalEntries(env, element, listOfTags, originalAccount, "Change in Unrealized Derivatives Contracts at Fair Value", realizedPnl * -1); } else // SELL { var realizedPnl = buyValue + sellValue; var debit = new Journal(accountBuy, Event.SETTLED_CASH, env.ValueDate) { Source = element.LpOrderId, Fund = env.GetFund(element), FxCurrency = element.TradeCurrency, Symbol = element.Symbol, SecurityId = element.SecurityId, Quantity = Convert.ToDouble(buy.Quantity), FxRate = buyFx.Rate, StartPrice = 0, EndPrice = 0, Value = env.SignedValue(accountBuy, accountSell, true, buy.Quantity * buyFx.Rate), CreditDebit = env.DebitOrCredit(accountBuy, buy.Quantity), }; var credit = new Journal(accountSell, Event.SETTLED_CASH, env.ValueDate) { Source = element.LpOrderId, Fund = env.GetFund(element), FxCurrency = element.SettleCurrency, Symbol = element.Symbol, SecurityId = element.SecurityId, Quantity = Convert.ToDouble(sell.Quantity), FxRate = sellFx.Rate, StartPrice = 0, EndPrice = 0, Value = env.SignedValue(accountBuy, accountSell, true, sell.Quantity * sellFx.Rate), CreditDebit = env.DebitOrCredit(accountSell, sell.Quantity), }; env.Journals.AddRange(new[] { credit, debit }); var originalAccount = AccountUtils.GetDerivativeAccountType(realizedPnl); // Realized Pnl to go along with the Settled Cash CommonRules.GenerateJournalEntry(env, element, listOfTags, realizedAccountType, Event.REALIZED_PNL, realizedPnl); CommonRules.GenerateJournalEntries(env, element, listOfTags, originalAccount, "Change in Unrealized Derivatives Contracts at Fair Value", realizedPnl * -1); } if (taxlot.Quantity != 0) { var buyTrade = env.FindTrade(taxlot.OpenId); CommonRules.RelieveTaxLot(env, buyTrade, element, taxlot.Quantity * -1, true); taxlot.Quantity = 0; taxlot.Status = "Closed"; //Now we have Realized Pnl } } }