private void OnRtnDepthMarketData_callback(IntPtr pMdUserApi, ref CZQThostFtdcDepthMarketDataField pDepthMarketData) { if (null != OnRtnDepthMarketData) { OnRtnDepthMarketData(this, new OnRtnDepthMarketDataArgs(pMdUserApi, ref pDepthMarketData)); } }
public void FireOnRtnDepthMarketData(CZQThostFtdcDepthMarketDataField pDepthMarketData) { if (null != OnRtnDepthMarketData) { OnRtnDepthMarketData(pDepthMarketData); } }
private void OnRspQryDepthMarketData_callback(IntPtr pTraderApi, ref CZQThostFtdcDepthMarketDataField pDepthMarketData, ref CZQThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (null != OnRspQryDepthMarketData) { OnRspQryDepthMarketData(this, new OnRspQryDepthMarketDataArgs(pTraderApi, ref pDepthMarketData, ref pRspInfo, nRequestID, bIsLast)); } }
public OnRspQryDepthMarketDataArgs(IntPtr pTraderApi, ref CZQThostFtdcDepthMarketDataField pDepthMarketData, ref CZQThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { this.pTraderApi = pTraderApi; this.pDepthMarketData = pDepthMarketData; this.pRspInfo = pRspInfo; this.nRequestID = nRequestID; this.bIsLast = bIsLast; }
public static bool TryConvert(OpenQuant.API.Quote quote, ref CZQThostFtdcDepthMarketDataField DepthMarketData) { if (quoteField == null) { quoteField = typeof(OpenQuant.API.Quote).GetField("quote", BindingFlags.NonPublic | BindingFlags.Instance); } CTPZQQuote q = quoteField.GetValue(quote) as CTPZQQuote; if (null != q) { DepthMarketData = q.DepthMarketData; return(true); } return(false); }
public static bool TryConvert(OpenQuant.API.Trade trade, ref CZQThostFtdcDepthMarketDataField DepthMarketData) { if (tradeField == null) { tradeField = typeof(OpenQuant.API.Trade).GetField("trade", BindingFlags.NonPublic | BindingFlags.Instance); } CTPZQTrade t = tradeField.GetValue(trade) as CTPZQTrade; if (null != t) { DepthMarketData = t.DepthMarketData; return(true); } return(false); }
public void OnRspQryDepthMarketData(IntPtr pTraderApi, ref CZQThostFtdcDepthMarketDataField pDepthMarketData, ref CZQThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (0 == pRspInfo.ErrorID) { CZQThostFtdcDepthMarketDataField DepthMarket; string symbol = GetYahooSymbol(pDepthMarketData.InstrumentID, pDepthMarketData.ExchangeID); if (!_dictDepthMarketData.TryGetValue(symbol, out DepthMarket)) { //没找到此元素,保存一下 _dictDepthMarketData[symbol] = pDepthMarketData; } tdlog.Info("已经接收查询深度行情 {0}", symbol); //通知单例 CTPZQAPI.GetInstance().FireOnRspQryDepthMarketData(pDepthMarketData); } else { tdlog.Error("nRequestID:{0},ErrorID:{1},OnRspQryDepthMarketData:{2}", nRequestID, pRspInfo.ErrorID, pRspInfo.ErrorMsg); EmitError(nRequestID, pRspInfo.ErrorID, "OnRspQryDepthMarketData:" + pRspInfo.ErrorMsg); } }
public OnRtnDepthMarketDataArgs(IntPtr pMdUserApi, ref CZQThostFtdcDepthMarketDataField pDepthMarketData) { this.pMdUserApi = pMdUserApi; this.pDepthMarketData = pDepthMarketData; }
public void SendMarketDataRequest(FIXMarketDataRequest request) { lock (this) { switch (request.SubscriptionRequestType) { case DataManager.MARKET_DATA_SUBSCRIBE: if (!_bMdConnected) { EmitError(-1, -1, "行情服务器没有连接,无法订阅行情"); return; } for (int i = 0; i < request.NoRelatedSym; ++i) { FIXRelatedSymGroup group = request.GetRelatedSymGroup(i); //通过订阅的方式,由平台传入合约对象,在行情接收处将要使用到合约 CZQThostFtdcDepthMarketDataField DepthMarket; Instrument inst = InstrumentManager.Instruments[group.Symbol]; string altSymbol = inst.GetSymbol(this.Name); string altExchange = inst.GetSecurityExchange(this.Name); if (!_dictDepthMarketData.TryGetValue(altSymbol, out DepthMarket)) { DepthMarket = new CZQThostFtdcDepthMarketDataField(); _dictDepthMarketData.Add(altSymbol, DepthMarket); } _dictAltSymbol2Instrument[altSymbol] = inst; Console.WriteLine("MdApi:订阅合约 {0} {1}", altSymbol, altExchange); MdApi.MD_Subscribe(m_pMdApi, altSymbol, altExchange); } if (!_bTdConnected) { EmitError(-1, -1, "交易服务器没有连接,无法保证持仓真实"); return; } TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, null); timerPonstion.Enabled = false; timerPonstion.Enabled = true; break; case DataManager.MARKET_DATA_UNSUBSCRIBE: if (!_bMdConnected) { EmitError(-1, -1, "行情服务器没有连接,退订合约无效"); return; } for (int i = 0; i < request.NoRelatedSym; ++i) { FIXRelatedSymGroup group = request.GetRelatedSymGroup(i); Instrument inst = InstrumentManager.Instruments[group.Symbol]; string altSymbol = inst.GetSymbol(this.Name); string altExchange = inst.GetSecurityExchange(this.Name); _dictDepthMarketData.Remove(altSymbol); Console.WriteLine("MdApi:取消订阅 {0} {1}", altSymbol, altExchange); MdApi.MD_Unsubscribe(m_pMdApi, altSymbol, altExchange); } break; default: throw new ArgumentException("Unknown subscription type: " + request.SubscriptionRequestType.ToString()); } } }
private void OnRtnDepthMarketData(IntPtr pApi, ref CZQThostFtdcDepthMarketDataField pDepthMarketData) { string symbol = GetYahooSymbol(pDepthMarketData.InstrumentID, pDepthMarketData.ExchangeID); DataRecord record; if (!_dictAltSymbol2Instrument.TryGetValue(symbol, out record)) { mdlog.Warn("合约{0}不在订阅列表中却收到了数据", symbol); return; } Instrument instrument = record.Instrument; CZQThostFtdcDepthMarketDataField DepthMarket; _dictDepthMarketData.TryGetValue(symbol, out DepthMarket); //将更新字典的功能提前,因为如果一开始就OnTrade中下单,涨跌停没有更新 _dictDepthMarketData[symbol] = pDepthMarketData; if (TimeMode.LocalTime == _TimeMode) { //为了生成正确的Bar,使用本地时间 _dateTime = Clock.Now; } else { //直接按HH:mm:ss来解析,测试过这种方法目前是效率比较高的方法 try { // 只有使用交易所行情时才需要处理跨天的问题 ChangeActionDay(pDepthMarketData.TradingDay); int HH = int.Parse(pDepthMarketData.UpdateTime.Substring(0, 2)); int mm = int.Parse(pDepthMarketData.UpdateTime.Substring(3, 2)); int ss = int.Parse(pDepthMarketData.UpdateTime.Substring(6, 2)); _dateTime = new DateTime(_yyyy, _MM, _dd, HH, mm, ss, pDepthMarketData.UpdateMillisec); } catch (Exception) { _dateTime = Clock.Now; } } if (record.TradeRequested) { //通过测试,发现IB的Trade与Quote在行情过来时数量是不同的,在这也做到不同 if (DepthMarket.LastPrice == pDepthMarketData.LastPrice && DepthMarket.Volume == pDepthMarketData.Volume) { } else { //行情过来时是今天累计成交量,得转换成每个tick中成交量之差 int volume = pDepthMarketData.Volume - DepthMarket.Volume; if (0 == DepthMarket.Volume) { //没有接收到最开始的一条,所以这计算每个Bar的数据时肯定超大,强行设置为0 volume = 0; } else if (volume < 0) { //如果隔夜运行,会出现今早成交量0-昨收盘成交量,出现负数,所以当发现为负时要修改 volume = pDepthMarketData.Volume; } // 使用新的类,保存更多信息 CTPZQTrade trade = new CTPZQTrade(_dateTime, pDepthMarketData.LastPrice == double.MaxValue ? 0 : pDepthMarketData.LastPrice, volume); // 记录深度数据 trade.DepthMarketData = pDepthMarketData; EmitNewTradeEvent(instrument, trade); } } if (record.QuoteRequested) { //if ( //DepthMarket.BidVolume1 == pDepthMarketData.BidVolume1 //&& DepthMarket.AskVolume1 == pDepthMarketData.AskVolume1 //&& DepthMarket.BidPrice1 == pDepthMarketData.BidPrice1 //&& DepthMarket.AskPrice1 == pDepthMarketData.AskPrice1 //) //{ } //else { CTPZQQuote quote = new CTPZQQuote(_dateTime, pDepthMarketData.BidPrice1 == double.MaxValue ? 0 : pDepthMarketData.BidPrice1, pDepthMarketData.BidVolume1, pDepthMarketData.AskPrice1 == double.MaxValue ? 0 : pDepthMarketData.AskPrice1, pDepthMarketData.AskVolume1 ); quote.DepthMarketData = pDepthMarketData; EmitNewQuoteEvent(instrument, quote); } } if (record.MarketDepthRequested) { EmitNewMarketDepth(instrument, _dateTime, 0, MDSide.Ask, pDepthMarketData.AskPrice1, pDepthMarketData.AskVolume1); EmitNewMarketDepth(instrument, _dateTime, 0, MDSide.Bid, pDepthMarketData.BidPrice1, pDepthMarketData.BidVolume1); EmitNewMarketDepth(instrument, _dateTime, 1, MDSide.Ask, pDepthMarketData.AskPrice2, pDepthMarketData.AskVolume2); EmitNewMarketDepth(instrument, _dateTime, 1, MDSide.Bid, pDepthMarketData.BidPrice2, pDepthMarketData.BidVolume2); EmitNewMarketDepth(instrument, _dateTime, 2, MDSide.Ask, pDepthMarketData.AskPrice3, pDepthMarketData.AskVolume3); EmitNewMarketDepth(instrument, _dateTime, 2, MDSide.Bid, pDepthMarketData.BidPrice3, pDepthMarketData.BidVolume3); EmitNewMarketDepth(instrument, _dateTime, 3, MDSide.Ask, pDepthMarketData.AskPrice4, pDepthMarketData.AskVolume4); EmitNewMarketDepth(instrument, _dateTime, 3, MDSide.Bid, pDepthMarketData.BidPrice4, pDepthMarketData.BidVolume4); EmitNewMarketDepth(instrument, _dateTime, 4, MDSide.Ask, pDepthMarketData.AskPrice5, pDepthMarketData.AskVolume5); EmitNewMarketDepth(instrument, _dateTime, 4, MDSide.Bid, pDepthMarketData.BidPrice5, pDepthMarketData.BidVolume5); } // 直接回报CTP的行情信息 if (EmitOnRtnDepthMarketData) { CTPZQAPI.GetInstance().FireOnRtnDepthMarketData(pDepthMarketData); } }