private void OptStockT(Object param, bool sign) { try { Monitor.Enter(lockTline); if (Utils.IsTradeTime() == 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 ", time_hhmm = dt.ToString("HHmm");//行情时间; 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)) { int j = 0; List <string> stringLot = new List <string>(); 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 + "t", keyhis = stock_code + "this"; bool bRet2 = TradeX.TdxHq_GetMinuteTimeData((byte)stock_market, stock_code, sResult, sErrInfo); if (bRet2 == 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 >= 2) { if (z == 1) { #region ====================行情数据 string stock_code_s = dr["stock_code_s"].ToString(); //股票最新行情 string[] stock_info = RedisHelper.GetValues(stock_code_s).Split(','); if (stock_info.Length > 30) { //开盘价、收盘价、最高、最低、当前价、成交量、成交额 decimal open = Utils.StrToDecimal(stock_info[1], 0), preClose = Utils.StrToDecimal(stock_info[2], 0), highest = Utils.StrToDecimal(stock_info[4], 0), lowest = Utils.StrToDecimal(stock_info[5], 0), fprice = Utils.StrToDecimal(stock_info[3], 0), deal_num = Utils.StrToDecimal(stock_info[8], 0), deal_price = Utils.StrToDecimal(stock_info[9], 0); string old_str = RedisHelper.GetValues(key), //旧数据 old_his_str = RedisHelper.GetValues(keyhis); //旧数据 //记录历史交易量、交易额 RedisHelper.Set <string>(keyhis, deal_num + "," + deal_price); //获取上一条行情对比成交量成交额 string[] deal_his = old_his_str.Split(','); if (deal_his.Length == 2) { deal_num = deal_num - Utils.StrToDecimal(deal_his[0], 0); deal_price = deal_price - Utils.StrToDecimal(deal_his[1], 0); deal_num = (deal_num < 0 ? 0 : deal_num); deal_price = (deal_price < 0 ? 0 : deal_price); } if (time_hhmm == "0930" || old_str.Trim().Length < 32) { #region ====================初始数据 Model.StockTLine modelt = new Model.StockTLine() { quote = new Model.quote() { stock_name = stock_info[0], time = dt.ToString("yyyyMMddHHmmss"), open = open, preClose = preClose, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price } }; Model.mins mins = new Model.mins() { price = fprice, volume = deal_num, amount = deal_price, time = time_hhmm }; modelt.mins.Add(mins); string json_first = JsonHelper.GetJson <Model.StockTLine>(modelt); RedisHelper.Set <string>(key, json_first); #endregion } else { #region ====================更新数据 Model.StockTLine modelt = JsonHelper.ParseFromJson <Model.StockTLine>(old_str); bool is_has = modelt.mins.Any(p => p.time == time_hhmm); if (is_has == true) { modelt.mins.Remove(modelt.mins.Where(p => p.time == time_hhmm).Single()); } Model.quote quote = new Model.quote() { stock_name = stock_info[0], time = dt.ToString("yyyyMMddHHmmss"), open = open, preClose = preClose, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; Model.mins mins = new Model.mins() { price = fprice, volume = deal_num, amount = deal_price, time = time_hhmm }; modelt.quote = quote; modelt.mins.Add(mins); string json_first = JsonHelper.GetJson <Model.StockTLine>(modelt); RedisHelper.Set <string>(key, json_first); if (time_hhmm == "1130" || time_hhmm == "1500") { //更新分时线 old_str = RedisHelper.GetValues(key); stringLot.Add(" update c_stock_code set t_line='" + old_str + "' where stock_code='" + stock_code + "';"); } #endregion } j++; } #endregion } } } } } } //多条更新 int num = dal.TranLot(stringLot); RunEndCheck("共更新【" + j + "】只票的分时数据"); } } TradeX.TdxHq_Disconnect(); } } } catch (Exception ex) { Log.WriteError("", ex.ToString()); RunEndCheck(ex.ToString()); } finally { Monitor.Exit(lockTline); } }
//请求开始时由ASP.NET调用此方法 public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) { context.Response.ContentType = "text/plain"; string code = AppRequest.GetQueryString("code"); string url = AppRequest.GetQueryString("url"); string time = AppRequest.GetQueryString("time"); string token = AppRequest.GetQueryString("token"); int flag = AppRequest.GetQueryInt("flag", 0); if (!Utils.tokenCk(url, time, token)) { context.Response.Write("token校验失败,请刷新页面重试"); } else { if (flag == 100) { #region 使用自身行情 string keys_bak = code + "t_bak"; context.Response.Write(RedisHelper.GetValues(keys_bak)); #endregion } else { #region 使用接口行情 if (code.Length != 6) { context.Response.Write("fail"); } else { string key = code + "t" + (flag > 0 ? flag.ToString() : ""); string hq = RedisHelper.GetValues(key); int count = 0; DateTime dt = DateTime.Now; int time_hhmm = Utils.StrToInt(dt.ToString("HHmm"), 0);//行情时间; if (hq.Length > 32) { Model.StockTLine modelt = JsonHelper.ParseFromJson <Model.StockTLine>(hq); count = modelt.mins.Count; if (count > 0) { //检查交易时间是否已经有数据 if (((time_hhmm > 930 && time_hhmm < 1130) || (time_hhmm > 1300 && time_hhmm < 1500))) { int countCk = modelt.mins.Where(t => t.time == time_hhmm.ToString()).Count(); if (countCk > 0) { count = 240; } else { count = 0; } } } } if (count < 240 || count > 245) { StringBuilder sErrInfo = new StringBuilder(256); StringBuilder sResult = new StringBuilder(1024 * 1024); bool bRet = TradeX.TdxHq_Connect(AppKeys.QuotationAPI, 7709, sResult, sErrInfo); //Log.WriteLog("行情", sResult.ToString() + "++++" + sErrInfo.ToString()); if (bRet == true) { string stock_code_s = Utils.GetFullStockCode(code, flag); if (time_hhmm >= 1500) { dt = Utils.StrToDateTime(DateTime.Now.ToString("yyyy-MM-dd 15:00:00")); } byte stock_market = 0; string cyb = Utils.subStr(code, 0, 1); if (flag == 0) { string cyb2 = Utils.subStr(code, 0, 2); if (cyb == "6" || cyb2 == "11" || cyb2 == "51" || cyb2 == "50") { stock_market = 1; } } else { switch (cyb) { case "0": stock_market = 1; break; case "5": stock_market = 1; break; default: break; } } //股票最新行情 string[] stock_info = RedisHelper.GetValues(stock_code_s).Split(','); if (stock_info.Length < 30) { stock_info = Utils.GetStockData(code, 0, 0).Split(','); } bool bRet2 = TradeX.TdxHq_GetMinuteTimeData(stock_market, code, sResult, sErrInfo); if (bRet2 == true && stock_info.Length > 30) { string[] hq_time = AppKeys.CACHE_HQ_TIME.ToString().Split(','); string[] k_list = sResult.ToString().Replace("\n", "#").Replace("\t", ",").Replace("-", "").Split('#'); if (k_list.Length > 2 && hq_time.Length >= 240) { int lgt = k_list.Length, lgt_ck = hq_time.Length; if (lgt > lgt_ck) { lgt = lgt_ck; } for (int z = 1; z < lgt; z++) { string[] k_split = k_list[z].Split(','); if (k_split.Length >= 2) { //现价,成交量,保留 decimal price_now = Utils.StrToDecimal(k_split[0], 0), price_num = Utils.StrToDecimal(k_split[1], 0), price_keep = Utils.StrToDecimal(k_split[2], 0); //开盘价、收盘价、最高、最低、当前价、成交量、成交额 decimal open = Utils.StrToDecimal(stock_info[1], 0), preClose = Utils.StrToDecimal(stock_info[2], 0), highest = Utils.StrToDecimal(stock_info[4], 0), lowest = Utils.StrToDecimal(stock_info[5], 0), fprice = Utils.StrToDecimal(stock_info[3], 0), deal_num = Utils.StrToDecimal(stock_info[8], 0), deal_price = Utils.StrToDecimal(stock_info[9], 0); #region ====================行情数据 if (z == 1) { #region ====================初始数据 Model.StockTLine modelt = new Model.StockTLine() { quote = new Model.quote() { stock_name = stock_info[0], time = dt.ToString("yyyyMMddHHmmss"), open = open, preClose = preClose, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price } }; Model.mins mins = new Model.mins() { price = price_now, volume = price_num, amount = 0, time = hq_time[z - 1] }; modelt.mins.Add(mins); string json_first = JsonHelper.GetJson <Model.StockTLine>(modelt); RedisHelper.Set <string>(key, json_first); #endregion } else { #region ====================更新数据 string old_str = RedisHelper.GetValues(key);//旧分时数据 if (!string.IsNullOrEmpty(old_str)) { Model.StockTLine modelt = JsonHelper.ParseFromJson <Model.StockTLine>(old_str); Model.quote quote = new Model.quote() { stock_name = stock_info[0], time = dt.ToString("yyyyMMddHHmmss"), open = open, preClose = preClose, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; Model.mins mins = new Model.mins() { price = price_now, volume = price_num, amount = 0, time = hq_time[z - 1] }; modelt.quote = quote; modelt.mins.Add(mins); string json_first = JsonHelper.GetJson <Model.StockTLine>(modelt); RedisHelper.Set <string>(key, json_first); } #endregion } #endregion } } } //断开连接 TradeX.TdxHq_Disconnect(); } } hq = RedisHelper.GetValues(key); } else { hq = RedisHelper.GetValues(key); } context.Response.Write(hq); } #endregion } } //构建异步结果并返回 var result = new WebAsyncResult(cb, context); result.SetComplete(); return(result); }
private void stock_t(HttpContext context) { string url = AppRequest.GetQueryString("url", true); string time = AppRequest.GetQueryString("time", true); string token = AppRequest.GetQueryString("token", true); int flag = AppRequest.GetQueryInt("flag", 0); string code = AppRequest.GetQueryString("code", true);//股票代码 if (code.Length != 6) { context.Response.Write("fail"); return; } if (!Utils.tokenCk(url, time, token)) { context.Response.Write("token校验失败,请刷新页面重试"); return; } if (!IPLimitHelper.CheckIsAble()) { context.Response.Write("访问频率过高,请稍后访问"); return; } string json_str = "", key; if (flag == 100) { key = code + "t_bak" + (flag > 0 ? flag.ToString() : ""); } else { key = code + "t" + (flag > 0 ? flag.ToString() : ""); } if (Utils.IsTradeTime() == 1) { decimal open = 0, preClose = 0, highest = 0, lowest = 0, fprice = 0, deal_num = 0, deal_price = 0; string[] stock_info = Utils.GetStockData(code, 0, flag).Split(','); if (stock_info.Length > 30) { #region ====================更新数据 //redis-Key值、旧值 DateTime dt = DateTime.Now; string old_str = RedisHelper.GetValues(key); //旧数据 string time_hhmm = dt.ToString("HHmm"); //行情时间; //开盘价、收盘价、最高、最低、当前价、成交量、成交额 open = Utils.StrToDecimal(stock_info[1], 0); preClose = Utils.StrToDecimal(stock_info[2], 0); highest = Utils.StrToDecimal(stock_info[4], 0); lowest = Utils.StrToDecimal(stock_info[5], 0); fprice = Utils.StrToDecimal(stock_info[3], 0); deal_num = Utils.StrToDecimal(stock_info[8], 0) / 100; deal_price = Utils.StrToDecimal(stock_info[9], 0); if (old_str.Length > 32) { Model.StockTLine modelt = JsonHelper.ParseFromJson <Model.StockTLine>(old_str); bool is_has = modelt.mins.Any(p => p.time == time_hhmm); if (is_has == true) { modelt.mins.Remove(modelt.mins.Where(p => p.time == time_hhmm).Single()); } Model.quote quote = new Model.quote() { stock_name = stock_info[0], time = dt.ToString("yyyyMMddHHmmss"), open = open, preClose = preClose, highest = highest, lowest = lowest, price = fprice, volume = deal_num, amount = deal_price }; if (modelt.mins.Count > 0) { decimal deal_num_his = modelt.mins.Sum(p => p.volume);//以前的成交量 decimal volume = (deal_num - deal_num_his) / 100; Model.mins mins = new Model.mins() { price = fprice, volume = volume < 0 ? 0 : volume, amount = deal_price, time = time_hhmm }; modelt.quote = quote; modelt.mins.Add(mins); } json_str = JsonHelper.GetJson <Model.StockTLine>(modelt); } #endregion } } else { json_str = RedisHelper.GetValues(key);//旧数据 } context.Response.Write(json_str); return; }