跳至主要内容

快速入門

本指南將引導您使用 Webull SDK 完成第一筆交易。完成後,您將能夠查詢帳戶列表、檢查餘額並下單。

前置條件

步驟 1:獲取帳戶列表

下單前,您需要先獲取 Account ID。

from webull.core.client import ApiClient
from webull.trade.trade_client import TradeClient

api_client = ApiClient("<your_app_key>", "<your_app_secret>", "hk")
api_client.add_endpoint("hk", "<api_endpoint>")

trade_client = TradeClient(api_client)
res = trade_client.get_account_list()
if res.status_code == 200:
print("Accounts:", res.json())

保存回應中的 account_id,後續所有呼叫都需要使用。

API 端點
  • Trading API 正式環境:api.webull.hk
  • Broker API 正式環境:broker-api-event-push.webull.hk
  • 沙盒環境:api.sandbox.webull.hk

步驟 2:查詢帳戶餘額

account_id = "<your_account_id>"
res = trade_client.get_account_balance(account_id=account_id)
if res.status_code == 200:
print("Balance:", res.json())

步驟 3:下單

下一個簡單的限價買入訂單(100 股):

order_params = {
"account_id": "<your_account_id>",
"instrument_id": "<instrument_id>",
"side": "BUY",
"order_type": "LIMIT",
"quantity": "100",
"price": "150.00",
"time_in_force": "DAY"
}

res = trade_client.place_order(**order_params)
if res.status_code == 200:
print("Order placed:", res.json())

步驟 4:訂閱訂單狀態更新

透過 gRPC 串流即時監控訂單狀態變更:

from webull.trade.events.types import ORDER_STATUS_CHANGED, EVENT_TYPE_ORDER
from webull.trade.trade_events_client import TradeEventsClient

def on_event(event_type, subscribe_type, payload, raw_message):
if EVENT_TYPE_ORDER == event_type and ORDER_STATUS_CHANGED == subscribe_type:
print("Order update:", payload)

events_client = TradeEventsClient("<your_app_key>", "<your_app_secret>", "hk")
events_client.on_events_message = on_event
events_client.do_subscribe(["<your_account_id>"])
提示
  • 正式環境 gRPC:events-api.webull.hk
  • 沙盒環境 gRPC:events-api.sandbox.webull.hk

下一步