2 Replies - 1891 Views - Last Post: 26 December 2012 - 03:56 AM Rate Topic: -----

#1 ally257   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-December 12

convert yahoo finance csv data into 2d array

Posted 25 December 2012 - 08:59 PM

Hi Everyone!

This is my first time posting here.
I'm trying to write code to download historical stock price data for companies in S&P500, then convert the data into arrays. So far, I have downloaded the stock data, and I'm trying to convert the csv data into arrays. I would like to convert each csv file into a 2 dimensional array containing date and price, and then ideally name the array as the ticker. I'm going through all the tickers by creating an array of tickers, and a loop that goes through the array one by one, but there's two problems: first, each csv file has a different number of lines, how can I convert them into arrays without specifying the number of rows for the new array? second, how can I name each array as the ticker of the stock it represents?

So far the code I have is here:
using System;
using System.Net;
using System.IO;

public class MainClass
    public static void Main()
        string[] SPX;
        SPX = new string[483] { "MMM", "ABT", "ANF", "ACE", "ACN", "ADBE", "AMD", "AES", "AET", "AFL", "A", "GAS", "APD", "ARG", "AKAM", "AA", "ATI", "AGN", "ALL", "ALTR", "MO", "AMZN", "AEE", "AEP", "AXP", "AIG", "AMT", "AMP", "ABC", "AMGN", "APH", "APC", "ADI", "AON", "APA", "AIV", "APOL", "AAPL", "AMAT", "ADM", "AIZ", "T", "ADSK", "ADP", "AN", "AZO", "AVB", "AVY", "AVP", "BHI", "BLL", "BAC", "BK", "BCR", "BAX", "BBT", "BEAM", "BDX", "BBBY", "BMS", "BRK.B", "BBY", "BIG", "BIIB", "BLK", "HRB", "BMC", "BA", "BXP", "BSX", "BMY", "BRCM", "BF.B", "CHRW", "CA", "CVC", "COG", "CAM", "CPB", "COF", "CAH", "CFN", "KMX", "CCL", "CAT", "CBG", "CBS", "CELG", "CNP", "CTL", "CERN", "CF", "SCHW", "CHK", "CVX", "CMG", "CB", "CI", "CINF", "CTAS", "CSCO", "C", "CTXS", "CLF", "CLX", "CME", "CMS", "COH", "KO", "CCE", "CTSH", "CL", "CMCSA", "CMA", "CSC", "CAG", "COP", "CNX", "ED", "STZ", "GLW", "COST", "CVH", "COV", "CCI", "CSX", "CMI", "CVS", "DHI", "DHR", "DRI", "DVA", "DF", "DE", "DELL", "DNR", "XRAY", "DVN", "DO", "DTV", "DFS", "DISCA", "D", "DOV", "DOW", "DPS", "DTE", "DD", "DUK", "DNB", "ETFC", "EMN", "ETN", "EBAY", "ECL", "EIX", "EW", "EA", "EMC", "EMR", "ETR", "EOG", "EQT", "EFX", "EQR", "EL", "EXC", "EXPE", "EXPD", "ESRX", "XOM", "FFIV", "FDO", "FAST", "FII", "FDX", "FIS", "FITB", "FHN", "FSLR", "FE", "FISV", "FLIR", "FLS", "FLR", "FMC", "FTI", "F", "FRX", "FOSL", "BEN", "FCX", "FTR", "GME", "GCI", "GPS", "GD", "GE", "GIS", "GPC", "GNW", "GILD", "GS", "GT", "GOOG", "GWW", "HAL", "HOG", "HAR", "HRS", "HIG", "HAS", "HCP", "HCN", "HNZ", "HP", "HES", "HPQ", "HD", "HON", "HRL", "HSP", "HST", "HCBK", "HUM", "HBAN", "ITW", "IR", "TEG", "INTC", "ICE", "IBM", "IFF", "IGT", "IP", "IPG", "INTU", "ISRG", "IVZ", "IRM", "JBL", "JEC", "JDSU", "JNJ", "JCI", "JOY", "JPM", "JNPR", "K", "KEY", "KMB", "KIM", "KLAC", "KSS", "KR", "LLL", "LH", "LRCX", "LM", "LEG", "LEN", "LUK", "LIFE", "LLY", "LTD", "LNC", "LLTC", "LMT", "L", "LO", "LOW", "LSI", "MTB", "M", "MRO", "MPC", "MAR", "MMC", "MAS", "MA", "MAT", "MKC", "MCD", "MHP", "MCK", "MJN", "MWV", "MDT", "MRK", "MET", "PCS", "MCHP", "MU", "MSFT", "MOLX", "TAP", "MON", "MCO", "MS", "MOS", "MSI", "MUR", "MYL", "NBR", "NDAQ", "NOV", "NTAP", "NFLX", "NWL", "NFX", "NEM", "NWSA", "NEE", "NKE", "NI", "NE", "NBL", "JWN", "NSC", "NTRS", "NOC", "NU", "NRG", "NUE", "NVDA", "NYX", "ORLY", "OXY", "OMC", "OKE", "ORCL", "OI", "PCAR", "PLL", "PH", "PDCO", "PAYX", "BTU", "JCP", "PBCT", "POM", "PEP", "PKI", "PFE", "PCG", "PM", "PNW", "PXD", "PBI", "PCL", "PNC", "RL", "PPG", "PPL", "PX", "PCP", "PCLN", "PFG", "PG", "PGR", "PLD", "PRU", "PEG", "PSA", "PHM", "QEP", "PWR", "QCOM", "DGX", "RRC", "RTN", "RHT", "RF", "RSG", "RAI", "RHI", "ROK", "COL", "ROP", "ROST", "RDC", "R", "SWY", "SAI", "CRM", "SNDK", "SCG", "SLB", "SNI", "SEE", "SRE", "SHW", "SIAL", "SPG", "SLM", "SJM", "SNA", "SO", "LUV", "SWN", "SE", "S", "STJ", "SWK", "SPLS", "SBUX", "HOT", "STT", "SRCL", "SYK", "STI", "SYMC", "SYY", "TROW", "TGT", "TEL", "TE", "THC", "TDC", "TER", "TSO", "TXN", "TXT", "HSY", "TRV", "TMO", "TIF", "TWX", "TWC", "TIE", "TJX", "TMK", "TSS", "TRIP", "TSN", "TYC", "USB", "UNP", "UNH", "UPS", "X", "UTX", "UNM", "URBN", "VFC", "VLO", "VAR", "VTR", "VRSN", "VZ", "VIAB", "V", "VNO", "VMC", "WMT", "WAG", "DIS", "WPO", "WM", "WAT", "WPI", "WLP", "WFC", "WDC", "WU", "WY", "WHR", "WFM", "WMB", "WIN", "WEC", "WPX", "WYN", "WYNN", "XEL", "XRX", "XLNX", "XL", "XYL", "YHOO", "YUM", "ZMH", "ZION"};
        int number = 0;
        while(number < 483)
            string ticker = SPX[number];

        WebRequest req = WebRequest.Create("http://ichart.finance.yahoo.com/table.csv?s=" + ticker + "&d=11&e=20&f=2012&g=d&a=0&b=2&c=1970&ignore=.csv");
        StreamReader inp = new StreamReader(req.GetResponse().GetResponseStream());
        String line;
        while ((line = inp.ReadLine()) != null)
            //code for declaring a 2d array whose name is the ticker goes here
            //code for splitting string into 2d array goes here
        number = number + 1;

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: convert yahoo finance csv data into 2d array

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: convert yahoo finance csv data into 2d array

Posted 26 December 2012 - 12:29 AM

In line 10 and 12 where does the magic number 483 come from? I really do know where it comes from, I'm just asking why you use it at all? It's best not to have numbers like this that come from, apparently, no where (thus the 'magic number'). Let the system figure it out for you.

Use List<T> to store the data, then the ToArray() method to create an array.

You really can't name the array after the stock ticker (unless you want to get into reflection, and your code will get ugly, fast). I'd just create a Dictionary with a String as a key and your array as the data. Then you can look up any set easily.

This post has been edited by Momerath: 26 December 2012 - 12:31 AM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: convert yahoo finance csv data into 2d array

Posted 26 December 2012 - 03:56 AM

Consider perhaps using this open-source library to make your life easier. Example of what you're trying to do..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1