private void UpdatePrice(int centerId, bool bFirst) { try { var redisPrice = new RedisClient(ConfigRedis.PriceHost, ConfigRedis.PricePort); var redis = new RedisClient(ConfigRedis.Host, ConfigRedis.Port); //var pdt = bFirst ? sql.GetAllPrice(centerId) : sql.GetChangedPriceSymbols(centerId); var sql = new SqlDb(); var isInTrading = Utils.InTradingTime(centerId); var bRealtime = isInTrading; if (!isInTrading) { var rlddt = sql.GetLastRealtimePriceDate(centerId); var impdt = sql.GetLastImportPriceDate(centerId); if (impdt.Rows.Count == 0) { bRealtime = rlddt.Rows.Count > 0; } else { if (rlddt.Rows.Count == 0) bRealtime = false; else { bRealtime = int.Parse(rlddt.Rows[0][0].ToString().Replace(".", "")) > int.Parse(impdt.Rows[0][0].ToString().Replace(".", "")); } } } //log.WriteEntry("UpdatePrice : " + centerId + " : " + bRealtime, EventLogEntryType.Information); var pdt = bRealtime ? sql.GetRealtimePrice(centerId) : sql.GetPriceData(centerId); var allkey = string.Format(RedisKey.KeyRealTimePrice, centerId); if (redis.ContainsKey(allkey)) redis.Set(allkey, Utils.Serialize(pdt)); else redis.Add(allkey, Utils.Serialize(pdt)); var stats = new List<int> { 0, 0, 0, 0, 0 }; var rows = pdt.Select("Symbol<>'CENTER'"); //log.WriteEntry("UpdatePrice : " + centerId + " : " + bRealtime + " : " + rows[0]["TradeDate"], EventLogEntryType.Information); foreach (var pdr in rows) { var symbol = pdr["Symbol"].ToString(); try { #region Get Stock Price Data //var cdt = sql.GetCenterId(symbol); //if (cdt.Rows.Count == 0) continue; //var centerId = int.Parse(pdr["centerId"].ToString()); //var pdt = sql.GetRealtimePriceData(symbol, centerId); //if (pdt.Rows.Count == 0) continue; //var pdr = pdt.Rows[0]; var bAvg = centerId != 1 && !isInTrading; var price = redis.Get<StockPrice>(String.Format(RedisKey.PriceKey, symbol)) ?? new StockPrice(); price.Symbol = symbol; price.LastTradeDate = (DateTime)pdr["TradeDate"]; price.Price = double.Parse(pdr[bAvg ? "AveragePrice" : "TradingPrice"].ToString()); price.RefPrice = double.Parse(pdr["Ref"].ToString()); price.CeilingPrice = double.Parse(pdr["Ceiling"].ToString()); price.FloorPrice = double.Parse(pdr["Floor"].ToString()); price.Volume = double.Parse(pdr["TradingVol"].ToString()); price.Value = double.Parse(pdr["TotalTradingValue"].ToString()); price.HighPrice = double.Parse(pdr["TradingPriceMax"].ToString()); price.LowPrice = double.Parse(pdr["TradingPriceMin"].ToString()); price.OpenPrice = double.Parse(pdr["OpenPrice"].ToString()); price.ClosePrice = double.Parse(pdr["TradingPrice"].ToString()); try { price.AvgPrice = double.Parse(pdr["AveragePrice"].ToString()); price.ClosePrice = double.Parse(pdr["TradingPrice"].ToString()); } catch (Exception) { } price.BidTotalOrder = double.Parse(pdr["TotalBidOrder"].ToString()); price.BidTotalVolume = double.Parse(pdr["TotalBidVolume"].ToString()); price.AskTotalOrder = double.Parse(pdr["TotalAskOrder"].ToString()); price.AskTotalVolume = double.Parse(pdr["TotalAskVolume"].ToString()); if (double.Parse(pdr["HasForeign"].ToString()) >= 0) { price.ForeignCurrentRoom = double.Parse(pdr["RemainFrRoom"].ToString()); price.ForeignTotalRoom = double.Parse(pdr["FrTotalRoom"].ToString()); price.ForeignBuyValue = double.Parse(pdr["BuyFrValue"].ToString()); price.ForeignBuyVolume = double.Parse(pdr["BuyFrVolume"].ToString()); price.ForeignSellValue = double.Parse(pdr["SellFrValue"].ToString()); price.ForeignSellVolume = double.Parse(pdr["SellFrVolume"].ToString()); } if (bRealtime) { price.AskPrice01 = double.Parse(pdr["SellPrice1"].ToString()); price.AskPrice02 = double.Parse(pdr["SellPrice2"].ToString()); price.AskPrice03 = double.Parse(pdr["SellPrice3"].ToString()); price.AskVolume01 = double.Parse(pdr["SellVol1"].ToString()); price.AskVolume02 = double.Parse(pdr["SellVol2"].ToString()); price.AskVolume03 = double.Parse(pdr["SellVol3"].ToString()); price.BidPrice01 = double.Parse(pdr["BidPrice1"].ToString()); price.BidPrice02 = double.Parse(pdr["BidPrice2"].ToString()); price.BidPrice03 = double.Parse(pdr["BidPrice3"].ToString()); price.BidVolume01 = double.Parse(pdr["BidVol1"].ToString()); price.BidVolume02 = double.Parse(pdr["BidVol2"].ToString()); price.BidVolume03 = double.Parse(pdr["BidVol3"].ToString()); } #endregion #region Update Center Stats var key = string.Format(RedisKey.PriceKey, symbol); if (redis.ContainsKey(key)) redis.Set<StockPrice>(key, price); else redis.Add<StockPrice>(key, price); if (redisPrice.ContainsKey(key)) redisPrice.Set<StockPrice>(key, price); else redisPrice.Add<StockPrice>(key, price); if (price.Price <= 0) continue; if (Math.Round(price.Price, 2) <= Math.Round(price.FloorPrice, 2)) stats[0]++; else if (Math.Round(price.Price, 2) < Math.Round(price.RefPrice, 2)) stats[1]++; else if (Math.Round(price.Price, 2) >= Math.Round(price.CeilingPrice, 2)) stats[4]++; else if (Math.Round(price.Price, 2) > Math.Round(price.RefPrice, 2)) stats[3]++; else stats[2]++; #endregion } catch (Exception ex) { log.WriteEntry("Price " + symbol + ":" + ex.ToString(), EventLogEntryType.Information); } } #region Center stats rows = pdt.Select("Symbol='CENTER'"); if (rows.Length == 0) return; //update center stats var centerkey = string.Format(RedisKey.KeyCenterIndex, centerId); var center = redis.ContainsKey(centerkey) ? redis.Get<TradeCenterStats>(centerkey) : new TradeCenterStats() { TradeCenterId = centerId }; center.Ceiling = stats[4]; center.Up = stats[3]; center.Normal = stats[2]; center.Down = stats[1]; center.Floor = stats[0]; center.CurrentDate = (DateTime)rows[0]["TradeDate"]; //log.WriteEntry("UpdatePrice : " + centerId + " : Center : " + rows[0]["TradeDate"], EventLogEntryType.Information); center.CurrentIndex = double.Parse(rows[0]["TradingPrice"].ToString()); center.PrevIndex = double.Parse(rows[0]["Ref"].ToString()); center.CurrentVolume = double.Parse(rows[0]["TotalTradingVolume"].ToString()); center.CurrentValue = double.Parse(rows[0]["TotalTradingValue"].ToString()); center.ForeignBuyVolume = double.Parse(rows[0]["BuyFrVolume"].ToString()); center.ForeignBuyValue = double.Parse(rows[0]["BuyFrValue"].ToString()); center.ForeignSellVolume = double.Parse(rows[0]["SellFrVolume"].ToString()); center.ForeignSellValue = double.Parse(rows[0]["SellFrValue"].ToString()); //chỉ số theo đợt if (centerId == 1) { center.Index1 = double.Parse(rows[0]["BidPrice1"].ToString()); center.Volume1 = double.Parse(rows[0]["BidVol1"].ToString()); center.Value1 = double.Parse(rows[0]["BidPrice2"].ToString()); center.Index2 = double.Parse(rows[0]["BidVol2"].ToString()); center.Volume2 = double.Parse(rows[0]["BidPrice3"].ToString()); center.Value2 = double.Parse(rows[0]["BidVol3"].ToString()); center.Index3 = double.Parse(rows[0]["SellPrice1"].ToString()); center.Volume3 = double.Parse(rows[0]["SellVol1"].ToString()); center.Value3 = double.Parse(rows[0]["SellPrice2"].ToString()); } if (!bRealtime) { center.ChartFolder = rows[0]["SecName"].ToString(); } if (redis.ContainsKey(centerkey)) redis.Set(centerkey, center); else redis.Add(centerkey, center); #endregion } catch (Exception ex) { log.WriteEntry("PriceData - " + centerId + ": " + ex.ToString(), EventLogEntryType.Error); } }