Ejemplo n.º 1
0
        /// <summary>
        /// uses LINQ to generate monthly and weekly data from daily data
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public aStock generateOtherPeriod(aPeriodType pt)
        {
            //// create new aCandlestick object and set it to cStick
            List<aCandlestick> candlesticks = new List<aCandlestick>();

            // create new aStock object
            aStock newstock = new aStock(startingDate, endingDate, pt, candlesticks);

            if (pt == (aStock.aPeriodType)Enum.Parse(typeof(aStock.aPeriodType), "MONTHLY"))
            {
                // group candlesticks by year and month using LINQ
                var dateGroup = from candlestick in cStick // take the candlesticks...
                                group candlestick by candlestick.StartingDate.Year into ygroup // and GROUP them by year (LINQ)
                                from mgroup in
                                    (from candlestick in ygroup // take year groups...
                                     group candlestick by candlestick.StartingDate.Month)
                                group mgroup by ygroup.Key; // and GROUP them by month (LINQ)

                // now add the grouped candlesticks to a list
                foreach (var candlestickYear in dateGroup)
                {
                    // for each inner month group inside of outer year group
                    foreach (var candlestickMonth in candlestickYear)
                    {
                        // set candlestick values
                        decimal newlow = 1000000;
                        decimal newhigh = 0;
                        DateTime newdate = candlestickMonth.Last().StartingDate;
                        decimal newopen = candlestickMonth.First().Open;
                        decimal newclose = candlestickMonth.Last().Close;
                        double newvolume = 0;

                        // find the highest, lowest, and sum the volumes
                        foreach (var stick in candlestickMonth)
                        {
                            newvolume += stick.Volume;

                            if (stick.Low < newlow)
                            {
                                newlow = stick.Low;
                            }

                            if (stick.High > newhigh)
                            {
                                newhigh = stick.High;
                            }
                        }

                        // create a new aCandlestick and populate it with candlestick data
                        aCandlestick outputStick = new aCandlestick(newdate, newopen, newhigh, newlow, newclose, newvolume);

                        // add it to the newstock object
                        newstock.Candlestick.Add(outputStick);
                    }

                }

            }
            else if (pt == (aStock.aPeriodType)Enum.Parse(typeof(aStock.aPeriodType), "WEEKLY")) // if period is weekly
            {
                // set calendar
                Calendar cal = new GregorianCalendar();
                DayOfWeek firstDay = DayOfWeek.Sunday;

                // set calendar rule
                CalendarWeekRule rule;
                rule = CalendarWeekRule.FirstDay;

                // group candlesticks by year and week using LINQ
                var dateGroup = from candlestick in cStick // take the candlesticks...
                                group candlestick by candlestick.StartingDate.Year into ygroup // and GROUP them by year (LINQ)
                                from wgroup in
                                    (from candlestick in ygroup // take the year groups...
                                     group candlestick by cal.GetWeekOfYear(candlestick.StartingDate, rule, firstDay))
                                group wgroup by ygroup.Key; // and GROUP them by week of the year (LINQ)

                // now add the grouped candlesticks to a list
                foreach (var candlestickYear in dateGroup)
                {
                    // for each inner week group inside of outer year group
                    foreach (var candlestickWeek in candlestickYear)
                    {
                        // set candlesick values
                        decimal newlow = 1000000;
                        decimal newhigh = 0;
                        DateTime newdate = candlestickWeek.Last().StartingDate;
                        decimal newopen = candlestickWeek.First().Open;
                        decimal newclose = candlestickWeek.Last().Close;
                        double newvolume = 0;

                        // find the highest, lowest, and sum the volumes
                        foreach (var stick in candlestickWeek)
                        {
                            newvolume += stick.Volume;

                            if (stick.Low < newlow)
                            {
                                newlow = stick.Low;
                            }

                            if (stick.High > newhigh)
                            {
                                newhigh = stick.High;
                            }
                        }

                        // create a new aCandlestick and populate it with candlestick data
                        aCandlestick outputStick = new aCandlestick(newdate, newopen, newhigh, newlow, newclose, newvolume);

                        // add it to the newstock object
                        newstock.Candlestick.Add(outputStick);
                    }

                }

            }

            // return the newstock to the caller, to be displayed
            return newstock;
        }
Ejemplo n.º 2
0
        /// <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
        }