/// <summary>
        /// 取得基本分析指標資料
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<StockChartData> StockPriceDataToChart(List<StockPrice> source)
        {
            List<StockChartData> result = new List<StockChartData>();
            int idx = 0;
            StockPrice priceYesterday = new StockPrice();
            foreach (StockPrice priceData in source.OrderBy(s => s.Date))
            {
                ++idx;
                StockChartData chartDataToday = new StockChartData();
                chartDataToday.PriceToday = priceData;
                if (idx > 1)
                {
                    chartDataToday.PriceYesterday = priceYesterday;
                }
                chartDataToday.ChartIdx = idx;

                result.Add(chartDataToday);

                processTechnicalAnalysisIndicators(result, idx, chartDataToday);

                priceYesterday = chartDataToday.PriceToday;
            }

            return result;
        }
Пример #2
0
        public List<StockPrice> GetStockRealTimePrice(List<StockData> stockList, DateTime date)
        {
            List<StockPrice> result = new List<StockPrice>();

            // 先處理要查詢的字串
            List<string> queryStocks = new List<string>();
            foreach (StockData stock in stockList)
            {
                if (stock.Class == "上櫃")
                {
                    queryStocks.Add(String.Format("otc_{0}.tw_{1}", stock.StockId, date.ToString("yyyyMMdd")));
                }
                else
                {
                    queryStocks.Add(String.Format("tse_{0}.tw_{1}", stock.StockId, date.ToString("yyyyMMdd")));
                }
            }

            // 下載json
            WebClient wc = new WebClient();
            wc.Headers.Add("User-Agent", HttpHelper.GetRandomAgent());
            wc.Encoding = Encoding.UTF8;
            string json = wc.DownloadString(String.Format(ConfigHelper.StockRealDataUrl, String.Join("|", queryStocks.ToArray()), date.ToString("yyyyMMdd")));

            // 分析json資料
            StockRealData realData = JsonConvert.DeserializeObject<StockRealData>(json);
            if (realData.msgArray.Count() > 0)
            {
                foreach (Msgarray msg in realData.msgArray)
                {
                    StockPrice price = new StockPrice();
                    price.StockId = msg.c.Trim();
                    price.Date = date;// Convert.ToDateTime(String.Format("{0} {1}", date.ToString("yyyy/MM/dd"), realData.queryTime.sysTime));
                    price.Open = Convert.ToDecimal(msg.o);
                    price.High = Convert.ToDecimal(msg.h);
                    price.Low = Convert.ToDecimal(msg.l);
                    price.Close = Convert.ToDecimal(msg.z);
                    price.Amount = Convert.ToInt32(msg.v);

                    result.Add(price);
                }
            }
            return result;
        }
Пример #3
0
        /// <summary>
        /// 從網路直接下載上市櫃股票價格檔並解析
        /// </summary>
        /// <param name="stock"></param>
        /// <param name="fetchDate"></param>
        /// <param name="type">1: 上市, 2: 上櫃</param>
        /// <returns></returns>
        private List<MyStockAnalyzer.Classes.StockPrice> downloadStockPriceData(MyStockAnalyzer.Classes.StockData stock, DateTime fetchDate, string type)
        {
            List<MyStockAnalyzer.Classes.StockPrice> result = new List<MyStockAnalyzer.Classes.StockPrice>();
            string downloadUrl = String.Format(type == "1" ? ConfigHelper.StockPriceUrl1 : ConfigHelper.StockPriceUrl2,
                type == "1" ? fetchDate.Year.ToString() : (fetchDate.Year - 1911).ToString("000"), fetchDate.Month.ToString("00"), stock.StockId);

            WebClient wc = new WebClient();
            string csvText = wc.DownloadString(downloadUrl);
            string[] lines = csvText.Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in lines.Skip(type == "1" ? 2 : 5))
            {
                // 上櫃檔案最後一行跳過
                if (type == "2" && line.Equals(lines.Last())) continue;

                string[] fields = CSVHelper.ParseCSVLine(line, ",");
                if (fields.Length == 9)
                {
                    if (fields[3].Equals("--") || fields[4].Equals("--") || fields[5].Equals("--") || fields[6].Equals("--"))
                    {
                        continue;
                    }

                    string[] strDate = (fields[0].Trim().StartsWith("1") ? fields[0].Trim().Substring(0, 9) : fields[0].Trim().Substring(0, 8)).Split(new string[] {"/"}, StringSplitOptions.None);
                    int year = Convert.ToInt32(strDate[0]);
                    if(year < 1000) year += 1911;
                    DateTime date = new DateTime(year, Convert.ToInt32(strDate[1]), Convert.ToInt32(strDate[2]));
                    // date = date.AddYears(1911);

                    StockPrice priceData = new StockPrice();
                    priceData.StockId = stock.StockId;
                    priceData.Date = date;

                    int amount = Convert.ToInt32(fields[1].Replace(",", ""));

                    if (amount != 0)
                    {
                        if (type == "1")
                        {
                            priceData.Amount = amount / 1000;
                        }
                        else if (type == "2") // 上櫃欄位直接是千股
                        {
                            priceData.Amount = amount;
                        }

                        priceData.Open = Convert.ToDecimal(fields[3].Replace(",", ""));
                        priceData.High = Convert.ToDecimal(fields[4].Replace(",", ""));
                        priceData.Low = Convert.ToDecimal(fields[5].Replace(",", ""));
                        priceData.Close = Convert.ToDecimal(fields[6].Replace(",", ""));

                        result.Add(priceData);
                    }
                }
            }

            return result;
        }