如何接入美股行情API
【摘要】 美股行情API产品存在几种不同的类型,每种接口都有其特定的功能和用途:延迟行情接口:顾名思义,行情数据存在延迟,一般是15分钟,也就是说你看到的成交价格是发生在15分钟之前的。这种是最常见的接口,比如你在雪球,证券交易app中看到的行情价格都是存在延迟的。实时行情接口:这种接口提供即时更新的股票行情数据,包括股票的实时价格、成交量、涨跌幅等信息。一般做量化交易的对延迟会比较敏感,这就需要用到...
美股行情API产品存在几种不同的类型,每种接口都有其特定的功能和用途:
延迟行情接口:顾名思义,行情数据存在延迟,一般是15分钟,也就是说你看到的成交价格是发生在15分钟之前的。这种是最常见的接口,比如你在雪球,证券交易app中看到的行情价格都是存在延迟的。
实时行情接口:这种接口提供即时更新的股票行情数据,包括股票的实时价格、成交量、涨跌幅等信息。一般做量化交易的对延迟会比较敏感,这就需要用到实时行情接口。
历史行情接口:历史行情接口提供过去某段时间内的股票行情数据,包括开盘价、收盘价、最高价、最低价等信息。这种接口对于进行技术分析和制定投资策略非常有用。
在当今全球化的金融市场中,美股行情API已经成为许多应用的重要组成部分。通过接入美股行情API,用户能够获取实时、准确的市场数据,从而在各类场景中获得竞争优势。例如,量化交易团队利用美股行情API来开发和优化交易策略,以确保决策基于最新的市场动态。个人投资者和金融分析师则可通过这些数据进行深度的市场分析和趋势预测。此外,投资应用和财经新闻平台也需要实时的行情数据来为用户提供及时的市场信息,提升用户体验和信任度。无论是机构投资还是个人交易者,美股行情API都为他们提供了关键的市场洞察力,使他们能够在瞬息万变的市场中做出更明智的决策。
需要注意的是,美股行情API基本都需要付费,而且国内基本没有能用的产品,目前比较稳定的是Alltick团队出的接口,下面是接入的代码示例。
HTTP请求示例
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
# 备用地址:https://alltick.io
# 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": "700.HK", "kline_type": 1, "kline_timestamp_end": 0, "query_kline_num": 2, "adjust_type": 0}}
{"trace": "python_http_test2", "data": {"symbol_list": [{"code": "700.HK"}, {"code": "UNH.US"}]}}
{"trace": "python_http_test3", "data": {"symbol_list": [{"code": "700.HK"}, {"code": "UNH.US"}]}}
'''
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%22700.HK%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%22700.HK%22%7D%2C%7B%22code%22%3A%20%22UNH.US%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%22700.HK%22%7D%2C%7B%22code%22%3A%20%22UNH.US%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
# 备用地址:https://alltick.io
# 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 (an example)
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,
}
]
}
}
# 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 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()
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)