private void BuyButton_Click(object sender, RoutedEventArgs e) { var item = (StockIndex)ListOfRealStocks.SelectedItem; if (item != null) { ulong quantity; try { quantity = ulong.Parse(BuyQuantityTextBox.Text); } catch { quantity = 0; } if (buyPrice != 0 && quantity != 0) { var myStock = new MyStock { symbol = item.symbol, indexPrice = item.price, transactionDate = DateTime.Now.AddYears(-1), transactionVolume = quantity, profit = 0 }; AddNewBoughtStock(myStock); } } BuyQuantityTextBox.Text = String.Empty; OnPropertyChanged("buyQuantity"); }
/// <summary> /// Metoda usuwa zakupiony indeks z bazy danych /// i odswieza widok w razie potrzeby /// </summary> /// <param name="myStock">Transakcja do odswiezenia</param> /// <param name="quantity">Ilosc sprzedanych akcji</param> private void RemoveSoldStock(MyStock myStock, int quantity) { DatabaseController dbController = new DatabaseController(); if (myStock.transactionVolume == quantity) { m_myStockIndexesList.Remove(m_myStockIndexesList.Find(x => x.transactionID == myStock.transactionID)); dbController.myStockProfits.RemoveRange(dbController.myStockProfits.Where(x => x.transactionID == myStock.transactionID)); dbController.Remove(myStock); if (m_myStockIndexesList.Count > 0) { ListOfMyStocks.SelectedIndex = 0; } else { RealStockTab.IsSelected = true; MyStockTab.IsEnabled = false; ListOfMyStocks.SelectedIndex = -1; } } else { myStock.transactionVolume -= quantity; dbController.Update(myStock); } dbController.SaveChanges(); OnPropertyChanged("myStockIndexesList"); UpdateMyStockDetailData(); UpdateSelectedMyStock(); }
/// <summary> /// Metoda dodaje nowy zakupiony indeks do bazy danych /// </summary> /// <param name="myStock">Indeks do dodania</param> private void AddNewBoughtStock(MyStock myStock) { DatabaseController dbController = new DatabaseController(); dbController.Add(myStock); dbController.SaveChanges(); m_myStockIndexesList.Add(dbController.myStocks.OrderBy(x => x.transactionDate).Last()); MyStockTab.IsEnabled = true; UpdateMyStockDetailData(); OnPropertyChanged("myStockIndexesList"); }
/// <summary> /// Metoda odswieza dane o historycznych zarobkach zakupionych indeksow /// </summary> /// <param name="myStock">transakcja dla ktorej odswiezyc dane</param> private void UpdateMyStockProfit(MyStock myStock) { DatabaseController databaseController = new DatabaseController(); DateTime newestProfit; if (databaseController.myStockProfits.Where(x => x.transactionID == myStock.transactionID).Any()) { newestProfit = databaseController.myStockProfits .Where(x => x.symbol == myStock.symbol) .OrderByDescending(x => x.date) .FirstOrDefault().date; } else { newestProfit = myStock.transactionDate; } bool NeedToUpdateHistorical = !databaseController.historicalIndexes .Where(x => x.symbol == myStock.symbol) .Where(x => x.date.Date == DateTime.Today.AddDays(-1)) .Any(); if (NeedToUpdateHistorical && myStock.transactionDate < DateTime.Today) { databaseController.FillHistoricalTable(myStock.symbol); } var list = databaseController.historicalIndexes .Where(x => x.symbol == myStock.symbol) .Where(x => x.date > newestProfit) .OrderByDescending(x => x.date) .ToList(); foreach (var l in list) { databaseController.myStockProfits.Add(new MyStockProfit { date = l.date, profit = l.price - myStock.indexPrice, transactionID = myStock.transactionID, symbol = myStock.symbol }); databaseController.SaveChanges(); } }
/// <summary> /// Metoda odswieza oraz tworzy wykres zarobkow w czasie /// </summary> /// <param name="myStock">transkacja dla ktorej stworzyc wykres</param> private void UpdateMyStockProfitGraph(MyStock myStock) { DatabaseController databaseController = new DatabaseController(); bool isMyStockProfitUpdated = databaseController.myStockProfits .Where(x => x.symbol == myStock.symbol) .Where(x => x.date.Date == DateTime.Today.Date.AddDays(-1)) .Any(); if (!isMyStockProfitUpdated) { UpdateMyStockProfit(myStock); } Dispatcher.Invoke(() => { LineSeries stepLineSeries = new LineSeries(); stepLineSeries.Values = new ChartValues <MyStockProfit>(); stepLineSeries.Values.AddRange(databaseController.myStockProfits.Where(x => x.transactionID == myStock.transactionID).OrderByDescending(x => x.date)); ProfitChartSeries.Values = stepLineSeries.Values; }); }