Python接入实时股票行情数据

Python接入Alltick API的方法,接入后可以查询股票的实时和历史报价,亲测可用。

请求K线

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。
2.2、请求最新成交报价数据

获取最新成交报价数据对于量化策略的分析和判断至关重要。接下来,我将分享如何直接获取这些数据的代码示例:
echarts render error:
SyntaxError: Unexpected token ‘i’, “import tim”… is not valid JSON
上面代码中symbol_list是可以同时传入多个的,分别传入不同的市场的产品也是可以的。

获取最新盘口报价数据


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)
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!