예제 #1
0
        public Taifex Option_Top10(Taifex TaifexItem)
        {
            try
            {
                //指定來源網頁
                WebClient url = new WebClient();
                //將網頁來源資料暫存到記憶體內
                MemoryStream ms = new MemoryStream(url.DownloadData("http://www.taifex.com.tw/chinese/3/7_9_tbl.asp"));
                //以GoogleFinance為範例

                // 使用預設編碼讀入 HTML
                HtmlDocument doc = new HtmlDocument();
                HtmlNode     tempNode;
                string       Text;

                doc.Load(ms, Encoding.UTF8);
                tempNode = doc.DocumentNode.SelectSingleNode(@"/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[3]/table[1]/tr[2]/td[1]/table[1]");
                //買權
                Text = tempNode.SelectSingleNode(@"tr[5]/td[2]").InnerText;
                TaifexItem.Option_Top5_Long_Buy = Decimal.Parse(Text.Remove(Text.IndexOf("(")).Replace(",", "").Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[5]/td[3]").InnerText;
                TaifexItem.Option_Top5_Long_Buy_Percent = Decimal.Parse(Text.Remove(Text.IndexOf("%")).Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[5]/td[6]").InnerText;
                TaifexItem.Option_Top5_Short_Buy = Decimal.Parse(Text.Remove(Text.IndexOf("(")).Replace(",", "").Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[5]/td[7]").InnerText;
                TaifexItem.Option_Top5_Short_Buy_Percent = Decimal.Parse(Text.Remove(Text.IndexOf("%")).Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[5]/td[4]").InnerText;
                TaifexItem.Option_Top10_Long_Buy = Decimal.Parse(Text.Remove(Text.IndexOf("(")).Replace(",", "").Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[5]/td[5]").InnerText;
                TaifexItem.Option_Top10_Long_Buy_Percent = Decimal.Parse(Text.Remove(Text.IndexOf("%")).Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[5]/td[8]").InnerText;
                TaifexItem.Option_Top10_Short_Buy = Decimal.Parse(Text.Remove(Text.IndexOf("(")).Replace(",", "").Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[5]/td[9]").InnerText;
                TaifexItem.Option_Top10_Short_Buy_Percent = Decimal.Parse(Text.Remove(Text.IndexOf("%")).Trim().ToString());
                //賣權
                Text = tempNode.SelectSingleNode(@"tr[8]/td[2]").InnerText;
                TaifexItem.Option_Top5_Long_Sell = Decimal.Parse(Text.Remove(Text.IndexOf("(")).Replace(",", "").Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[8]/td[3]").InnerText;
                TaifexItem.Option_Top5_Long_Sell_Percent = Decimal.Parse(Text.Remove(Text.IndexOf("%")).Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[8]/td[6]").InnerText;
                TaifexItem.Option_Top5_Short_Sell = Decimal.Parse(Text.Remove(Text.IndexOf("(")).Replace(",", "").Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[8]/td[7]").InnerText;
                TaifexItem.Option_Top5_Short_Sell_Percent = Decimal.Parse(Text.Remove(Text.IndexOf("%")).Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[8]/td[4]").InnerText;
                TaifexItem.Option_Top10_Long_Sell = Decimal.Parse(Text.Remove(Text.IndexOf("(")).Replace(",", "").Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[8]/td[5]").InnerText;
                TaifexItem.Option_Top10_Long_Sell_Percent = Decimal.Parse(Text.Remove(Text.IndexOf("%")).Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[8]/td[8]").InnerText;
                TaifexItem.Option_Top10_Short_Sell = Decimal.Parse(Text.Remove(Text.IndexOf("(")).Replace(",", "").Trim().ToString());
                Text = tempNode.SelectSingleNode(@"tr[8]/td[9]").InnerText;
                TaifexItem.Option_Top10_Short_Sell_Percent = Decimal.Parse(Text.Remove(Text.IndexOf("%")).Trim().ToString());
                //清除資料
                doc = null;
                url = null;
                ms.Close();
                return(TaifexItem);
            }
            catch (System.IO.IOException e)
            {
                logger.Error(LogUtility.GetExceptionDetails(e));
                return(null);
            }
        }
예제 #2
0
        public NewsView RSSNews()
        {
            try
            {
                string[] StockURL = new string[] {
                    "https://tw.finance.yahoo.com/rss/url/d/e/R1.html",
                    "https://tw.finance.yahoo.com/rss/url/d/e/N2.html",
                    "https://tw.finance.yahoo.com/rss/url/d/e/N10.html",
                    "https://tw.finance.yahoo.com/rss/url/d/e/N11.html",
                    "https://tw.finance.yahoo.com/rss/url/d/e/R3.html"
                };

                string[] GlobalURL = new string[] {
                    "https://tw.finance.yahoo.com/rss/url/d/e/N12.html",
                    "https://tw.finance.yahoo.com/rss/url/d/e/R6.html"
                };

                NewsView    StockNews = new NewsView();
                XmlDocument xmlDoc = new XmlDocument();
                XmlNodeList publishList, titleList, descriptionList, linkList;
                StockNews.NewsList       = new List <News>();
                StockNews.GlobalNewsList = new List <News>();

                for (int j = 0; j < StockURL.Count(); j++)
                {
                    xmlDoc.Load(StockURL[j]);
                    publishList     = xmlDoc.SelectNodes("//pubDate");
                    titleList       = xmlDoc.SelectNodes("//title");
                    descriptionList = xmlDoc.SelectNodes("//description");
                    linkList        = xmlDoc.SelectNodes("//link");

                    for (int i = 2; i < 7; i++)
                    {
                        News TempNews = new News();

                        TempNews.publishDate = publishList[i].InnerText;
                        TempNews.title       = titleList[i].InnerText;
                        TempNews.description = descriptionList[i].InnerText;
                        TempNews.link        = linkList[i].InnerText;

                        StockNews.NewsList.Add(TempNews);
                    }
                }

                for (int j = 0; j < GlobalURL.Count(); j++)
                {
                    xmlDoc.Load(StockURL[j]);
                    publishList     = xmlDoc.SelectNodes("//pubDate");
                    titleList       = xmlDoc.SelectNodes("//title");
                    descriptionList = xmlDoc.SelectNodes("//description");
                    linkList        = xmlDoc.SelectNodes("//link");

                    for (int i = 2; i < 10; i++)
                    {
                        News TempNews = new News();

                        TempNews.publishDate = publishList[i].InnerText;
                        TempNews.title       = titleList[i].InnerText;
                        TempNews.description = descriptionList[i].InnerText;
                        TempNews.link        = linkList[i].InnerText;

                        StockNews.GlobalNewsList.Add(TempNews);
                    }
                }

                StockNews.NewsList       = StockNews.NewsList.OrderByDescending(p => p.publishDate).Distinct(new NewsDataRowComparer()).Take(5).ToList();
                StockNews.GlobalNewsList = StockNews.GlobalNewsList.OrderByDescending(p => p.publishDate).Distinct(new NewsDataRowComparer()).Take(5).ToList();

                return(StockNews);
            }
            catch (System.IO.IOException e)
            {
                logger.Error(LogUtility.GetExceptionDetails(e));
                return(null);
            }
        }
예제 #3
0
        public void UpdateMacroEconomic()
        {
            try
            {
                clearData("Economic", "", 0);

                List <Macroeconomic> MacroeconomicList = new List <Macroeconomic>();

                //指定來源網頁
                WebClient url = new WebClient();
                //將網頁來源資料暫存到記憶體內
                MemoryStream ms = new MemoryStream(url.DownloadData("http://www.cnyes.com/economy/indicator/EconomicalFollow/Usa_Major.aspx?id=4"));
                //以台灣銀行為範例

                // 使用預設編碼讀入 HTML
                HtmlDocument doc = new HtmlDocument();
                doc.Load(ms, Encoding.UTF8);
                HtmlNode tempNode;

                tempNode = doc.DocumentNode.CssSelect("#container").ToList()[0];
                tempNode = tempNode.SelectSingleNode(@"div[2]/div[2]/div[2]//div[1]/table[1]");

                // 取得匯率
                for (int i = 3; i < 10; i++)
                {
                    Macroeconomic Index = new Macroeconomic();

                    Index.Publish_Date              = DateTime.Parse(tempNode.SelectSingleNode(@"tr[1]/th[" + i.ToString() + "]").InnerText);
                    Index.CPI                       = Decimal.Parse(tempNode.SelectSingleNode(@"tr[2]/td[" + i.ToString() + "]").InnerText);
                    Index.CPI_GrowthRate            = Decimal.Parse(tempNode.SelectSingleNode(@"tr[4]/td[" + i.ToString() + "]").InnerText);
                    Index.CCPI                      = Decimal.Parse(tempNode.SelectSingleNode(@"tr[5]/td[" + i.ToString() + "]").InnerText);
                    Index.CCPI_GrowthRate           = Decimal.Parse(tempNode.SelectSingleNode(@"tr[7]/td[" + i.ToString() + "]").InnerText);
                    Index.PPI_GrowthRate            = Decimal.Parse(tempNode.SelectSingleNode(@"tr[8]/td[" + i.ToString() + "]").InnerText);
                    Index.Unemployment_Rate         = Decimal.Parse(tempNode.SelectSingleNode(@"tr[9]/td[" + i.ToString() + "]").InnerText);
                    Index.NFP                       = int.Parse(tempNode.SelectSingleNode(@"tr[10]/td[" + i.ToString() + "]").InnerText);
                    Index.Weekly_Work_Hours         = Decimal.Parse(tempNode.SelectSingleNode(@"tr[11]/td[" + i.ToString() + "]").InnerText);
                    Index.Hourly_WageRate           = Decimal.Parse(tempNode.SelectSingleNode(@"tr[14]/td[" + i.ToString() + "]").InnerText);
                    Index.Export_IncreasedRate      = Decimal.Parse(tempNode.SelectSingleNode(@"tr[23]/td[" + i.ToString() + "]").InnerText);
                    Index.Export_PriceRate          = Decimal.Parse(tempNode.SelectSingleNode(@"tr[29]/td[" + i.ToString() + "]").InnerText);
                    Index.Import_IncreasedRate      = Decimal.Parse(tempNode.SelectSingleNode(@"tr[25]/td[" + i.ToString() + "]").InnerText);
                    Index.Import_PriceRate          = Decimal.Parse(tempNode.SelectSingleNode(@"tr[27]/td[" + i.ToString() + "]").InnerText);
                    Index.Housing_Starts            = Decimal.Parse(tempNode.SelectSingleNode(@"tr[30]/td[" + i.ToString() + "]").InnerText);
                    Index.Housing_Starts_GrowthRate = Decimal.Parse(tempNode.SelectSingleNode(@"tr[32]/td[" + i.ToString() + "]").InnerText);
                    Index.Retail_Sales_Rate         = Decimal.Parse(tempNode.SelectSingleNode(@"tr[50]/td[" + i.ToString() + "]").InnerText);
                    Index.Retail_Sales_Rate_NoCar   = Decimal.Parse(tempNode.SelectSingleNode(@"tr[52]/td[" + i.ToString() + "]").InnerText);
                    Index.Personal_Expenditure_Rate = Decimal.Parse(tempNode.SelectSingleNode(@"tr[54]/td[" + i.ToString() + "]").InnerText);
                    Index.Individual_Income_Rate    = Decimal.Parse(tempNode.SelectSingleNode(@"tr[56]/td[" + i.ToString() + "]").InnerText);
                    Index.ICS                       = Decimal.Parse(tempNode.SelectSingleNode(@"tr[63]/td[" + i.ToString() + "]").InnerText);
                    Index.Capacity_Utilization      = Decimal.Parse(tempNode.SelectSingleNode(@"tr[66]/td[" + i.ToString() + "]").InnerText);
                    Index.Durable_Orders            = Decimal.Parse(tempNode.SelectSingleNode(@"tr[70]/td[" + i.ToString() + "]").InnerText);
                    Index.Factory_Orders            = Decimal.Parse(tempNode.SelectSingleNode(@"tr[74]/td[" + i.ToString() + "]").InnerText);
                    Index.Factory_Stock             = Decimal.Parse(tempNode.SelectSingleNode(@"tr[76]/td[" + i.ToString() + "]").InnerText);
                    Index.Factory_Shipments         = Decimal.Parse(tempNode.SelectSingleNode(@"tr[78]/td[" + i.ToString() + "]").InnerText);
                    Index.Manufacturing_PMI         = Decimal.Parse(tempNode.SelectSingleNode(@"tr[87]/td[" + i.ToString() + "]").InnerText);
                    Index.PMI                       = Decimal.Parse(tempNode.SelectSingleNode(@"tr[89]/td[" + i.ToString() + "]").InnerText);

                    MacroeconomicList.Add(Index);
                }

                foreach (var item in MacroeconomicList)
                {
                    db.Macroeconomics.Add(item);
                }
                db.SaveChanges();

                //清除資料
                doc = null;
                url = null;
                ms.Close();
            }
            catch (System.IO.IOException e)
            {
                logger.Error(LogUtility.GetExceptionDetails(e));
                return;
            }
        }
예제 #4
0
 public void clearData(string dataType, string Currency, int Id)
 {
     try
     {
         if (dataType == "Stock")
         {
             IQueryable <Stock> tempDataList = db.Stocks.Where(p => p.Id.Equals(Id));
             List <Stock>       stockItems   = tempDataList.ToList();
             foreach (var item in stockItems)
             {
                 db.Stocks.Remove(item);
             }
             db.SaveChanges();
         }
         else if (dataType == "Exchange")
         {
             IQueryable <Exchange> tempDataList  = db.Exchanges.Where(p => p.Currency.Equals(Currency));
             List <Exchange>       exchangeItems = tempDataList.ToList();
             foreach (var item in exchangeItems)
             {
                 db.Exchanges.Remove(item);
             }
             db.SaveChanges();
         }
         else if (dataType == "Economic")
         {
             IQueryable <Macroeconomic> tempDataList  = db.Macroeconomics;
             List <Macroeconomic>       EconomicItems = tempDataList.ToList();
             foreach (var item in EconomicItems)
             {
                 db.Macroeconomics.Remove(item);
             }
             db.SaveChanges();
         }
         else if (dataType == "CYQ")
         {
             IQueryable <CYQ> tempDataList = db.CYQs.Where(p => p.Stock_Number.Equals(Id));
             List <CYQ>       CYQItems     = tempDataList.ToList();
             foreach (var item in CYQItems)
             {
                 db.CYQs.Remove(item);
             }
             db.SaveChanges();
         }
         else if (dataType == "Voucher")
         {
             IQueryable <Voucher> tempDataList = db.Vouchers.Where(p => p.Stock_Number.Equals(Id));
             List <Voucher>       VoucherItems = tempDataList.ToList();
             foreach (var item in VoucherItems)
             {
                 db.Vouchers.Remove(item);
             }
             db.SaveChanges();
         }
         else if (dataType == "BDI")
         {
             IQueryable <CRB_BDI> tempDataList = db.CRB_BDI;
             List <CRB_BDI>       CRB_BDIItems = tempDataList.ToList();
             foreach (var item in CRB_BDIItems)
             {
                 db.CRB_BDI.Remove(item);
             }
             db.SaveChanges();
         }
     }
     catch (System.IO.IOException e)
     {
         logger.Error(LogUtility.GetExceptionDetails(e));
         return;
     }
 }
예제 #5
0
        public StockItem getStock(string Num)
        {
            try
            {
                StockItem stock = new StockItem();
                //指定來源網頁
                WebClient url = new WebClient();
                //將網頁來源資料暫存到記憶體內
                MemoryStream ms = new MemoryStream(url.DownloadData("https://tw.stock.yahoo.com/q/q?s=" + Num.ToString()));
                //以GoogleFinance為範例

                // 使用預設編碼讀入 HTML
                HtmlDocument    doc = new HtmlDocument();
                HtmlNode        tempNode;
                List <HtmlNode> tdNode;
                string          Text1, Text2;
                doc.Load(ms, Encoding.GetEncoding("big5"));

                tempNode = doc.DocumentNode.SelectSingleNode(@"/html[1]/body[1]/center[1]/table[2]/tr[1]/td[1]/table[1]/tr[2]");
                // 取得股價
                stock.Name = tempNode.SelectSingleNode(@"td[1]").InnerText.ToString();
                if (stock.Name.IndexOf("加") > 0)
                {
                    stock.Name = stock.Name.Remove(stock.Name.IndexOf("加"));
                }
                stock.Time = tempNode.SelectSingleNode(@"td[2]").InnerText.ToString();
                stock.Deal = Decimal.Parse(tempNode.SelectSingleNode(@"td[3]").InnerText.ToString());
                if (tempNode.SelectSingleNode(@"td[4]").InnerText.ToString() == "-")
                {
                    stock.Buy = Decimal.Parse(tempNode.SelectSingleNode(@"td[5]").InnerText.ToString());
                }
                else
                {
                    stock.Buy = Decimal.Parse(tempNode.SelectSingleNode(@"td[4]").InnerText.ToString());
                }
                if (tempNode.SelectSingleNode(@"td[5]").InnerText.ToString() == "-")
                {
                    stock.Sell = Decimal.Parse(tempNode.SelectSingleNode(@"td[4]").InnerText.ToString());
                }
                else
                {
                    stock.Sell = Decimal.Parse(tempNode.SelectSingleNode(@"td[5]").InnerText.ToString());
                }

                tdNode = tempNode.CssSelect("td").ToList();

                Text1        = tdNode[5].InnerText.ToString();
                Text2        = tdNode[6].InnerText.ToString();
                stock.Change = Text1.Remove(Text1.IndexOf(Text2)).Trim();

                stock.Volume  = int.Parse(tdNode[6].InnerText.ToString().Replace(",", "").Trim());
                stock.lastDay = Decimal.Parse(tdNode[7].InnerText.ToString().Trim());
                stock.Open    = Decimal.Parse(tdNode[8].InnerText.ToString().Trim());
                stock.High    = Decimal.Parse(tdNode[9].InnerText.ToString().Trim());
                stock.Low     = Decimal.Parse(tdNode[10].InnerText.ToString().Trim());

                //清除資料
                doc = null;
                url = null;
                ms.Close();

                return(stock);
            }
            catch (System.IO.IOException e)
            {
                logger.Error(LogUtility.GetExceptionDetails(e));
                return(null);
            }
        }
예제 #6
0
        public void UpdateStock(int Num)
        {
            try
            {
                List <Stock> stockList = new List <Stock>();
                int          nodes;
                //指定來源網頁
                WebClient url = new WebClient();
                //將網頁來源資料暫存到記憶體內
                MemoryStream ms = new MemoryStream(url.DownloadData("https://www.google.com/finance/historical?q=TPE%3A" + Num + "&start=0&num=220"));
                //以GoogleFinance為範例

                // 使用預設編碼讀入 HTML
                HtmlDocument    doc = new HtmlDocument();
                List <HtmlNode> tdNode;
                HtmlNode        tempNode;
                string          Text0;
                doc.Load(ms, Encoding.Default);

                Text0    = ((doc.DocumentNode.SelectSingleNode(@"/html[1]/body[1]").CssSelect("#gf-viewc").ToList())[0].CssSelect("h3").ToList())[0].InnerHtml.ToString();
                tempNode = (doc.DocumentNode.SelectSingleNode(@"/html[1]/body[1]").CssSelect("#prices").ToList())[0];
                tdNode   = tempNode.CssSelect("td").ToList();
                nodes    = (tempNode.CssSelect("tr").Count()) / 2;

                // 取得股價
                for (int num = 0; num < nodes; num++)
                {
                    string Text1, Text2;
                    Stock  stock_temp = new Stock();
                    stock_temp.Id = Num;

                    if (Text0.IndexOf("Co.") > 0)
                    {
                        stock_temp.stockName = Text0.Remove(Text0.IndexOf("Co.")).Trim();
                    }
                    else
                    {
                        stock_temp.stockName = Text0.Trim();
                    }

                    Text1 = tdNode[0 + 6 * num].InnerText;
                    Text2 = tdNode[1 + 6 * num].InnerText;
                    stock_temp.Price_Date = DateTime.Parse(Text(Text1, Text2));

                    Text1 = tdNode[1 + 6 * num].InnerText;
                    Text2 = tdNode[2 + 6 * num].InnerText;
                    stock_temp.Price_Open = Decimal.Parse(Text(Text1, Text2));

                    Text1 = tdNode[2 + 6 * num].InnerText;
                    Text2 = tdNode[3 + 6 * num].InnerText;
                    stock_temp.Price_High = Decimal.Parse(Text(Text1, Text2));

                    Text1 = tdNode[3 + 6 * num].InnerText;
                    Text2 = tdNode[4 + 6 * num].InnerText;
                    stock_temp.Price_Low = Decimal.Parse(Text(Text1, Text2));

                    Text1 = tdNode[4 + 6 * num].InnerText;
                    Text2 = tdNode[5 + 6 * num].InnerText;
                    stock_temp.Price_Close = Decimal.Parse(Text(Text1, Text2));

                    Text1             = tdNode[5 + 6 * num].InnerText;
                    Text2             = tdNode[6 + 6 * num].InnerText;
                    stock_temp.Volume = Int32.Parse(Text(Text1, Text2));

                    stockList.Add(stock_temp);
                }

                foreach (var item in stockList)
                {
                    db.Stocks.Add(item);
                }
                db.SaveChanges();

                //清除資料
                doc = null;
                url = null;
                ms.Close();
            }
            catch (System.IO.IOException e)
            {
                logger.Error(LogUtility.GetExceptionDetails(e));
                return;
            }
        }