////////////////////////////////////////////////////////// //deals with loading stocks public static StockInformation[] Loadstocks(string _folderName) { //gets all the filenames within the stock directory string[] _fileNames = Directory.GetFiles(_folderName, "*.xml").Select(Path.GetFileNameWithoutExtension).ToArray(); StockInformation[] _stockInfo = new StockInformation[_fileNames.Length]; //creating an array to store info //loops through all filenames and gets the stock info for (int i = 0; i < _fileNames.Length; i++) { //creates xml reader object to get stock info XmlTextReader _reader = new XmlTextReader(_fileNames[i] + ".xml"); string _working = ""; //only saves information when there is information present while (_reader.Read()) { switch (_reader.NodeType) { case XmlNodeType.Text: { _working = _reader.Value; break; } } } _stockInfo[i] = ConvertStringToStockInformation(_working); } return(_stockInfo); }
private void UpdateStock(string _file) { StockInformation _stock = FileHandling.LoadStock(_file); lbl_StockName.Text = _stock.name; lbl_Price.Text = Convert.ToString(_stock.price); lbl_Dividend.Text = Convert.ToString(_stock.dividend); picb_Chart.Load(HtmlHandling.GetChartAddress(_stock.name)); MainForm.Instance.stock = _stock; }
///calculates forward price bounds given price, dividend predictions, time until maturity and interest region public static float[] GetForwardPrices(StockInformation _stock, float[] _dividends, int _timeUntilMaturity, InterestRegion _region) { float[] prices = new float[3]; float riskFreeRate = GetIntrestRate(_region, _timeUntilMaturity); for (int i = 0; i < _dividends.Length; i++) { prices[i] = GetForwardPrice(_stock, _dividends[i], _timeUntilMaturity, riskFreeRate); } return(prices); }
////////////////////////////////////////////////////////// // deals with saving a new stock ///saves the newly found stock public static void SaveStockInformation(StockInformation _stockInformation) { //creates a new xml writing object using (XmlWriter _writer = XmlWriter.Create(_stockInformation.name + ".xml")) { _writer.WriteStartDocument(); _writer.WriteStartElement("Contains_Stock_Information"); //writes stock info _writer.WriteElementString("Stock_Info", string.Format("{0}|{1}|{2}|{3}", _stockInformation.name, _stockInformation.price, _stockInformation.dividend, ConvertArrayToString(_stockInformation.dates))); _writer.WriteEndElement(); _writer.WriteEndDocument(); } }
private void Init() { lbl_StockName.BackColor = Colours.calculator; lbl_Price.BackColor = Colours.calculator; lbl_Date.BackColor = Colours.calculator; StockInformation _stock = MainForm.Instance.stock; float[] _dividends = MainForm.Instance.dividendPrediction; int _days = MainForm.Instance.days; lbl_StockName.Text = _stock.name; lbl_Price.Text = _stock.price.ToString(); lbl_Days.Text = _days.ToString() + " days"; lbl_Date.Text = DateTime.Now.ToShortDateString(); lbl_Dividends.Text = MainForm.Instance.divNumber.ToString() + " dividends"; float[] _prices = Calculation.GetForwardPrices(_stock, _dividends, _days, MainForm.Instance.interestRegion); lbl_Bounds.Text = string.Format("Upper: {0}\r\n\r\nMedian: {1}\r\n\r\nLower: {2}", _prices[0], _prices[1], _prices[2]); }
//calculates indivdual forward price private static float GetForwardPrice(StockInformation _stock, float dividend, int days, float riskFreeRate) { //initial double _forward = _stock.price * Math.Exp(riskFreeRate / 100 * days); int[] _daysUntilPayments = new int[_stock.dates.Length]; //array to store days until dividend payment //loops through all dividends and gets the days until the payment for (int i = 0; i < _daysUntilPayments.Length; i++) { _daysUntilPayments[i] = DateHandling.DaysUntil(_stock.dates[i]); } //loops through checking if dividend will be paid within time, if so it takes payment off price for (int i = 0; i < _daysUntilPayments.Length; i++) { if (_daysUntilPayments[i] < days) { _forward -= dividend * Math.Exp(riskFreeRate * (days - _daysUntilPayments[i])); } } return((float)_forward); }