下面的例子为私有模式的常用接口:
在私有模式里并没有连接API与CTP服务器的行情函数。
当CTP服务器发出私有数据流时, CThostFtdcTradeSpi的callback函数将被激活。
所有callback函数的第一个参数都是CTP服务器返回的内容。
OnErrRtnCThostFtdcTradeSpi 函数的第二个参数是报错时详细的错误信息。
3.3 广播模式的编程接口
使用广播模式时,客户端可以用以下两种方式与CTP服务器进行通讯:
callback函数OnRtnInstrumentStatus 用于通知客户端设备状态的变化。 callback函数OnRtnDepthMarketData用于公布最新的交易所行情数据。
第四章 运作模式
4.1 工作线程
CTP客户端进程需要两种线程,一是应用程序线程,另一种是交易API工作线程。如果客户端想要接收行情数据,那么也需要行情API工作线程。API工作线程连接了客户端和CTP服务器。
交易API和行情API是安全线程,客户端的应用程序可以同时使用两种或多种的工作线程,而无须担心线程冲突。客户单的应用程序要能尽快的处理callback信息,这样才能避免未处理的callback信息堵塞工作线程。要避免通讯堵塞,客户端的应用程序需要使用缓冲层来储存从CTP接收的数据,当然缓冲层也可以用来保护客户端自有数据,以便使之与CTP API的数据区分开。
4.2 文件
CTP API 的动态链接数据库会产生一些文件来储存运行过程中的数据,这些文件的后缀名为“.con”。交易客户端的应用程序通过CreateFtdcTraderApi() 或CreateFtdcMdApi() 函数的第一个参数来判断这些文件的本地路径。
4.3 商业术语与接口函数对比
第五章 CTP API 特别说明
5.1 一般规则
客户端应用程序需要经过两步才能连接到CTP服务器:初始化与功能启用。 使用交易API,客户端交易应用程序需要编写: (1)创建一个“CThostFtdcTraderApi”实例。
(2)创建一个处理来自“CThostFtdcTraderSpi”接口的事件处理器,然后使用“CThostFtdcTraderApi”的“RegisterSpi”函数记录下这些事件。
(3)使用“CThostFtdcTraderApi”的“SubscribePrivateTopic”函数处理私有数据流。
(4)使用“CThostFtdcTraderApi”的“SubscribePublicTopic” 函数处理公共数据流。
(5)使用“CThostFtdcTraderApi”的“RegisterFront” 函数记录CTP服务器的前端地址。客户端多运行几次这种函数,以便与服务器建立更可靠的联系。强烈建议。
(6)使用“CThostFtdcTraderApi”的“Init“函数来连接CTP服务器。 (7)服务器连上之后,“CThostFtdcTraderSpi”接口的callback函数“OnFrontConnected”将被激活。函数运行过程中,客户端的应用程序需要使用“CThostFtdcTraderApi”的“ReqUserLogin” 函数来提交“login”请求。
(8)当CTP服务器确认登陆成功后,“CThostFtdcTraderSpi”接口的callback 函数“OnRspUserLogin” 将被激活。
(9)这样,客户端与CTP服务器的通讯就建立起来了。客户端交易应用程序可以使用其他CTP API来与CTP服务器进行通讯。
如果客户端应用程序需要使用行情API,客户端需要采取以下步骤来描述先前的数据流,不包括私有数据流和公共信息流。具体如下:
(1)所有request函数的参数都不能为空值;
(2)如果函数返回的数值为“int”,数值0意味着函数运行良好,其他数值则表示返回错误,具体的错误信息在“error.xml”文件里面。
5.2 CThostFtdcTraderSpi
CTP用CThostFtdcTraderSpi表示事件接口。客户端应用程序可以通过CThostFtdcTraderSpi 函数获得CTP服务器发出的通知。
5.2.1 OnFrontConnected
当客户端与CTP服务器连接上后,此函数被激活,进而,客户端可以使用“ReqUserLogin”来发送登陆请求。
定义:void OnFrontConnected();
5.2.2 OnFrontDisconnected
当连接终止或中断时,此函数被激活。如果该信息没有被处理,那么API会使用登陆地址列表上的一个前端地址,自动重新连接CTP服务器。
定义:void OnFrontDisconnected (int nReason); 参数:
nReason:the reason of disconnecion 0x1001 network reading failed 0x1002 network writing failed
0x2001 heartbeat receiving timeout 0x2002 heartbeat sending timeout 0x2003 received an error message
5.2.3 OnHeartBeatWarning
此函数用来表示与服务器长时间连接的接口是否可用。 定义:void OnHeartBeatWarning(int nTimeLapse);
参数: nTimeLapse:Length of time elapsed since the last received message
5.2.4 OnRspUserLogin
CTP服务器使用次callback 函数OnRspUserLogin 来通知客户端登陆函数OnRspUserLogin是否被服务器接受。
定义:void OnRspUserLogin(
CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
参数:
pRspUserLogin:The pointer of the structure for user’s login response. The following is definition of the structure: struct CThostFtdcRspUserLoginField {
///trading day
TThostFtdcDateType TradingDay; ///time of login
TThostFtdcTimeType LoginTime; ///broker id
TThostFtdcBrokerIDType BrokerID; ///user id
TThostFtdcUserIDType UserID; ///trade system name
TThostFtdcSystemNameType SystemName; };
pRspInfo:Pointer of the structure for system response. The following is definition of the structure:
struct CThostFtdcRspInfoField {
///error id
TThostFtdcErrorIDType ErrorID; ///error information
TThostFtdcErrorMsgType ErrorMsg; };
5.2.5 OnRspUserLogout
CTP服务器利用此callback函数来通知客户端“RspUserLogout”是否运行成功。
定义:void OnRspUserLogout(
CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
参数:
pRspUserLogout:Pointerof the structure for user’s logout response. The following is definition of the structure: struct CThostFtdcUserLogoutField {
///broker id
TThostFtdcBrokerIDType BrokerID; ///user id
TThostFtdcUserIDType UserID; };
5.2.6 OnRspUserPasswordUpdate
CTP服务器使用此callback函数来通知客户端函数“RspUserPasswordUpdate”是否运行成功。
定义:void OnRspUserPasswordUpdate(
CThostFtdcUserPasswordUpdateField *pUserPasswordUpdate,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
参数:
pUserPasswordUpdate:Pointer of the structure for the response of user’s password modification. The following is definition of the structure: struct CThostFtdcUserPasswordUpdateField {
///broker id
TThostFtdcBrokerIDType BrokerID; ///user id
TThostFtdcUserIDType UserID; ///old password
TThostFtdcPasswordTypeOldPassword; ///new password
TThostFtdcPasswordTypeNewPassword; };
5.2.7 OnRspTradingAccountPasswordUpdate
CTP服务器使用此callback函数来通知客户端是否函数“RspTradingAccountPasswordUpdate”运行成功。
定义:void OnRspTradingAccountPasswordUpdate( CThostFtdcTradingAccountPasswordUpdateField *pTradingAccountPasswordUpdate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);