快速入門
從零開始完成您的第一個行情數據請求。我們將安裝 SDK、設定認證,並執行兩個範例:獲取歷史 K 線和訂閱即時報價。
前置條件
- Python 3.8–3.13 或 Java JDK 8+
- App Key 和 App Secret。參閱 Trading API 申請流程或使用共享的測試帳戶。
警告
存取行情數據(包括歷史和即時數據)需要有效的 OpenAPI 行情數據訂閱。如果執行以下範例時收到 403 錯誤,您可能需要先訂閱。詳情請參閱訂閱進階報價。
目前測試帳戶僅允許存取標的 AAPL 的行情數據介面和即時行情推送。
步驟 1:安裝 SDK
- Python
- Java
pip3 install --upgrade webull-openapi-python-sdk
<dependency>
<groupId>com.webull.openapi</groupId>
<artifactId>webull-openapi-java-sdk</artifactId>
<version>1.0.3</version> <!-- Check https://central.sonatype.com/artifact/com.webull.openapi/webull-openapi-java-sdk for the latest version -->
</dependency>
步驟 2:獲取歷史數據
以下範例獲取 AAPL 的 1 分鐘 K 線數據:
from webull.data.common.category import Category
from webull.data.common.timespan import Timespan
from webull.core.client import ApiClient
from webull.data.data_client import DataClient
api_client = ApiClient("<your_app_key>", "<your_app_secret>", "hk")
api_client.add_endpoint("hk", "<api_endpoint>")
data_client = DataClient(api_client)
# 單一標的
res = data_client.market_data.get_history_bar("AAPL", Category.US_STOCK.name, Timespan.M1.name)
if res.status_code == 200:
print("History bar:", res.json())
# 批量查詢(多個標的)
res = data_client.market_data.get_batch_history_bar(
["AAPL", "TSLA"], Category.US_STOCK.name, Timespan.M1.name, 1
)
if res.status_code == 200:
print("Batch history bar:", res.json())
API 端點
- 正式環境:
api.webull.hk - 沙盒環境:
api.sandbox.webull.hk
步驟 3:訂閱即時報價
以下範例連接 MQTT 串流服務,並訂閱 AAPL 的即時報價、快照和逐筆成交數據:
from webull.data.common.category import Category
from webull.data.common.subscribe_type import SubscribeType
from webull.data.data_streaming_client import DataStreamingClient
data_streaming_client = DataStreamingClient(
"<your_app_key>",
"<your_app_secret>",
"hk",
"demo_session_1",
http_host="<api_endpoint>",
mqtt_host="<data_api_endpoint>",
)
def on_connect(client, api_client, session_id):
print("Connected:", client.get_session_id())
client.subscribe(
["AAPL"],
Category.US_STOCK.name,
[SubscribeType.QUOTE.name, SubscribeType.SNAPSHOT.name, SubscribeType.TICK.name],
)
def on_message(client, topic, quotes):
print("Topic:", topic, "Data:", quotes)
def on_subscribe(client, api_client, session_id):
print("Subscribed:", client.get_session_id())
data_streaming_client.on_connect_success = on_connect
data_streaming_client.on_quotes_message = on_message
data_streaming_client.on_subscribe_success = on_subscribe
data_streaming_client.connect_and_loop_forever()
串流端點
- 正式環境 MQTT:
data-api.webull.hk - 沙盒環境 MQTT:
data-api.sandbox.webull.hk
如果您不想使用 SDK 進行串流,請參閱 Data Streaming API 了解原始 MQTT 整合指南。
下一步
- Data API — 用於歷史和快照數據的 HTTP 端點
- Data Streaming API — 即時串流的 MQTT 協議詳情
- Market Data API 概覽 — 完整的可用端點和速率限制列表