今天我想分享一个非常实用的技术内容,即如何通过接口API来实现订阅并接入实时行情数据源的报价信息。这个技术可以帮助你获取最新的市场数据,为你的应用程序或交易策略提供及时的信息支持。接入实时行情数据源可以让你了解市场动态并快速作出决策,非常有助于优化你的交易策略和投资决策。希望这个干货分享对你有所帮助,让我们一起来探讨如何实现吧!
一、API地址及传参说明
支持以下产品品类: 美股、港股、A股、外汇、贵金属、商品、数字币
github: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
token申请:https://alltick.co
把下面url中的testtoken替换为您自己的token
外汇,数字币,贵金属的api址:
wss://quote.tradeswitcher.com/quote-b-ws-api
港美股api地址:
wss://quote.tradeswitcher.com/quote-stock-b-ws-api
建立连接:
wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=testtoken
建立连接之后,就可以订阅具体的接口数据了,具体调用方式,请看下文介绍
二、免费申请Token
到它的官方网站使用邮箱直接注册即可: https://alltick.co
顺便说一下它的github开源站点:https://github.com/alltick/free-quote
三、请查看code产品列表
请到github或者官网上面查看并选择
四、开始真正的订阅实时行情数据源报价
当你选择好产品code列表后就可以开始真正的订阅实时行情数据源报价了。请看下面的实例代码:
import json
import websocket # pip install websocket-client
'''
# 特别注意:
# github: https://github.com/alltick/free-quote
# token申请:https://alltick.co
# 把下面url中的testtoken替换为您自己的token
# 外汇,数字币,贵金属的api址:
# wss://quote.tradeswitcher.com/quote-b-ws-api
# 港美股api地址:
# wss://quote.tradeswitcher.com/quote-stock-b-ws-api
'''
class Feed(object):
def __init__(self):
self.url = 'wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=testtoken' # 这里输入websocket的url
self.ws = None
def on_open(self, ws):
"""
Callback object which is called at opening websocket.
1 argument:
@ ws: the WebSocketApp object
"""
print('A new WebSocketApp is opened!')
# 开始订阅(举个例子)
sub_param = {
"cmd_id": 22002,
"seq_id": 123,
"trace":"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806",
"data":{
"symbol_list":[
{
"code": "700.HK",
"depth_level": 5,
},
{
"code": "UNH.US",
"depth_level": 5,
}
]
}
}
#如果希望长时间运行,除了需要发送订阅之外,还需要修改代码,定时发送心跳,避免连接断开,具体查看接口文档
sub_str = json.dumps(sub_param)
ws.send(sub_str)
print("depth quote are subscribed!")
def on_data(self, ws, string, type, continue_flag):
"""
4 argument.
The 1st argument is this class object.
The 2nd argument is utf-8 string which we get from the server.
The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.
The 4th argument is continue flag. If 0, the data continue
"""
def on_message(self, ws, message):
"""
Callback object which is called when received data.
2 arguments:
@ ws: the WebSocketApp object
@ message: utf-8 data received from the server
"""
# 对收到的message进行解析
result = eval(message)
print(result)
def on_error(self, ws, error):
"""
Callback object which is called when got an error.
2 arguments:
@ ws: the WebSocketApp object
@ error: exception object
"""
print(error)
def on_close(self, ws, close_status_code, close_msg):
"""
Callback object which is called when the connection is closed.
2 arguments:
@ ws: the WebSocketApp object
@ close_status_code
@ close_msg
"""
print('The connection is closed!')
def start(self):
self.ws = websocket.WebSocketApp(
self.url,
on_open=self.on_open,
on_message=self.on_message,
on_data=self.on_data,
on_error=self.on_error,
on_close=self.on_close,
)
self.ws.run_forever()
if __name__ == "__main__":
feed = Feed()
feed.start()
五、解析推送数据
5.1、最新成交报价解析
{
"cmd_id":22998,
"data":{
"code": "1288.HK",
"seq": "1605509068000001",
"tick_time": "1605509068",
"price": "651.12",
"volume": "300",
"turnover": "12345.6",
"trade_direction": 1,
}
}
5.2、最新5档深度数据解析
{
"cmd_id":22999,
"data":{
"code": "1288.HK",
"seq": "1605509068000001",
"tick_time": "1605509068",
"bids": [
{
"pric": "9.12",
"volume": "9.12",
},
{
"pric": "9.12",
"volume": "9.12",
},
{
"pric": "9.12",
"volume": "9.12",
},
{
"pric": "9.12",
"volume": "9.12",
},
{
"pric": "9.12",
"volume": "9.12",
}
],
"asks": [
{
"price": "147.12",
"volume": "147.12",
},
{
"price": "147.12",
"volume": "147.12",
},
{
"price": "147.12",
"volume": "147.12",
},
{
"price": "147.12",
"volume": "147.12",
},
{
"price": "147.12",
"volume": "147.12",
}
],
}
}
标签:Java,Python,quote,self,object,volume,api,ws,数据
From: https://www.cnblogs.com/api-store/p/18097887