private void OptStockTClear(Object param, bool sign) { try { Monitor.Enter(lockTlineClear); DateTime dt = DateTime.Now; DateTime trade_time = Convert.ToDateTime(dt.ToString("HH:mm:ss")); if (trade_time >= Convert.ToDateTime("09:10:00") && trade_time < Convert.ToDateTime("09:20:30")) { if (dal.GetIsWeek(trade_time) == 0) { #region ====================分时图 DataSet ds = dal.GetSList(" 1=1 "); if (!Utils.DataSetIsNull(ds)) { #region ====================初始分时数据 Model.StockTLine modelt = new Model.StockTLine() { quote = new Model.quote() { time = dt.ToString("yyyyMMddHHmmss"), open = 0, preClose = 0, highest = 0, lowest = 0, price = 0, volume = 0, amount = 0 } }; Model.mins mins = new Model.mins() { price = 0, volume = 0, amount = 0, time = "0" }; modelt.mins.Add(mins); string json_first = JsonHelper.GetJson <Model.StockTLine>(modelt); #endregion #region ====================清空分时 RunEndCheck("清空分时开始"); foreach (DataRow dr in ds.Tables[0].Rows) { string stock_code = dr["stock_code"].ToString(); RedisHelper.Set <string>(stock_code + "t", json_first); } RunEndCheck("清空分时结束"); #endregion #region ====================K图初始化 StringBuilder sErrInfo = new StringBuilder(256); StringBuilder sResult = new StringBuilder(1024 * 1024); bool bRet = TradeX.TdxHq_Connect("14.17.75.71", 7709, sResult, sErrInfo); if (bRet == true) { RunEndCheck("更新K线数据开始..."); foreach (DataRow dr in ds.Tables[0].Rows) { string stock_code = dr["stock_code"].ToString(); int stock_market = Utils.StrToInt(dr["stock_market"].ToString(), 0); string key = stock_code + "k"; //先清理 RedisHelper.Remove(key); RunEndCheck("更新K线数据..." + stock_code); #region ====================查询接口数据 short nCount = 300; bRet = TradeX.TdxHq_GetSecurityBars(4, (byte)stock_market, stock_code, 0, ref nCount, sResult, sErrInfo); if (bRet == true) { string[] k_list = sResult.ToString().Replace("\n", "#").Replace("\t", ",").Replace("-", "").Split('#'); if (k_list.Length > 2) { for (int i = 1; i < k_list.Length; i++) { string[] k_split = k_list[i].Split(','); if (k_split.Length >= 7) { #region ====================更新redis,K线数据 //开盘价、收盘价、最高、最低、当前价、成交量、成交额 decimal open = Utils.StrToDecimal(k_split[1], 0), preClose = Utils.StrToDecimal(k_split[2], 0), highest = Utils.StrToDecimal(k_split[3], 0), lowest = Utils.StrToDecimal(k_split[4], 0), fprice = Utils.StrToDecimal(k_split[2], 0), deal_num = Utils.StrToDecimal(k_split[5], 0), deal_price = Utils.StrToDecimal(k_split[6], 0); //redis-Key值、行情时间、旧数据 string old_str = RedisHelper.GetValues(key), time_hhmm = k_split[0]; int time_hhmm_int = Utils.StrToInt(time_hhmm, 0); //时间数字化 if (old_str.Trim().Length < 32) //第一次插入 { #region ====================初始数据 Model.StockKLine modelk = new Model.StockKLine(); Model.kmins minsk = new Model.kmins() { time = time_hhmm_int, preClose = preClose, open = open, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; modelk.kmins.Add(minsk); string json_kline = JsonHelper.GetJson <Model.StockKLine>(modelk); RedisHelper.Set <string>(key, json_kline); #endregion ; } else { #region ====================更新数据 Model.StockKLine modelk = JsonHelper.ParseFromJson <Model.StockKLine>(old_str); bool is_has = modelk.kmins.Any(p => p.time == time_hhmm_int); if (is_has == true) { modelk.kmins.Remove(modelk.kmins.Where(p => p.time == time_hhmm_int).Single()); } Model.kmins minsk = new Model.kmins() { time = time_hhmm_int, preClose = preClose, open = open, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; modelk.kmins.Add(minsk); string json_kline = JsonHelper.GetJson <Model.StockKLine>(modelk); RedisHelper.Set <string>(key, json_kline); #endregion } #endregion } } } } #endregion } TradeX.TdxHq_Disconnect(); RunEndCheck("更新K线数据完成..."); } #endregion } #endregion Log.WriteError("清空分时,更新K线数据", ""); } } } catch (Exception ex) { Log.WriteError("清空分时,更新K线数据错误:", ex.ToString()); RunEndCheck(ex.ToString()); } finally { Monitor.Exit(lockTlineClear); } }
//请求开始时由ASP.NET调用此方法 public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) { context.Response.ContentType = "text/plain"; string code = AppRequest.GetQueryString("code", true); int flag = AppRequest.GetQueryInt("flag", 0); if (code.Length != 6) { context.Response.Write("fail"); } else { string key = code + "kw" + (flag > 0 ? flag.ToString() : ""); string hq = RedisHelper.GetValues(key); if (hq.Trim().Length < 32) { RedisHelper.Remove(key); StringBuilder sErrInfo = new StringBuilder(256); StringBuilder sResult = new StringBuilder(1024 * 1024); bool bRet = TradeX.TdxHq_Connect(AppKeys.QuotationAPI, 7709, sResult, sErrInfo); if (bRet == true) { DateTime dt = DateTime.Now; byte stock_market = 0; string cyb = Utils.subStr(code, 0, 1); if (flag == 0) { switch (cyb) { case "6": stock_market = 1; break; case "5": stock_market = 1; break; default: break; } } else { switch (cyb) { case "0": stock_market = 1; break; default: break; } } #region ====================查询接口数据 short nCount = 300; if (flag == 1) { bRet = TradeX.TdxHq_GetIndexBars(5, stock_market, code, 0, ref nCount, sResult, sErrInfo); } else { bRet = TradeX.TdxHq_GetSecurityBars(5, stock_market, code, 0, ref nCount, sResult, sErrInfo); } if (bRet == true) { string[] k_list = sResult.ToString().Replace("\n", "#").Replace("\t", ",").Replace("-", "").Split('#'); if (k_list.Length > 2) { for (int z = 1; z < k_list.Length; z++) { string[] k_split = k_list[z].Split(','); if (k_split.Length >= 7) { #region ====================更新redis,K线数据 //开盘价、收盘价、最高、最低、当前价、成交量、成交额 decimal open = Utils.StrToDecimal(k_split[1], 0), preClose = Utils.StrToDecimal(k_split[2], 0), highest = Utils.StrToDecimal(k_split[3], 0), lowest = Utils.StrToDecimal(k_split[4], 0), fprice = Utils.StrToDecimal(k_split[2], 0), deal_num = Utils.StrToDecimal(k_split[5], 0), deal_price = Utils.StrToDecimal(k_split[6], 0); //redis-Key值、行情时间、旧数据 string old_str = RedisHelper.GetValues(key), time_hhmm = k_split[0]; int time_hhmm_int = Utils.StrToInt(time_hhmm, 0); //时间数字化 if (old_str.Trim().Length < 32) //第一次插入 { #region ====================初始数据 Model.StockKLine modelk = new Model.StockKLine(); Model.kmins minsk = new Model.kmins() { time = time_hhmm_int, preClose = preClose, open = open, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; modelk.kmins.Add(minsk); string json_kline = JsonHelper.GetJson <Model.StockKLine>(modelk); RedisHelper.Set <string>(key, json_kline); #endregion ; } else { #region ====================更新数据 Model.StockKLine modelk = JsonHelper.ParseFromJson <Model.StockKLine>(old_str); bool is_has = modelk.kmins.Any(p => p.time == time_hhmm_int); if (is_has == true) { modelk.kmins.Remove(modelk.kmins.Where(p => p.time == time_hhmm_int).Single()); } Model.kmins minsk = new Model.kmins() { time = time_hhmm_int, preClose = preClose, open = open, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; modelk.kmins.Add(minsk); string json_kline = JsonHelper.GetJson <Model.StockKLine>(modelk); RedisHelper.Set <string>(key, json_kline); #endregion } #endregion } } } } #endregion } } context.Response.Write(RedisHelper.GetValues(key)); } //构建异步结果并返回 var result = new WebAsyncResult(cb, context); result.SetComplete(); return(result); }
private void OptStockK(Object param, bool sign) { try { Monitor.Enter(lockKline); if (dal.GetIsTradeTimeTline() == 1) { StringBuilder sErrInfo = new StringBuilder(256); StringBuilder sResult = new StringBuilder(1024 * 1024); bool bRet = TradeX.TdxHq_Connect("14.17.75.71", 7709, sResult, sErrInfo); if (bRet == true) { DateTime dt = DateTime.Now; string strWhere = " 1=1 "; int count = dal.GetRecordCount(strWhere), pagesize = 500; int pageall = (count + pagesize - 1) / pagesize; for (int i = 1; i <= pageall; i++) { DataSet ds = dal.GetSList(strWhere, "id desc", pagesize, i); if (!Utils.DataSetIsNull(ds)) { #region ====================K图初始化 RunEndCheck("更新K线数据开始..."); foreach (DataRow dr in ds.Tables[0].Rows) { string stock_code = dr["stock_code"].ToString(); int stock_market = Utils.StrToInt(dr["stock_market"].ToString(), 0); string key = stock_code + "k"; RunEndCheck("更新K线数据..." + stock_code); #region ====================查询接口数据 short nCount = 300; bRet = TradeX.TdxHq_GetSecurityBars(4, (byte)stock_market, stock_code, 0, ref nCount, sResult, sErrInfo); if (bRet == true) { string[] k_list = sResult.ToString().Replace("\n", "#").Replace("\t", ",").Replace("-", "").Split('#'); if (k_list.Length > 2) { for (int z = 1; z < k_list.Length; z++) { string[] k_split = k_list[z].Split(','); if (k_split.Length >= 7) { #region ====================更新redis,K线数据 //开盘价、收盘价、最高、最低、当前价、成交量、成交额 decimal open = Utils.StrToDecimal(k_split[1], 0), preClose = Utils.StrToDecimal(k_split[2], 0), highest = Utils.StrToDecimal(k_split[3], 0), lowest = Utils.StrToDecimal(k_split[4], 0), fprice = Utils.StrToDecimal(k_split[2], 0), deal_num = Utils.StrToDecimal(k_split[5], 0), deal_price = Utils.StrToDecimal(k_split[6], 0); //redis-Key值、行情时间、旧数据 string old_str = RedisHelper.GetValues(key), time_hhmm = k_split[0]; int time_hhmm_int = Utils.StrToInt(time_hhmm, 0); //时间数字化 if (old_str.Trim().Length < 32) //第一次插入 { #region ====================初始数据 Model.StockKLine modelk = new Model.StockKLine(); Model.kmins minsk = new Model.kmins() { time = time_hhmm_int, preClose = preClose, open = open, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; modelk.kmins.Add(minsk); string json_kline = JsonHelper.GetJson <Model.StockKLine>(modelk); RedisHelper.Set <string>(key, json_kline); #endregion ; } else { #region ====================更新数据 Model.StockKLine modelk = JsonHelper.ParseFromJson <Model.StockKLine>(old_str); bool is_has = modelk.kmins.Any(p => p.time == time_hhmm_int); if (is_has == true) { modelk.kmins.Remove(modelk.kmins.Where(p => p.time == time_hhmm_int).Single()); } Model.kmins minsk = new Model.kmins() { time = time_hhmm_int, preClose = preClose, open = open, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; modelk.kmins.Add(minsk); string json_kline = JsonHelper.GetJson <Model.StockKLine>(modelk); RedisHelper.Set <string>(key, json_kline); #endregion } #endregion } } } } #endregion } #endregion RunEndCheck("更新K线数据完成..."); } } RunEndCheck("共更新【" + count + "】只票的K线数据"); TradeX.TdxHq_Disconnect(); } } } catch (Exception ex) { RunEndCheck(ex.ToString()); } finally { Monitor.Exit(lockKline); } }