/// <summary> /// code that executes when user clicks the go button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void go_button_Click(object sender, EventArgs e) { // Initialize variables string full_url = ""; string resolution = ""; string stock_ticker = stockTickerBox.Text; const string url = "http://ichart.yahoo.com/table.csv?s="; // variables to hold start and end dates for download url (1/1/1990 - today) string start_month = "01"; string start_day = "01"; string start_year = "1990"; string start_date = "01/01/1990"; string end_month = DateTime.Now.Month.ToString(); string end_day = DateTime.Now.Day.ToString(); string end_year = DateTime.Now.Year.ToString(); // Look to see if download button is checked if (download_radio_button.Checked) { // make sure that end date is not before the start date if (validRange(startDatePicker.Value, endDatePicker.Value) && startDatePicker.Value < DateTime.Now.AddDays(-1) && endDatePicker.Value <= DateTime.Now) { // if all is valid, pull data from Yahoo's API welcomeLabel.Text = "Downloading..."; full_url = url + stock_ticker + "&a=" + start_month + "&b=" + start_day + "&c=" + start_year + "&d=" + end_month + "&e=" + end_day + "&f=" + end_year; resolution = ""; char[] resolutions = { 'd', 'w', 'm' }; string[] directories = createDirectories(); foreach (char res in resolutions) { // set the correct resolution if (res == 'd') { resolution = "daily"; } else if (res == 'w') { resolution = "weekly"; } else { resolution = "monthly"; } // initialize the aStock object with period type, dates, and resolutions as arguments aStock.aPeriodType period = (aStock.aPeriodType)Enum.Parse(typeof(aStock.aPeriodType), resolution.ToUpper()); List<aCandlestick> cStick = new List<aCandlestick>(); aStock newstock = new aStock(DateTime.Parse(start_date), DateTime.Now, period, cStick); // build the download link string download_link = full_url + "&g=" + res + "&ignore=.csv"; // read the stock from yahoo and download it to a file newstock.ReadFromURL(download_link); foreach (string directory in directories) { if (Char.ToUpper(res) == directory[17]) // 17 is the position of the resolution in the directory { string filename = directory + stock_ticker + ".CSV"; // append .CSV to the end of the filename newstock.SaveToFile(filename); // finally, save data to csv file } } } welcomeLabel.Text = "Successfully downloaded data for " + stock_ticker + "."; //MessageBox.Show("Successfully downloaded data for " + stock_ticker + "."); } else { // Throw an error MessageBox.Show("Please enter a valid date range."); return; } } else { welcomeLabel.Text = "Welcome! Please select an option below."; // make sure date range for displaying data is valid if (validRange(startDatePicker.Value, endDatePicker.Value) && startDatePicker.Value < DateTime.Now.AddDays(-1) && endDatePicker.Value <= DateTime.Now) { // if daily resolution button is checked if (daily_radio_button.Checked) { // set resolution name to daily resolution = "Daily"; // create filename full_url = url + stock_ticker + "&a=" + start_month + "&b=" + start_day + "&c=" + start_year + "&d=" + end_month + "&e=" + end_day + "&f=" + end_year; string filename = "C:\\DEV\\STOCKDATA\\" + resolution.ToUpper() + "\\" + stock_ticker + ".CSV"; // pull data from stock file if it exists if (File.Exists(filename)) { // create an aStock oject and populate it aStock.aPeriodType period = (aStock.aPeriodType)Enum.Parse(typeof(aStock.aPeriodType), resolution.ToUpper()); List<aCandlestick> cStick = new List<aCandlestick>(); aStock newstock = new aStock(startDatePicker.Value.Date, endDatePicker.Value.Date, period, cStick); newstock.ReadFromFile(filename); // create a modeless form that will display the stock data Form2 f = new Form2(); f.Owner = this; f.Show(this); f.createCandleStickChart(); // create a new candlestick chart for the modeless window // display the data f.displayData(newstock.Candlestick, stock_ticker, period.ToString(), newstock.StartingDate, newstock.EndingDate); //pull data from CSV and populate the chart return; } else { // ... otherwise instruct user to download the data first MessageBox.Show("Please download the stock data first."); return; } } else if (weekly_radio_button.Checked) { // set resolution resolution = "Weekly"; // create filename full_url = url + stock_ticker + "&a=" + start_month + "&b=" + start_day + "&c=" + start_year + "&d=" + end_month + "&e=" + end_day + "&f=" + end_year; string filename = "C:\\DEV\\STOCKDATA\\" + resolution.ToUpper() + "\\" + stock_ticker + ".CSV"; // pull data from stock file if it exists if (File.Exists(filename)) { aStock.aPeriodType period = (aStock.aPeriodType)Enum.Parse(typeof(aStock.aPeriodType), resolution.ToUpper()); List<aCandlestick> cStick = new List<aCandlestick>(); aStock newstock = new aStock(startDatePicker.Value.Date, endDatePicker.Value.Date, period, cStick); newstock.ReadFromFile(filename); Form2 f = new Form2(); f.Owner = this; f.Show(this); f.createCandleStickChart(); // create a new one f.displayData(newstock.Candlestick, stock_ticker, period.ToString(), newstock.StartingDate, newstock.EndingDate); //pull data from CSV and populate the chart return; } else { MessageBox.Show("Please download the stock data first."); return; } } else { // set resolution resolution = "Monthly"; // create filename full_url = url + stock_ticker + "&a=" + start_month + "&b=" + start_day + "&c=" + start_year + "&d=" + end_month + "&e=" + end_day + "&f=" + end_year; string filename = "C:\\DEV\\STOCKDATA\\" + resolution.ToUpper() + "\\" + stock_ticker + ".CSV"; // pull data from stock file if it exists if (File.Exists(filename)) { aStock.aPeriodType period = (aStock.aPeriodType)Enum.Parse(typeof(aStock.aPeriodType), resolution.ToUpper()); List<aCandlestick> cStick = new List<aCandlestick>(); aStock newstock = new aStock(startDatePicker.Value.Date, endDatePicker.Value.Date, period, cStick); newstock.ReadFromFile(filename); Form2 f = new Form2(); f.Owner = this; f.Show(this); f.createCandleStickChart(); // create a new one f.displayData(newstock.Candlestick, stock_ticker, period.ToString(), newstock.StartingDate, newstock.EndingDate); //pull data from CSV and populate the chart return; } else { MessageBox.Show("Please download the stock data first."); return; } // end else } // end else } // end if else { // otherwise throw an error MessageBox.Show("Please enter valid date range."); return; } } // end else }