public async Task UpdateSecurityInfo(SecuritiesDIM security) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { context.Securities.Update(security); await context.SaveChangesAsync(); } }
public async Task UpdateManualPrices(List <SecurityPriceStore> prices) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { context.SecurityPriceData.UpdateRange(prices); await context.SaveChangesAsync(); } }
public async Task DeleteSecurityInfo(string symbol) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { SecuritiesDIM security = context.Securities.Where(s => s.Symbol == symbol).FirstOrDefault(); context.Securities.Remove(security); await context.SaveChangesAsync(); } }
public async Task UpdateTransaction(TransactionsBO transaction) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { TransactionsBO originalTransaction = context.Transactions.First(t => t.TransactionId == transaction.TransactionId); context.Entry(originalTransaction).CurrentValues.SetValues(transaction); await context.SaveChangesAsync(); } }
public async Task <TransferAgencyBO> UpdateInvestorAction(TransferAgencyBO investorAction) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { context.TransferAgent.Update(investorAction); await context.SaveChangesAsync(); return(investorAction); } }
public async Task <SecuritiesDIM> AddSecurityInfo(SecuritiesDIM security) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { EntityEntry <SecuritiesDIM> res = await context.Securities.AddAsync(security); await context.SaveChangesAsync(); return(res.Entity); } }
public async Task <InvestorsDIM> CreateInvestor(InvestorsDIM investor) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { EntityEntry <InvestorsDIM> res = await context.Investors.AddAsync(investor); await context.SaveChangesAsync(); return(res.Entity); } }
public async Task <TransactionsBO> CreateTransaction(TransactionsBO transaction) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { EntityEntry <TransactionsBO> res = await context.Transactions.AddAsync(transaction); await context.SaveChangesAsync(); return(res.Entity); } }
public async Task <TransferAgencyBO> CreateInvestorAction(TransferAgencyBO investorAction) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { EntityEntry <TransferAgencyBO> res = await context.TransferAgent.AddAsync(investorAction); await context.SaveChangesAsync(); return(res.Entity); } }
public async Task <Fund> UpdateFund(Fund fund) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { context.Funds.Update(fund); await context.SaveChangesAsync(); return(fund); } }
public async Task <Fund> CreateFund(Fund fund) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { EntityEntry <Fund> res = await context.Funds.AddAsync(fund); await context.SaveChangesAsync(); // Once the fund has been created, I then create the accounting periods // set the monthly or daily account periods for up to one year ahead... DateTime startDate = fund.LaunchDate; List <DateTime> allPeriods; if (fund.NAVFrequency == "Daily") { // get daily dates from fund launch to a year ahead allPeriods = DateSettings.AnnualWorkingDays(startDate); } else { // get month end dates from fund launch to a year ahead allPeriods = DateSettings.AnnualMonthEnds(startDate); } // add all the dates to the new periods List <AccountingPeriodsDIM> initialPeriods = new List <AccountingPeriodsDIM>(); foreach (DateTime period in allPeriods) { AccountingPeriodsDIM newPeriod; newPeriod = new AccountingPeriodsDIM { AccountingDate = period.Date, isLocked = false, FundId = fund.FundId }; initialPeriods.Add(newPeriod); } context.Periods.AddRange(initialPeriods); await context.SaveChangesAsync(); return(res.Entity); } }
public async Task CreateCashTransfer(List <TransactionsBO> transfers) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { // Created LinkedTransactions Here LinkedTradesBO linkReference = new LinkedTradesBO(); foreach (TransactionsBO transfer in transfers) { transfer.LinkedTrades = linkReference; } context.Transactions.AddRange(transfers); await context.SaveChangesAsync(); } }
public async Task UpdateAPIKeys(string alphaVantageKeyValue, string FMPKeyValue) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { ApplicationSettings AvKey = context.AppSettings.Where(ap => ap.SettingName == "AlphaVantageAPI").First(); ApplicationSettings FMPKey = context.AppSettings.Where(ap => ap.SettingName == "FMPrepAPI").First(); AvKey.SettingValue = alphaVantageKeyValue; FMPKey.SettingValue = FMPKeyValue; context.Update(AvKey); context.Update(FMPKey); await context.SaveChangesAsync(); } }
public async Task RestoreFXTransaction(TransactionsBO transaction) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { IEnumerable <TransactionsBO> linkedTransactions = context.Transactions.Where(t => t.LinkedTradeId == transaction.LinkedTradeId); // To DO im not happy with this it needs to be neater foreach (TransactionsBO linkedTransaction in linkedTransactions) { TransactionsBO tempTransaction = linkedTransaction; tempTransaction.isActive = true; context.Entry(linkedTransaction).CurrentValues.SetValues(tempTransaction); } await context.SaveChangesAsync(); } }
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 <Fund> DeleteFund(int id) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { Fund fund = await context.Funds.FindAsync(id); if (fund == null) { return(fund); } context.Funds.Remove(fund); await context.SaveChangesAsync(); return(fund); } }
public async Task <TransferAgencyBO> DeleteInvestorAction(int id) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { TransferAgencyBO investorAction = await context.TransferAgent.FindAsync(id); if (investorAction == null) { return(investorAction); } context.TransferAgent.Remove(investorAction); await context.SaveChangesAsync(); return(investorAction); } }
public async Task AddImportedSecurities(List <SecuritiesDIM> newSecurities) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { HashSet <string> existingSecurities = context.Securities.Select(s => s.Symbol).ToHashSet(); foreach (SecuritiesDIM security in newSecurities) { if (!existingSecurities.Contains(security.Symbol)) { existingSecurities.Add(security.Symbol); context.Securities.Add(security); } } await context.SaveChangesAsync(); } }
public async Task <int> AddDailyPrices(SecuritiesDIM security) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { string avKey = context.AppSettings.Where(ap => ap.SettingName == "AlphaVantageAPI").First().SettingValue; AlphaVantageConnection avConn = _dataFactory.CreateAlphaVantageClient(avKey); IEnumerable <AVSecurityPriceData> allPrices = await avConn.GetPricesAsync(security); HashSet <DateTime> existingDates = context.SecurityPriceData.Where(spd => spd.Security.Symbol == security.Symbol).Select(spd => spd.Date).ToHashSet(); string assetClass = security.AssetClass.Name; int pricesSaved = 0; foreach (AVSecurityPriceData price in allPrices) { if (!existingDates.Contains(price.TimeStamp)) { // i should the indirect quote therefore i inverse the price here... if (assetClass == "FX") { price.Close = 1 / price.Close; } if (security.Currency.Symbol == "GBP" && assetClass != "FX") { price.Close /= 100; } SecurityPriceStore newPrice = new SecurityPriceStore { Date = price.TimeStamp, ClosePrice = price.Close, SecurityId = security.SecurityId, PriceSource = price.PriceSource }; context.SecurityPriceData.Add(newPrice); pricesSaved += 1; } } await context.SaveChangesAsync(); return(pricesSaved); } }
public async Task AddImportedPrices(Dictionary <string, List <SecurityPriceStore> > newPrices) { /* Use the symbol (Key) to get a hashset of all the currently saved dates. * get all the prices from the list (Value) and check if they already exist in the hashset. * if they dont then add them. */ using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { foreach (KeyValuePair <string, List <SecurityPriceStore> > kvp in newPrices) { HashSet <DateTime> existingDates = context.SecurityPriceData.Where(spd => spd.Security.Symbol == kvp.Key).Select(spd => spd.Date).ToHashSet(); foreach (SecurityPriceStore price in kvp.Value) { if (!existingDates.Contains(price.Date)) { existingDates.Add(price.Date); context.SecurityPriceData.Add(price); } } } await context.SaveChangesAsync(); } }
public async Task UnlockNav(DateTime asOfDate, int fundId) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { Fund fund = context.Funds.Find(fundId); AccountingPeriodsDIM period = context.Periods.Where(p => p.FundId == fund.FundId && p.AccountingDate == asOfDate).FirstOrDefault(); period.isLocked = false; context.Periods.Update(period); List <TransactionsBO> allTransactions; if (fund.NAVFrequency == "Daily") { allTransactions = context.Transactions.Where(t => t.FundId == fund.FundId && t.TradeDate == asOfDate).Include(t => t.TransactionType).ToList(); } else { // TODO issue here . what if the month is that same as the month the fund was launched??? allTransactions = context.Transactions.Where(t => t.FundId == fund.FundId && t.TradeDate.Month == asOfDate.Month).Include(t => t.TransactionType).ToList(); } List <TransactionsBO> deletedTransactions = new List <TransactionsBO>(); foreach (TransactionsBO transaction in allTransactions) { transaction.isLocked = false; if (transaction.TransactionType.TypeClass == "CapitalTrade") { // this removes all the deposits and withdrawals that where booked for today... deletedTransactions.Add(transaction); } } context.Transactions.UpdateRange(allTransactions); context.Transactions.RemoveRange(deletedTransactions); List <TransferAgencyBO> allInvestorActions; if (fund.NAVFrequency == "Daily") { allInvestorActions = context.TransferAgent.Where(ta => ta.FundId == fund.FundId && ta.TransactionDate == asOfDate).ToList(); } else { allInvestorActions = context.TransferAgent.Where(ta => ta.FundId == fund.FundId && ta.TransactionDate.Month == asOfDate.Month).ToList(); } foreach (TransferAgencyBO action in allInvestorActions) { // Sets the subscriptions and redemptions back to pending status. if (action.IssueType == "Subscription") { action.Units = decimal.Zero; action.NAVPrice = decimal.Zero; action.IsNavFinal = false; } else { action.TradeAmount = decimal.Zero; action.NAVPrice = decimal.Zero; action.IsNavFinal = false; } } context.TransferAgent.UpdateRange(allInvestorActions); NAVPriceStoreFACT navPrice = context.NavPriceData.Where(npd => npd.FinalisedDate == asOfDate && npd.FundId == fund.FundId).FirstOrDefault(); context.NavPriceData.Remove(navPrice); IEnumerable <PositionFACT> storedPositions = context.Positions.Where(p => p.PositionDate == asOfDate && p.FundId == fund.FundId); context.Positions.RemoveRange(storedPositions); IEnumerable <InvestorHoldingsFACT> storedHoldings = context.InvestorHoldings.Where(i => i.HoldingDate == asOfDate && i.FundId == fund.FundId); context.InvestorHoldings.RemoveRange(storedHoldings); await context.SaveChangesAsync(); } }
public async Task <TransactionsBO> CreateFXTransaction(ForexDTO fxTransaction) { using (PortfolioAceDbContext context = _contextFactory.CreateDbContext()) { SecuritiesDIM fxSecurity = context.Securities.AsNoTracking().Where(s => s.Symbol == fxTransaction.Symbol).FirstOrDefault(); AssetClassDIM assetClass = context.AssetClasses.AsNoTracking().Where(a => a.Name == "FXForward").FirstOrDefault(); CustodiansDIM custodian = context.Custodians.AsNoTracking().Where(c => c.Name == fxTransaction.Custodian).FirstOrDefault(); IEnumerable <TransactionTypeDIM> transactionTypes = context.TransactionTypes; List <CurrenciesDIM> currencies = context.Currencies.AsNoTracking().ToList(); CurrenciesDIM buyCurrency = context.Currencies.AsNoTracking().Where(c => c.Symbol == fxTransaction.BuyCurrency).First(); CurrenciesDIM sellCurrency = context.Currencies.AsNoTracking().Where(c => c.Symbol == fxTransaction.SellCurrency).First(); if (fxSecurity == null) { fxSecurity = new SecuritiesDIM { AssetClassId = assetClass.AssetClassId, CurrencyId = buyCurrency.CurrencyId, SecurityName = fxTransaction.Name, Symbol = fxTransaction.Symbol }; context.Securities.Add(fxSecurity); } // Created LinkedTransactions Here LinkedTradesBO linkReference = new LinkedTradesBO(); TransactionsBO refTransaction = new TransactionsBO { Security = fxSecurity, FundId = fxTransaction.FundId, isActive = true, isLocked = false, TradeAmount = 0, Price = fxTransaction.Price, Quantity = fxTransaction.BuyAmount, CurrencyId = buyCurrency.CurrencyId, TransactionTypeId = transactionTypes.Where(tt => tt.TypeName == "FXTrade").First().TransactionTypeId, Comment = "", CustodianId = custodian.CustodianId, Fees = 0, isCashTransaction = false, TradeDate = fxTransaction.TradeDate, SettleDate = fxTransaction.SettleDate, CreatedDate = DateTime.Now, LastModified = DateTime.Now, LinkedTrades = linkReference }; TransactionsBO refTransactionCollapse = new TransactionsBO { Security = fxSecurity, FundId = fxTransaction.FundId, isActive = true, isLocked = false, TradeAmount = 0, Price = fxTransaction.Price, Quantity = fxTransaction.BuyAmount * -1, CurrencyId = buyCurrency.CurrencyId, TransactionTypeId = transactionTypes.Where(tt => tt.TypeName == "FXTradeCollapse").First().TransactionTypeId, Comment = "", CustodianId = custodian.CustodianId, Fees = 0, isCashTransaction = false, TradeDate = fxTransaction.SettleDate, SettleDate = fxTransaction.SettleDate, CreatedDate = DateTime.Now, LastModified = DateTime.Now, LinkedTrades = linkReference }; EntityEntry <TransactionsBO> res = await context.Transactions.AddAsync(refTransaction); context.Transactions.Add(refTransactionCollapse); SecuritiesDIM fxBuySecurity = context.Securities.AsNoTracking().Where(s => s.Symbol == $"{fxTransaction.BuyCurrency}c").FirstOrDefault(); SecuritiesDIM fxSellSecurity = context.Securities.AsNoTracking().Where(s => s.Symbol == $"{fxTransaction.SellCurrency}c").FirstOrDefault(); TransactionsBO fxBuyLegCash = new TransactionsBO { SecurityId = fxBuySecurity.SecurityId, FundId = fxTransaction.FundId, isActive = true, isLocked = false, TradeAmount = fxTransaction.BuyAmount, Price = 1, Quantity = fxTransaction.BuyAmount, CurrencyId = buyCurrency.CurrencyId, TransactionTypeId = transactionTypes.Where(tt => tt.TypeName == "FXBuy").First().TransactionTypeId, Comment = fxTransaction.Description, CustodianId = custodian.CustodianId, Fees = 0, isCashTransaction = false, TradeDate = fxTransaction.SettleDate, SettleDate = fxTransaction.SettleDate, CreatedDate = DateTime.Now, LastModified = DateTime.Now, LinkedTrades = linkReference }; TransactionsBO fxSellLegCash = new TransactionsBO { SecurityId = fxSellSecurity.SecurityId, FundId = fxTransaction.FundId, isActive = true, isLocked = false, TradeAmount = fxTransaction.SellAmount, Price = 1, Quantity = fxTransaction.SellAmount, CurrencyId = sellCurrency.CurrencyId, TransactionTypeId = transactionTypes.Where(tt => tt.TypeName == "FXSell").First().TransactionTypeId, Comment = fxTransaction.Description, CustodianId = custodian.CustodianId, Fees = 0, isCashTransaction = false, TradeDate = fxTransaction.SettleDate, SettleDate = fxTransaction.SettleDate, CreatedDate = DateTime.Now, LastModified = DateTime.Now, LinkedTrades = linkReference }; context.Transactions.Add(fxBuyLegCash); context.Transactions.Add(fxSellLegCash); await context.SaveChangesAsync(); return(refTransaction); } }
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 } }