/// <summary> /// updates the latest stock prices in listview /// </summary> private async void updateStockPricesInListView() { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Aktuellste Wertpapierdaten werden abgefragt..."); for (int i = 0; i < lvWatchlist.Items.Count; i++) { StockListitem slItem = (StockListitem)lvWatchlist.Items[i].Tag; FinanceAPI rest = new FinanceAPI(webFinanceStrategy, slItem.symbol); Task <SingleQuote> taskStockData = Task.Run(() => rest.getLatestQuote()); SingleQuote stockData = await taskStockData; if (stockData != null) { if (webFinanceStrategy == FinanceAPI.FinanceStrategies.Yahoo) { var yahooStock = (YahooSingleQuote)stockData; slItem.BookValue = yahooStock.BookValue; slItem.MarketCapitalization = yahooStock.MarketCapitalization; slItem.FiftydayMovingAverage = yahooStock.FiftydayMovingAverage; slItem.TwoHundreddayMovingAverage = yahooStock.TwoHundreddayMovingAverage; } slItem.Change = stockData.Change; slItem.PriceCurrent = Convert.ToDouble(stockData.CurrentPrice); lvWatchlist.Items[i] = new ListViewItem(slItem.toStringArray(webFinanceStrategy)); lvWatchlist.Items[i].Tag = slItem; } } mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); }
/// <summary> /// Draws the chart asynchronously /// </summary> private async void drawChartAsync(SeriesChartType chartType = SeriesChartType.Line) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Aktiendaten werden geladen..."); splitMain.Panel1.Controls.Add(pbLoader); pbLoader.BringToFront(); FinanceAPI rest = new FinanceAPI(webFinanceStrategy, stock.symbol); Task <List <HistoricalQuote> > taskHistoricalData = Task.Run(() => rest.getHistoricalQuotesList(startDate, endDate)); stockHistoricalPrices = await taskHistoricalData; chartStock.Series.Clear(); if (stockHistoricalPrices != null) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Chart wird gezeichnet..."); var series = new Series(); series.ChartArea = chartStock.ChartAreas[0].Name; var volumes = new Series(); volumes.ChartArea = chartStock.ChartAreas[1].Name; series.ChartType = chartType; series.BorderWidth = 3; foreach (HistoricalQuote stockData in stockHistoricalPrices) { series.Points.AddXY(stockData.Date, stockData.Close); if (!seriesDict.ContainsKey(stockData.Date.ToOADate())) { seriesDict.Add(stockData.Date.ToOADate(), stockData); } volumes.Points.AddXY(stockData.Date, stockData.Volume); } chartStock.Series.Add(series); chartStock.Series.Add(volumes); drawChartStockAxis(); drawLinesAsync(); splitMain.Panel1.Controls.Remove(pbLoader); if (series.Points.Count == 0) { splitMain.Panel1.Controls.Clear(); var info = new Label(); info.Text = "Abruf historischer Daten war nicht möglich"; info.Size = new Size(300, 30); info.Location = new Point(20, 30); splitMain.Panel1.Controls.Add(info); } } mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); }
/// <summary> /// Stores the stock to database if bank has money /// </summary> private async void saveStockAsync(StockListitem watchlistStock) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Wertpapier wird gekauft..."); FinanceAPI rest = new FinanceAPI(webFinanceStrategy, watchlistStock.symbol); Task <SingleQuote> taskStockData = Task.Run(() => rest.getLatestQuote()); stockLatestPrice = await taskStockData; if (stockLatestPrice != null) { watchlistStock.PurchaseDate = DateTime.Now; watchlistStock.PricePurchase = stockLatestPrice.CurrentPrice; watchlistStock.PriceCurrent = (double)stockLatestPrice.CurrentPrice; if (webFinanceStrategy == FinanceAPI.FinanceStrategies.Yahoo) { var yahooStock = stockLatestPrice as YahooSingleQuote; watchlistStock.BookValue = yahooStock.BookValue; watchlistStock.MarketCapitalization = yahooStock.MarketCapitalization; watchlistStock.FiftydayMovingAverage = yahooStock.FiftydayMovingAverage; watchlistStock.TwoHundreddayMovingAverage = yahooStock.TwoHundreddayMovingAverage; } decimal totalSum = Convert.ToDecimal(watchlistStock.PriceCurrent * watchlistStock.Quantity); var bank = bankModel.getBank(); if (totalSum <= bank.AccountBalance) { Task <int> insertTask = Task.Run(() => depotModel.insertStockToDepot(watchlistStock)); int id = await insertTask; if (id > 0) { depotModel.saveTransaction(TransactionItem.TransactionTypeEnum.Purchase, watchlistStock); watchlistStock.StocksMapID = id; // Add to listview var lvItem = new ListViewItem(watchlistStock.toStringArray()); lvItem.Tag = watchlistStock; lvDepot.Items.Add(lvItem); // Draw Chart selectedStock = watchlistStock; loadChartDataAsync(selectedStock); } } else { MessageBox.Show("Nicht genügend Geld auf der Bank"); } } mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); }
/// <summary> /// Prepares data for stock chart and detail view /// </summary> private async void loadChartDataAsync(StockListitem selectedItem = null) { if (selectedItem != null) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Wertpapierdaten für " + selectedItem.name + " werden abgefragt..."); FinanceAPI rest = new FinanceAPI(webFinanceStrategy, selectedItem.symbol); drawChart(selectedItem); Task <SingleQuote> taskStockData = Task.Run(() => rest.getLatestQuote()); stockLatestPrice = await taskStockData; fillRightSidebarWithData(); mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); } }
/// <summary> /// Asynchronous method to load the depot value /// </summary> private async void btnUpdateDepot_Click(object sender, EventArgs e) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Börsendaten werden abgerufen..."); var depot = depotModel.getUserDepot(); var stocks = depotModel.getStocks(); decimal?depotBalance = 0; foreach (var item in stocks) { FinanceAPI rest = new FinanceAPI(webFinanceStrategy, item.symbol); Task <SingleQuote> taskStockData = Task.Run(() => rest.getLatestQuote()); SingleQuote stockData = await taskStockData; if (stockData != null) { if (stockData.Currency.ShortCode == defaultCurrency) { depotBalance += (stockData.CurrentPrice * item.Quantity); } else { string currencyPair = "" + stockData.Currency.ShortCode + defaultCurrency; if (!currencyRates.ContainsKey(currencyPair)) { Task <CurrencyRate> taskCurrencyData = Task.Run(() => new FinanceAPI(webFinanceStrategy, currencyPair).getCurrencyRate()); CurrencyRate currencyRate = await taskCurrencyData; if (currencyRate != null) { depotBalance += stockData.CurrentPrice * item.Quantity * Convert.ToDecimal(currencyRate.Rate); currencyRates.Add(currencyPair, currencyRate.Rate); } } else { depotBalance += stockData.CurrentPrice * item.Quantity * Convert.ToDecimal(currencyRates[currencyPair]); } } } } lblDepotBalance.Text = ((double)depotBalance).ToString("0.00"); mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); }
private async void getCurrencyRatesAsync(Currency selected) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Währungsdaten " + selected.Name + " geladen..."); FinanceAPI finance; switch (webFinanceStrategy) { case FinanceAPI.FinanceStrategies.Yahoo: StringBuilder symbol = new StringBuilder(); for (int i = 0; i < currencies.Count; i++) { symbol.Append(selected.ShortCode + currencies[i].ShortCode.ToString()); if (i < (currencies.Count - 1)) { symbol.Append("','"); } } finance = new FinanceAPI(webFinanceStrategy, symbol.ToString()); Task <List <CurrencyRate> > task = Task.Run(() => finance.getCurrencyRates()); currencyRates = await task; break; case FinanceAPI.FinanceStrategies.Google: foreach (var item in currencies) { string currencySymbol = "" + selected.ShortCode + item.ShortCode; finance = new FinanceAPI(webFinanceStrategy, currencySymbol); Task <CurrencyRate> taskGoogle = Task.Run(() => finance.getCurrencyRate()); var currencyRate = await taskGoogle; if (currencyRate != null) { currencyRates.Add(currencyRate); } } break; } drawCurrencyRates(currencyRates); mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); }
/// <summary> /// prepares data for stock chart and detail view /// </summary> private async void loadChartDataAsync(StockListitem selectedItem) { if (selectedItem != null) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Chart wird gezeichnet..."); FinanceAPI rest = new FinanceAPI(webFinanceStrategy, selectedItem.symbol); drawChart(selectedItem); Task <SingleQuote> taskStockData = Task.Run(() => rest.getLatestQuote()); stockLatestPrice = await taskStockData; if (stockLatestPrice != null) { fillRightSidebarWithData(); additionalStockData(selectedItem); mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); } } }
/// <summary> /// Inserts the stock to the watchlist /// </summary> private async void insertStockAsync(StockListitem watchlistStock) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Aktie wird gespeichert..."); FinanceAPI rest = new FinanceAPI(webFinanceStrategy, watchlistStock.symbol); Task <SingleQuote> taskStockData = Task.Run(() => rest.getLatestQuote()); stockLatestPrice = await taskStockData; if (stockLatestPrice != null) { watchlistStock.Change = stockLatestPrice.Change; watchlistStock.PurchaseDate = DateTime.Now; watchlistStock.PricePurchase = stockLatestPrice.CurrentPrice; watchlistStock.PriceCurrent = Convert.ToDouble(stockLatestPrice.CurrentPrice); watchlistStock.StocksMapID = model.insertStockToWatchlist(watchlistStock); if (webFinanceStrategy == FinanceAPI.FinanceStrategies.Yahoo) { var yahooStock = stockLatestPrice as YahooSingleQuote; watchlistStock.BookValue = yahooStock.BookValue; watchlistStock.MarketCapitalization = yahooStock.MarketCapitalization; watchlistStock.FiftydayMovingAverage = yahooStock.FiftydayMovingAverage; watchlistStock.TwoHundreddayMovingAverage = yahooStock.TwoHundreddayMovingAverage; } selectedStock = watchlistStock; loadChartDataAsync(watchlistStock); // Add to listview var lvItem = new ListViewItem(watchlistStock.toStringArray()); lvItem.Tag = watchlistStock; lvWatchlist.Items.Add(lvItem); } else { MessageBox.Show("Es konnten keine Preisinformationen für das Wertpapier gefunden werden."); } mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); }
/// <summary> /// When user selects a stock, another dialog occurs where he can see his proposal /// </summary> private async void makeBuyProposition() { if (selectedStock != null) { FinanceAPI rest = new FinanceAPI(webFinanceStrategy, selectedStock.symbol); Task <SingleQuote> taskStockData = Task.Run(() => rest.getLatestQuote()); stockLatestPrice = await taskStockData; if (stockLatestPrice != null && stockLatestPrice.CurrentPrice > 0) { toggleToolbar(false); selectedStock.PriceCurrent = Convert.ToDouble(stockLatestPrice.CurrentPrice); lblBuyName.Text = selectedStock.name; lblBuyPrice.Text = selectedStock.toPriceFormat(selectedStock.PriceCurrent); var total = (selectedStock.PriceCurrent * Convert.ToDouble(nupStockQuantity.Value)); lblBuyTotal.Text = selectedStock.toPriceFormat(total); } else { MessageBox.Show("Es konnten keine Preisinformationen für das Wertpapier gefunden werden."); } } }
/// <summary> /// updates the latest stock prices in listview /// </summary> private async void updateStockPricesInListViewAsync() { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Wertpapierdaten werden aktualisiert..."); double depotCurrent = 0.00; double depotStart = 0.00; for (int i = 0; i < lvDepot.Items.Count && lvDepot.Items.Count > 0; i++) { StockListitem slItem = (StockListitem)lvDepot.Items[i].Tag; FinanceAPI rest = new FinanceAPI(webFinanceStrategy, slItem.symbol); Task <SingleQuote> taskStockData = Task.Run(() => rest.getLatestQuote()); SingleQuote stockData = await taskStockData; if (stockData != null && null != lvDepot.Items[i]) { if (webFinanceStrategy == FinanceAPI.FinanceStrategies.Yahoo) { var yahooStock = (YahooSingleQuote)stockData; slItem.BookValue = yahooStock.BookValue; slItem.MarketCapitalization = yahooStock.MarketCapitalization; slItem.FiftydayMovingAverage = yahooStock.FiftydayMovingAverage; slItem.TwoHundreddayMovingAverage = yahooStock.TwoHundreddayMovingAverage; } slItem.Change = stockData.Change; slItem.PriceCurrent = Convert.ToDouble(stockData.CurrentPrice); lvDepot.Items[i] = new ListViewItem(slItem.toStringArray(webFinanceStrategy)); lvDepot.Items[i].Tag = slItem; if (slItem.Currency.ShortCode == defaultCurrency) { depotCurrent += (double)slItem.PriceCurrent * (double)slItem.Quantity; depotStart += (double)slItem.PricePurchase * (double)slItem.Quantity; } else { string currencyPair = "" + slItem.Currency.ShortCode + defaultCurrency; if (!currencyRates.ContainsKey(currencyPair)) { Task <CurrencyRate> taskCurrencyData = Task.Run(() => new FinanceAPI(webFinanceStrategy, currencyPair).getCurrencyRate()); CurrencyRate currencyRate = await taskCurrencyData; if (currencyRate != null) { depotCurrent += (double)slItem.PriceCurrent * (double)slItem.Quantity * currencyRate.Rate; depotStart += (double)slItem.PricePurchase * (double)slItem.Quantity * currencyRate.Rate; currencyRates.Add(currencyPair, currencyRate.Rate); } } else { depotCurrent += (double)slItem.PriceCurrent * (double)slItem.Quantity * currencyRates[currencyPair]; depotStart += (double)slItem.PricePurchase * (double)slItem.Quantity * currencyRates[currencyPair]; } } } } lblDepotCurrent.Text = depotCurrent.ToString("0.00"); lblDepotStart.Text = depotStart.ToString("0.00"); calculateDepotChanges(); mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); }