public void ExecuteTask() { try { var isTrading = false; var bfirstTime = true; var isBeginTrading = false; var isEndTrading = false; var ss = new List<StockCompact>(); var keys = new List<string>(); var priceKeys = new List<string>(); var syms = new List<string>(); var objs = new Dictionary<string, SessionPriceData>(); var redis = new RedisClient(ConfigRedis.Host, ConfigRedis.Port); var redisPrice = new RedisClient(ConfigRedis.PriceHost, ConfigRedis.PricePort); while (ServiceStarted) { try { var b = Utils.InTradingTime(TradeCenterId); isBeginTrading = (!isTrading && b); isEndTrading = (isTrading && !b); isTrading = b; if (!bfirstTime) Thread.Sleep(isTrading ? tradeInterval : noTradeInterval); if (bfirstTime || isBeginTrading) { var key = string.Format(RedisKey.KeyStockListByCenter, TradeCenterId); ss = redis.Get<List<StockCompact>>(key); keys = new List<string>(); syms = new List<string>(); objs = new Dictionary<string, SessionPriceData>(); foreach (var s in ss) { keys.Add(string.Format(RedisKey.RealtimeSessionPrice, s.Symbol)); priceKeys.Add(string.Format(RedisKey.PriceKey, s.Symbol)); syms.Add(s.Symbol); objs.Add(string.Format(RedisKey.RealtimeSessionPrice, s.Symbol), new SessionPriceData() { Symbol = s.Symbol, Price = -1, TotalValue = 0, TotalVolume = 0, TradeDate = DateTime.Now, Volume = 0 }); } //init objects if (isBeginTrading) { redis.SetAll(objs); } else { objs = (Dictionary<string, SessionPriceData>)redis.GetAll<SessionPriceData>(keys); } } bfirstTime = false; if (isTrading) { //detect change var prices = redisPrice.GetAll<StockPrice>(priceKeys); foreach (var sym in syms) { var price = prices[string.Format(RedisKey.PriceKey, sym)]; var obj = objs[string.Format(RedisKey.RealtimeSessionPrice, sym)]; if (price == null || obj == null) continue; if (obj.Price != price.Price || obj.TotalVolume != price.Volume) { var vol = price.Volume - obj.TotalVolume; //trade time --> save if (obj.Price > -1) //don't save the initial value { var key = string.Format(RedisKey.SessionPrice, obj.Symbol, DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("HHmmss")); var o = new SessionPriceData() { Symbol = obj.Symbol, Price = price.Price, TotalValue = price.Value, TotalVolume = price.Volume, TradeDate = DateTime.Now, Volume = vol }; if (redis.ContainsKey(key)) redis.Set(key, o); else redis.Add(key, o); } //save realtime object obj.Price = price.Price; obj.Volume = vol; obj.TotalVolume = price.Volume; obj.TotalValue = price.Value; objs[string.Format(RedisKey.RealtimeSessionPrice, sym)] = obj; } } redis.SetAll(objs); } } catch (Exception ex) { log.WriteEntry(ex.ToString(), EventLogEntryType.Error); } } } catch (Exception ex) { log.WriteEntry(ex.ToString(), EventLogEntryType.Error); } }