AllTick作为一家专业的金融市场数据服务商,致力于为全球交易平台、量化团队以及金融科技企业提供稳定、精准的实时行情数据。AllTick的实时行情API支持多种数据接入方式,包括HTTP和WebSocket,满足用户在不同场景下对数据访问的需求。无论是构建交易策略、分析市场动态,还是开发高频交易系统,AllTick都能为您提供可靠的技术支持和灵活的接入方案。这篇文章将详细介绍如何接入AllTick的行情API。我们提供免费试用账号,直接注册即可开启试用,欢迎测试。
1. 行情测试
在注册账号之前,你可以先在网站测试我们的接口。点击上面的图片,可以直达测试的页面。只需要在产品代码输入你想查询的股票代码,点击请求就会返回实时数据。比如上图我们请求了中国平安(601318.SH)的K线,下面就是返回的价格数据。除了A股,你也可以请求港股和美股的价格,你可以尝试输入700.HK(港股腾讯控股),或者TSLA.US(美股特斯拉),在这份谷歌表格你可以查询到所有我们支持的品种。
2. 账号注册
点击下面图片可以直接直达注册页面:
只需要输入电子邮箱和密码就能完成注册,不需要验证手机号
3. 获取Token
注册完自动跳转到管理后台,可以看到专属的API秘钥。到此已经完成了注册。
4. 接入行情
我们提供了详细的接入文档,可以先了解一下具体的接入步骤:
下面是接入AllTick API的示例:
HTTP:
import time import requests import json # Extra headers test_headers = { 'Content-Type': 'application/json' } ''' # Replace "testtoken" in the URL below with your own token ''' # K-line data request for Tencent Holdings (700.HK) test_url1 = 'https://quote.tradeswitcher.com/quote-stock-b-api/kline?token=testtoken&query=%7B%22trace%22%20%3A%20%22python_http_test1%22%2C%22data%22%20%3A%20%7B%22code%22%20%3A%20%22700.HK%22%2C%22kline_type%22%20%3A%201%2C%22kline_timestamp_end%22%20%3A%200%2C%22query_kline_num%22%20%3A%202%2C%22adjust_type%22%3A%200%7D%7D' # Depth tick data request for Tencent Holdings (700.HK) test_url2 = 'https://quote.tradeswitcher.com/quote-stock-b-api/depth-tick?token=testtoken&query=%7B%22trace%22%20%3A%20%22python_http_test2%22%2C%22data%22%20%3A%20%7B%22symbol_list%22%3A%20%5B%7B%22code%22%3A%20%22700.HK%22%7D%5D%7D%7D' # Trade tick data request for Tencent Holdings (700.HK) test_url3 = 'https://quote.tradeswitcher.com/quote-stock-b-api/trade-tick?token=testtoken&query=%7B%22trace%22%20%3A%20%22python_http_test3%22%2C%22data%22%20%3A%20%7B%22symbol_list%22%3A%20%5B%7B%22code%22%3A%20%22700.HK%22%7D%5D%7D%7D' # Send requests and print responses resp1 = requests.get(url=test_url1, headers=test_headers) time.sleep(1) resp2 = requests.get(url=test_url2, headers=test_headers) time.sleep(1) resp3 = requests.get(url=test_url3, headers=test_headers) # Decoded text returned by the request text1 = resp1.text print(text1) text2 = resp2.text print(text2) text3 = resp3.text print(text3)
WebSocket:
import json import websocket # pip install websocket-client ''' # Replace "testtoken" in the URL below with your own token ''' class Feed(object): def __init__(self): self.url = 'wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=testtoken' # Enter your websocket URL here 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!') # Start subscribing (modified for 700.HK) sub_param = { "cmd_id": 22002, "seq_id": 123, "trace": "3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806", "data": { "symbol_list": [ { "code": "700.HK", "depth_level": 5, } ] } } # If you want to run for a long time, you need to modify the code to send heartbeats periodically to avoid disconnection, please refer to the API documentation for details sub_str = json.dumps(sub_param) ws.send(sub_str) print("Depth quote for 700.HK is subscribed!") def on_data(self, ws, string, type, continue_flag): """ 4 arguments. 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 """ # Parse the received 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. 联系客服
如果在接入的过程中有任何问题,或者对我们产品有任何疑问,可以随时与我们客服团队联系: