public double Sell(DateStructure sell) { double bal = tempBal; if (Owned.Where(o => o.Date == sell.BoughtDate).Count() > 0) { foreach (var s in sell.StockAndPrices) { bal += Owned.Where(o => o.Date == sell.BoughtDate).SelectMany(z => z.StockAndPrices).Where(x => x.Stock == s.Stock).Select(p => p.Price).Sum() * sell.StockAndPrices.Single(x => x.Stock == s.Stock).Price; } var prof = 0.0; if (bal > startBal) { prof = bal - startBal; } profit += prof; bal -= prof; if (Balances.SingleOrDefault(b => b.Date == sell.SellDate) == null) { Balances.Add(new Balance { Date = sell.SellDate.DateTime, Inv = bal, Int = profit }); } else { Balances.SingleOrDefault(b => b.Date == sell.SellDate).Inv += bal; Balances.SingleOrDefault(b => b.Date == sell.SellDate).Int = profit; } } return(bal); }
public void Buy(double bal, DateTimeOffset date) { var availDates = Buys.Where(b => b.Date >= date); if (availDates.Count() > 0 && bal != 0) { //Price for bought = Quantity var buys = availDates.OrderBy(b => b.Date).First(); var buyDate = buys.Date; var bought = new DateStructure { Date = buyDate, StockAndPrices = new List <StockAndPrice>() }; var stop = false; while (!stop) { foreach (var s in buys.StockAndPrices) { if (bal >= s.Price) { if (bought.StockAndPrices.SingleOrDefault(x => x.Stock == s.Stock) == null) { bought.StockAndPrices.Add(new StockAndPrice { Stock = s.Stock, Price = 1 }); } else { bought.StockAndPrices.SingleOrDefault(x => x.Stock == s.Stock).Price++; } bal -= s.Price; } else { stop = true; tempBal = bal; } } } Owned.Add(bought); } }