コード例 #1
0
        /// <summary>
        /// 株価データをローカルキャッシュからロードする
        /// </summary>
        /// <param name="symbol">Ticker Symbol</param>
        /// <param name="FromDate">開始日</param>
        /// <param name="ToDate">終了日</param>
        /// <returns></returns>
        private static List <HistoryPrice> GetStockPricesFromFile(string symbol, DateTime FromDate, DateTime ToDate)
        {
            //キャッシュのパス
            string[] pricelines = File.ReadAllLines(@"StockData\" + symbol + "_"
                                                    + FromDate.ToShortDateString().Replace(@"/", "") + "_" +
                                                    ToDate.ToShortDateString().Replace(@"/", "") + ".txt");

            List <HistoryPrice> prices = new List <HistoryPrice>();

            foreach (var line in pricelines)
            {
                var      h     = new HistoryPrice();
                string[] items = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                h.Date   = DateTime.ParseExact(items[0], "yyyy/MM/dd", null);
                h.Open   = double.Parse(items[1]);
                h.Close  = double.Parse(items[2]);
                h.High   = double.Parse(items[3]);
                h.Low    = double.Parse(items[4]);
                h.Volume = long.Parse(items[5]);

                prices.Add(h);
            }

            return(prices);
        }
コード例 #2
0
        private static async Task <List <HistoryPrice> > ParsePriceAsync(string csvData)
        {
            return(await Task.Run(() =>
            {
                var lst = new List <HistoryPrice>();

                try
                {
                    var rows = csvData.Split(Convert.ToChar(10));

                    for (var i = 1; i <= rows.Length - 1; i++)
                    {
                        var row = rows[i];
                        if (string.IsNullOrEmpty(row))
                        {
                            continue;
                        }

                        var cols = row.Split(',');
                        if (cols[1] == "null")
                        {
                            continue;
                        }

                        var itm = new HistoryPrice
                        {
                            Date = DateTime.Parse(cols[0]),
                            Open = Convert.ToDouble(cols[1]),
                            High = Convert.ToDouble(cols[2]),
                            Low = Convert.ToDouble(cols[3]),
                            Close = Convert.ToDouble(cols[4]),
                            AdjClose = Convert.ToDouble(cols[5])
                        };

                        if (cols[6] != "null")
                        {
                            itm.Volume = Convert.ToInt64(cols[6]);
                        }

                        lst.Add(itm);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                return lst;
            }).ConfigureAwait(false));
        }