API基本信息:
可查詢:美股、港股、A股、外匯、期貨、加密貨幣,包含實時和歷史成交價格
接口註冊:https://alltick.co/register
官方對接文檔:https://apis.alltick.co/
美股行情API産品存在幾種不同的類型,每種接口都有其特定的功能和用途:
延遲行情接口:顧名思義,行情數據存在延遲,一般是15分鐘,也就是説你看到的成交價格是髮生在15分鐘之前的。這種是最常見的接口,比如你在雪球,証券交易app中看到的行情價格都是存在延遲的。
實時行情接口:這種接口提供即時更新的股票行情數據,包括股票的實時價格、成交量、漲跌幅等信息。一般做量化交易的對延遲會比較敏感,這就需要用到實時行情接口。
曆史行情接口:曆史行情接口提供過去某段時間內的股票行情數據,包括開盤價、收盤價、最高價、最低價等信息。這種接口對於進行技術分析和製定投資策略非常有用。
在當今全球化的金融市場中,美股行情API已經成爲許多應用的重要組成部分。通過接入美股行情API,用戶能夠獲取實時、準確的市場數據,從而在各類場景中獲得競爭優勢。例如,量化交易團隊利用美股行情API來開髮和優化交易策略,以確保決策基於最新的市場動態。個人投資者和金融分析師則可通過這些數據進行深度的市場分析和趨勢預測。此外,投資應用和財經新聞平颱也需要實時的行情數據來爲用戶提供及時的市場信息,提昇用戶體驗和信任度。無論是機構投資還是個人交易者,美股行情API都爲他們提供了關鍵的市場洞察力,使他們能夠在瞬息萬變的市場中做出更明智的決策。
下麵是接入美股行情API的代碼示例。需要注意的是,美股行情API基本都需要付費,如果你不清楚如何選購,推薦先看下這篇教程再入坑。
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
# 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) tag
AllTick API可以查詢A股、港股、美股、外匯、期貨、加密貨幣等不同資產。只需要把對應的code代入到請求中即可。
另外還能通過Websocket訂閱實時行情數據:
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
# 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() tag

评论
发表评论