优宽量化平台作为专业的量化交易平台,致力于方便广大用户进行量化交易。平台对CTP原始API函数进行了高度封装,提供了CTP系统全API实现的同时,简化了CTP原始返回函数中的诸多细节。这种封装使得重要信息更加突出,去除了多余的字段和合并多项函数结果,帮助用户更容易地进行交易,对新手用户特别友好。
当然,对于寻求更深入系统调用和获取原始字段信息的高级用户,优宽量化平台也提供了强大的工具。通过exchange.IO
函数,用户能够完全兼容官方的API名称,进行深层次的系统调用,访问CTP系统的更多原生功能。这一功能满足了高级用户对复杂交易策略的需求,提供了灵活性和强大的系统访问能力。
CTP(Comprehensive Transaction Platform)是中国金融期货交易所推出的一套综合交易平台,主要服务于期货公司,同时也适用于基金公司、投资公司等进行期货交易。CTP平台以其高并发处理能力、快速响应和高可靠性而著称,特别适用于程序化交易和短线交易者。CTP提供了丰富的行情和交易接口,允许用户通过编程方式接收实时市场数据和执行交易操作。行情接口(MdApi)主要负责市场数据的接收和处理,而交易接口(TraderApi)则处理订单的提交、撤单以及其他交易相关操作。
CTP行情接口支持订阅市场数据,如合约的实时价格、成交量等信息。用户可以通过API进行市场数据的订阅和取消订阅操作,同时接口也会推送相关的市场行情数据。CTP交易接口则更为复杂,提供了登录、报单、撤单、资金转账、信息查询等功能。开发者需要通过继承并实现CThostFtdcTraderSpi接口来创建自己的交易处理类,并通过CThostFtdcTraderApi类来与CTP服务器进行通信。此外,交易接口还涉及到身份认证、结算单确认等步骤。
CTP系统的设计注重了可用性、并发处理能力、安全性、可扩展性以及业务规则的隔离。它采用了基于TCP协议的FTD协议进行通信,并提供了对话通讯模式、私有通讯模式和广播通讯模式三种通讯方式。
在这个简化的流程图中:
以上展示了CTP系统的主要功能和交易执行的基本流程。当引入量化交易中,我们就需要调取原始CTP的API进行行情数据的获取,交易信息的计算,以及后续交易操作的执行。但是原始API函数返回的结果较为复杂,对于新手用户来说可能难以理解,并且处理起来较为繁琐。
为了简化这一过程,提高用户体验,优宽量化API提供了更加方便和直观的接口。通过优宽量化API,用户可以更加容易地获取交易信息,进行行情数据分析,以及执行交易操作。
我们通过优宽量化平台封装的API来实现量化交易。下面展示几个常用函数的对比及应用实例:
CTP原始API函数 | 优宽量化API函数 | 功能描述 |
---|---|---|
ReqQryInvestorPosition | exchange.GetPosition | 获取当前持仓信息 |
ReqQryOrder | exchange.GetHistoryOrders | 获取当日订单信息 |
ReqQryTrade | exchange.GetHistoryOrders | 获取当日已成交信息 |
ReqQryTradingAccount | exchange.GetAccount | 获取账户资金和盈亏信息 |
可以看到,优宽量化对于ReqQryOrder
和ReqQryTrade
两个函数进行了整合,成为exchange.GetHistoryOrders
,不仅可以获取当日订单价格信息(挂单价格),也可以获取实际成交价格,更加方便量化交易使用。
为了方便对比,这里我们首先使用exchange.IO("api", "ReqQryTrade")
来获取当日成交信息。可以看到返回结果包含了很多的字段,而对于交易来说,我们仅需要成交价格,成交方向等几个关键字段。
function main() {
let items = exchange.IO("api", "ReqQryTrade")
Log(items)
}
# 返回结果如下:
[[{"Name":"CThostFtdcTradeField","Value":{"ParticipantID":"0005","TradeDate":"20240808","ClearingPartID":"0005","ExchangeInstID":"SA409","reserve1":"SA409","ExchangeID":"CZCE","Direction":49,"TraderID":"000501","TradeSource":48,"HedgeFlag":49,"TradeTime":"10:56:00","BusinessUnit":"000501","TradingDay":"20240808","BrokerID":"10010","ClientID":"400609293","reserve2":"SA409","SettlementID":1,"TradeID":" 6853510","OffsetFlag":48,"Volume":1,"SequenceNo":260,"BrokerOrderSeq":539,"OrderLocalID":" 161","InvestUnitID":"","InvestorID":"00609293","OrderRef":"2","UserID":"00609293","TradeType":48,"PriceSource":49,"InstrumentID":"SA409","OrderSysID":" 30178205","TradingRole":49,"Price":1703}},
{"Name":"CThostFtdcTradeField","Value":{"ExchangeInstID":"SA409","ExchangeID":"CZCE","SequenceNo":430,"TradingDay":"20240808","SettlementID":1,"InstrumentID":"SA409","OrderSysID":" 42045470","TradingRole":49,"HedgeFlag":49,"TradeTime":"14:26:51","Volume":1,"OrderLocalID":" 197","OrderRef":"2","UserID":"00609293","ClientID":"400609293","reserve2":"SA409","BusinessUnit":"000501","BrokerOrderSeq":686,"InvestUnitID":"","Direction":49,"OffsetFlag":48,"TraderID":"000501","ClearingPartID":"0005","BrokerID":"10010","TradeType":48,"TradeSource":48,"InvestorID":"00609293","ParticipantID":"0005","Price":1705,"TradeDate":"20240808","reserve1":"SA409","TradeID":" 9956391","PriceSource":49}},
{"Name":"CThostFtdcTradeField","Value":{"OrderSysID":" 39490988","OrderLocalID":" 194","InvestUnitID":"","TradeTime":"14:05:35","ClearingPartID":"0005","ExchangeID":"CZCE","ClientID":"400609293","TradingRole":49,"SequenceNo":424,"TradingDay":"20240808","InstrumentID":"SA409","ParticipantID":"0005","reserve2":"SA409","BusinessUnit":"000501","PriceSource":50,"Direction":48,"OffsetFlag":49,"TradeDate":"20240808","SettlementID":1,"BrokerID":"10010","InvestorID":"00609293","UserID":"00609293","ExchangeInstID":"SA409","OrderRef":"3","HedgeFlag":49,"BrokerOrderSeq":673,"Volume":1,"TradeType":48,"TraderID":"000501","TradeSource":48,"reserve1":"SA409","TradeID":" 9210457","Price":1701}}]]
通过使用优宽量化函数exchange.GetHistoryOrders
,可以获得以下简化后的结果(这里去除了包含原始信息的Info字段):
function main() {
let res = exchange.GetHistoryOrders()
res = res.map(item => {
return Object.assign({}, item, { Info: undefined });
});
Log(res)
}
# 返回结果
[{"Id":"5:485439450:CZCE:2","Price":1693,"Amount":1,"DealAmount":1,"AvgPrice":1703,"Status":1,"Type":1,"Offset":0,"Symbol":"SA409","ContractType":"SA409"},
{"Id":"5:1691702221:CZCE:2","Price":1694,"Amount":1,"DealAmount":1,"AvgPrice":1705,"Status":1,"Type":1,"Offset":0,"Symbol":"SA409","ContractType":"SA409"},
{"Id":"5:485439450:CZCE:3","Price":1702,"Amount":1,"DealAmount":1,"AvgPrice":1701,"Status":1,"Type":0,"Offset":1,"Symbol":"SA409","ContractType":"SA409"}]
通过对比,可以看到优宽量化平台对信息进行了提取和处理,方便量化交易的进行。
参考链接