Esempio n. 1
0
        private void button1_Click(object sender, EventArgs e)
        {


            var start = DateTime.Now;
            textBox1.Text = start.ToString();

            var redis = new RedisClient(ConfigRedis.Host, ConfigRedis.Port);
            var sdt = SqlDb.GetSymbolList(-1);
            foreach (DataRow dr in sdt.Rows)
            {


                Thread.Sleep(1000);
                var start2 = DateTime.Now;

                string symbol = dr["Symbol"].ToString().ToUpper();
                symbol = "SSI";
                try
                {
                    #region Update stock from sql
                    //stock.Symbol = "AAA";
                    //load stock data 
                    var dt = SqlDb.GetSymbolData(symbol);
                    if (dt.Rows.Count <= 0) return;

                    var row = dt.Rows[0];
                    var stock = new Stock() { Symbol = row["Symbol"].ToString(), TradeCenterId = int.Parse(row["TradeCenterId"].ToString()), IsDisabled = row["IsDisabled"].ToString() == "1", StatusText = row["StatusText"].ToString(), ShowTradeCenter = row["ShowTradeCenter"].ToString() == "1", FolderImage = row["FolderChart"].ToString() };

                    //profile
                    var profile = new CompanyProfile { Symbol = stock.Symbol };
                    //profile - basicInfo
                    var basicInfo = new BasicInfo() { Symbol = stock.Symbol, Name = row["CompanyName"].ToString(), TradeCenter = stock.TradeCenterId.ToString() };
                    //profile - basicInfo - basicCommon
                    /*PE = double.Parse(row["PE"].ToString()),*/
                    var basicCommon = new BasicCommon() { AverageVolume = double.Parse(row["AVG10SS"].ToString()), Beta = double.Parse(row["Beta"].ToString()), EPS = double.Parse(row["EPS"].ToString()), Symbol = stock.Symbol, TotalValue = double.Parse(row["MarketCap"].ToString()), ValuePerStock = double.Parse(row["BookValue"].ToString()), VolumeTotal = double.Parse(row["SLCPNY"].ToString()) };
                    basicCommon.PE = basicCommon.EPS != 0 ? (double.Parse(row["LastPrice"].ToString()) / basicCommon.EPS) : 0;

                    basicInfo.basicCommon = basicCommon;
                    //profile - basicInfo - category
                    var category = new CategoryObject() { ID = int.Parse(row["CategoryId"].ToString()), Name = row["CategoryName"].ToString() };
                    basicInfo.category = category;
                    //profile - basicInfo - firstInfo
                    var firstInfo = new FirstInfo() { FirstPrice = double.Parse(row["FirstPrice"].ToString()), FirstTrade = row["FirstTrade"].Equals(DBNull.Value) ? null : ((DateTime?)row["FirstTrade"]), FirstVolume = double.Parse(row["FirstVolume"].ToString()), Symbol = stock.Symbol };
                    basicInfo.firstInfo = firstInfo;

                    profile.basicInfos = basicInfo;

                    //profile - subsidiaries
                    var subsidiaries = new List<OtherCompany>();
                    var associates = new List<OtherCompany>();
                    var cdt = SqlDb.GetChildrenCompany(stock.Symbol);
                    var i = 0;
                    foreach (DataRow cdr in cdt.Rows)
                    {
                        i++;
                        var child = new OtherCompany() { Name = cdr["CompanyName"].ToString(), Note = cdr["NoteInfo"].ToString(), OwnershipRate = double.Parse(cdr["Rate"].ToString()), Order = i, SharedCapital = double.Parse(cdr["TotalShareValue"].ToString()), Symbol = stock.Symbol, TotalCapital = double.Parse(cdr["CharterCapital"].ToString()) };
                        if (cdr["isCongTyCon"].ToString() == "1") subsidiaries.Add(child);
                        else associates.Add(child);
                    }
                    profile.Subsidiaries = subsidiaries;
                    profile.AssociatedCompanies = associates;

                    //profile - commonInfo
                    var commonInfo = new CommonInfo() { Symbol = stock.Symbol, Capital = double.Parse(row["VonDieuLe"].ToString()), Category = row["CategoryName"].ToString(), Content = row["About"].ToString(), OutstandingVolume = double.Parse(row["TotalShare"].ToString()), TotalVolume = double.Parse(row["SLCPNY"].ToString()) };
                    commonInfo.Content += "<p><b>Địa chỉ:</b> " + row["Address"].ToString() + "</p>";
                    commonInfo.Content += "<p><b>Điện thoại:</b> " + row["Phone"].ToString() + "</p>";
                    commonInfo.Content += "<p><b>Người phát ngôn:</b> " + row["Spokenman"].ToString() + "</p>";
                    if (string.IsNullOrEmpty(row["Email"].ToString())) commonInfo.Content += "<p><b>Email:</b> <a href='mailto:" + row["Email"] + "'>" + row["Email"] + "</a></p>";
                    if (string.IsNullOrEmpty(row["Website"].ToString())) commonInfo.Content += "<p><b>Website:</b> <a href='" + row["Website"] + "'>" + row["Website"] + "</a></p>";

                    profile.commonInfos = commonInfo;


                    //profile - financePeriod
                    var fyt = SqlDb.GetFinancePeriod(stock.Symbol);
                    var periods = new List<FinancePeriod>();
                    FinancePeriod period = null;
                    var tmp = 0;
                    foreach (DataRow fyr in fyt.Rows)
                    {
                        if (period == null || tmp != int.Parse(fyr["Year"].ToString()) * 10 + int.Parse(fyr["QuarterType"].ToString()))
                        {
                            if (period != null) { period.UpdateTitle(); periods.Add(period); }
                            period = new FinancePeriod() { Quarter = int.Parse(fyr["QuarterType"].ToString()), Year = int.Parse(fyr["Year"].ToString()) };
                        }
                        tmp = int.Parse(fyr["Year"].ToString()) * 10 + int.Parse(fyr["QuarterType"].ToString());
                        switch (fyr["MaChiTieu"].ToString())
                        {
                            case "Audited":
                                period.SubTitle = fyr["TieuDeNhom"].ToString();
                                break;
                            case "QuarterModify":
                                var qrt = fyr["TieuDeNhom"].ToString();
                                var qrti = 0;
                                if (qrt.EndsWith("T")) { period.QuarterTitle = qrt.Remove(qrt.Length - 1) + " tháng"; }
                                else if (int.TryParse(qrt, out qrti) && qrti >= 1 && qrti < 5)
                                {
                                    period.QuarterTitle = "Quý " + qrti;
                                }
                                else period.QuarterTitle = "";
                                break;
                            case "YearModify":
                                if (int.TryParse(fyr["TieuDeNhom"].ToString(), out qrti))
                                {
                                    period.YearTitle = "Năm " + qrti;
                                }
                                else period.YearTitle = "";
                                break;
                            case "FromDate":
                                qrt = fyr["TieuDeNhom"].ToString();
                                if (qrt.Contains("/"))
                                    period.BeginTitle = qrt.Substring(0, qrt.LastIndexOf("/"));
                                break;
                            case "ToDate":
                                qrt = fyr["TieuDeNhom"].ToString();
                                if (qrt.Contains("/"))
                                    period.EndTitle = qrt.Substring(0, qrt.LastIndexOf("/"));
                                break;
                            default:
                                break;
                        }
                    }
                    if (period != null) { period.UpdateTitle(); periods.Add(period); }
                    profile.FinancePeriods = periods;

                    //profile - financeInfo
                    var financeInfo = new List<FinanceInfo>();
                    var fit = SqlDb.GetChiTieuFinance(stock.Symbol);
                    var fvt = SqlDb.GetFinanceData(stock.Symbol);
                    var groupId = 0;
                    FinanceInfo info = null;
                    foreach (DataRow fir in fit.Rows)
                    {
                        if (info == null || groupId != int.Parse(fir["LoaiChiTieu"].ToString()))
                        {
                            if (info != null) financeInfo.Add(info);
                            info = new FinanceInfo() { NhomChiTieuId = groupId, Symbol = stock.Symbol, TenNhomChiTieu = fir["TenLoaiChiTieu"].ToString() };
                        }
                        groupId = int.Parse(fir["LoaiChiTieu"].ToString());
                        var chiTieu = new FinanceChiTieu() { ChiTieuId = fir["MaChiTieu"].ToString(), TenChiTieu = fir["TieuDeKhac"].ToString() };
                        if (fir["MaChiTieu"].ToString() == "ROA")
                        {
                            int b = 0;
                        }
                        foreach (var financePeriod in periods)
                        {
                            var fvrs = fvt.Select("MaChiTieu = '" + fir["MaChiTieu"] + "' AND Year = " + financePeriod.Year + " AND QuarterType = " + financePeriod.Quarter);
                            if (fvrs.Length > 0)
                            {
                                chiTieu.Values.Add(new FinanceValue() { Quarter = financePeriod.Quarter, Year = financePeriod.Year, Value = double.Parse(fvrs[0]["FinanceValue"].ToString()) });
                            }
                            else
                            {
                                chiTieu.Values.Add(new FinanceValue() { Quarter = financePeriod.Quarter, Year = financePeriod.Year, Value = 0 });
                            }
                        }

                        info.ChiTieus.Add(chiTieu);
                    }
                    if (info != null) financeInfo.Add(info);
                    profile.financeInfos = financeInfo;

                    //profile - leader
                    var leaders = new List<Leader>();
                    var ldt = SqlDb.GetCeos(stock.Symbol);
                    foreach (DataRow ldr in ldt.Rows)
                    {
                        leaders.Add(new Leader() { GroupID = ldr["ParentId"].ToString(), Name = ldr["FullName"].ToString(), Positions = ldr["TenNhom"].ToString() });
                    }
                    profile.Leaders = leaders;

                    //profile - owner
                    var owners = new List<MajorOwner>();
                    var odt = SqlDb.GetShareHolders(stock.Symbol);
                    foreach (DataRow odr in odt.Rows)
                    {
                        owners.Add(new MajorOwner() { Name = odr["FullName"].ToString(), Rate = double.Parse(odr["ShareHoldPct"].ToString()), ToDate = (DateTime)odr["DenNgay"], Volume = double.Parse(odr["SoCoPhieu"].ToString()) });
                    }
                    profile.MajorOwners = owners;

                    stock.CompanyProfile = profile;

                    //business plans
                    var plans = new List<BusinessPlan>();
                    if (row["HasPlan"].ToString() == "1")
                    {
                        plans.Add(new BusinessPlan() { Body = row["PlanNote"].ToString(), Date = (DateTime)row["PlanDate"], DividendsMoney = double.Parse(row["Dividend"].ToString()), DividendsStock = double.Parse(row["DivStock"].ToString()), ID = int.Parse(row["PlanId"].ToString()), IncreaseExpected = double.Parse(row["CapitalRaising"].ToString()), ProfitATax = double.Parse(row["NetIncome"].ToString()), ProfitBTax = double.Parse(row["TotalProfit"].ToString()), Revenue = double.Parse(row["TotalIncome"].ToString()), Symbol = stock.Symbol, Year = int.Parse(row["KYear"].ToString()) });
                    }
                    stock.BusinessPlans1 = plans;

                    //dividend histories
                    var divs = new List<DividendHistory>();
                    var ddt = SqlDb.GetDividendHistory(stock.Symbol);
                    foreach (DataRow ddr in ddt.Rows)
                    {
                        divs.Add(new DividendHistory() { DonViDoiTuong = ddr["DonViDoiTuong"].ToString(), NgayGDKHQ = (DateTime)ddr["NgayGDKHQ"], GhiChu = ddr["GhiChu"].ToString(), SuKien = ddr["SuKien"].ToString(), Symbol = stock.Symbol, TiLe = ddr["TiLe"].ToString() });
                    }
                    stock.DividendHistorys = divs;

                    //báo cáo phân tích
                    var reports = new List<Reports>();
                    var rdt = SqlDb.GetAnalysisReports(stock.Symbol);
                    foreach (DataRow rdr in rdt.Rows)
                    {
                        reports.Add(new Reports() { ID = int.Parse(rdr["ID"].ToString()), Title = rdr["title"].ToString(), DateDeploy = (DateTime)rdr["PublishDate"], ResourceCode = rdr["Source"].ToString(), IsHot = rdr["IsHot"].ToString().ToLower() == "true" });
                    }
                    stock.Reports3 = reports;

                    //công ty cùng ngành
                    var samecateCompanies = new List<StockShortInfo>();
                    var scdt = SqlDb.GetSameCateCompanies(stock.Symbol);
                    foreach (DataRow scdr in scdt.Rows)
                    {
                        samecateCompanies.Add(new StockShortInfo() { Symbol = scdr["StockSymbol"].ToString(), TradeCenterId = int.Parse(scdr["TradeCenterId"].ToString()), Name = scdr["FullName"].ToString(), EPS = double.Parse(scdr["EPS"].ToString()) });
                    }
                    stock.SameCategory = samecateCompanies;

                    //eps tương đương
                    var sameEPSCompanies = new List<StockShortInfo>();
                    var sedt = SqlDb.GetSameEPSCompanies(stock.Symbol);
                    foreach (DataRow sedr in sedt.Rows)
                    {
                        sameEPSCompanies.Add(new StockShortInfo() { Symbol = sedr["StockSymbol"].ToString(), TradeCenterId = int.Parse(sedr["TradeCenterId"].ToString()), Name = sedr["FullName"].ToString(), EPS = double.Parse(sedr["EPS"].ToString()), MarketValue = double.Parse(sedr["MarketCap"].ToString()) });
                    }
                    stock.SameEPS = sameEPSCompanies;

                    //pe tương đương
                    var samePECompanies = new List<StockShortInfo>();
                    var spdt = SqlDb.GetSamePECompanies(stock.Symbol);
                    foreach (DataRow spdr in spdt.Rows)
                    {
                        samePECompanies.Add(new StockShortInfo() { Symbol = spdr["StockSymbol"].ToString(), TradeCenterId = int.Parse(spdr["TradeCenterId"].ToString()), Name = spdr["FullName"].ToString(), EPS = double.Parse(spdr["EPS"].ToString()), MarketValue = double.Parse(spdr["MarketCap"].ToString()) });
                    }
                    stock.SamePE = samePECompanies;

                    //stock history
                    var history = new StockCompactHistory();

                    //stock history - price
                    var price = new List<PriceCompactHistory>();
                    var pdt = SqlDb.GetPriceHistory(stock.Symbol, 10);
                    foreach (DataRow pdr in pdt.Rows)
                    {
                        price.Add(new PriceCompactHistory() { ClosePrice = double.Parse(pdr["Price"].ToString()), BasicPrice = double.Parse(pdr["BasicPrice"].ToString()), Ceiling = double.Parse(pdr["Ceiling"].ToString()), Floor = double.Parse(pdr["Floor"].ToString()), Volume = double.Parse(pdr["Volume"].ToString()), TotalValue = double.Parse(pdr["TotalValue"].ToString()), TradeDate = (DateTime)pdr["TradeDate"] });

                    }
                    history.Price = price;

                    //stock history - order
                    var order = new List<OrderCompactHistory>();
                    var hodt = SqlDb.GetOrderHistory(stock.Symbol, 10);
                    foreach (DataRow hodr in hodt.Rows)
                    {
                        order.Add(new OrderCompactHistory() { AskAverageVolume = double.Parse(hodr["AskAverage"].ToString()), AskLeft = double.Parse(hodr["AskLeft"].ToString()), BidAverageVolume = double.Parse(hodr["BidAverage"].ToString()), BidLeft = double.Parse(hodr["BidLeft"].ToString()), TradeDate = (DateTime)hodr["Trading_Date"] });
                    }
                    history.Orders = order;

                    //stock history - foreign
                    var foreign = new List<ForeignCompactHistory>();
                    var fdt = SqlDb.GetForeignHistory(stock.Symbol, 10);
                    foreach (DataRow fdr in fdt.Rows)
                    {
                        foreign.Add(new ForeignCompactHistory() { BuyPercent = double.Parse(fdr["FBuyPercent"].ToString()), SellPercent = double.Parse(fdr["FSellPercent"].ToString()), NetVolume = double.Parse(fdr["FNetVolume"].ToString()), NetValue = double.Parse(fdr["FNetValue"].ToString()), TradeDate = (DateTime)fdr["Trading_Date"] });
                    }
                    history.Foreign = foreign;

                    stock.StockPriceHistory = history;

                    //tin tức và sự kiện
                    var news = new List<StockNews>();
                    var ndt = SqlDb.GetCompanyNews(stock.Symbol, -1);
                    foreach (DataRow ndr in ndt.Rows)
                    {
                        news.Add(new StockNews() { DateDeploy = (DateTime)ndr["PostTime"], ID = int.Parse(ndr["Id"].ToString()), Title = ndr["Title"].ToString(), TypeID = ndr["ConfigId"].ToString() });
                    }
                    stock.StockNews = news;

                    #endregion
                    var end2 = DateTime.Now;

                    //var st = BLFACTORY.RedisClient.Get<Stock>("stock:stockid:ACB:Object");
                    string key = string.Format(RedisKey.Key, symbol);
                    //var redis = new RedisClient(ConfigRedis.Host, ConfigRedis.Port);
                    if (redis.ContainsKey(key))
                        redis.Set<Stock>(key, stock);
                    else
                        redis.Add<Stock>(key, stock);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(symbol + " : " + ex.ToString());
                }

            }
            DateTime end = DateTime.Now;
            MessageBox.Show("DOne");


            //var set = DateTime.Now;

            //textBox3.Text = set.ToString();

            ////var st = redis.Get<Stock>(key);
            //var get = DateTime.Now;
            //textBox4.Text = get.ToString();

        }
Esempio n. 2
0
 private void GenDataFirstInfo(FirstInfo first)
 {
     if (first == null) return;
     ltrNgayGiaoDich.Text = first.FirstTrade == null ? "" : first.FirstTrade.Value.ToString("dd/MM/yyyy");
     ltrFirstPrice.Text = String.Format("{0:#,##0.0}", ConvertUtility.ToDouble(first.FirstPrice));
     ltrFirstVolume.Text = String.Format("{0:#,##}", ConvertUtility.ToDouble(first.FirstVolume));
 }