예제 #1
0
        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);
            }
        }