}//end of function public void grep_OTC_daily_trading_data(DateTime search_date) { //http://www.otc.org.tw/web/stock/aftertrading/daily_trading_info/st43_print.php?l=zh-tw&d=102/4&stkno=6104&s=0,asc,0 //個股日成交資訊 股票代號:6104 股票名稱:創惟 資料年月:102/4 int num = 0; string url = "http://www.otc.org.tw/web/stock/aftertrading/daily_trading_info/st43_print.php?l=zh-tw&d=" + (search_date.Year - 1911) + "/" + search_date.Month + "&stkno=" + security_code + "&s=0,asc,0"; string select_html_string = "/html[1]/body[1]/table[1]/tbody[1]"; string web_result = network_setting.connect_by_httpwebrequest_get(url, "utf-8"); HtmlDocument web_context = new HtmlDocument(); web_context.LoadHtml(web_result); HtmlNodeCollection child_nodes = web_context.DocumentNode.SelectSingleNode(select_html_string).ChildNodes; foreach (HtmlAgilityPack.HtmlNode node in child_nodes) { if (node.Name == "tr") { num++; daily_trading_value_content content = new daily_trading_value_content(); string date = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[1]").InnerText; string[] dates = date.Split('/'); if (dates[2].Length > 2) { Debug.error(this, debug_level, "刪除股票上櫃掛牌首日的星號"); dates[2] = dates[2].Remove(2);//刪除股票上櫃掛牌首日的星號 } string trade_volume = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[2]").InnerText;//單位是千股 string trade_value = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[3]").InnerText;//單位是千元 trade_volume = trade_volume + ",000";//單位改成股 trade_value = trade_value + ",000";//單位改成元 content.date = (int.Parse(dates[0]) + 1911).ToString() + "/" + dates[1] + "/" + dates[2]; content.trade_volume = trade_volume; content.trade_value = trade_value; content.opening_price = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[4]").InnerText; content.highest_price = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[5]").InnerText; content.lowest_price = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[6]").InnerText; content.closing_price = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[7]").InnerText; content.change = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[8]").InnerText; content.transaction = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[9]").InnerText; content.security_code = security_code.ToString(); date = date.Replace('/', '_'); date = date.Replace("_", ""); string key = security_code + (int.Parse(dates[0]) + 1911).ToString() + dates[1].ToString() + dates[2].ToString();//ex: 173020141001 stock_database.Add(key, content); } } }
//從xml讀取資料到Dictionary,並取得最大成交量和交易金額最大和最小值 個股每月工作天數 public void load_xml_data() { Debug.error(this, debug_level, "load_xml_data"); stock_dictionary.stock_daily_trading_value.Clear(); stock_dictionary.stock_working_day_information.Clear(); string file_path = Path.daily_trading_value_path + security_code + ".xml"; XDocument xml = XDocument.Load(file_path); var linq_stock = from linq in xml.Descendants() where linq.Name.LocalName == "stock" select new { date_linq = linq.Attribute("date"), trade_volume_linq = linq.Attribute("trade_volume"), trade_value_linq = linq.Attribute("trade_value"), opening_price_linq = linq.Attribute("opening_price"), highest_price_linq = linq.Attribute("highest_price"), lowest_price_linq = linq.Attribute("lowest_price"), closing_price_linq = linq.Attribute("closing_price"), change_linq = linq.Attribute("change"), transaction_linq = linq.Attribute("transaction"), id_linq = linq.Attribute("id"), }; foreach (var result in linq_stock) { string opening_price = result.opening_price_linq.Value; string lowest_price = result.lowest_price_linq.Value; string highest_price = result.highest_price_linq.Value; string closing_price = result.closing_price_linq.Value; if ("--" == opening_price) { opening_price = "0"; } if ("--" == lowest_price) { lowest_price = "0"; } if ("--" == highest_price) { highest_price = "0"; } if ("--" == closing_price) { closing_price = "0"; } daily_trading_value_content content = new daily_trading_value_content(); content.date = result.date_linq.Value; content.trade_volume = result.trade_volume_linq.Value; content.trade_value = result.trade_value_linq.Value; content.opening_price = opening_price; content.highest_price = highest_price; content.lowest_price = lowest_price; content.closing_price = closing_price; content.change = result.change_linq.Value; content.transaction = result.transaction_linq.Value; stock_dictionary.stock_daily_trading_value.Add(result.id_linq.Value, content); //如果交易日內容有問題則不載入這天的交易內容 //為何還要加入這天有問題的日子?因為時間軸是只要在daily_trading_value/xxxx.xml裡面有出現的日子都會加入 //為了保持時間軸的寬度正確仍然加入這天 if ("0" == result.transaction_linq.Value)//交易次數是0 { continue; } } //找出交易金額的最大值和最小值 for (DateTime date = start_date; date <= end_date; date = date.AddDays(1)) { string key = security_code.ToString() + date.Year.ToString() + date.Month.ToString("00") + date.Day.ToString("00"); if (stock_dictionary.stock_daily_trading_value.ContainsKey(key)) { decimal lowest_price = decimal.Parse(stock_dictionary.stock_daily_trading_value[key].lowest_price); decimal highest_price = decimal.Parse(stock_dictionary.stock_daily_trading_value[key].highest_price); if (lowest_price < min_price) { min_price = lowest_price - lowest_price * 1 / 10; } if (highest_price > max_price) { max_price = highest_price + highest_price * 2 / 10; } } } //計算每月工作天 int working_day = 0; int non_working_day = 0; for (DateTime date = start_date; date <= end_date; date = date.AddDays(1)) { string stock_key = security_code.ToString() + date.Year.ToString() + date.Month.ToString("00") + date.Day.ToString("00"); if (stock_dictionary.stock_daily_trading_value.ContainsKey(stock_key)) { working_day++; } else { non_working_day++; } //該月最後一天或迴圈最後一天就結算 if (date.Month != date.AddDays(1).Month || date == DateTime.Today) { for (DateTime create_date = DateTime.Parse(date.Year.ToString() + "/" + date.Month.ToString() + "/1"); create_date <= date; create_date = create_date.AddDays(1)) { string stock_working_day_key = create_date.Year.ToString() + create_date.Month.ToString("00") + create_date.Day.ToString("00") + security_code.ToString(); working_date date_content = new working_date(); date_content.working_days = working_day; date_content.non_working_days = non_working_day; stock_dictionary.stock_working_day_information.Add(stock_working_day_key, date_content); } working_day = 0; non_working_day = 0; } } Debug.error(this, debug_level, "load_xml_data X"); }
public void grep_daily_trading_data(DateTime search_date) { int num = 0; //http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/genpage/Report200910/200910_F3_1_8_2342.php?STK_NO=2342&myear=2009&mmon=10 string url = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/genpage/Report" + search_date.Year.ToString() + search_date.Month.ToString("00") + "/" + search_date.Year.ToString() + search_date.Month.ToString("00") + "_F3_1_8_" + security_code + ".php?STK_NO=" + security_code + "&myear=" + search_date.Year.ToString() + "&mmon=" + search_date.Month.ToString("00"); string select_html_string = "/html[1]/body[1]/table[1]/tr[3]/td[1]/table[3]"; string web_result = network_setting.connect_by_httpwebrequest_get(url, "big5"); if ("404" == web_result) { Debug.error(this, debug_level, "找不到網頁"); return; } HtmlDocument web_context = new HtmlDocument(); web_context.LoadHtml(web_result); HtmlNodeCollection child_nodes = web_context.DocumentNode.SelectSingleNode(select_html_string).ChildNodes; foreach (HtmlAgilityPack.HtmlNode node in child_nodes) { if (node.Name == "tr") { num++;//該月份的天數 if (num > 2)//第一個是大標題 第二個是小標題 個股資訊由第三個<tr>開始 { daily_trading_value_content content = new daily_trading_value_content(); string date = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[1]").InnerText; string[] dates = date.Split('/'); content.date = (int.Parse(dates[0]) + 1911).ToString() + "/" + dates[1] + "/" + dates[2]; content.trade_volume = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[2]").InnerText; content.trade_value = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[3]").InnerText; content.opening_price = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[4]").InnerText; content.highest_price = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[5]").InnerText; content.lowest_price = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[6]").InnerText; content.closing_price = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[7]").InnerText; content.change = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[8]").InnerText; content.transaction = web_context.DocumentNode.SelectSingleNode(select_html_string + "/tr[" + num + "]/td[9]").InnerText; content.security_code = security_code.ToString(); date = date.Replace('/', '_'); date = date.Replace("_", ""); string key = security_code + (int.Parse(dates[0]) + 1911).ToString() + dates[1].ToString() + dates[2].ToString();//ex: 173020141001 stock_database.Add(key, content); } } } }//end of function