实时股票数据API接口,量化数据分析必备的数据接口工具
一、量化交易的核心在于数据
实时股票API接口是量化股票数据分析中不可或缺的数据接口工具。通过股票API,我们可以获取到最新的股票数据,并且实时更新包括k线数据。这些股票数据包括股票价格、成交量、涨跌幅等重要指标,为我们提供了丰富的信息来进行量化分析,比较常见的像港股、美股、A股等。
对于量化交易策略的开发和优化而言,股票数据是必备的基础。通过股票API接口,我们可以获取到各类股票数据,包括K线数据。K线数据是股票市场中常用的技术分析工具,它提供了股票价格在一定时间段内的开盘价、收盘价、最高价和最低价等信息,有助于我们对股票市场的趋势和波动进行分析和预测。
不仅如此,股票API接口还涵盖了多个市场,包括港股、美股、A股等。这意味着我们可以通过API接口获取到不同市场的股票数据,不受地域限制。无论我们对港股、美股还是A股感兴趣,股票API接口都能为我们提供所需的数据支持。
二、实时股票数据API接口介绍
【Github】https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
具体能提供哪些数据接口我就不说了,放一张接口说明的图,大多数常用市场实时数据都有,包括K线,最新一口价及股票报价实时推送。
三、股票数据使用方法
使用方法超级简单,github上面有真实示例供参数,也可以直接参考下面的代码使用示例
3.1、请求K线数据
在策略的回测中,我们需要使用各种K线数据。虽然具体的策略不在此讨论范围内,但接下来我将介绍如何直接调用已经封装好的API,使用AllTick提供的python代码示例。以下是示例代码:
import time
import requests
import json
# Extra headers
test_headers = {
'Content-Type':'application/json'
}
'''
github:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
申请免费token:https://alltick.co/register
官网:https://alltick.co
将如下JSON进行url的encode,复制到http的查询字符串的query字段里
{"trace":"python_http_test1","data":{"code":"AAPL.US","kline_type":1,"kline_timestamp_end":0,"query_kline_num":2,"adjust_type":0}}
'''
test_url1 = 'https://quote.tradeswitcher.com/quote-stock-b-api/kline?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&query=%7B%22trace%22%3A%22python_http_test1%22%2C%22data%22%3A%7B%22code%22%3A%22AAPL.US%22%2C%22kline_type%22%3A1%2C%22kline_timestamp_end%22%3A0%2C%22query_kline_num%22%3A2%2C%22adjust_type%22%3A0%7D%7D'
resp1 = requests.get(url=test_url1, headers=test_headers)
# Decoded text returned by the request
text1 = resp1.text
print(text1)
上面代码中是以查询苹果股票(AAPL.US)分钟K线为例子的,如果想查询其它类型的K线数据则kline_type传入以下值:1-分钟K,2-为5分钟K,3-为15分钟K,4-为30分钟K,5-为小时K,6-为2小时K,7-为4小时K,8-为日K,9-为周K,10-为月K。
3.1、请求最新成交报价数据
获取最新成交报价数据对于量化策略的分析和判断至关重要。接下来,我将分享如何直接获取这些数据的代码示例:
import time
import requests
import json
# Extra headers
test_headers = {
'Content-Type':'application/json'
}
'''
github:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
申请免费token:https://alltick.co/register
官网:https://alltick.co
将如下JSON进行url的encode,复制到http的查询字符串的query字段里
{"trace":"python_http_test2","data":{"symbol_list":[{"code": "700.HK"},{"code": "UNH.US"},{"code": "600416.SH"}]}}
'''
test_url1 = 'https://quote.tradeswitcher.com/quote-stock-b-api/trade-tick?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&query=%7B%22trace%22%3A%22python_http_test2%22%2C%22data%22%3A%7B%22symbol_list%22%3A%5B%7B%22code%22%3A%20%22700.HK%22%7D%2C%7B%22code%22%3A%20%22UNH.US%22%7D%2C%7B%22code%22%3A%20%22600416.SH%22%7D%5D%7D%7D'
resp1 = requests.get(url=test_url1, headers=test_headers)
# Decoded text returned by the request
text1 = resp1.text
print(text1)
上面代码中symbol_list是可以同时传入多个的,分别传入不同的市场的产品也是可以的。
3.3、获取最新盘口报价数据
import time
import requests
import json
# Extra headers
test_headers = {
'Content-Type':'application/json'
}
'''
github:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
申请免费token:https://alltick.co/register
官网:https://alltick.co
将如下JSON进行url的encode,复制到http的查询字符串的query字段里
{"trace":"python_http_test2","data":{"symbol_list":[{"code": "700.HK"},{"code": "UNH.US"},{"code": "600416.SH"}]}}
'''
test_url1 = 'https://quote.tradeswitcher.com/quote-stock-b-api/depth-tick?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&query=%7B%22trace%22%3A%22python_http_test2%22%2C%22data%22%3A%7B%22symbol_list%22%3A%5B%7B%22code%22%3A%20%22700.HK%22%7D%2C%7B%22code%22%3A%20%22UNH.US%22%7D%2C%7B%22code%22%3A%20%22600416.SH%22%7D%5D%7D%7D'
resp1 = requests.get(url=test_url1, headers=test_headers)
# Decoded text returned by the request
text1 = resp1.text
print(text1)
上面代码中symbol_list是可以同时传入多个的,分别传入不同的市场的产品也是可以的。
3.4、通过websockety订阅获取实时股票行情数据
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/register
官网:https://alltick.co
'''
class Feed(object):
def __init__(self):
self.url = 'wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806' # 这里输入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,
},
{
"code": "600416.SH",
"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()
上面的代码中symbol_list代表你要订阅的产品列表,可以同时传入多个市场的多个产品, cmd_id=22002是订阅盘口数据,当cmd_id=22004时订阅的是成交报价,一旦订阅成功,实时股票行情数据就会源源不断的推送过来,并且是及时的。
好了。今天的技术干货就分享到这里,感谢大家支持。
- 点赞
- 收藏
- 关注作者
评论(0)