///gets all stock information from a year ago to today private static TimeInterval[] GetTimeIntervals(string _stockName) { bool _unavailable = false; //bool to work out when to stop scraping information int _n = 0; // integar to count page numbers List <TimeInterval> _timeIntervals = new List <TimeInterval>(); //list to store everyday of stock information specified string _startDate = DateHandling.GetCurrentYahooDate(0); string _endDate = DateHandling.GetStartDate(); //this loop will only run while there is information to scrape while (!_unavailable) { string _address = GetAddress(_stockName, _startDate, _endDate, _n); // address for next webpage TimeInterval[] _currentTimeIntervals = GetInformationOfPage(_address); // getting stock info of webpage if (_currentTimeIntervals == null) { _unavailable = true; } else { _timeIntervals.AddRange(_currentTimeIntervals); } _n++; } _timeIntervals.Reverse(); return(_timeIntervals.ToArray()); }
///gets all the stock information from a specified webpage private static TimeInterval[] GetInformationOfPage(string _address) { List <TimeInterval> _timeIntervals = new List <TimeInterval>(); //list to store stock info from webpage HtmlDocument _doc = LoadDoc(_address); //grabbing all the information of the webpage //array containing all the rows in the table HtmlNodeCollection _rows = _doc.DocumentNode.SelectNodes("//*[@class='yfnc_tabledata1']"); if (_rows == null) { return(null); //if rows are empty, no more information to scrape } string _previousDate = ""; //string to make sure same date isn't entered twice and instead the original date is changed int _missingCells = 0; //integar to keep the row and timeInterval count alligned in the case of duplicate dates //loop going through each row and creating or ammending timeIntervals with the information for (int i = 0; i < _rows.Count; i++) { int _cell = (i * 7) - _missingCells; //integar that splits the row into itss seperate columns if (_cell >= _rows.Count) { break; //if the last cell has been reached, stop scraping } if (_rows[_cell].InnerText.Contains("adjusted")) { break; //last cell could contain text, in this case stop scraping } //checking if the date is a duplicate, could be something happened to stock on that day or dividend bool _sameDate = (_rows[_cell].InnerText == _previousDate); bool _dividend = (_rows[_cell + 1].InnerText.Contains("Dividend")); //if the cell is a dividend, ammends the previous time interval to include dividend payment if (_dividend) { _timeIntervals[_timeIntervals.Count - 1].dividend = ConvertDividendToFloat(_rows[_cell + 1].InnerText); } //normal circumstances, adding a new time interval to the list if (!_sameDate && DateHandling.IsDate(_rows[_cell].InnerText)) { _timeIntervals.Add( new TimeInterval( _rows[_cell].InnerText, //date ConvertStringToFloat(_rows[_cell + 1].InnerText), //open ConvertStringToFloat(_rows[_cell + 2].InnerText), //high ConvertStringToFloat(_rows[_cell + 3].InnerText), //low ConvertStringToFloat(_rows[_cell + 4].InnerText), //close ConvertStringToFloat(_rows[_cell + 5].InnerText), //volume 0 //dividend ) ); _previousDate = _rows[_cell].InnerText; //sets previous date to this date } if (_sameDate) { _missingCells += 5; //makes up for missing cells due to dividend or other text } } return(_timeIntervals.ToArray()); }
///returns string[] of dividend dates private static string[] GetDividendDates(TimeInterval[] _timeIntervals) { List <string> _dates = new List <string>(); //list to store dates //loops through all dates forwards to get dividend dates for (int i = 0; i < _timeIntervals.Length; i++) { if (_timeIntervals[i].dividend != 0) { _dates.Add(_timeIntervals[i].date); } } return(DateHandling.FixDates(_dates.ToArray())); }
private void Init() { lbl_DividendForecaster.BackColor = Colours.dividend; radiob_Same.BackColor = Colours.dividend; radiob_Different.BackColor = Colours.dividend; int _days = MainForm.Instance.days; try { dates = DateHandling.GetDatesWithinTime(MainForm.Instance.stock.dates, _days); } catch { MessageBox.Show("Cannot enter dividends until stock and timescale have been confirmed"); this.Close(); return; } MainForm.Instance.divNumber = dates.Length; this.Show(); combob_Date.Items.AddRange(dates); foreach (string date in dates) { predictions.Add(new float[3]); } }
public static string[] GetDatesWithinTime(string[] _dates, int _days) { List <string> _datesWithinTime = new List <string>(); int[] _daysUntilPayments = new int[_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(_dates[i]); } for (int i = 0; i < _daysUntilPayments.Length; i++) { if (_daysUntilPayments[i] <= _days) { _datesWithinTime.Add(_dates[i]); } } return(_datesWithinTime.ToArray()); }
//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); }