private void GetDataButton_Click(object sender, RoutedEventArgs e) { bool securityFilled = (ticker.Text != "") ? true : false; // TRUE if ticker field has been indicated if (DateTime.Compare(Convert.ToDateTime(dateFrom.Text), Convert.ToDateTime(dateTo.Text)) < 0) { if (securityFilled) { try { BlgWrapper wrap = new BlgWrapper(); List <string> tickers = new List <string>(); List <string> fields = new List <string>(); List <string> fields2 = new List <string>() { "PX_LAST" }; List <double> histoValues = new List <double>(); Dictionary <string, Dictionary <string, string> > res_ref = new Dictionary <string, Dictionary <string, string> >(); Dictionary <string, Dictionary <string, List <HistoData> > > res_histo = new Dictionary <string, Dictionary <string, List <HistoData> > >(); Dictionary <string, Dictionary <string, List <HistoData> > > res_histo2 = new Dictionary <string, Dictionary <string, List <HistoData> > >(); MainModel mm = GridMain.DataContext as MainModel; ResultModel rm = GridResult.DataContext as ResultModel; tickers.Add(mm.Ticker); fields.Add("PX_LAST"); // By default, we resquest the security's last price // We need to convert dateTo & dateFrom value into a DateTime object // to be able to use the method GetHistoricalData rm.DateTo = Convert.ToDateTime(dateTo.Text); rm.DateFrom = Convert.ToDateTime(dateFrom.Text); if (wrap.GetHistoricalData(tickers, fields, BlgPeriodOpt.periodicity_sel, BlgPeriods.daily, rm.DateFrom, rm.DateTo)) { res_histo = wrap.ResultHisto; rm.Donnees1 = res_histo[fields.First()].Values.First(); } else { string message = ""; foreach (var kvp in wrap.Errors) { message += kvp.Key + " : " + kvp.Value + Environment.NewLine; MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } // Computing the perfomance with the formula we got during the course List <double> perf = new List <double>(); perf.Add(0); // We initialize the performance of the first date as 0% for (int i = 1; i < rm.Donnees1.Count; i++) { perf.Add((rm.Donnees1[i].Price / rm.Donnees1[i - 1].Price) - 1); } for (int i = 0; i < rm.Donnees1.Count; i++) { rm.Donnees1[i].Price = perf[i]; } pcprod.ItemsSource = null; pcprod.Items.Clear(); pcprod.ItemsSource = rm.DonneesChartable; #region BLOOMBERG FIELDS /* We add the Bloomberg fields which we need to get the data * All these data will be save an ResultModel object * Downloading data with GetReferenceData method */ fields.Clear(); fields.Add("NAME"); fields.Add("CIE_DES"); fields.Add("PX_LAST"); fields.Add("INDUSTRY_SECTOR"); fields.Add("PRIMARY_EXCHANGE_NAME"); fields.Add("NUM_OF_EMPLOYEES"); fields.Add("CURR_ENTP_VAL"); fields.Add("RTG_SP_LT_LC_ISSUER_CREDIT"); fields.Add("ALL_CALENDAR_DAYS"); fields.Add("PREVIOUS_VALUE"); fields.Add("PX_OPEN"); fields.Add("PREV_CLOSE_VAL"); fields.Add("HIGH_52WEEK"); fields.Add("LOW_52WEEK"); #endregion // Download all Bloomberg data fields if (wrap.GetReferenceData(tickers, fields)) { res_ref = wrap.ResultRef; // Get the value with the corresponding Bloomberg field from the wrap rm.Name = res_ref["NAME"].Values.FirstOrDefault(); rm.Description = res_ref["CIE_DES"].Values.FirstOrDefault(); // Get Description Data rm.Sector = res_ref["INDUSTRY_SECTOR"].Values.FirstOrDefault(); // Sector rm.PrimaryExchange = res_ref["PRIMARY_EXCHANGE_NAME"].Values.FirstOrDefault(); rm.Employee = res_ref["NUM_OF_EMPLOYEES"].Values.FirstOrDefault(); // Number of employees rm.CompanyValue = res_ref["CURR_ENTP_VAL"].Values.FirstOrDefault(); // Value of the company rm.Rating = res_ref["RTG_SP_LT_LC_ISSUER_CREDIT"].Values.FirstOrDefault(); // S&P Rating // Get Data Data rm.LastPrice = res_ref["PX_LAST"].Values.FirstOrDefault(); // Last current price rm.Open = res_ref["PX_OPEN"].Values.FirstOrDefault(); // Open price rm.PreviousClose = res_ref["PREV_CLOSE_VAL"].Values.FirstOrDefault(); // Close Price day D-1 rm.WKhigh = res_ref["HIGH_52WEEK"].Values.FirstOrDefault(); // Highest price last year rm.WKlow = res_ref["LOW_52WEEK"].Values.FirstOrDefault(); // Lowest price last year } else { string message = ""; foreach (var kvp in wrap.Errors) { message += kvp.Key + " : " + kvp.Value + Environment.NewLine; MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } // Download Volatility Data (1 year) if (wrap.GetHistoricalData(tickers, fields, BlgPeriodOpt.periodicity_sel, BlgPeriods.daily, DateTime.Now.AddDays(-365), DateTime.Now)) { res_histo2 = wrap.ResultHisto; List <HistoData> data = res_histo2[fields.First()].Values.First(); foreach (HistoData hd in data) { histoValues.Add(hd.Price); } // We use the function Volatility() to compute vol rm.Volatility30D = Math.Round(Volatility(histoValues.GetRange(histoValues.Count - 20, 20)), 2).ToString(); rm.Volatility90D = Math.Round(Volatility(histoValues.GetRange(histoValues.Count - 60, 60)), 2).ToString(); rm.Volatility180D = Math.Round(Volatility(histoValues.GetRange(histoValues.Count - 120, 120)), 2).ToString(); rm.Volatility1Y = Math.Round(Volatility(histoValues.GetRange(histoValues.Count - 180, 180)), 2).ToString(); } else { string message = ""; foreach (var kvp in wrap.Errors) { message += kvp.Key + " : " + kvp.Value + Environment.NewLine; MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } } catch (Exception error) { MessageBox.Show(error.Message); } } else { MessageBox.Show("Please fill in all the required fields"); } } else { MessageBox.Show("Please verify dates"); } }
private void GetDataButton_Click(object sender, RoutedEventArgs e) { bool securityFilled = ((v_ticker1.Text != "") && (v_ticker2.Text != "") && (v_ticker3.Text != "")) ? true : false; // TRUE if v_ticker fields have been indicated // We first check if dates are correct (dateFrom < dateTo) if ((DateTime.Compare(Convert.ToDateTime(dateFrom.Text), Convert.ToDateTime(dateTo.Text)) < 0) && (DateTime.Compare(Convert.ToDateTime(dateTo.Text), Convert.ToDateTime(DateTime.Now)) < 0)) { if (securityFilled) { // Then if all number of shares are positive if ((Convert.ToInt32(v_nbShares1.Text) > 0) && (Convert.ToInt32(v_nbShares2.Text) > 0) && (Convert.ToInt32(v_nbShares3.Text) > 0) && (v_nbShares1.Text != "") && (v_nbShares2.Text != "") && (v_nbShares3.Text != "")) { try { BlgWrapper wrap = new BlgWrapper(); List <string> tickers = new List <string>(); List <string> fields = new List <string>(); List <int> nbShares = new List <int>(); MainModel mm = GridMain.DataContext as MainModel; ResultModel rm = GridResult.DataContext as ResultModel; rm.DateTo = Convert.ToDateTime(dateTo.Text); rm.DateFrom = Convert.ToDateTime(dateFrom.Text); // Adding security & number of shares to the list of data we will download tickers.Add(v_ticker1.Text); tickers.Add(v_ticker2.Text); tickers.Add(v_ticker3.Text); nbShares.Add(Convert.ToInt32(v_nbShares1.Text)); nbShares.Add(Convert.ToInt32(v_nbShares2.Text)); nbShares.Add(Convert.ToInt32(v_nbShares3.Text)); fields.Add("PX_LAST"); // Data obtain from this Bloomberg fields will be plotted on the reporting Dictionary <string, Dictionary <string, List <HistoData> > > res_histo2 = new Dictionary <string, Dictionary <string, List <HistoData> > >(); List <string> _ticker; List <List <HistoData> > histoPX = new List <List <HistoData> >(); // Download data for each security foreach (string ticker in tickers) { _ticker = new List <string>(); _ticker.Add(ticker); if (wrap.GetHistoricalData(_ticker, fields, BlgPeriodOpt.periodicity_sel, BlgPeriods.daily, rm.DateFrom, rm.DateTo)) { res_histo2 = wrap.ResultHisto; List <HistoData> result = res_histo2[fields.First()].Values.First(); histoPX.Add(result); } else { MessageBox.Show("No data collected for the security " + ticker); return; } } // Get the number of rows of the smallest List<HistoData> int numberOfRows = histoPX[0].Count; foreach (List <HistoData> hd in histoPX) { if (hd.Count < numberOfRows) { numberOfRows = hd.Count; } } #region COMPUTING THE PERFORMANCE OF THE PORTFOLIO FOR GRAPH List <double> value_portfolio = new List <double>(); double sum = 0; for (int i = 0; i < numberOfRows; i++) { sum = 0; for (int j = 0; j < histoPX.Count; j++) //histoPX.Count = number of tickers { sum += histoPX[j][i].Price * nbShares[j]; } value_portfolio.Add(sum); } List <double> perf_portfolio = new List <double>(); perf_portfolio.Add(0); //compute the perf of the portfolio for (int i = 1; i < numberOfRows; i++) { perf_portfolio.Add((value_portfolio[i] / value_portfolio[i - 1]) - 1); } rm.Donnees1 = new List <HistoData>(); for (int i = 0; i < perf_portfolio.Count; i++) { HistoData data = new HistoData(res_histo2[fields.First()].Values.First()[i].Date, perf_portfolio[i]); rm.Donnees1.Add(data); } graph_perf.ItemsSource = null; graph_perf.Items.Clear(); graph_perf.ItemsSource = rm.DonneesChartable; #endregion #region COMPUTING THE VOLATILITY OF THE PORTFOLIO FOR GRAPH rm.Donnees1 = new List <HistoData>(); for (int i = 0; i < numberOfRows; i++) { HistoData data = new HistoData(res_histo2[fields.First()].Values.First()[i].Date, Volatility(histoPX, nbShares, i)); rm.Donnees1.Add(data); } graph_volat.ItemsSource = null; graph_volat.Items.Clear(); graph_volat.ItemsSource = rm.DonneesChartable; #endregion #region COMPUTING PERFOMANCE OF EACH SECURITY FOR GRAPH // Perf of first security rm.Donnees1 = new List <HistoData>(); HistoData hdTMP = new HistoData(histoPX[0][0].Date, 0); rm.Donnees1.Add(hdTMP); for (int i = 1; i < histoPX[0].Count; i++) { hdTMP = new HistoData(histoPX[0][i].Date, (histoPX[0][i].Price / histoPX[0][i - 1].Price) - 1); rm.Donnees1.Add(hdTMP); } graph_1.ItemsSource = null; graph_1.Items.Clear(); graph_1.ItemsSource = rm.DonneesChartable; // Perf of second security rm.Donnees1 = new List <HistoData>(); hdTMP = new HistoData(histoPX[1][0].Date, 0); rm.Donnees1.Add(hdTMP); for (int i = 1; i < histoPX[1].Count; i++) { hdTMP = new HistoData(histoPX[1][i].Date, (histoPX[1][i].Price / histoPX[1][i - 1].Price) - 1); rm.Donnees1.Add(hdTMP); } graph_2.ItemsSource = null; graph_2.Items.Clear(); graph_2.ItemsSource = rm.DonneesChartable; // Perf of third security rm.Donnees1 = new List <HistoData>(); hdTMP = new HistoData(histoPX[2][0].Date, 0); rm.Donnees1.Add(hdTMP); for (int i = 1; i < histoPX[2].Count; i++) { hdTMP = new HistoData(histoPX[2][i].Date, (histoPX[2][i].Price / histoPX[2][i - 1].Price) - 1); rm.Donnees1.Add(hdTMP); } graph_3.ItemsSource = null; graph_3.Items.Clear(); graph_3.ItemsSource = rm.DonneesChartable; #endregion #region COMPUTING THE INITIAL AND LAST VALUES OF THE PORTFOLIO AND THE P&L double price_portfolio_begin = 0; for (int i = 0; i < histoPX.Count; i++) { price_portfolio_begin += histoPX[i][0].Price * nbShares[i]; } double price_portfolio_end = price_portfolio_begin; for (int i = 0; i < perf_portfolio.Count; i++) { price_portfolio_end *= 1 + perf_portfolio[i]; } v_priceportofolio_end.Text = Math.Round(price_portfolio_end, 2).ToString(); // We keep two significant digits v_priceportofolio_begin.Text = Math.Round(price_portfolio_begin, 2).ToString(); // Compute P&L = Last_value - Initial_value v_pl.Text = Math.Round(price_portfolio_end - price_portfolio_begin, 2).ToString(); if (price_portfolio_begin - price_portfolio_end >= 0) { v_pl.Background = Brushes.Red; // Case of negative P&L } else { v_pl.Background = Brushes.Green; // Case of postivie P&L } #endregion // Once the reporting is complete, we can print it PrintButton.IsEnabled = true; } catch (Exception error) { MessageBox.Show(error.Message); } } else { MessageBox.Show("Enter positive values for the number of parts !"); } } else { MessageBox.Show("Please fill in all the required fields"); } } else { MessageBox.Show("Please verify dates"); } }