private void OnRspQryInvestorPosition(IntPtr pTraderApi, ref CThostFtdcInvestorPositionField pInvestorPosition, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (0 == pRspInfo.ErrorID) { string key = GetPositionKey(pInvestorPosition); _dictPositions[key] = pInvestorPosition; CTPAPI.GetInstance().FireOnRspReqQryInvestorPosition(pInvestorPosition); timerPonstion.Enabled = false; timerPonstion.Enabled = true; } else { tdlog.Error("nRequestID:{0},ErrorID:{1},OnRspQryInvestorPosition:{2}", nRequestID, pRspInfo.ErrorID, pRspInfo.ErrorMsg); EmitError(nRequestID, pRspInfo.ErrorID, "OnRspQryInvestorPosition:" + pRspInfo.ErrorMsg); } }
//建立行情 private void Connect_MD() { lock (_lockMd) { if (_bWantMdConnect && (null == m_pMdApi || IntPtr.Zero == m_pMdApi)) { m_pMdApi = MdApi.MD_CreateMdApi(); MdApi.CTP_RegOnRtnDepthMarketData(m_pMsgQueue, _fnOnRtnDepthMarketData_Holder); MdApi.MD_RegMsgQueue2MdApi(m_pMdApi, m_pMsgQueue); MdApi.MD_Connect(m_pMdApi, _newTempPath, string.Join(";", server.MarketData.ToArray()), server.BrokerID, account.InvestorId, account.Password); //向单例对象中注入操作用句柄 CTPAPI.GetInstance().__RegMdApi(m_pMdApi); } } }
//建立交易 private void Connect_TD() { lock (_lockTd) { if (_bWantTdConnect && (null == m_pTdApi || IntPtr.Zero == m_pTdApi)) { m_pTdApi = TraderApi.TD_CreateTdApi(); TraderApi.CTP_RegOnErrRtnOrderAction(m_pMsgQueue, _fnOnErrRtnOrderAction_Holder); TraderApi.CTP_RegOnErrRtnOrderInsert(m_pMsgQueue, _fnOnErrRtnOrderInsert_Holder); TraderApi.CTP_RegOnRspOrderAction(m_pMsgQueue, _fnOnRspOrderAction_Holder); TraderApi.CTP_RegOnRspOrderInsert(m_pMsgQueue, _fnOnRspOrderInsert_Holder); TraderApi.CTP_RegOnRspQryDepthMarketData(m_pMsgQueue, _fnOnRspQryDepthMarketData_Holder); TraderApi.CTP_RegOnRspQryInstrument(m_pMsgQueue, _fnOnRspQryInstrument_Holder); TraderApi.CTP_RegOnRspQryInstrumentCommissionRate(m_pMsgQueue, _fnOnRspQryInstrumentCommissionRate_Holder); TraderApi.CTP_RegOnRspQryInvestorPosition(m_pMsgQueue, _fnOnRspQryInvestorPosition_Holder); TraderApi.CTP_RegOnRspQryTradingAccount(m_pMsgQueue, _fnOnRspQryTradingAccount_Holder); TraderApi.CTP_RegOnRtnInstrumentStatus(m_pMsgQueue, _fnOnRtnInstrumentStatus_Holder); TraderApi.CTP_RegOnRtnOrder(m_pMsgQueue, _fnOnRtnOrder_Holder); TraderApi.CTP_RegOnRtnTrade(m_pMsgQueue, _fnOnRtnTrade_Holder); #if CTP TraderApi.CTP_RegOnRspQryInstrumentMarginRate(m_pMsgQueue, _fnOnRspQryInstrumentMarginRate_Holder); #endif TraderApi.CTP_RegOnErrRtnQuoteAction(m_pMsgQueue, _fnOnErrRtnQuoteAction_Holder); TraderApi.CTP_RegOnErrRtnQuoteInsert(m_pMsgQueue, _fnOnErrRtnQuoteInsert_Holder); TraderApi.CTP_RegOnRspQuoteAction(m_pMsgQueue, _fnOnRspQuoteAction_Holder); TraderApi.CTP_RegOnRspQuoteInsert(m_pMsgQueue, _fnOnRspQuoteInsert_Holder); TraderApi.CTP_RegOnRtnQuote(m_pMsgQueue, _fnOnRtnQuote_Holder); TraderApi.TD_RegMsgQueue2TdApi(m_pTdApi, m_pMsgQueue); TraderApi.TD_Connect(m_pTdApi, _newTempPath, string.Join(";", server.Trading.ToArray()), server.BrokerID, account.InvestorId, account.Password, ResumeType, server.UserProductInfo, server.AuthCode); //向单例对象中注入操作用句柄 CTPAPI.GetInstance().__RegTdApi(m_pTdApi); } } }
private void OnRspQryTradingAccount(IntPtr pTraderApi, ref CThostFtdcTradingAccountField pTradingAccount, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLastt) { if (0 == pRspInfo.ErrorID) { m_TradingAccount = pTradingAccount; //有资金信息过来了,重新计时 timerAccount.Enabled = false; timerAccount.Enabled = true; //通知单例,还是使用GetBrokerInfo来取呢? CTPAPI.GetInstance().__RegTradingAccount(m_TradingAccount); CTPAPI.GetInstance().FireOnRspQryTradingAccount(pTradingAccount); } else { tdlog.Error("nRequestID:{0},ErrorID:{1},OnRspQryTradingAccount:{2}", nRequestID, pRspInfo.ErrorID, pRspInfo.ErrorMsg); EmitError(nRequestID, pRspInfo.ErrorID, "OnRspQryTradingAccount:" + pRspInfo.ErrorMsg); } }
public void OnRspQryDepthMarketData(IntPtr pTraderApi, ref CThostFtdcDepthMarketDataField pDepthMarketData, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast) { if (0 == pRspInfo.ErrorID) { CThostFtdcDepthMarketDataField DepthMarket; if (!_dictDepthMarketData.TryGetValue(pDepthMarketData.InstrumentID, out DepthMarket)) { //没找到此元素,保存一下 _dictDepthMarketData[pDepthMarketData.InstrumentID] = pDepthMarketData; } tdlog.Info("已经接收查询深度行情 {0}", pDepthMarketData.InstrumentID); //通知单例 CTPAPI.GetInstance().FireOnRspQryDepthMarketData(pDepthMarketData); } else { tdlog.Error("nRequestID:{0},ErrorID:{1},OnRspQryDepthMarketData:{2}", nRequestID, pRspInfo.ErrorID, pRspInfo.ErrorMsg); EmitError(nRequestID, pRspInfo.ErrorID, "OnRspQryDepthMarketData:" + pRspInfo.ErrorMsg); } }
private void _Disconnect() { timerConnect.Enabled = false; timerDisconnect.Enabled = false; timerAccount.Enabled = false; timerPonstion.Enabled = false; CTPAPI.GetInstance().__RegInstrumentDictionary(null); CTPAPI.GetInstance().__RegInstrumentCommissionRateDictionary(null); CTPAPI.GetInstance().__RegInstrumentMarginRateDictionary(null); CTPAPI.GetInstance().__RegDepthMarketDataDictionary(null); Disconnect_MD(); Disconnect_TD(); Disconnect_MsgQueue(); Clear(); ChangeStatus(ProviderStatus.Disconnected); isConnected = false; EmitDisconnectedEvent(); }
private void _Connect() { // 限制低版本的OpenQuant不能使用,其它产品不做限制 switch (Framework.Installation.MainProduct) { case "OpenQuant": if (Assembly.GetEntryAssembly().GetName().Version < new Version(3, 9, 3)) { MessageBox.Show("您的OpenQuant版本过低,请装最新版"); return; } break; case "QuantRouter": break; case "QuantBase": break; case "QuantTrader": break; } CTPAPI.GetInstance().__RegInstrumentDictionary(_dictInstruments); CTPAPI.GetInstance().__RegInstrumentCommissionRateDictionary(_dictCommissionRate); CTPAPI.GetInstance().__RegDepthMarketDataDictionary(_dictDepthMarketData); CTPAPI.GetInstance().__RegInvestorPositionDictionary(_dictPositions); #if CTP CTPAPI.GetInstance().__RegInstrumentMarginRateDictionary(_dictMarginRate); #endif server = null; account = null; bool bCheckOk = false; do { if (0 == serversList.Count) { MessageBox.Show("您还没有设置 服务器 信息,目前只选择第一条进行连接"); break; } if (0 == accountsList.Count) { MessageBox.Show("您还没有设置 账号 信息,目前只选择第一条进行连接"); break; } server = serversList[0]; account = accountsList[0]; if (string.IsNullOrEmpty(server.BrokerID)) { MessageBox.Show("BrokerID不能为空"); break; } if (_bWantTdConnect && 0 == server.Trading.Count()) { MessageBox.Show("交易服务器地址不全"); break; } if (_bWantMdConnect && 0 == server.MarketData.Count()) { MessageBox.Show("行情服务器信息不全"); break; } if (string.IsNullOrEmpty(account.InvestorId) || string.IsNullOrEmpty(account.Password)) { MessageBox.Show("账号信息不全"); break; } bCheckOk = true; } while (false); if (false == bCheckOk) { ChangeStatus(ProviderStatus.Disconnected); isConnected = false; return; } //新建目录 _newTempPath = string.Format("{1}{0}{2}{0}{3}{0}{4}", Path.DirectorySeparatorChar, ApiTempPath, this.Name, server.BrokerID, account.InvestorId); Directory.CreateDirectory(_newTempPath); ChangeStatus(ProviderStatus.Connecting); //如果前面一次连接一直连不上,新改地址后也会没响应,所以先删除 Disconnect_MD(); Disconnect_TD(); if (_bWantMdConnect || _bWantTdConnect) { timerDisconnect.Enabled = true; Connect_MsgQueue(); } if (_bWantMdConnect) { Connect_MD(); } if (_bWantTdConnect) { Connect_TD(); } }
private void _Connect() { CTPAPI.GetInstance().__RegInstrumentDictionary(_dictInstruments); CTPAPI.GetInstance().__RegInstrumentCommissionRateDictionary(_dictCommissionRate); CTPAPI.GetInstance().__RegInstrumentMarginRateDictionary(_dictMarginRate); CTPAPI.GetInstance().__RegDepthMarketDataDictionary(_dictDepthMarketData); server = null; account = null; bool bCheckOk = false; do { if (0 == serversList.Count) { MessageBox.Show("您还没有设置 服务器 信息,目前只选择第一条进行连接"); break; } if (0 == accountsList.Count) { MessageBox.Show("您还没有设置 账号 信息,目前只选择第一条进行连接"); break; } server = serversList[0]; account = accountsList[0]; if (string.IsNullOrEmpty(server.BrokerID)) { MessageBox.Show("BrokerID不能为空"); break; } if (_bWantTdConnect && 0 == server.Trading.Count()) { MessageBox.Show("交易服务器地址不全"); break; } if (_bWantMdConnect && 0 == server.MarketData.Count()) { MessageBox.Show("行情服务器信息不全"); break; } if (string.IsNullOrEmpty(account.InvestorId) || string.IsNullOrEmpty(account.Password)) { MessageBox.Show("账号信息不全"); break; } bCheckOk = true; } while (false); if (false == bCheckOk) { ChangeStatus(ProviderStatus.Disconnected); isConnected = false; return; } //新建目录 _newTempPath = ApiTempPath + Path.DirectorySeparatorChar + server.BrokerID + Path.DirectorySeparatorChar + account.InvestorId; Directory.CreateDirectory(_newTempPath); ChangeStatus(ProviderStatus.Connecting); //如果前面一次连接一直连不上,新改地址后也会没响应,所以先删除 Disconnect_MD(); Disconnect_TD(); if (_bWantMdConnect || _bWantTdConnect) { timerDisconnect.Enabled = true; Connect_MsgQueue(); } if (_bWantMdConnect) { Connect_MD(); } if (_bWantTdConnect) { Connect_TD(); } }
private void OnRtnForQuoteRsp(IntPtr pMdUserApi, ref CThostFtdcForQuoteRspField pForQuoteRsp) { Console.WriteLine("OnRtnForQuoteRsp"); // 询价请求 CTPAPI.GetInstance().FireOnRtnForQuoteRsp(pForQuoteRsp); }
private void OnRtnDepthMarketData(IntPtr pApi, ref CThostFtdcDepthMarketDataField pDepthMarketData) { DataRecord record; if (!_dictAltSymbol2Instrument.TryGetValue(pDepthMarketData.InstrumentID, out record)) { mdlog.Warn("合约{0}不在订阅列表中却收到了数据", pDepthMarketData.InstrumentID); return; } Instrument instrument = record.Instrument; CThostFtdcDepthMarketDataField DepthMarket; _dictDepthMarketData.TryGetValue(pDepthMarketData.InstrumentID, out DepthMarket); //将更新字典的功能提前,因为如果一开始就OnTrade中下单,涨跌停没有更新 _dictDepthMarketData[pDepthMarketData.InstrumentID] = pDepthMarketData; if (TimeMode.LocalTime == _TimeMode) { //为了生成正确的Bar,使用本地时间 _dateTime = Clock.Now; } else { //直接按HH:mm:ss来解析,测试过这种方法目前是效率比较高的方法 try { // 只有使用交易所行情时才需要处理跨天的问题 ChangeTradingDay(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; } Trade trade = new Trade(_dateTime, pDepthMarketData.LastPrice == double.MaxValue ? 0 : pDepthMarketData.LastPrice, volume); EmitNewTradeEvent(instrument, trade); } } if (record.QuoteRequested) { //if ( //DepthMarket.BidVolume1 == pDepthMarketData.BidVolume1 //&& DepthMarket.AskVolume1 == pDepthMarketData.AskVolume1 //&& DepthMarket.BidPrice1 == pDepthMarketData.BidPrice1 //&& DepthMarket.AskPrice1 == pDepthMarketData.AskPrice1 //) //{ } //else { Quote quote = new Quote(_dateTime, pDepthMarketData.BidPrice1 == double.MaxValue ? 0 : pDepthMarketData.BidPrice1, pDepthMarketData.BidVolume1, pDepthMarketData.AskPrice1 == double.MaxValue ? 0 : pDepthMarketData.AskPrice1, pDepthMarketData.AskVolume1 ); 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); } // 价差生成功能 do { if (null == CTPAPI.GetInstance().SpreadMarketData) { break; } ISpreadMarketData SpreadMarketData = CTPAPI.GetInstance().SpreadMarketData; var ticks = SpreadMarketData.CalculateSpread(pDepthMarketData); if (null == ticks) { break; } foreach (var tick in ticks) { Instrument inst = InstrumentManager.Instruments[tick.Symbol]; if (null == inst) { continue; } if (!double.IsNaN(tick.Price)) { Trade trade = new Trade(_dateTime, tick.Price, tick.Size); trade.ProviderId = tick.ProviderId; EmitNewTradeEvent(inst, trade); } if (!double.IsNaN(tick.Ask) && !double.IsNaN(tick.Bid)) { Quote quote = new Quote(_dateTime, tick.Bid, tick.BidSize, tick.Ask, tick.AskSize); quote.ProviderId = tick.ProviderId; EmitNewQuoteEvent(inst, quote); } } } while (false); // 直接回报CTP的行情信息 if (EmitOnRtnDepthMarketData) { CTPAPI.GetInstance().FireOnRtnDepthMarketData(pDepthMarketData); } }