下面是使用外部接口查询股票历史数据的示例。我们可以使用下面的代码向Alltick API发送请求,查询苹果股票的价格。这个接口支持查询A股、港股和美股。

注意其中的参数kline_type 代表的是k线类型,1为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

import time
import requests    # pip3 install 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
备用地址:https://alltick.io

code请查看code列表,选择你要查询的code
kline_type k线类型,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
query_kline_num    查询多少根K线,最多1000根

将如下JSON进行url的encode,复制到http的查询字符串的query字段里
{"trace" : "python_http_test1","data" : {"code" : "AAPL","kline_type" : 1,"kline_timestamp_end" : 0,"query_kline_num" : 2,"adjust_type": 0}}
{"trace" : "python_http_test2","data" : {"symbol_list": [{"code": "AAPL"}]}}
{"trace" : "python_http_test3","data" : {"symbol_list": [{"code": "AAPL"}]}}
'''
test_url1 = 'https://quote.aatest.online/quote-b-api/kline?token=3662a972-1a5d-4bb1-88b4-66ca0c402a03-1688712831841&query=%7B%22trace%22%20%3A%20%22python_http_test1%22%2C%22data%22%20%3A%20%7B%22code%22%20%3A%20%22AAPL%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'

resp1 = requests.get(url=test_url1, headers=test_headers)

# Decoded text returned by the request
text1 = resp1.text
print(text1)

TickTank
2 声望1 粉丝

高频tick数据行情接口