1

I've got a huge string with stock quotes information. It looks like this:

Date,Open,High,Low,Close,Volume,Adj Close
2016-05-06,49.919998,50.389999,49.66,50.389999,24715600,50.389999
2016-05-05,49.869999,50.299999,49.73,49.939999,25309500,49.939999
2016-05-04,49.84,50.060001,49.459999,49.869999,24171400,49.869999...........

How can I package it into generic List<Stock>:

Stock hs = new Stock();
hs.Date = Convert.ToDateTime();
hs.Open = Convert.ToDouble();
etc.

MyList.Add(hs)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Noooom
  • 67
  • 5
  • 2
    Step 1 - split the one big string into lines. Step 2 - split each line into separate values (a string array, using `string.Split`). Step 3 - convert each array of values into a `Stock`. LINQ will make the code for the last step simpler. – Jon Skeet May 08 '16 at 07:56
  • Have a look into this: http://stackoverflow.com/questions/1898/csv-file-imports-in-net or this http://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-into-an-array – Alex May 08 '16 at 07:57
  • Are these quotes for the same stock? If not how do you differentiate the quotes of each stock? – Steve May 08 '16 at 08:03
  • @Steve Yes, There are quotes of Apple. I got stocks quotes info for the 5 year. – Noooom May 08 '16 at 08:32
  • @Noooom Do you have single string or multiple strings? I don't see in your example two lines separated by any character. – Hari Prasad May 08 '16 at 10:04
  • @Noooom but your answer says you have multiple strings. – Hari Prasad May 08 '16 at 11:00
  • @HariPrasad Yes, because I choosed another way to realize it. In the first one I did it like this one `string data = web.DownloadString(string.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&c={1}", ticker, yearToStartFrom));` And It hadn't any columns or rows. That's why csv was needed. – Noooom May 08 '16 at 11:13

2 Answers2

2

With following assumptions,

  1. Every values is separated by , and your input is one long string with the mentioned pattern.
  2. The first row has column names.

.You could do this using these Linq statements.

    string input = @"Date,Open,High,Low,Close,Volume,AdjClose,
                    2016-05-06,49.919998,50.389999,49.66,50.389999,24715600,50.389999,
                    2016-05-05,49.869999,50.299999,49.73,49.939999,25309500,49.939999,
                    2016-05-04,49.84,50.060001,49.459999,49.869999,24171400,49.869999";

    var stacks = input.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries)
                      .Select((x,i)=> new {index= i/7, item=x }) // split with 7 columns
                      .Where(x=>x.index !=0) // skip header row.
                      .GroupBy(x=>x.index)                      
                      .Select(x=> new Stack() 
                             {
                                 Date = DateTime.ParseExact(x.First().item.Trim(),"yyyy-MM-dd", CultureInfo.InvariantCulture),
                                 Open = double.Parse(x.Skip(1).First().item),
                                 High = double.Parse(x.Skip(2).First().item),
                                 Low = double.Parse(x.Skip(3).First().item),
                                 Close = double.Parse(x.Skip(4).First().item),
                                 Volume = double.Parse(x.Skip(5).First().item),
                                 AdjClose = double.Parse(x.Skip(6).First().item),

                             })
                      .ToList();

Check this Demo

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0

So, I did it in this way:

My HisctoricalStock class:

public class HistoricalStock
    {
        public DateTime Date { get; set; }
        public double Open { get; set; }
        public double High { get; set; }
        public double Low { get; set; }
        public double Close { get; set; }
        public double Volume { get; set; }
        public double AdjClose { get; set; }
    }

And I solved my problem using external csv file (But it's optional).

List<HistoricalStock> retval = new List<HistoricalStock>();

                try
                {
                    File.WriteAllText("G:/Test.csv", web.DownloadString(string.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&c={1}", ticker, yearToStartFrom)));
                }
                catch (FileNotFoundException exc)
                {
                    Console.WriteLine(exc.Message);
                }

                StreamReader sr = new StreamReader("G:/Test.csv");

                string currentLine;
                List<string> stoksList = new List<string>();

                // while stockList isn't empty
                while ((currentLine = sr.ReadLine()) != null)
                    stoksList.Add(currentLine);

                // First row is a header 
                stoksList.RemoveAt(0);

                foreach (string str in stoksList)
                {
                    string[] parsedString = str.Split(',');

                    HistoricalStock hs = new HistoricalStock();

                    hs.Date = Convert.ToDateTime(parsedString[0]);
                    hs.Open = Convert.ToDouble(parsedString[1]);
                    hs.High = Convert.ToDouble(parsedString[2]);
                    hs.Low = Convert.ToDouble(parsedString[3]);
                    hs.Close = Convert.ToDouble(parsedString[4]);
                    hs.Volume = Convert.ToDouble(parsedString[5]);
                    hs.AdjClose = Convert.ToDouble(parsedString[6]);

                    retval.Add(hs);
                }
Noooom
  • 67
  • 5