AI量化知识树

全球股票指数API接口接入方法 |港股股票指数 | 美股股票指数|全球市场

由bqey5d84创建,最终由bqey5d84 被浏览 52 用户

想知道如何利用量化策略接入全球股票指数API接口吗?别担心,我们这篇文章将用通俗易懂的语言,手把手教您如何快速获取全球市场的股票指数最新动态,轻松掌握实时港股、实时美股指数等股票指数相关的实时数据。全球股票指数API要求稳定,快速,实时,股票指数API接口还要求产品种类数据繁多。

一、获取API地址以及传参说明

支持以下产品品类:

  • 实时美股、实时美股指数
  • 实时港股、实时港股指数
  • 实时A股、实时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

2.1 到它的官方网站使用邮箱直接注册即可:​​https://alltick.co​

2.2 它的github开源站点:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api

三、请查看code产品列表

请到github或者官网上面查看并选择

四、开始真正的订阅实时行情数据源报价

一旦您确定了所需的产品代码列表,就可以立即开始订阅实时行情数据源的报价了!现在让我们来看一下下面的实例代码,它将向您展示如何实际进行订阅操作。通过这个实例,您将亲身体验到如何使用代码订阅接口,以获取最新的实时行情数据

import json
import websocket    # pip install websocket-client
 
'''
# 特别注意:
# 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
'''
 
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",
            }
        ],
    }
}

\