private void updateOptionBidAskBook(long reqId) { ///======================================== ///注意: /// 1)该方法只需要执行一次即无限循环,直到闭市自动退出,请勿多次运行 /// 2)StreamReader读取文件时会锁住文件,建议先复制到本地再进行读取 ///======================================== bool isMarketOff = false; while (htSubscribe.Contains(reqId) && (bool)htSubscribe[reqId] && !isMarketOff) { try { string filecontent = this.getMarketFileContent(); string markettime = ""; #region 解释行情 string[] lines = null; if (filecontent == null || filecontent.Trim().Length == 0) { MessageManager.GetInstance().Add(MessageType.Error, "上交所期权行情文件出错"); break; } lines = filecontent.Split("\n".ToCharArray()); #region 文件头 string[] fieldshead = lines[0].Split("|".ToCharArray()); if (markettime != fieldshead[6].ToString().Trim()) { markettime = fieldshead[6].ToString().Trim(); //历史备份 if (flag_option_price_savehistory) { string filename = markettime.Replace(":", "-").Replace(".", "-") + ".txt"; File.Copy(filepath_option_price_target, filepath_option_price_history + filename, true); } } else { Thread.Sleep(100); continue; } #endregion #region 文件体 if (!isMarketOff) { string[] fields; string line = ""; for (int i = 0; i < lines.Length; i++) { try { line = lines[i].Trim(); if (line != null && line.Length > 0) { fields = line.Split("|".ToCharArray()); if (fields.Length >= 35) { #region 更新BidAskBook string optioncode = fields[1].Trim(); if (htSubscribedOptions.Contains(optioncode)) { BidAskBook babook = (BidAskBook)htSubscribedOptions[optioncode]; string tradetime = fields[34].Trim(); //HH:MM:SS.000 ///======================= ///无法用时间戳做数据变更的判断 /// 存在这样的情形,数据变了时间戳没更新 ///======================= babook.tradetime = tradetime; #region 交易状态 char[] status = fields[33].Trim().ToUpper().ToCharArray(); switch (status[0]) { case 'S': babook.phase = TradingPhase.START; break; case 'C': babook.phase = TradingPhase.CALL; break; case 'T': babook.phase = TradingPhase.TRADE; break; case 'B': babook.phase = TradingPhase.BREAK; break; case 'E': babook.phase = TradingPhase.END; break; case 'V': babook.phase = TradingPhase.VOLBREAK; break; case 'P': babook.phase = TradingPhase.PAUSE; break; case 'U': babook.phase = TradingPhase.UCALL; break; default: babook.phase = TradingPhase.NONE; break; } #endregion #region 盘口价格 babook.presettle = Utility.ConvertToDouble(fields[5].Trim(), 0); babook.open = Utility.ConvertToDouble(fields[6].Trim(), 0); babook.high = Utility.ConvertToDouble(fields[9].Trim(), 0); babook.low = Utility.ConvertToDouble(fields[10].Trim(), 0); babook.last = Utility.ConvertToDouble(fields[11].Trim(), 0); if (babook.phase == TradingPhase.TRADE)//仅在连续竞价阶段更新 { babook.lasttrade = babook.last; } babook.bid[0] = Utility.ConvertToDouble(fields[12].Trim(), 0); babook.bid[1] = Utility.ConvertToDouble(fields[16].Trim(), 0); babook.bid[2] = Utility.ConvertToDouble(fields[20].Trim(), 0); babook.bid[3] = Utility.ConvertToDouble(fields[24].Trim(), 0); babook.bid[4] = Utility.ConvertToDouble(fields[28].Trim(), 0); babook.bidsize[0] = (int)Utility.ConvertToDouble(fields[13].Trim(), 0); babook.bidsize[1] = (int)Utility.ConvertToDouble(fields[17].Trim(), 0); babook.bidsize[2] = (int)Utility.ConvertToDouble(fields[21].Trim(), 0); babook.bidsize[3] = (int)Utility.ConvertToDouble(fields[25].Trim(), 0); babook.bidsize[4] = (int)Utility.ConvertToDouble(fields[29].Trim(), 0); babook.ask[0] = Utility.ConvertToDouble(fields[14].Trim(), 0); babook.ask[1] = Utility.ConvertToDouble(fields[18].Trim(), 0); babook.ask[2] = Utility.ConvertToDouble(fields[22].Trim(), 0); babook.ask[3] = Utility.ConvertToDouble(fields[26].Trim(), 0); babook.ask[4] = Utility.ConvertToDouble(fields[30].Trim(), 0); babook.asksize[0] = (int)Utility.ConvertToDouble(fields[15].Trim(), 0); babook.asksize[1] = (int)Utility.ConvertToDouble(fields[19].Trim(), 0); babook.asksize[2] = (int)Utility.ConvertToDouble(fields[23].Trim(), 0); babook.asksize[3] = (int)Utility.ConvertToDouble(fields[27].Trim(), 0); babook.asksize[4] = (int)Utility.ConvertToDouble(fields[31].Trim(), 0); #endregion } #endregion } } } catch (Exception ex) { throw ex; } } } #endregion string marketstatus = fieldshead[8].Trim().ToUpper(); char[] tradestatus = marketstatus.ToCharArray(); switch (tradestatus[0]) { //交易所已闭市 case 'E': isMarketOff = true; break; default: break; } #endregion } catch (Exception ex) { throw ex; } } }
private int callBackOnSubscribeBidAskBook(QuantEvent quantEvent) { ////////////////////////////////// //异步订阅:htSubscribeObject ////////////////////////////////// if (!this.htSubscribe.Contains(quantEvent.RequestID)) { MessageManager.GetInstance().Add(MessageType.Error, string.Format("没有找到订阅对象:id={0}", quantEvent.RequestID)); return(-1); } if (!isCallBackDataValid(quantEvent)) { return(-1); } BidAskBook babook = (BidAskBook)this.htSubscribe[quantEvent.RequestID]; double[] data; data = (double[])quantEvent.quantData.MatrixData; #region 更新BidAskBook for (int i = 0; i < data.Length; i++) { string field = quantEvent.quantData.ArrWindFields[i].ToLower(); switch (field) { case "rt_time": babook.tradetime = data[i].ToString().Substring(0, 6); break; case "rt_pre_settle": babook.presettle = data[i]; break; case "rt_pre_close": babook.preclose = data[i]; break; case "rt_open": babook.open = data[i]; break; case "rt_high": babook.high = data[i]; break; case "rt_low": babook.low = data[i]; break; case "rt_last": babook.last = data[i]; babook.lasttrade = babook.last; break; default: int idx = -1; if (field.IndexOf("rt_ask") == 0) { idx = Convert.ToInt16(field.Substring(6)); babook.ask[idx - 1] = data[i]; } else if (field.IndexOf("rt_bid") == 0) { idx = Convert.ToInt16(field.Substring(6)); babook.bid[idx - 1] = data[i]; } else if (field.IndexOf("rt_asize") == 0) { idx = Convert.ToInt16(field.Substring(8)); babook.asksize[idx - 1] = (int)data[i]; } else if (field.IndexOf("rt_bsize") == 0) { idx = Convert.ToInt16(field.Substring(8)); babook.bidsize[idx - 1] = (int)data[i]; } break; } } #endregion babook.DebugPrint(); return((int)quantEvent.ErrCode); }