public PositionFactPerformance(PositionFACT position) { this.Position = position; if (position.Quantity >= 0) { GainPercent = (position.Price / position.AverageCost) - 1; GainValue = position.Quantity * (position.Price - position.AverageCost); } else { // if the position is short. GainPercent = ((position.Price / position.AverageCost) - 1) * -1; GainValue = position.Quantity * ((position.Price - position.AverageCost) * -1); } }
public async Task InitialiseFundAction(Fund fund, List <TransferAgencyBO> investorSubscriptions, List <TransactionsBO> transactions, NAVPriceStoreFACT initialNav, List <FundInvestorBO> fundInvestors, List <InvestorHoldingsFACT> investorHoldings) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { // I will need to create a positions Fact table AND a investor holdings here too. // position is easy sum all the transactions together... decimal cashBalance = transactions.Sum(s => s.TradeAmount); int assetClassId = context.Securities.Find(transactions[0].SecurityId).AssetClassId; PositionFACT cashPosition = new PositionFACT { PositionDate = fund.LaunchDate, AssetClassId = assetClassId, AverageCost = decimal.One, CurrencyId = transactions[0].CurrencyId, UnrealisedPnl = decimal.Zero, FundId = fund.FundId, MarketValue = cashBalance, Price = decimal.One, Quantity = cashBalance, RealisedPnl = decimal.Zero, SecurityId = transactions[0].SecurityId }; await context.Positions.AddAsync(cashPosition); // lock period AccountingPeriodsDIM period = context.Periods.Find(initialNav.NAVPeriodId); period.isLocked = true; context.Periods.Update(period); await context.InvestorHoldings.AddRangeAsync(investorHoldings); // Saves the fund investors that are attached to the transferagent subscriptions await context.FundInvestor.AddRangeAsync(fundInvestors); // Saves the first Nav await context.NavPriceData.AddAsync(initialNav); // saves the investors to the database context.TransferAgent.AddRange(investorSubscriptions); context.Transactions.AddRange(transactions); // Saves the funds state to initialised context.Funds.Update(fund); await context.SaveChangesAsync(); } }
public async Task LockNav(NavValuations navValuations) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { DateTime asOfDate = navValuations.AsOfDate; int fundId = navValuations.fund.FundId; AccountingPeriodsDIM period = context.Periods.Where(p => p.FundId == fundId && p.AccountingDate == asOfDate).FirstOrDefault(); period.isLocked = true; context.Entry(period).CurrentValues.SetValues(period); List <TransactionsBO> allTransactions; if (navValuations.fund.NAVFrequency == "Daily") { allTransactions = navValuations.fund.Transactions.Where(t => t.TradeDate == asOfDate).ToList(); } else { allTransactions = navValuations.fund.Transactions.Where(t => t.TradeDate.Month == asOfDate.Month).ToList(); } foreach (TransactionsBO transaction in allTransactions) { transaction.isLocked = true; } context.Transactions.UpdateRange(allTransactions); NAVPriceStoreFACT newNavPrice = new NAVPriceStoreFACT { FinalisedDate = asOfDate, Currency = navValuations.fund.BaseCurrency, FundId = fundId, NAVPeriodId = period.PeriodId, SharesOutstanding = navValuations.SharesOutstanding, NetAssetValue = navValuations.NetAssetValue, NAVPrice = navValuations.NetAssetValuePerShare, }; context.NavPriceData.Add(newNavPrice); List <PositionFACT> newPositions = new List <PositionFACT>(); foreach (ValuedSecurityPosition secPosition in navValuations.SecurityPositions) { PositionFACT newPosition = new PositionFACT { PositionDate = secPosition.AsOfDate, SecurityId = secPosition.Position.Security.SecurityId, AssetClassId = secPosition.Position.Security.AssetClassId, FundId = fundId, AverageCost = secPosition.Position.AverageCost, CurrencyId = secPosition.Position.Security.CurrencyId, MarketValue = secPosition.MarketValueBase, Price = secPosition.MarketPrice, Quantity = secPosition.Position.NetQuantity, RealisedPnl = secPosition.Position.RealisedPnL, UnrealisedPnl = secPosition.UnrealisedPnl }; newPositions.Add(newPosition); } foreach (ValuedCashPosition cashPosition in navValuations.CashPositions) { string currencySecSymbol = $"{cashPosition.CashPosition.Currency.Symbol}c"; SecuritiesDIM securitisedCash = context.Securities.AsNoTracking().Where(s => s.Symbol == currencySecSymbol).Include(s => s.AssetClass).FirstOrDefault(); PositionFACT newPosition = new PositionFACT { PositionDate = cashPosition.AsOfDate, SecurityId = securitisedCash.SecurityId, AssetClassId = securitisedCash.AssetClassId, FundId = fundId, AverageCost = 1, CurrencyId = cashPosition.CashPosition.Currency.CurrencyId, MarketValue = cashPosition.MarketValueBase, Price = cashPosition.fxRate, Quantity = cashPosition.CashPosition.NetQuantity, RealisedPnl = 0, UnrealisedPnl = 0 }; newPositions.Add(newPosition); } await context.Positions.AddRangeAsync(newPositions); List <InvestorHoldingsFACT> newHoldings = new List <InvestorHoldingsFACT>(); foreach (ClientHoldingValuation clientHolding in navValuations.ClientHoldings) { InvestorHoldingsFACT newHolding = new InvestorHoldingsFACT { NetValuation = clientHolding.NetValuation, AverageCost = clientHolding.Holding.AverageCost, HighWaterMark = clientHolding.Holding.Investor.HighWaterMark, ManagementFeesAccrued = clientHolding.ManagementFeesAccrued, Units = clientHolding.Holding.Units, PerformanceFeesAccrued = clientHolding.PerformanceFeesAccrued, HoldingDate = asOfDate, FundId = fundId, InvestorId = clientHolding.Holding.Investor.InvestorId }; newHoldings.Add(newHolding); } await context.InvestorHoldings.AddRangeAsync(newHoldings); var pendingTAs = navValuations.fund.TransferAgent.Where(ta => !ta.IsNavFinal && ta.TransactionDate == asOfDate).ToList(); foreach (var pendingTA in pendingTAs) { SecuritiesDIM security = context.Securities.AsNoTracking().Where(s => s.Symbol == $"{pendingTA.Currency}c").First(); int secId = security.SecurityId; int currId = security.CurrencyId; TransactionTypeDIM tradeType; int custodianId = context.Custodians.AsNoTracking().Where(c => c.Name == "Default").First().CustodianId; // FOR NOW TODO if (pendingTA.IssueType == "Subscription") { //add tradeamount.. pendingTA.Units = pendingTA.TradeAmount / navValuations.NetAssetValuePerShare; pendingTA.NAVPrice = navValuations.NetAssetValuePerShare; pendingTA.IsNavFinal = true; tradeType = context.TransactionTypes.AsNoTracking().Where(tt => tt.TypeName == "Deposit").First(); } else { pendingTA.TradeAmount = pendingTA.Units * navValuations.NetAssetValuePerShare; pendingTA.NAVPrice = navValuations.NetAssetValuePerShare; pendingTA.IsNavFinal = true; tradeType = context.TransactionTypes.AsNoTracking().Where(tt => tt.TypeName == "Withdrawal").First(); //reduce units.. } // id need to create deposit and withdrawals here as well as save these updated TAs // I need to set main custodian for a fund where all subs and reds initially go to.TODO TransactionsBO newCashTrade = new TransactionsBO { SecurityId = secId, Quantity = pendingTA.Units, Price = pendingTA.NAVPrice, TradeAmount = pendingTA.TradeAmount, TradeDate = pendingTA.TransactionDate, SettleDate = pendingTA.TransactionSettleDate, CreatedDate = DateTime.Now, LastModified = DateTime.Now, Fees = decimal.Zero, isActive = true, isLocked = true, isCashTransaction = false, FundId = fundId, TransactionTypeId = tradeType.TransactionTypeId, CurrencyId = currId, Comment = pendingTA.IssueType.ToUpper(), CustodianId = custodianId }; context.Transactions.Add(newCashTrade); context.TransferAgent.Update(pendingTA); } await context.SaveChangesAsync(); // Lock all transactions with this trade Date... DONE // Add to Position SnapShot Fact Table... DONE // Lock Period... DONE // Update TransferAgent Fact Table.... DONE // NavPrices DONE } }