快速入門
本指南將引導您使用 Webull SDK 完成第一筆交易。完成後,您將能夠查詢帳戶列表、檢查餘額並下單。
前置條件
- 已安裝 Webull SDK(SDK 和工具)
- App Key 和 App Secret(Trading API 申請流程)
- 狀態為
NORMAL的有效存取 Token(Token)
步驟 1:獲取帳戶列表
下單前,您需要先獲取 Account ID。
- Python
- Java
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())
import com.webull.openapi.core.http.HttpApiConfig;
import com.webull.openapi.trade.TradeClient;
HttpApiConfig config = HttpApiConfig.builder()
.appKey("<your_app_key>")
.appSecret("<your_app_secret>")
.regionId("hk")
.endpoint("<api_endpoint>")
.build();
TradeClient client = new TradeClient(config);
System.out.println("Accounts: " + client.getAccountList());
保存回應中的 account_id,後續所有呼叫都需要使用。
API 端點
- Trading API 正式環境:
api.webull.hk - Broker API 正式環境:
broker-api-event-push.webull.hk - 沙盒環境:
api.sandbox.webull.hk
步驟 2:查詢帳戶餘額
- Python
- Java
account_id = "<your_account_id>"
res = trade_client.get_account_balance(account_id=account_id)
if res.status_code == 200:
print("Balance:", res.json())
String accountId = "<your_account_id>";
Object balance = client.getAccountBalance(accountId);
System.out.println("Balance: " + balance);
步驟 3:下單
下一個簡單的限價買入訂單(100 股):
- Python
- Java
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())
Object order = client.placeOrder(
"<your_account_id>",
"<instrument_id>",
"BUY",
"LIMIT",
"100",
"150.00",
"DAY"
);
System.out.println("Order placed: " + order);
步驟 4:訂閱訂單狀態更新
透過 gRPC 串流即時監控訂單狀態變更:
- Python
- Java
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>"])
import com.webull.openapi.trade.events.subscribe.*;
import com.webull.openapi.trade.events.subscribe.message.*;
ITradeEventClient eventClient = ITradeEventClient.builder()
.appKey("<your_app_key>")
.appSecret("<your_app_secret>")
.regionId("hk")
.onMessage(response -> {
System.out.println("Order update: " + response.getPayload());
})
.build();
SubscribeRequest request = new SubscribeRequest("<your_account_id>");
ISubscription subscription = eventClient.subscribe(request);
subscription.blockingAwait();
提示
- 正式環境 gRPC:
events-api.webull.hk - 沙盒環境 gRPC:
events-api.sandbox.webull.hk
下一步
- 帳戶 — 查詢餘額和持倉
- 訂單 — 訂單類型、改單和撤單
- Trading API 常見問題 — 常見問題和故障排除