private static void UpdatePortfolio(Dictionary <string, Tuple <int, int> > buyOrSellInfo, DateTime forDate) { foreach (var item in buyOrSellInfo) { var stock = item.Key; var isSell = (item.Value.Item2 > DistinctSelles); var isBuy = (item.Value.Item1 >= DistinctByers) && !isSell; var repository = new Repository.Repository(); var stockEntity = repository.GetPortfolio().Result.FirstOrDefault(s => s.Stock.Equals(stock)); var ticker = repository.GetTickers().FirstOrDefault(t => t.FullName.Equals(stock)); //post in not one of the stocks in list to follow if (ticker == null) { continue; } var price = QuoteService.GetHistoricalPrice(ticker.TickerName, forDate); if (price.Last == null) { continue; } var addBuyToPortfolio = (stockEntity == null && isBuy); var addSellToPortfolio = stockEntity != null && isSell; var calcPrice = Double.Parse(price.Last, System.Globalization.CultureInfo.InvariantCulture); var number = 10000 / Double.Parse(price.Last, System.Globalization.CultureInfo. InvariantCulture); if (addBuyToPortfolio) { repository.StoreTransaction(stock, forDate, "Köp", number, calcPrice, 10000, 0); repository.AddPostToPortfolio(new Portfolio { Stock = stock, BuyPrice = calcPrice, BuyAmount = 10000, BuyNumber = number, BuyDate = forDate, CurrentPrice = calcPrice } ); } else if (addSellToPortfolio) { if (forDate > stockEntity.BuyDate) { var entity = repository.Context.Portfolio.First(i => i.Id.Equals(stockEntity.Id)); repository.Context.Portfolio.Remove(entity); repository.SaveChanges(); var todaysPrice = Double.Parse(QuoteService.GetTodaysPrice(ticker.TickerName).Last, System.Globalization.CultureInfo.InvariantCulture); repository.StoreTransaction(stock, forDate, "Försäljning", stockEntity.BuyNumber, todaysPrice, (calcPrice * stockEntity.BuyNumber), (calcPrice * stockEntity.BuyNumber) - 10000); } } } }