跳至主要内容

快速入門

從零開始完成您的第一個行情數據請求。我們將安裝 SDK、設定認證,並執行兩個範例:獲取歷史 K 線和訂閱即時報價。

前置條件

警告

存取行情數據(包括歷史和即時數據)需要有效的 OpenAPI 行情數據訂閱。如果執行以下範例時收到 403 錯誤,您可能需要先訂閱。詳情請參閱訂閱進階報價

目前測試帳戶僅允許存取標的 AAPL 的行情數據介面和即時行情推送。

步驟 1:安裝 SDK

pip3 install --upgrade webull-openapi-python-sdk

步驟 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 整合指南。

下一步