【量化入门】alltick:Python开源实时股票行情数据源
一、量化交易的核心在于数据
无论是历史交易数据、财务数据还是宏观数据等实时行情报价数据源,都是我们必不可少的资源。我们的目标是从这些数据中提取出有用的信息,以指导我们的投资策略。
在寻找数据的过程中,我曾经尝试过多种方法,从使用Tushare到自己编写网易股票页面的爬虫、申万行业数据的爬虫,再到同花顺问财的爬虫,甚至使用了聚宽的免费数据API。然而,直到最终,我找到了AllTick,才真正找到了理想中的实时行情数据源解决方案。
二、AllTick介绍
具体能提供哪些数据接口我就不说了,放一张接口说明的图,大多数常用市场实时数据都有,包括K线,最新一口价及股票报价实时推送。
三、数据使用方法
使用方法超级简单,github上面有真实示例供参数,也可以直接参考下面的代码使用示例
3.1、请求K线数据
在策略的回测中,我们需要使用各种K线数据。虽然具体的策略不在此讨论范围内,但接下来我将介绍如何直接调用已经封装好的API,使用AllTick提供的FeeQuote。以下是示例代码:
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)
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)
3.4、通过websockety订阅获取实时股票行情数据
好了。今天的技术干货就分享到这里,感谢大家支持。
浙公网安备 33010602011771号