1. 環境載入與宣告

在使用之前,需先匯入模組並建立 Unitrade 物件實體。

import unitrade
from unitrade.unitrade import *

# 初始 API 物件
api = Unitrade()

2. 註冊錯誤事件 (Error Handling)

建議在執行登入前先註冊錯誤監聽事件,以便在 API 運作過程中發生異常時能即時收到通知。

def on_error(err):
    print(f"API Error: {err}")

# 註冊錯誤回調函式
api.on_error = on_error

3. 系統登入 (Login)

使用 login 方法進行連線登入。

參數說明

  1. URL: 登入伺服器網址
  2. Account: 交易帳號
  3. Password: 交易密碼
  4. Cert Path: 憑證檔案路徑 (例如 C:\cert\user.pfx)
  5. Cert Password: 憑證密碼
# 執行登入
login_response = api.login(
    "登入url",      # URL
    "帳號",         # Account
    "密碼",         # Password
    "憑證檔名",     # CA Path
    "憑證密碼"      # CA Password
)

4. 檢查登入結果

登入方法會回傳一個 LoginResponse 物件。請檢查 ok 屬性以確認登入是否成功。

# 檢查回傳結果
if login_response.ok:
    print("登入成功")
    # 登入成功後,通常會接續取得帳號列表
    accounts = unitrade.get_accounts()
    print(f"可用帳號: {accounts}")
else:
    print(f"登入失敗,錯誤代碼: {login_response.errorcode}, 訊息: {login_response.errormsg}")

# LoginResponse 物件結構範例:
# ------------------------------------------------
# LoginResponse(
#   ok=True, 
#   errorcode='', 
#   errormsg='', 
#   ...
# )

詳細 API 規格請參考 API Reference/unitrade

```


Back to top

支援Python版本: 3.7 3.8 3.9 3.10 3.11 3.12

支援 OS: Linux, macOS, Windows