C# = Retrieving yahoo historical prices

Quote from TraderMojo:


Code:
private void button1_Click(object sender, EventArgs e)
        {
            //Using an Array list to store the retrieved data. Probably better alternatives...
            ArrayList historicalData = new ArrayList();

            //Retrieve the data from Yahoo! using the parameters specified...
            String yahooURL = String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&a={1}&b={2}&c={3}&d={4}&e={5}&f={6}&g=d&ignore=.csv",this.tickerSymbol.Text,this.startDateMonth.Text,this.startDateDay.Text,this.startDateYear.Text,this.endDateMonth.Text,this.endDateDay.Text,this.endDateYear.Text);
 
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(yahooURL);
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.ASCII);
                
                //Discard the first line with the headers
                String currentLine = reader.ReadLine();

                //Read the rest of the lines
                while (reader.Peek() >= 0)
                {
                    currentLine = reader.ReadLine();
                    //Parse the current line with the comma delimiter
                    string[] values = currentLine.Split(',');
                    
                    //If there are 7 values in this line then assume there is valid OHLC data and it is not a dividend line
                    if (values.Length == 7)
                    {
                        //Add it to our historical data
                        historicalData.Add(new OHLC(values[0], values[1], values[2], values[3],values[4]));
                    }
                }
                reader.Close();
            }
            catch (Exception ee)
            {
                //Alert user that unable to retrieve historical data
                MessageBox.Show(ee.ToString());
            }
            
            //Bind the ArrayList to the datagrid
            resultsGrid.DataSource = historicalData;
        }





I follow most of this. I have a question about the

resultsGrid.DataSource = historicalData;

Do I need to initialize resultsGrid, or include a header or something to use that object?

-uncleTom
 
Quote from uncleTom:


Do I need to initialize resultsGrid, or include a header or something to use that object?

-uncleTom

I just dropped a DataGridView component from the toolbox on the form and gave it a name (resultsGrid). No initialization or headers required. The only code I added to the form is the code in my earlier post.

I did this in Visual C# community edition. My first piece of C# code ever...you've been warned.

Oh, the other bit of code was the OHLC class. Something like this:

Code:
class OHLC
    {
        private string date;
        private string open;
        private string high;
        private string low;
        private string close;

        public OHLC(string date, string open, string high, string low, string close)
        {
            this.date = date;
            this.open = open;
            this.high = high;
            this.low = low;
            this.close = close;
        }

        public string Date
        {
            get { return date; }
        }

        public string Open
        {
            get { return open; }
        }

        public string High
        {
            get { return high; }
        }

        public string Low
        {
            get { return low; }
        }

        public string Close
        {
            get { return close; }
        }

    }

Note: everything is a string. No datatype conversions etc. and no Volume property which you'd probably want to add.

If I knew I was going to get so many questions about this (PMs) I would have saved the code!
 
Mojo,

Big help, thanks a bunch. Good luck with your automation stuff by the way. I'm sort of "climbing that same ladder". Going to a lot of interviews in Chicago, looking for a entry position.


ANyway - first time with C#. Seems like you can do a lot of stuff like Vb.net. I'm going to read up on the diff. within in C++.

Thanks again
-chris
 
Back
Top