干货分享 - 实时行情数据源接入指南WebSocket

今天,我们将为大家分享一个技术干货,如何通过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

要开始使用Alltick API,您需要一个Token。这非常简单,只需访问官方网站并使用您的邮箱注册即可。注册地址是:AllTick API 

此外,如果您对技术细节感兴趣,可以访问Alltick的GitHub开源站点:GitHub 

三、查看产品代码列表

在开始订阅之前,您需要查看并选择您感兴趣的产品代码。这些信息可以在GitHub或官方网站上找到。

四、订阅实时行情数据源报价

当您选择了产品代码列表后,就可以开始订阅实时行情数据源报价了。以下是一段示例代码,供您参考:

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",
            }
        ],
    }
}

通过以上步骤,您可以轻松地接入API,获取实时的市场行情数据。这不仅能够提升您的交易效率,还能帮助您做出更加明智的投资决策。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值