C/C++教程

易盛内外通用版交易API转websocket

本文主要是介绍易盛内外通用版交易API转websocket,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、前言

    因为一些工作需要,所以研究了一下易盛交易API,然后想到C++对于很多人来说还是非常不友好的所以就想着说要不写一个C++转websocket(以下简称WS)的接口,用WS接口不就可以跨全部平台来交易了吗(特别是WEB和python)?后来在研究和转写的过程中,发现从技术的角度来说其实很没有必要,所以弄了一半,搞了个框架就懒得搞了。但是折腾了几个月不能浪费了对吧,所以就分享出来给大家一起研究学习了。

    此前已经写了两篇相关的,分别是如何搭建交易API的环境,以及WS server的demo:

    https://www.cnblogs.com/cation/p/15245161.html

    https://www.cnblogs.com/cation/p/15254165.html

二、思路

    1、主线程启动后,启动WS的Server,并一直监听client

    2、client连接后启动子进程

    3、子进程内获取client的消息

    4、通过对消息的识别来实现“登陆、登出、查询、下单”等操作

三、代码

    腾讯微云下载

    https://share.weiyun.com/qrgMVIzX

四、使用

    1、准备

    跟代码一起奉送的是一个WS client。

    另外你还得有个API授权码,不过易盛demo中是带了测试账号和授权码的。

    开发环境自己搞定,然后启动项目。

    2、登陆

    先启动项目,启动后再启动WS client。

    WS clinet连接成功时会报一个“Illegal JSON format”的error,是正常的,因为这个client会有个广告词启动时发给server,属于可识别范围外的字符所以就会报一个error。

 

     填入JSON字符,发送给server后就登陆API了。可以看到登陆成功后会收到一系列的RSP。

 

     登陆成功后还是发JSON字符串,比如下面做个查询。

 

 

五、已支持的指令

========登陆 登出========
启明星登陆
{"command":"login","ip":"XXXXXXX","port":XXXXXXX,"auth":"XXXXXXX","type":"QMX","username":"XXXXXXX","password":"XXXXXXX"}
北斗星登陆
{"command":"login","ip":"XXXXXXX","port":XXXXXXX,"auth":"XXXXXXX","type":"BDX","username":"XXXXXXX","password":"XXXXXXX"}
登出
{"command":"logout"}

========查询指令 qry========
查持仓
{"command":"qry","item":"position","username":"XXXXXXX"}
{"command":"qry","item":"position_sum","username":"XXXXXXX"}
查资金
{"command":"qry","item":"fund","username":"XXXXXXX"}
查委托
{"command":"qry","item":"order","username":"XXXXXXX"}
查成交
{"command":"qry","item":"fill","username":"XXXXXXX"}

========下单指令 order========
最常见:限价、期货、日内、买入、1手
{"command":"order","item":"insert","username":"XXXXXXX","ExchangeNo":"CME","CommodityType":"F","CommodityNo":"ES","ContractNo":"2112","TimeInForce":"GFD","OrderType":"limit","OrderSide":"B","OrderPrice":4300,"OrderQty":1}
{"command":"order","item":"insert","username":"XXXXXXX","ExchangeNo":"CME","CommodityType":"F","CommodityNo":"ES","ContractNo":"2112","TimeInForce":"GTD","ExpireTime":"2021-12-31","OrderType":"limit","OrderSide":"B","OrderPrice":4300,"OrderQty":1}

 

我自己瞎定义的Error code:
-1000 just one client allow
-1001 Illegal JSON forma
-1002 can not find key:command
-1003 login时的各种可能错误
-1004 login repeat
-1005 urecognized command
-1006 trans command error and catch...
-1007 command can not anlysis
-1008 GET指令执行时各种可能错误
-1009 QRY指令执行时各种可能错误
-1010 ORDER指令执行时各种可能错误
-1011 SET指令执行时各种可能错误
-1012 need login 没有登录就执行各种命令
-1013 登陆失败
-1014 API未准备好
-1015 rspMsg2Client(sClient, "error", "-1015", "InsertOrder Error iErr="+ iErr); 下单失败
success code:
1000 rspMsg2Client(sockClient, "rsp", "1000", "WSServer ready"); server 准备好了
1001 rspMsg2Client(sockClient, "rsp", "1001", "received"); 收到了client的消息 回报
1002 rspMsg2Client(sockClient, "rsp","1002", "login submit"); 提交了登陆信息,回报
1003 rspMsg2Client(sockClient, "rsp","1003","logout submit"); 提交了登出信息 回报
1004 rspMsg2Client(sClient,"rsp", "1004", "OnConnect"); 提交登陆后触发了onConnect
1005 rspMsg2Client(sClient, "rsp", "1005", "OnRspLogin"); 提交登陆后触发了OnRspLogin
1006 rspMsg2Client(sClient, "rsp", "1006", "OnAPIReady"); 提交登陆后触发了OnAPIReady
1007 rspMsg2Client(sClient, "rsp", "1007", "OnDisconnect"); 登陆后触发了OnDisconnect
1008 rspMsg2Client(sClient, "rsp", "1008", "OnRspSubmitUserLoginInfo"); 启明星用
1009 rspMsg2Client(sockClient, "rsp", "1009", "qry submit"); 提交了查询的回报
1010 rspMsg2Client(sClient, "rsp", "1010", charsw); 返回查询的position信息
1011 rspMsg2Client(sClient, "rsp", "1011", charsw); 返回查询的position_sum信息
1012 rspMsg2Client(sClient, "rsp", "1012", charsw); 返回查询的fund信息
1013 rspMsg2Client(sClient, "rsp", "1013", charsw); 返回查询的order信息
1014 rspMsg2Client(sClient, "rsp", "1014", charsw); 返回查询的fill信息
1015 rspMsg2Client(sockClient, "rsp", "1015", "order submit"); 提交了订单的回报
1016 rspMsg2Client(sClient, "rsp", "1016", "order insert done"); 成功下单的回报

1 OnConnect
1 OnAPIReady
1 OnDisconnect
1 OnRspLogin
1 logout_submit

 

六、回顾

    其实API本身并不复杂,甚至还有点简洁,这也是为啥我后来觉得转成WS接口没有意义了,不过折腾了不短的时间,就分享给大家,也算是一个思路了,有感兴趣的可以继续加把劲丰富一下!反正我是没精力了。

 

这篇关于易盛内外通用版交易API转websocket的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!