发布时间:2018-09-25
 
技术:C++11,动态库的制作
 

概述

CTP的接入Demo

详细

本文档不介绍CTP的具体流程,具体流程请参考上海期货交易所文档(下载).

一、概述

1.CTP是上期技术,提供的国内期货行情和交易的接口,自推出以来,各大券商均架设了CTP技术的接入,引入策略算法便可以初步形成一个自动交易的系统,这也就吸引了很多对自动交易,策略交易感兴趣的各路高人来使用。

2.CTP难点在于,一个库提供了行情和交易两套接口,各自均有一套业务流程,而且两者在业务上还存在部分业务关联,也就是说还要处理两套之间的业务同步问题,没有一些C++基础的人很难开发好用的CTP库。

3.本Demo目标是,在Windows环境下做两个程序:

一个封装CTP行情和交易接口成一个库,尽可能在高内聚低耦合的情况下,保持代码清晰,通俗尽可能让;

一个执行程序很容易的去调用这个库,这里没有使用MFC或QT,直接采用控制台程序,很清晰的展示使用库。

二、准备环境

1.开发工具:visual studio 2015或以上版本(下载

本Demo采用Visual studio 2015 专业版 update 3

Windows7 64位

2.下载上期CTP库(下载

综合交易平台API下载,下载列表中的windows下64位库(最新(2015-05-15)),解压如下:

3.申请模拟账号(申请)

主页右上角,注册账号,例子中方便开箱即用,会使用我自己申请好的,请自行换成自己的帐号。

三、程序介绍

1.先看下Demo的运行效果。

2.程序目录结构

①Show all files模式下,VS工程目录结构如下图:

共两个项目,CTPSample和CTPServer,CTPSample为封装交易所CTP的动态库,CTPServer为使用库的UI程序。

②代码目录结构

bin64

-----CTP CTP产生的共享文件

-----Log 日志文件

config.cfg 配置文件

build

CTPServer.sln 存放工程文件

Src

------CTPSample DLL代码

------CTPServer UI代码

------Framwork 框架代码,避免复杂,不做公开(这里主要提供日志,数据定义,可自己替换)

说明:confg.cfg主要保存了CTP的地址(其他均为非本Demo演示的主要功能):

[info]

CTPFront.trade1 = tcp://180.168.146.187:10030

CTPFront.quote1 = tcp://180.168.146.187:10031

CTPFront.trade2 = tcp://180.168.146.187:10001

CTPFront.quote2 = tcp://180.168.146.187:10011

CTPFront.trade3 = tcp://218.202.237.33:10002

CTPFront.quote3 = tcp://ctp1-md11.citicsf.com:41213

3.模块介绍

CTPSample模块

CTPBase.h 动态库的导出定义

MyCTPQuote.h/MyCTPQuote.CPP 交易的封装

MyCTPQuote.h/MyCTPQuote.cpp 行情的封装

CTPServer模块

TradeManager.h/TradeManager.cpp UI主逻辑

CTPServer.cpp main启动

四、程序解析

1. CTPSample模块之MyCTPQuote.h

  1. //定义一个管理器,管理行情接受
  2. class CTPSAMPLE_EXPORT MyCTPQuote
  3. {
  4. //嵌入行情回报类
  5. class MyRecv : public CThostFtdcMdSpi
  6. {
  7. public:
  8. MyRecv():m_Parent(nullptr) {};
  9. ~MyRecv() {};
  10. void Bind(MyCTPQuote *Parent) { m_Parent = Parent; }
  11. public:
  12. ///当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。
  13. virtual void OnFrontConnected() { m_Parent->OnFrontConnected(); }
  14.  
  15. ///当客户端与交易后台通信连接断开时,该方法被调用。当发生这个情况后,API会自动重新连接,客户端可不做处理。
  16. ///@param nReason 错误原因
  17. /// 0x1001 网络读失败
  18. /// 0x1002 网络写失败
  19. /// 0x2001 接收心跳超时
  20. /// 0x2002 发送心跳失败
  21. /// 0x2003 收到错误报文
  22. virtual void OnFrontDisconnected(int nReason) { m_Parent->OnFrontDisconnected(nReason); }
  23.  
  24. ///心跳超时警告。当长时间未收到报文时,该方法被调用。
  25. ///@param nTimeLapse 距离上次接收报文的时间
  26. virtual void OnHeartBeatWarning(int nTimeLapse) { m_Parent->OnHeartBeatWarning(nTimeLapse); }
  27.  
  28. ///登录请求响应
  29. virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  30. { m_Parent->OnRspUserLogin(pRspUserLogin, pRspInfo, nRequestID, bIsLast); }
  31.  
  32. ///登出请求响应
  33. virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  34. { m_Parent->OnRspUserLogout(pUserLogout, pRspInfo, nRequestID, bIsLast); }
  35.  
  36. ///错误应答
  37. virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { m_Parent->OnRspError(pRspInfo, nRequestID, bIsLast); }
  38.  
  39. ///订阅行情应答
  40. virtual void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  41. { m_Parent->OnRspSubMarketData(pSpecificInstrument, pRspInfo, nRequestID, bIsLast); }
  42.  
  43. ///取消订阅行情应答
  44. virtual void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  45. { m_Parent->OnRspUnSubMarketData(pSpecificInstrument, pRspInfo, nRequestID, bIsLast); }
  46.  
  47. ///订阅询价应答
  48. virtual void OnRspSubForQuoteRsp(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  49. { m_Parent->OnRspSubForQuoteRsp(pSpecificInstrument, pRspInfo, nRequestID, bIsLast); }
  50.  
  51. ///取消订阅询价应答
  52. virtual void OnRspUnSubForQuoteRsp(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  53. { m_Parent->OnRspUnSubForQuoteRsp(pSpecificInstrument, pRspInfo, nRequestID, bIsLast); }
  54.  
  55. ///深度行情通知
  56. virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) { m_Parent->OnRtnDepthMarketData(pDepthMarketData); }
  57.  
  58. ///询价通知
  59. virtual void OnRtnForQuoteRsp(CThostFtdcForQuoteRspField *pForQuoteRsp) { m_Parent->OnRtnForQuoteRsp(pForQuoteRsp); }
  60.  
  61. public:
  62.  
  63. MyCTPQuote *m_Parent;
  64. };
  65.  
  66. //thread
  67. public:
  68. MyCTPQuote( );
  69. ~MyCTPQuote();
  70.  
  71. void setMainWidget(void *widget);
  72. // 订阅广播报文
  73. void Subscribe(CPacketReceiver *pPacketReceiver);
  74.  
  75. // 取消广播报文订阅
  76. void Unsubscribe(CPacketReceiver *pPacketReceiver);
  77.  
  78. static MyCTPQuote *Instance();
  79.  
  80. void Init(const char *User, const char *pswd, const char *broker, const char *pszAddress);
  81. void FinishQryInstrument();
  82.  
  83. void setLog(const string& str);
  84. bool m_bLoginSuccessed;
  85. public:
  86. ///////////////////////////////////////请求类函数,提供主要逻辑,供外部使用////////////////////////////////////////////////////////////////////////////
  87. ///用户登录请求
  88. int ReqUserLogin(CThostFtdcReqUserLoginField *pReqUserLoginField, int nRequestID)
  89. {
  90. return m_reqApi->ReqUserLogin(pReqUserLoginField, nRequestID );
  91. }
  92.  
  93. ///登出请求
  94. int ReqUserLogout(CThostFtdcUserLogoutField *pUserLogout, int nRequestID) { return m_reqApi->ReqUserLogout(pUserLogout, nRequestID); }
  95.  
  96. ///获取当前交易日
  97. ///@retrun 获取到的交易日
  98. ///@remark 只有登录成功后,才能得到正确的交易日
  99. const char *GetTradingDay() { return m_reqApi->GetTradingDay(); }
  100. ///订阅行情。
  101. ///@param ppInstrumentID 合约ID
  102. ///@param nCount 要订阅/退订行情的合约个数
  103. ///@remark
  104. int SubscribeMarketData(char *ppInstrumentID[], int nCount) { return m_reqApi->SubscribeMarketData(ppInstrumentID, nCount); }
  105.  
  106. ///退订行情。
  107. ///@param ppInstrumentID 合约ID
  108. ///@param nCount 要订阅/退订行情的合约个数
  109. ///@remark
  110. int UnSubscribeMarketData(char *ppInstrumentID[], int nCount) { return m_reqApi->UnSubscribeMarketData(ppInstrumentID, nCount); }
  111.  
  112. public:
  113. ///////////////////////////////////////回报类函数,收到数据,更新本地行情信息////////////////////////////////////////////////////////////////////////////
  114.  
  115. ///当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。
  116. void OnFrontConnected();
  117.  
  118. ///当客户端与交易后台通信连接断开时,该方法被调用。当发生这个情况后,API会自动重新连接,客户端可不做处理。
  119. ///@param nReason 错误原因
  120. /// 0x1001 网络读失败
  121. /// 0x1002 网络写失败
  122. /// 0x2001 接收心跳超时
  123. /// 0x2002 发送心跳失败
  124. /// 0x2003 收到错误报文
  125. void OnFrontDisconnected(int nReason);
  126.  
  127. ///心跳超时警告。当长时间未收到报文时,该方法被调用。
  128. ///@param nTimeLapse 距离上次接收报文的时间
  129. void OnHeartBeatWarning(int nTimeLapse);
  130.  
  131. ///登录请求响应
  132. void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  133.  
  134. ///登出请求响应
  135. void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  136.  
  137. ///错误应答
  138. void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  139.  
  140. ///订阅行情应答
  141. void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  142.  
  143. ///取消订阅行情应答
  144. void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  145.  
  146. ///订阅询价应答
  147. void OnRspSubForQuoteRsp(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  148.  
  149. ///取消订阅询价应答
  150. void OnRspUnSubForQuoteRsp(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  151.  
  152. ///深度行情通知
  153. void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData);
  154.  
  155. ///询价通知
  156. void OnRtnForQuoteRsp(CThostFtdcForQuoteRspField *pForQuoteRsp);
  157.  
  158. private:
  159. CThostFtdcMdApi* m_reqApi;
  160. MyRecv *m_RecvSpi;
  161. map<string /*InstrumentID*/, CThostFtdcInstrumentField> m_mapInstrument;//合约ID,合约信息
  162. map<string /*InstrumentID*/,CThostFtdcDepthMarketDataField /*Quotation*/> m_mapInstrumentQuote;//保存 合约ID-最新行情 对,合约ID唯一。
  163. vector<string /*InstrumentID*/> m_vecInstrumentId;//合约ID
  164. int requestID;
  165. string m_TradingDay;
  166.  
  167. int m_session ;
  168. int m_frontId ;
  169. string OrderRef ;
  170.  
  171. mutex m_mutex;
  172. ///用户登录信息
  173. CThostFtdcReqUserLoginField *m_userLoginInfo;
  174. bool m_bQryInstrumentOK;
  175.  
  176. //用于给上层订阅
  177. typedef vector<CPacketReceiver*> VPKTRECEIVER;
  178. VPKTRECEIVER m_vPketReceiver;
  179.  
  180. };

主要功能点:

①模块管理:

CTP的行情模块分两块,一个是同步查询API(CThostFtdcMdApi),一个是异步回报API(CThostFtdcMdSpi),这里定义了一个包装类,来管理这两类API,即

MyRecv来封装CThostFtdcMdSpi,CThostFtdcMdApi直接使用。

②给UI回报接口

本模块提供给上层的接口,定义了一个虚基类:

  1. class CPacketReceiver
  2. {
  3. public:
  4. virtual void Receive(CBroadcastPacket &pkt) = 0;
  5. };
  6.  
  7. 这里运用到actor模式,接受UI订阅:
  8. typedef vector<CPacketReceiver*> VPKTRECEIVER;
  9. VPKTRECEIVER m_vPketReceiver;

③数据保存

数据收集,定义几个map

  1. map<string /*InstrumentID*/, CThostFtdcInstrumentField> m_mapInstrument;//合约ID,合约信息
  2. map<string /*InstrumentID*/,CThostFtdcDepthMarketDataField /*Quotation*/> m_mapInstrumentQuote;//保存 合约ID-最新行情 对,合约ID唯一。
  3. vector<string /*InstrumentID*/> m_vecInstrumentId;//合约ID

2. CTPSample模块之MyCTPTrade.h

  1. //定义一个管理器,管理行情接受
  2. class CTPSAMPLE_EXPORT MyCTPTrade
  3. {
  4. //嵌入行情回报类
  5. class MyRecv : public CThostFtdcTraderSpi
  6. {
  7. public:
  8. MyRecv():m_Parent(nullptr), m_nRequestId(0){};
  9. ~MyRecv() {};
  10. void Bind(MyCTPTrade *Parent) { m_Parent = Parent; }
  11. public:
  12. ///当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。
  13. virtual void OnFrontConnected() { m_Parent->OnFrontConnected(); }
  14.  
  15. ///当客户端与交易后台通信连接断开时,该方法被调用。当发生这个情况后,API会自动重新连接,客户端可不做处理。
  16. ///@param nReason 错误原因
  17. /// 0x1001 网络读失败
  18. /// 0x1002 网络写失败
  19. /// 0x2001 接收心跳超时
  20. /// 0x2002 发送心跳失败
  21. /// 0x2003 收到错误报文
  22. virtual void OnFrontDisconnected(int nReason) { m_Parent->OnFrontDisconnected(nReason); }
  23.  
  24. ///客户端认证响应
  25. virtual void OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  26. { m_Parent->OnRspAuthenticate(pRspAuthenticateField,pRspInfo, nRequestID, bIsLast); }
  27.  
  28. ///心跳超时警告。当长时间未收到报文时,该方法被调用。
  29. ///@param nTimeLapse 距离上次接收报文的时间
  30. virtual void OnHeartBeatWarning(int nTimeLapse) { m_Parent->OnHeartBeatWarning(nTimeLapse); }
  31.  
  32. ///登录请求响应
  33. virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  34.  
  35. { m_Parent->OnRspUserLogin(pRspUserLogin, pRspInfo, nRequestID, bIsLast); }
  36.  
  37. ///登出请求响应
  38. virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  39. { m_Parent->OnRspUserLogout(pUserLogout, pRspInfo, nRequestID, bIsLast); }
  40.  
  41. ///错误应答
  42. virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {m_Parent->OnRspError(pRspInfo, nRequestID, bIsLast);}
  43.  
  44. /*
  45. ///用户口令更新请求响应
  46. virtual void OnRspUserPasswordUpdate(CThostFtdcUserPasswordUpdateField *pUserPasswordUpdate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  47.  
  48. ///资金账户口令更新请求响应
  49. virtual void OnRspTradingAccountPasswordUpdate(CThostFtdcTradingAccountPasswordUpdateField *pTradingAccountPasswordUpdate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  50.  
  51. ///报单录入请求响应
  52. virtual void OnRspOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  53.  
  54. ///预埋单录入请求响应
  55. virtual void OnRspParkedOrderInsert(CThostFtdcParkedOrderField *pParkedOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  56.  
  57. ///预埋撤单录入请求响应
  58. virtual void OnRspParkedOrderAction(CThostFtdcParkedOrderActionField *pParkedOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  59.  
  60. ///报单操作请求响应
  61. virtual void OnRspOrderAction(CThostFtdcInputOrderActionField *pInputOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  62.  
  63. ///查询最大报单数量响应
  64. virtual void OnRspQueryMaxOrderVolume(CThostFtdcQueryMaxOrderVolumeField *pQueryMaxOrderVolume, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  65.  
  66. ///投资者结算结果确认响应
  67. virtual void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  68.  
  69. ///删除预埋单响应
  70. virtual void OnRspRemoveParkedOrder(CThostFtdcRemoveParkedOrderField *pRemoveParkedOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  71.  
  72. ///删除预埋撤单响应
  73. virtual void OnRspRemoveParkedOrderAction(CThostFtdcRemoveParkedOrderActionField *pRemoveParkedOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  74.  
  75. ///执行宣告录入请求响应
  76. virtual void OnRspExecOrderInsert(CThostFtdcInputExecOrderField *pInputExecOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  77.  
  78. ///执行宣告操作请求响应
  79. virtual void OnRspExecOrderAction(CThostFtdcInputExecOrderActionField *pInputExecOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  80.  
  81. ///询价录入请求响应
  82. virtual void OnRspForQuoteInsert(CThostFtdcInputForQuoteField *pInputForQuote, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  83.  
  84. ///报价录入请求响应
  85. virtual void OnRspQuoteInsert(CThostFtdcInputQuoteField *pInputQuote, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  86.  
  87. ///报价操作请求响应
  88. virtual void OnRspQuoteAction(CThostFtdcInputQuoteActionField *pInputQuoteAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  89.  
  90. ///批量报单操作请求响应
  91. virtual void OnRspBatchOrderAction(CThostFtdcInputBatchOrderActionField *pInputBatchOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  92.  
  93. ///期权自对冲录入请求响应
  94. virtual void OnRspOptionSelfCloseInsert(CThostFtdcInputOptionSelfCloseField *pInputOptionSelfClose, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  95.  
  96. ///期权自对冲操作请求响应
  97. virtual void OnRspOptionSelfCloseAction(CThostFtdcInputOptionSelfCloseActionField *pInputOptionSelfCloseAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  98.  
  99. ///申请组合录入请求响应
  100. virtual void OnRspCombActionInsert(CThostFtdcInputCombActionField *pInputCombAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  101.  
  102. ///请求查询报单响应
  103. virtual void OnRspQryOrder(CThostFtdcOrderField *pOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  104.  
  105. ///请求查询成交响应
  106. virtual void OnRspQryTrade(CThostFtdcTradeField *pTrade, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  107.  
  108. ///请求查询投资者持仓响应
  109. virtual void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  110.  
  111. ///请求查询资金账户响应
  112. virtual void OnRspQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  113.  
  114. ///请求查询投资者响应
  115. virtual void OnRspQryInvestor(CThostFtdcInvestorField *pInvestor, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  116.  
  117. ///请求查询交易编码响应
  118. virtual void OnRspQryTradingCode(CThostFtdcTradingCodeField *pTradingCode, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  119.  
  120. ///请求查询合约保证金率响应
  121. virtual void OnRspQryInstrumentMarginRate(CThostFtdcInstrumentMarginRateField *pInstrumentMarginRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  122.  
  123. ///请求查询合约手续费率响应
  124. virtual void OnRspQryInstrumentCommissionRate(CThostFtdcInstrumentCommissionRateField *pInstrumentCommissionRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  125.  
  126. ///请求查询交易所响应
  127. virtual void OnRspQryExchange(CThostFtdcExchangeField *pExchange, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  128.  
  129. ///请求查询产品响应
  130. virtual void OnRspQryProduct(CThostFtdcProductField *pProduct, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  131. */
  132. ///请求查询合约响应
  133. virtual void OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
  134. {
  135. m_Parent->OnRspQryInstrument(pInstrument, pRspInfo, nRequestID, bIsLast);
  136. };
  137. /*
  138. ///请求查询行情响应
  139. virtual void OnRspQryDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  140.  
  141. ///请求查询投资者结算结果响应
  142. virtual void OnRspQrySettlementInfo(CThostFtdcSettlementInfoField *pSettlementInfo, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  143.  
  144. ///请求查询转帐银行响应
  145. virtual void OnRspQryTransferBank(CThostFtdcTransferBankField *pTransferBank, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  146.  
  147. ///请求查询投资者持仓明细响应
  148. virtual void OnRspQryInvestorPositionDetail(CThostFtdcInvestorPositionDetailField *pInvestorPositionDetail, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  149.  
  150. ///请求查询客户通知响应
  151. virtual void OnRspQryNotice(CThostFtdcNoticeField *pNotice, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  152.  
  153. ///请求查询结算信息确认响应
  154. virtual void OnRspQrySettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  155.  
  156. ///请求查询投资者持仓明细响应
  157. virtual void OnRspQryInvestorPositionCombineDetail(CThostFtdcInvestorPositionCombineDetailField *pInvestorPositionCombineDetail, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  158.  
  159. ///查询保证金监管系统经纪公司资金账户密钥响应
  160. virtual void OnRspQryCFMMCTradingAccountKey(CThostFtdcCFMMCTradingAccountKeyField *pCFMMCTradingAccountKey, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  161.  
  162. ///请求查询仓单折抵信息响应
  163. virtual void OnRspQryEWarrantOffset(CThostFtdcEWarrantOffsetField *pEWarrantOffset, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  164.  
  165. ///请求查询投资者品种/跨品种保证金响应
  166. virtual void OnRspQryInvestorProductGroupMargin(CThostFtdcInvestorProductGroupMarginField *pInvestorProductGroupMargin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  167.  
  168. ///请求查询交易所保证金率响应
  169. virtual void OnRspQryExchangeMarginRate(CThostFtdcExchangeMarginRateField *pExchangeMarginRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  170.  
  171. ///请求查询交易所调整保证金率响应
  172. virtual void OnRspQryExchangeMarginRateAdjust(CThostFtdcExchangeMarginRateAdjustField *pExchangeMarginRateAdjust, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  173.  
  174. ///请求查询汇率响应
  175. virtual void OnRspQryExchangeRate(CThostFtdcExchangeRateField *pExchangeRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  176.  
  177. ///请求查询二级代理操作员银期权限响应
  178. virtual void OnRspQrySecAgentACIDMap(CThostFtdcSecAgentACIDMapField *pSecAgentACIDMap, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  179.  
  180. ///请求查询产品报价汇率
  181. virtual void OnRspQryProductExchRate(CThostFtdcProductExchRateField *pProductExchRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  182.  
  183. ///请求查询产品组
  184. virtual void OnRspQryProductGroup(CThostFtdcProductGroupField *pProductGroup, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  185.  
  186. ///请求查询做市商合约手续费率响应
  187. virtual void OnRspQryMMInstrumentCommissionRate(CThostFtdcMMInstrumentCommissionRateField *pMMInstrumentCommissionRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  188.  
  189. ///请求查询做市商期权合约手续费响应
  190. virtual void OnRspQryMMOptionInstrCommRate(CThostFtdcMMOptionInstrCommRateField *pMMOptionInstrCommRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  191.  
  192. ///请求查询报单手续费响应
  193. virtual void OnRspQryInstrumentOrderCommRate(CThostFtdcInstrumentOrderCommRateField *pInstrumentOrderCommRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  194.  
  195. ///请求查询资金账户响应
  196. virtual void OnRspQrySecAgentTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  197.  
  198. ///请求查询二级代理商资金校验模式响应
  199. virtual void OnRspQrySecAgentCheckMode(CThostFtdcSecAgentCheckModeField *pSecAgentCheckMode, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  200.  
  201. ///请求查询期权交易成本响应
  202. virtual void OnRspQryOptionInstrTradeCost(CThostFtdcOptionInstrTradeCostField *pOptionInstrTradeCost, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  203.  
  204. ///请求查询期权合约手续费响应
  205. virtual void OnRspQryOptionInstrCommRate(CThostFtdcOptionInstrCommRateField *pOptionInstrCommRate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  206.  
  207. ///请求查询执行宣告响应
  208. virtual void OnRspQryExecOrder(CThostFtdcExecOrderField *pExecOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  209.  
  210. ///请求查询询价响应
  211. virtual void OnRspQryForQuote(CThostFtdcForQuoteField *pForQuote, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  212.  
  213. ///请求查询报价响应
  214. virtual void OnRspQryQuote(CThostFtdcQuoteField *pQuote, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  215.  
  216. ///请求查询期权自对冲响应
  217. virtual void OnRspQryOptionSelfClose(CThostFtdcOptionSelfCloseField *pOptionSelfClose, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  218.  
  219. ///请求查询投资单元响应
  220. virtual void OnRspQryInvestUnit(CThostFtdcInvestUnitField *pInvestUnit, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  221.  
  222. ///请求查询组合合约安全系数响应
  223. virtual void OnRspQryCombInstrumentGuard(CThostFtdcCombInstrumentGuardField *pCombInstrumentGuard, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  224.  
  225. ///请求查询申请组合响应
  226. virtual void OnRspQryCombAction(CThostFtdcCombActionField *pCombAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  227.  
  228. ///请求查询转帐流水响应
  229. virtual void OnRspQryTransferSerial(CThostFtdcTransferSerialField *pTransferSerial, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  230.  
  231. ///请求查询银期签约关系响应
  232. virtual void OnRspQryAccountregister(CThostFtdcAccountregisterField *pAccountregister, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  233.  
  234. ///报单通知
  235. virtual void OnRtnOrder(CThostFtdcOrderField *pOrder) {};
  236.  
  237. ///成交通知
  238. virtual void OnRtnTrade(CThostFtdcTradeField *pTrade) {};
  239.  
  240. ///报单录入错误回报
  241. virtual void OnErrRtnOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo) {};
  242.  
  243. ///报单操作错误回报
  244. virtual void OnErrRtnOrderAction(CThostFtdcOrderActionField *pOrderAction, CThostFtdcRspInfoField *pRspInfo) {};
  245.  
  246. ///合约交易状态通知
  247. virtual void OnRtnInstrumentStatus(CThostFtdcInstrumentStatusField *pInstrumentStatus) {};
  248.  
  249. ///交易所公告通知
  250. virtual void OnRtnBulletin(CThostFtdcBulletinField *pBulletin) {};
  251.  
  252. ///交易通知
  253. virtual void OnRtnTradingNotice(CThostFtdcTradingNoticeInfoField *pTradingNoticeInfo) {};
  254.  
  255. ///提示条件单校验错误
  256. virtual void OnRtnErrorConditionalOrder(CThostFtdcErrorConditionalOrderField *pErrorConditionalOrder) {};
  257.  
  258. ///执行宣告通知
  259. virtual void OnRtnExecOrder(CThostFtdcExecOrderField *pExecOrder) {};
  260.  
  261. ///执行宣告录入错误回报
  262. virtual void OnErrRtnExecOrderInsert(CThostFtdcInputExecOrderField *pInputExecOrder, CThostFtdcRspInfoField *pRspInfo) {};
  263.  
  264. ///执行宣告操作错误回报
  265. virtual void OnErrRtnExecOrderAction(CThostFtdcExecOrderActionField *pExecOrderAction, CThostFtdcRspInfoField *pRspInfo) {};
  266.  
  267. ///询价录入错误回报
  268. virtual void OnErrRtnForQuoteInsert(CThostFtdcInputForQuoteField *pInputForQuote, CThostFtdcRspInfoField *pRspInfo) {};
  269.  
  270. ///报价通知
  271. virtual void OnRtnQuote(CThostFtdcQuoteField *pQuote) {};
  272.  
  273. ///报价录入错误回报
  274. virtual void OnErrRtnQuoteInsert(CThostFtdcInputQuoteField *pInputQuote, CThostFtdcRspInfoField *pRspInfo) {};
  275.  
  276. ///报价操作错误回报
  277. virtual void OnErrRtnQuoteAction(CThostFtdcQuoteActionField *pQuoteAction, CThostFtdcRspInfoField *pRspInfo) {};
  278.  
  279. ///询价通知
  280. virtual void OnRtnForQuoteRsp(CThostFtdcForQuoteRspField *pForQuoteRsp) {};
  281.  
  282. ///保证金监控中心用户令牌
  283. virtual void OnRtnCFMMCTradingAccountToken(CThostFtdcCFMMCTradingAccountTokenField *pCFMMCTradingAccountToken) {};
  284.  
  285. ///批量报单操作错误回报
  286. virtual void OnErrRtnBatchOrderAction(CThostFtdcBatchOrderActionField *pBatchOrderAction, CThostFtdcRspInfoField *pRspInfo) {};
  287.  
  288. ///期权自对冲通知
  289. virtual void OnRtnOptionSelfClose(CThostFtdcOptionSelfCloseField *pOptionSelfClose) {};
  290.  
  291. ///期权自对冲录入错误回报
  292. virtual void OnErrRtnOptionSelfCloseInsert(CThostFtdcInputOptionSelfCloseField *pInputOptionSelfClose, CThostFtdcRspInfoField *pRspInfo) {};
  293.  
  294. ///期权自对冲操作错误回报
  295. virtual void OnErrRtnOptionSelfCloseAction(CThostFtdcOptionSelfCloseActionField *pOptionSelfCloseAction, CThostFtdcRspInfoField *pRspInfo) {};
  296.  
  297. ///申请组合通知
  298. virtual void OnRtnCombAction(CThostFtdcCombActionField *pCombAction) {};
  299.  
  300. ///申请组合录入错误回报
  301. virtual void OnErrRtnCombActionInsert(CThostFtdcInputCombActionField *pInputCombAction, CThostFtdcRspInfoField *pRspInfo) {};
  302.  
  303. ///请求查询签约银行响应
  304. virtual void OnRspQryContractBank(CThostFtdcContractBankField *pContractBank, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  305.  
  306. ///请求查询预埋单响应
  307. virtual void OnRspQryParkedOrder(CThostFtdcParkedOrderField *pParkedOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  308.  
  309. ///请求查询预埋撤单响应
  310. virtual void OnRspQryParkedOrderAction(CThostFtdcParkedOrderActionField *pParkedOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  311.  
  312. ///请求查询交易通知响应
  313. virtual void OnRspQryTradingNotice(CThostFtdcTradingNoticeField *pTradingNotice, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  314.  
  315. ///请求查询经纪公司交易参数响应
  316. virtual void OnRspQryBrokerTradingParams(CThostFtdcBrokerTradingParamsField *pBrokerTradingParams, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  317.  
  318. ///请求查询经纪公司交易算法响应
  319. virtual void OnRspQryBrokerTradingAlgos(CThostFtdcBrokerTradingAlgosField *pBrokerTradingAlgos, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  320.  
  321. ///请求查询监控中心用户令牌
  322. virtual void OnRspQueryCFMMCTradingAccountToken(CThostFtdcQueryCFMMCTradingAccountTokenField *pQueryCFMMCTradingAccountToken, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  323.  
  324. ///银行发起银行资金转期货通知
  325. virtual void OnRtnFromBankToFutureByBank(CThostFtdcRspTransferField *pRspTransfer) {};
  326.  
  327. ///银行发起期货资金转银行通知
  328. virtual void OnRtnFromFutureToBankByBank(CThostFtdcRspTransferField *pRspTransfer) {};
  329.  
  330. ///银行发起冲正银行转期货通知
  331. virtual void OnRtnRepealFromBankToFutureByBank(CThostFtdcRspRepealField *pRspRepeal) {};
  332.  
  333. ///银行发起冲正期货转银行通知
  334. virtual void OnRtnRepealFromFutureToBankByBank(CThostFtdcRspRepealField *pRspRepeal) {}; ///期货发起银行资金转期货通知
  335. virtual void OnRtnFromBankToFutureByFuture(CThostFtdcRspTransferField *pRspTransfer) {};
  336.  
  337. ///期货发起期货资金转银行通知
  338. virtual void OnRtnFromFutureToBankByFuture(CThostFtdcRspTransferField *pRspTransfer) {};
  339.  
  340. ///系统运行时期货端手工发起冲正银行转期货请求,银行处理完毕后报盘发回的通知
  341. virtual void OnRtnRepealFromBankToFutureByFutureManual(CThostFtdcRspRepealField *pRspRepeal) {};
  342.  
  343. ///系统运行时期货端手工发起冲正期货转银行请求,银行处理完毕后报盘发回的通知
  344. virtual void OnRtnRepealFromFutureToBankByFutureManual(CThostFtdcRspRepealField *pRspRepeal) {};
  345.  
  346. ///期货发起查询银行余额通知
  347. virtual void OnRtnQueryBankBalanceByFuture(CThostFtdcNotifyQueryAccountField *pNotifyQueryAccount) {};
  348.  
  349. ///期货发起银行资金转期货错误回报
  350. virtual void OnErrRtnBankToFutureByFuture(CThostFtdcReqTransferField *pReqTransfer, CThostFtdcRspInfoField *pRspInfo) {};
  351.  
  352. ///期货发起期货资金转银行错误回报
  353. virtual void OnErrRtnFutureToBankByFuture(CThostFtdcReqTransferField *pReqTransfer, CThostFtdcRspInfoField *pRspInfo) {};
  354.  
  355. ///系统运行时期货端手工发起冲正银行转期货错误回报
  356. virtual void OnErrRtnRepealBankToFutureByFutureManual(CThostFtdcReqRepealField *pReqRepeal, CThostFtdcRspInfoField *pRspInfo) {};
  357.  
  358. ///系统运行时期货端手工发起冲正期货转银行错误回报
  359. virtual void OnErrRtnRepealFutureToBankByFutureManual(CThostFtdcReqRepealField *pReqRepeal, CThostFtdcRspInfoField *pRspInfo) {};
  360.  
  361. ///期货发起查询银行余额错误回报
  362. virtual void OnErrRtnQueryBankBalanceByFuture(CThostFtdcReqQueryAccountField *pReqQueryAccount, CThostFtdcRspInfoField *pRspInfo) {};
  363.  
  364. ///期货发起冲正银行转期货请求,银行处理完毕后报盘发回的通知
  365. virtual void OnRtnRepealFromBankToFutureByFuture(CThostFtdcRspRepealField *pRspRepeal) {};
  366.  
  367. ///期货发起冲正期货转银行请求,银行处理完毕后报盘发回的通知
  368. virtual void OnRtnRepealFromFutureToBankByFuture(CThostFtdcRspRepealField *pRspRepeal) {};
  369.  
  370. ///期货发起银行资金转期货应答
  371. virtual void OnRspFromBankToFutureByFuture(CThostFtdcReqTransferField *pReqTransfer, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  372.  
  373. ///期货发起期货资金转银行应答
  374. virtual void OnRspFromFutureToBankByFuture(CThostFtdcReqTransferField *pReqTransfer, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  375.  
  376. ///期货发起查询银行余额应答
  377. virtual void OnRspQueryBankAccountMoneyByFuture(CThostFtdcReqQueryAccountField *pReqQueryAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {};
  378.  
  379. ///银行发起银期开户通知
  380. virtual void OnRtnOpenAccountByBank(CThostFtdcOpenAccountField *pOpenAccount) {};
  381.  
  382. ///银行发起银期销户通知
  383. virtual void OnRtnCancelAccountByBank(CThostFtdcCancelAccountField *pCancelAccount) {};
  384.  
  385. ///银行发起变更银行账号通知
  386. virtual void OnRtnChangeAccountByBank(CThostFtdcChangeAccountField *pChangeAccount) {};
  387. */
  388. public:
  389.  
  390. //保证唯一
  391. int m_nRequestId;
  392.  
  393. int GetRequestId() {return ++m_nRequestId; }
  394. MyCTPTrade *m_Parent;
  395. };
  396.  
  397. //thread
  398. public:
  399. MyCTPTrade();
  400. ~MyCTPTrade();
  401.  
  402. static MyCTPTrade *Instance();
  403.  
  404. void setMainWidget(void *widget);
  405. void Init(const char *User, const char *pswd, const char *broker, const char *pszAddress);
  406. map<string, CThostFtdcInstrumentField>& GetMapInstrument();
  407.  
  408. void setLog(const string& str);
  409.  
  410. bool m_bLoginSuccessed;
  411. public:
  412. ///////////////////////////////////////请求类函数,提供主要逻辑,供外部使用////////////////////////////////////////////////////////////////////////////
  413. ///用户登录请求
  414. int ReqUserLogin(CThostFtdcReqUserLoginField *pReqUserLoginField, int nRequestID)
  415. {
  416. return m_reqApi->ReqUserLogin(pReqUserLoginField, nRequestID );
  417. }
  418.  
  419. ///登出请求
  420. int ReqUserLogout(CThostFtdcUserLogoutField *pUserLogout, int nRequestID) { return m_reqApi->ReqUserLogout(pUserLogout, nRequestID); }
  421.  
  422. public:
  423. ///////////////////////////////////////回报类函数,收到数据,更新本地行情信息////////////////////////////////////////////////////////////////////////////
  424.  
  425. ///当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。
  426. void OnFrontConnected();
  427.  
  428. ///当客户端与交易后台通信连接断开时,该方法被调用。当发生这个情况后,API会自动重新连接,客户端可不做处理。
  429. ///@param nReason 错误原因
  430. /// 0x1001 网络读失败
  431. /// 0x1002 网络写失败
  432. /// 0x2001 接收心跳超时
  433. /// 0x2002 发送心跳失败
  434. /// 0x2003 收到错误报文
  435. void OnFrontDisconnected(int nReason);
  436.  
  437. ///心跳超时警告。当长时间未收到报文时,该方法被调用。
  438. ///@param nTimeLapse 距离上次接收报文的时间
  439. void OnHeartBeatWarning(int nTimeLapse);
  440.  
  441. void OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  442.  
  443. ///登录请求响应
  444. void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  445.  
  446. ///登出请求响应
  447. void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  448.  
  449. void OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  450.  
  451. void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
  452.  
  453. public:
  454.  
  455. atomic
  456. <bool>
  457. m_bQryIns;// 是否正在查询合约,如果正在查询,行情服务就不自己主动去订阅行情,因为查询合约完成后,会有订阅行情的动作
  458. atomic
  459. <bool>
  460. m_bQryInsOK;// 是否查询合约成功,成功订阅行情的动作
  461.  
  462. private:
  463. CThostFtdcTraderApi* m_reqApi;
  464. MyRecv *m_RecvSpi;
  465. //vector
  466. <string /*InstrumentID*/>
  467. m_vecInstrumentId;//合约ID
  468.  
  469. //int requestID;
  470. string m_TradingDay;
  471.  
  472. int m_session ;
  473. int m_frontId ;
  474. string m_serverOrderRef ;
  475.  
  476. int m_nOrderRef;
  477.  
  478. ///用户登录信息
  479. CThostFtdcReqUserLoginField *m_userLoginInfo;
  480.  
  481. mutex m_getInstru;
  482. void *mainHandle;
  483. // 保存合约
  484. map<string, CThostFtdcInstrumentField> m_mapInstrument;//合约ID,合约信息
  485. map<wstring, map<wstring, map<wstring, CThostFtdcInstrumentField>>> m_mapInstrument2;//交易所,板块,合约信息
  486.  
  487. CConfigImpl* m_pConfig;
  488. };

①模块管理:

与CTP提供的行情库类似,这里定义了API和封装的回报API,如下:

  1. CThostFtdcTraderApi* m_reqApi;
  2. MyRecv *m_RecvSpi;

②与UI通信

由于交易API用的比较频繁,而回报主要在初始化阶段和数据更新,没有定义回报接口,定义的是全局单例模式,直接共上层使用。

③数据管理

  1. // 保存合约
  2. map<string, CThostFtdcInstrumentField> m_mapInstrument;//合约ID,合约信息
  3. map<wstring, map<wstring, map<wstring, CThostFtdcInstrumentField>>> m_mapInstrument2;//交易所,板块,合约信息

3.程序注意点

①UI初始化:

调用TradeManager::InitCTP启动CTP模块,主模块会订阅注册:

  1. MyCTPQuote::Instance()->Subscribe(this);

②交易模块与行情模块通信

当交易模块登陆成功后(通能说原子操作,来判断多条件满足),来调用行情的完成函数,已便行情模块来进行行情合约订阅

  1. int Qry = atomic_exchange(&MyCTPTrade::Instance()->m_bQryIns, false);
  2. int QryOK = atomic_exchange(&MyCTPTrade::Instance()->m_bQryInsOK, true);
  3.  
  4. if(!Qry && QryOK)
  5. FinishQryInstrument();

③行情回报

收到行情回报,会调用下函数:

  1. void MyCTPQuote::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData)

并组织结构,向订阅派发:

  1. CBroadcastPacket pack;
  2. ///深度行情通知
  3. ///每秒两次行情
  4. void MyCTPQuote::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData)
  5. {
  6. if (pDepthMarketData->OpenPrice >= LDBL_MAX)
  7. pDepthMarketData->OpenPrice = 0;
  8.  
  9. if (pDepthMarketData->HighestPrice >= LDBL_MAX)
  10. pDepthMarketData->HighestPrice = 0;
  11.  
  12. if (pDepthMarketData->LowestPrice >= LDBL_MAX)
  13. pDepthMarketData->LowestPrice = 0;
  14.  
  15. if (pDepthMarketData->ClosePrice >= LDBL_MAX)
  16. pDepthMarketData->ClosePrice = 0;
  17.  
  18. if (pDepthMarketData->PreClosePrice >= LDBL_MAX)
  19. pDepthMarketData->PreClosePrice = 0;
  20.  
  21. string str = "Quote SDK 接收行情信息:InstrumentID:";
  22. str += pDepthMarketData->InstrumentID ;
  23. str += " TradingDay:"; str += pDepthMarketData->TradingDay;
  24. str += " LastPrice:"; str += to_string(pDepthMarketData->LastPrice);
  25. str += " Volume:"; str += to_string(pDepthMarketData->Volume);
  26. str += " OpenPrice:"; str += to_string(pDepthMarketData->OpenPrice);
  27. str += " HighestPrice:"; str += to_string(pDepthMarketData->HighestPrice);
  28. str += " LowestPrice:"; str += to_string(pDepthMarketData->LowestPrice);
  29. str += " ClosePrice:"; str += to_string(pDepthMarketData->ClosePrice);
  30. str += " PreClosePrice:"; str += to_string(pDepthMarketData->PreClosePrice);
  31. str += " UpdateTime:"; str += pDepthMarketData->UpdateTime;
  32. CRLog(E_APPINFO, str.c_str());
  33. //本地保存
  34. //转发到界面
  35. //CThostFtdcDepthMarketDataField DepthMarketData;
  36. //memcpy_s(&DepthMarketData, sizeof(DepthMarketData) ,pDepthMarketData , sizeof(CThostFtdcDepthMarketDataField));
  37. //m_mapInstrumentQuote[pDepthMarketData->InstrumentID] = DepthMarketData;
  38. m_mapInstrumentQuote[pDepthMarketData->InstrumentID] = *pDepthMarketData;
  39.  
  40. pack.SetParameter("InstrumentId", pDepthMarketData->InstrumentID);
  41. pack.SetParameter("TradingDay", pDepthMarketData->TradingDay);
  42. pack.SetParameter("OpenPrice", pDepthMarketData->OpenPrice);
  43. pack.SetParameter("HighestPrice", pDepthMarketData->HighestPrice);
  44. pack.SetParameter("LowestPrice", pDepthMarketData->LowestPrice);
  45. pack.SetParameter("ClosePrice", pDepthMarketData->ClosePrice);
  46. pack.SetParameter("PreClosePrice", pDepthMarketData->PreClosePrice);
  47. pack.SetParameter("UpperLimitPrice", pDepthMarketData->UpperLimitPrice);
  48. pack.SetParameter("LowerLimitPrice", pDepthMarketData->LowerLimitPrice);
  49. pack.SetParameter("AskPrice1", pDepthMarketData->AskPrice1);
  50. pack.SetParameter("AskVolume1", pDepthMarketData->AskVolume1);
  51. pack.SetParameter("BidPrice1", pDepthMarketData->BidPrice1);
  52. pack.SetParameter("BidVolume1", pDepthMarketData->BidVolume1);
  53. pack.SetParameter("LastPrice", pDepthMarketData->LastPrice);
  54. pack.SetParameter("Volume", pDepthMarketData->Volume);
  55.  
  56. //给上层转发
  57. // 广播报文
  58. for (VPKTRECEIVER::iterator it = m_vPketReceiver.begin(); it < m_vPketReceiver.end(); it++)
  59. {
  60. (*it)->Receive(pack);
  61. }
  62.  
  63. }

④界面处理回报数据

收到数据可以按照自己的逻辑处理,本Demo进行了写共享队列。

  1. QUOTATION quo = { 0 };
  2. void TradeManager::Receive(CBroadcastPacket &pkt)
  3. {
  4. unsigned int nLen = 0;
  5. const char* pcBuf = pkt.Encode(nLen);
  6.  
  7. //分发给相应队列处理
  8. quo.Decode(pcBuf, nLen);
  9.  
  10. int uiCount = 0;
  11. string sXQueName, sTmp;
  12. for (vector< CXQueueIo<QUOTATION>* >::iterator it = m_vecQueueIo.begin(); it != m_vecQueueIo.end(); ++it)
  13. {
  14. uiCount++;
  15. if (0 != *it)
  16. {
  17. (*it)->Enque(quo);
  18.  
  19. sXQueName = "XQUE" + strutils::ToString<unsigned int>(uiCount);
  20. sXQueName += ".XQUE_NAME";
  21.  
  22. if (0 == m_pConfig->GetProperty(sXQueName, sTmp))
  23. sXQueName = sTmp;
  24.  
  25. CRLog(E_APPINFO, "共享队列XQueue[%s]写完成", sXQueName.c_str());
  26. }
  27.  
  28. }//end for
  29.  
  30. }

4.注意

内嵌Framwork所生成的动态库为本Demo提供日志和共享内存队列以及行情数据定义,可以自行定义替换,不存在bug,不影响演示,代码由于版权问题,暂不能公开,若有需要,请联系作者。

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权

上海期货交易所CTP行情和交易接入的更多相关文章

  1. 火币网API文档——REST 行情、交易API简介

    REST API 简介 火币为用户提供了一套全新的API,可以帮用户快速接入火币PRO站及HADAX站的交易系统,实现程序化交易. 访问地址 适用站点 适用功能 适用交易对 https://api.h ...

  2. 高仿MT4行情终端(K线图+操控+简单架构)

    技术:VS2015 Update3 + QT 5.11.2 + BOOST 1.68 + QT VS Tools + C++11   概述 模仿外汇MT4的界面 详细 代码下载:http://www. ...

  3. CTP API开发期货自动交易平台概论

    题目比较小众,先介绍一下CTP. 综合交易平台CTP(Comprehensive Transaction Platform)是由上海期货信息技术有限公司(上海期货交易所的全资子公司)开发的期货交易平台 ...

  4. CTP API开发之一:CTP API简介

    官网下载CTP API 综合交易平台CTP(Comprehensive Transaction Platform)是由上海期货信息技术有限公司(上海期货交易所的全资子公司)开发的期货交易平台,CTP平 ...

  5. 量化交易之下单函数和context对象

    一.下单函数 聚宽设计的函数(如前文所说准确叫法是API)的用法都写在API文档里,位置在聚宽网站导航栏-帮助-API文档 1.order按股数下单 order(security, amount, s ...

  6. canvas绘图,html5 k线图,股票行情图

    canvas绘图,html5 k线图,股票行情图 canvas跟其他标签一样,也可以通过css来定义样式.但这里需要注意的是:canvas的默认宽高为300px * 150px,在css中为canva ...

  7. 吐血整理,一文读懂中国金融衍生品市场的前世今生和未来[z]

    [z]https://www.gfedu.cn/cfrm/content_22687.shtml 从定义上来说,金融衍生工具(derivativesecurity)是在货币.债券.股票等传统金融工具的 ...

  8. R语言解读多元线性回归模型

    转载:http://blog.fens.me/r-multi-linear-regression/ 前言 本文接上一篇R语言解读一元线性回归模型.在许多生活和工作的实际问题中,影响因变量的因素可能不止 ...

  9. VNPY 文件目录结构

    VNPY ├─examples 可以运行起来的实际程序│ ├─CoinapiDataService│ ├─CryptoTrader│ ├─CtaBacktesting│ ├─CtaTrading│ ├ ...

随机推荐

  1. JavaScript:Date 对象

    ylbtech-JavaScript:Date 对象 1.返回顶部 Date 对象 Date 对象用于处理日期和时间. 创建 Date 对象的语法: var myDate=new Date() 注释: ...

  2. ios开发第三方库--cocoapods安装

    1. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&quo ...

  3. VS2010新建Web网站与新建Web应用程序的区别 (转)

    在Visual Studio 2010中,除了可以使用“创建Web应用程序”的方式来构建自己的Web项目之外,还可以通过创建“Web网站”的方式来构建Web项其中,Web网站的创建方法:打开Visua ...

  4. sgd学习率选择问题

    关于使用SGD时如何选择初始的学习率(这里SGD是指带动量的SGD,momentum=0.9): 训练一个epoch,把学习率从一个较小的值(10-8)上升到一个较大的值(10),画出学习率(取log ...

  5. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十一)定制一个arvo格式文件发送到kafka的topic,通过Structured Streaming读取kafka的数据

    将arvo格式数据发送到kafka的topic 第一步:定制avro schema: { "type": "record", "name": ...

  6. (转)Pixel-Fillrate显卡像素填充率

    [Pixel-Fillrate] “填充率“以每秒钟填充的像素点为单位,“三角形(多边形)生成速度“则表示每秒钟三角形(多边形)生成个数.现在的3D显卡的性能也主要看着两项指标,这两项指标的数值越大, ...

  7. 启动项目时出现Not a JAR.......Find JAR........一指循环就是起不来

    出现问题原因就是mapper的映射文件有问题,里面的返回类型如是实体找不到或者找重复的就会这样 解决办法就是:确保在用的实体(路径)能找到,切记不能有重名的实体

  8. 命令行下的html转pdf工具wkhtmltopdf

    基于webkit和qt的html转pdf的命令行工具,非常好使 http://code.google.com/p/wkhtmltopdf/ http://www.cnblogs.com/shanyou ...

  9. mysql zerofill 的使用

    转自:http://www.jquerycn.cn/blog/mysql/ 那这个int[M]中M是什么意义喃,在定义数值型数据类型的时候,可以在关键字括号内指定整数值(如:int(M),M的最大值为 ...

  10. Firebug Console Panel 控制台详解【转载+整理】

    本文内容 信息显示 信息显示的占位符 console.group 和 console.groupEnd 分组显示 console.dir console.dirxml console.assert c ...