Example #1
0
        // 从数据库获取的是旧数据,不一定是最新的,如果数据库的内容少于请求,返回所有数据
        // 判断list长度是否为0来判断是否获取成功, path是数据文件夹路径,如 "C:\data" 用反斜杠
        public static List <HistoryStockData> getHistoryDataFromFile(string stockCode, int dayNumber, string path)
        {
            List <HistoryStockData> data = new List <HistoryStockData>();

            using (StreamReader sr = new StreamReader(path + String.Format("\\{0}.csv", stockCode), Encoding.UTF8))
            {
                string sh; int i = 0;

                string[] s; HistoryStockData stockData;
                while (i < dayNumber && (sh = sr.ReadLine()) != null)
                {
                    s = sh.Split(',');

                    stockData              = new HistoryStockData();
                    stockData.date         = s[0];
                    stockData.openPrice    = double.Parse(s[1]);
                    stockData.closePrice   = double.Parse(s[3]);
                    stockData.highestPrice = double.Parse(s[4]);
                    stockData.lowestPrice  = double.Parse(s[2]);
                    stockData.volume       = int.Parse(s[5]);

                    data.Add(stockData);
                    i++;
                }
            }
            return(data);
        }
Example #2
0
        // 判断list长度是否为0来判断是否获取成功,path是数据文件夹路径,如 "C:\data" 严格按照windows资源管理器来
        public static List <HistoryStockData> getHistoryDataAndSaveToFile(string stockCode, int dayNumber, string path)
        {
            stockCode = stockCode.ToLower();

            List <HistoryStockData> data = new List <HistoryStockData>();

            string url = "http://table.finance.yahoo.com/table.csv?s=" + changeCode(stockCode);

            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Headers.Add("apikey", "50d2a55d48e3d9d157d0df783cc01ce2");

            System.Net.HttpWebResponse response;
            try
            {
                response = (System.Net.HttpWebResponse)request.GetResponse();
            }
            catch (Exception ex)
            {
                return(new List <HistoryStockData>());
            }

            StreamReader Reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            Reader.ReadLine();

            string[] s; HistoryStockData stockData;
            for (int i = 0; i < dayNumber; i++)
            {
                s = Reader.ReadLine().Split(',');

                stockData              = new HistoryStockData();
                stockData.date         = s[0];
                stockData.openPrice    = double.Parse(s[1]);
                stockData.closePrice   = double.Parse(s[3]);
                stockData.highestPrice = double.Parse(s[4]);
                stockData.lowestPrice  = double.Parse(s[2]);
                stockData.volume       = int.Parse(s[5]);

                data.Add(stockData);
            }

            reserveToFile(stockCode, data, path);

            return(data);
        }
Example #3
0
        // 从数据库获取的是旧数据,不一定是最新的,如果数据库的内容少于请求,返回所有数据
        // 判断list长度是否为0来判断是否获取成功
        public static List <HistoryStockData> getHistoryDataFormDatabase(string stockCode, int dayNumber)
        {
            string          mysql_str = "server=localhost; User ID=root; Password=123; database=stock_data";
            MySqlConnection con       = new MySqlConnection(mysql_str);

            con.Open();

            string sql;

            sql = String.Format("select * from {0}", stockCode);
            MySqlDataAdapter da = new MySqlDataAdapter(sql, con);
            DataSet          ds = new DataSet();

            da.Fill(ds);
            DataTable dt = ds.Tables[0];

            List <HistoryStockData> data = new List <HistoryStockData>();
            HistoryStockData        stockData;

            if (dt.Rows.Count < dayNumber)
            {
                dayNumber = dt.Rows.Count;
            }

            for (int i = dayNumber - 1; i >= 0; i--)
            {
                DataRow dr = dt.Rows[i];

                stockData              = new HistoryStockData();
                stockData.date         = dr[0].ToString();
                stockData.openPrice    = double.Parse(dr[1].ToString());
                stockData.closePrice   = double.Parse(dr[2].ToString());
                stockData.highestPrice = double.Parse(dr[3].ToString());
                stockData.lowestPrice  = double.Parse(dr[4].ToString());
                stockData.volume       = int.Parse(dr[5].ToString());

                data.Add(stockData);
            }

            con.Close();

            return(data);
        }