Пример #1
0
        private void SetStockCodeList(string url, List <StockPriceEntity> list)
        {
            HtmlUtil htmlUtil = new HtmlUtil();

            // urlからWebサイトに接続し情報を取得する
            XDocument xdoc = htmlUtil.ParseHtml(htmlUtil.GetHtml(url));
            var       ns   = xdoc.Root.Name.Namespace;

            // 次へのページが存在するか確認する
            string nextUrl = "";
            var    query1  =
                from q1 in xdoc.Descendants(ns + "ul")
                where (string)q1.Attribute("class") == "ymuiPagingBottom clearFix"
                select q1;

            foreach (var q in query1.Descendants("a"))
            {
                if (q.Value == "次へ")
                {
                    nextUrl = q.Attribute("href").Value;
                }
            }

            var query2 =
                from q2 in xdoc.Descendants(ns + "tr")
                select q2;

            bool first = true;

            foreach (var q1 in query2)
            {
                if (first)
                {
                    first = false;
                    continue;
                }

                StockPriceEntity stock = new StockPriceEntity();
                int  i       = 1;
                bool exitFor = false;

                foreach (var q2 in q1.Descendants())
                {
                    switch (i)
                    {
                    case 1:
                        if (q2.Value == "順位")
                        {
                            exitFor = true;
                            break;
                        }
                        break;

                    case 2:
                        if (Convert.ToDecimal(q2.Value) < 9999)
                        {
                            stock.StockCode = Convert.ToInt32(q2.Value);
                            list.Add(stock);
                        }
                        exitFor = true;
                        break;
                    }

                    i++;

                    if (exitFor)
                    {
                        break;
                    }
                }
            }

            if (nextUrl == "")
            {
                // 次のURLがなければ処理終了
                return;
            }
            else
            {
                // 次のURLがあれば再帰呼び出し
                SetStockCodeList(nextUrl, list);
            }
        }
Пример #2
0
        private async Task UpdateStockPriceTarget()
        {
            Stopwatch sw = new System.Diagnostics.Stopwatch();

            this.txtUpdateStatus.Text          += "株価更新開始" + Environment.NewLine;
            this.txtUpdateStatus.SelectionStart = this.txtUpdateStatus.TextLength;
            this.txtUpdateStatus.ScrollToCaret();


            // 情報の取得
            List <Utility.ProfileEntity>    listProfile    = new List <Utility.ProfileEntity>();
            List <Utility.StockPriceEntity> listStockPrice = new List <Utility.StockPriceEntity>();
            List <Utility.StockPriceEntity> listStock      = new List <Utility.StockPriceEntity>();

            Utility.StockPriceEntity sp;
            int code;

            for (int line = 0; line < this.txtStockCodes.Lines.Length; line++)
            {
                if (int.TryParse(this.txtStockCodes.Lines[line], out code))
                {
                    sp           = new Utility.StockPriceEntity();
                    sp.StockCode = code;
                    listStock.Add(sp);
                }
            }



            foreach (StockPriceEntity r in listStock)
            {
                sw.Restart();


                await Task.Run(() =>
                {
                    Utility.FinanceUtil finance = new Utility.FinanceUtil();
                    //listStockPrice = finance.GetStockPriceEntityList(r.StockCode,DateTime.Now.AddYears(-10).Date,DateTime.Now.Date);
                    listStockPrice = finance.GetStockPriceEntityList(r.StockCode, DateTime.Now.AddMonths(-60).Date, DateTime.Now.Date);
                });


                await Task.Run(() =>
                {
                    if (listStockPrice.Count == 0)
                    {
                        // 株価取得できない場合はスルー
                    }
                    else
                    {
                        using (Utility.DbUtil db = new Utility.DbUtil())
                        {
                            // 削除
                            var query = from q in listStockPrice
                                        where q.StockCode == r.StockCode
                                        select q;

                            db.DBUpdate("DELETE FROM stockprice WHERE StockCode = :StockCode AND StockDate BETWEEN :BeginDate AND :EndDate ",
                                        new { StockCode = r.StockCode, BeginDate = query.Min(stock => stock.StockDate), EndDate = query.Max(stock => stock.StockDate) });

                            // 登録
                            string insertSql = @"INSERT INTO stockprice
                                        ( 
                                          StockCode             
                                         ,CompanyName           
                                         ,StockDate             
                                         ,OpeningPrice          
                                         ,HighPrice             
                                         ,LowPrice              
                                         ,ClosingPrice          
                                         ,TradeVolume           
                                         ,AdjustmentClosingPrice
                                        ) VALUES (
                                          :StockCode             
                                         ,:CompanyName           
                                         ,:StockDate             
                                         ,:OpeningPrice          
                                         ,:HighPrice             
                                         ,:LowPrice              
                                         ,:ClosingPrice          
                                         ,:TradeVolume           
                                         ,:AdjustmentClosingPrice
                                        )";

                            db.DBInsert(insertSql, listStockPrice);
                        }
                    }
                });

                sw.Stop();
                this.txtUpdateStatus.Text          += r.StockCode.ToString().PadLeft(4, '0') + " データ更新 " + sw.Elapsed.ToString() + Environment.NewLine;
                this.txtUpdateStatus.SelectionStart = this.txtUpdateStatus.TextLength;
                this.txtUpdateStatus.ScrollToCaret();
            }

            this.txtUpdateStatus.Text          += "株価更新終了" + Environment.NewLine;
            this.txtUpdateStatus.SelectionStart = this.txtUpdateStatus.TextLength;
            this.txtUpdateStatus.ScrollToCaret();


            // 日経平均、ドル円
            sw.Restart();

            List <DollarYenEntity>     listDY = new List <DollarYenEntity>();
            List <NikkeiAverageEntity> listN  = new List <NikkeiAverageEntity>();


            await Task.Run(() =>
            {
                Utility.FinanceUtil finance = new Utility.FinanceUtil();
                listDY = finance.GetDollarYenEntityList();
                listN  = finance.GetNikkeiAverageEntityList();
            });


            await Task.Run(() =>
            {
                if (listDY.Count == 0)
                {
                    // ドル円が取得できない場合はスルー
                }
                else
                {
                    using (Utility.DbUtil db = new Utility.DbUtil())
                    {
                        // 削除
                        var query = from q in listDY
                                    select q;

                        db.DBUpdate("DELETE FROM dollaryen WHERE ExchangeDate BETWEEN :BeginDate AND :EndDate ",
                                    new { BeginDate = query.Min(dollerYen => dollerYen.ExchangeDate), EndDate = query.Max(dollerYen => dollerYen.ExchangeDate) });

                        // 登録
                        string insertSql = @"INSERT INTO dollaryen
                                        ( 
                                          ExchangeDate             
                                         ,OpeningPrice           
                                         ,HighPrice             
                                         ,LowPrice          
                                         ,ClosingPrice             
                                        ) VALUES (
                                          :ExchangeDate             
                                         ,:OpeningPrice           
                                         ,:HighPrice             
                                         ,:LowPrice          
                                         ,:ClosingPrice             
                                        )";

                        db.DBInsert(insertSql, listDY);
                    }
                }

                if (listN.Count == 0)
                {
                    // 日経平均が取得できない場合はスルー
                }
                else
                {
                    using (Utility.DbUtil db = new Utility.DbUtil())
                    {
                        // 削除
                        var query = from q in listN
                                    select q;

                        db.DBUpdate("DELETE FROM nikkeiaverage WHERE StockDate BETWEEN :BeginDate AND :EndDate ",
                                    new { BeginDate = query.Min(nikkei => nikkei.StockDate), EndDate = query.Max(nikkei => nikkei.StockDate) });

                        // 登録
                        string insertSql = @"INSERT INTO nikkeiaverage
                                        ( 
                                          StockDate             
                                         ,OpeningPrice           
                                         ,HighPrice             
                                         ,LowPrice          
                                         ,ClosingPrice             
                                        ) VALUES (
                                          :StockDate             
                                         ,:OpeningPrice           
                                         ,:HighPrice             
                                         ,:LowPrice          
                                         ,:ClosingPrice             
                                        )";

                        db.DBInsert(insertSql, listN);
                    }
                }
            });


            this.txtUpdateStatus.Text          += "日経平均・ドル/円更新終了" + sw.Elapsed.ToString() + Environment.NewLine;
            this.txtUpdateStatus.SelectionStart = this.txtUpdateStatus.TextLength;
            this.txtUpdateStatus.ScrollToCaret();
        }
Пример #3
0
        private void SetStockPriceEntityList(string url, List <StockPriceEntity> list)
        {
            HtmlUtil  htmlUtil = new HtmlUtil();
            XDocument xdoc;

            try
            {
                xdoc = htmlUtil.ParseHtml(htmlUtil.GetHtml(url));
            }
            catch (Exception)
            {
                xdoc = htmlUtil.ParseHtml(htmlUtil.GetHtml(url));
            }

            // urlからWebサイトに接続し情報を取得する
            var ns = xdoc.Root.Name.Namespace;

            // 次へのページが存在するか確認する
            string nextUrl = "";
            var    query1  =
                from q1 in xdoc.Descendants(ns + "ul")
                where (string)q1.Attribute("class") == "ymuiPagingBottom clearFix"
                select q1;

            foreach (var q in query1.Descendants("a"))
            {
                if (q.Value == "次へ")
                {
                    nextUrl = q.Attribute("href").Value;
                }
            }

            // 時系列株価情報を取得する
            var query2 =
                from q2 in xdoc.Descendants(ns + "table")
                where (string)q2.Attribute("class") == "boardFin yjSt marB6"
                select q2.Descendants(ns + "tr");

            var company =
                from c in xdoc.Descendants(ns + "h1")
                select c;

            var code =
                from c in xdoc.Descendants(ns + "dt")
                select c;


            StockPriceEntity stockPrice;


            foreach (var q1 in query2)
            {
                foreach (var q2 in q1)
                {
                    if (q2.Elements() == null)
                    {
                        continue;
                    }
                    if (q2.Elements().First().Value == "日付")
                    {
                        continue;
                    }

                    decimal d;
                    if (decimal.TryParse(q2.Elements().ElementAt(1).Value, out d) == false)
                    {
                        continue;
                    }

                    stockPrice                        = new StockPriceEntity();
                    stockPrice.StockCode              = Convert.ToInt32(code.First().Value);
                    stockPrice.CompanyName            = company.First().Value;
                    stockPrice.StockDate              = Convert.ToDateTime(q2.Elements().ElementAt(0).Value);
                    stockPrice.OpeningPrice           = Convert.ToDecimal(q2.Elements().ElementAt(1).Value);
                    stockPrice.HighPrice              = Convert.ToDecimal(q2.Elements().ElementAt(2).Value);
                    stockPrice.LowPrice               = Convert.ToDecimal(q2.Elements().ElementAt(3).Value);
                    stockPrice.ClosingPrice           = Convert.ToDecimal(q2.Elements().ElementAt(4).Value);
                    stockPrice.TradeVolume            = Convert.ToDecimal(q2.Elements().ElementAt(5).Value);
                    stockPrice.AdjustmentClosingPrice = Convert.ToDecimal(q2.Elements().ElementAt(6).Value);



                    list.Add(stockPrice);
                }
            }

            if (nextUrl == "")
            {
                // 次のURLがなければ処理終了
                return;
            }
            else
            {
                // 次のURLがあれば再帰呼び出し
                SetStockPriceEntityList(nextUrl, list);
            }
        }