在连续了将近一年的颓势之后,近期,A股市场呈现出强劲的上升势头。随着一系列利好政策的出台,沪深两市交易活跃度显著提升,成交量重回万亿。虽然十一假期开市之后高开低走,但上周六 (10月12日) 的新闻发布会上,财政部进一步推出一系列增量财政措施,虽然聚焦点还是在化债上,但很多投资者认为这将极大地扭转经济的整体基本面,长期来说是个利好。
在这样的背景下,许多做量化交易的投资者重回战场,加码对A股的投入。AllTick作为一家专注于做行情数据的公司,来给大家安利一波稳定好用的A股实时行情接口。
什么是API?
API,全称为应用程序编程接口 (Application Programming Interface),一般就叫它“接口”。它是一种允许不同软件系统之间进行通信的工具。它充当了一个桥梁,使得不同应用程序可以通过标准化的请求和响应来交换数据。比如,当您在手机上查天气时,应用程序便会通过API请求天气服务提供商的服务器获取实时天气数据,然后显示在您的屏幕上。
在金融市场中,API的作用尤为重要。它不仅能提供股票、外汇、期货等金融数据,还能快速、稳定地获取实时行情信息,让交易者能够及时跟踪市场变化。对于需要快速作出交易决策的量化交易者和高频交易员来说,这种数据获取方式尤为高效。通过集成API,用户可以获得实时数据更新,无需手动刷新,极大地提高了操作效率。
而AllTick提供的A股实时行情接口,主要是为用户提供沪深两市的实时报价、历史成交数据、分时数据、个股K线图等,十分方便。
使用教程
首先在我们官网注册账号,点击屏幕右上角的注册,也可以直接点这里。
输入邮箱和密码即可完成注册,也可以通过你的谷歌账户登录。注册完全免费,也不需要提供任何个人隐私信息。
注册完成后,账号自动获得免费试用。你可以在管理后台查看到你的专属秘钥。
接下来就可以开始你的测试了。我们提供了详细的对接文档,对于有基础的同学可能一看就懂了。没经验的同学也可以联系我们工作人员协助你喔!
常见问题
AllTick API能查询A股哪些股票?
我们支持查询三大指数:上证指数、深证成指、创业板指数,以及总计5140A股上市公司的股票。详细的产品列表可以看这个文件。
除了A股还能查询哪些市场数据?
我们也提供美股、港股、外汇、商品期货和加密货币。有需要可以联系客服。
可以查询历史行情数据吗?
可以!所有品种都支持查询历史数据。
AllTick的接口适用于哪些场景?
我们的产品被用在量化交易、交易所、自营交易、新闻咨询等各种需要高频数据的场景。
如何联系AllTick客服?
可以联系我们客服Telegram:https://t.me/alltick001,或者Skype:https://join.skype.com/invite/xokTc695huNu
请求示例
下面展示如何使用接口查询茅台的最新股价:
import time import requests import json # Extra headers test_headers = { 'Content-Type': 'application/json' } ''' # Special Note: # GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api # Token Application: https://alltick.co # Replace "testtoken" in the URL below with your own token # API addresses for forex, cryptocurrencies, and precious metals: # https://quote.tradeswitcher.com/quote-b-ws-api # Stock API address: # https://quote.tradeswitcher.com/quote-stock-b-ws-api Encode the following JSON and copy it to the "query" field of the HTTP query string {"trace": "python_http_test1", "data": {"code": "600519.SH", "kline_type": 1, "kline_timestamp_end": 0, "query_kline_num": 2, "adjust_type": 0}} {"trace": "python_http_test2", "data": {"symbol_list": [{"code": "600519.SH"}]}} {"trace": "python_http_test3", "data": {"symbol_list": [{"code": "600519.SH"}]}} ''' 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%22600519.SH%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' 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%22600519.SH%22%7D%5D%7D%7D' 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%22600519.SH%22%7D%5D%7D%7D' 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 ''' # Special Note: # GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api # Token Application: https://alltick.co # Replace "testtoken" in the URL below with your own token # API addresses for forex, cryptocurrencies, and precious metals: # wss://quote.tradeswitcher.com/quote-b-ws-api # Stock API address: # 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' # 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 600519.SH) sub_param = { "cmd_id": 22002, "seq_id": 123, "trace":"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806", "data":{ "symbol_list":[ { "code": "600519.SH", "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 600519.SH are 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()