Skip to main content

fubon-cli

PyPI version codecov

富邦證券 Fubon Neo Trading API(v2.2.8)的命令列工具,專為 AI 代理自動化設計。

所有指令皆輸出結構化 JSON 到 stdout,方便 AI 代理、腳本或管線解析與接續處理。

環境需求

  • Python 3.8 - 3.13
  • Fubon Neo SDK wheel 檔(fubon_neo-2.2.8-cp37-abi3-win_amd64.whl
  • 具電子憑證的富邦證券帳戶

安裝

# 從 PyPI 安裝(建議)
pip install fubon-cli

# 或從原始碼安裝(開發用)
pip install -e .

Star 歷史

Star History Chart

快速開始

# 1. 登入(儲存 session 供後續指令使用)
fubon login --id "A123456789" --password "yourpass" --cert-path "/path/to/cert.pfx" --cert-password "certpass"

# 2. 取得報價
fubon market quote 2330

# 3. 下買單
fubon stock buy 2330 1000 --price 580

# 4. 查詢委託
fubon stock orders

# 5. 查詢庫存
fubon account inventory

指令參考

認證

# 登入
fubon login --id <ID> --password <PW> --cert-path <PATH> [--cert-password <PW>]

# 查詢登入狀態
fubon login status

# 登出(清除儲存的認證)
fubon login logout

股票交易

# 買進(限價)
fubon stock buy <SYMBOL> <QUANTITY> --price <PRICE>

# 以跌停價買進
fubon stock buy 2881 2000 --price-type limit-down

# 以 IOC(立即成交或取消)買進
fubon stock buy 2330 500 --price 580 --time-in-force IOC

# 賣出
fubon stock sell <SYMBOL> <QUANTITY> --price <PRICE>

# 市價賣出
fubon stock sell 2330 1000 --price-type market

# 當沖委託
fubon stock buy 2330 1000 --price 580 --order-type day-trade

# 零股委託
fubon stock buy 2330 50 --price 580 --market-type intraday-odd

# 查詢委託
fubon stock orders

# 取消委託
fubon stock cancel <ORDER_NO>

# 改價
fubon stock modify-price <ORDER_NO> <NEW_PRICE>

# 改量
fubon stock modify-quantity <ORDER_NO> <NEW_QUANTITY>

委託選項:

參數 預設
--price-type limit, market, limit-up, limit-down, reference limit
--time-in-force ROD, IOC, FOK ROD
--order-type stock, margin, short, sbl, day-trade stock
--market-type common, odd, intraday-odd, fixing, emg, emg-odd common

帳務查詢

# 查詢股票庫存(持倉)
fubon account inventory

# 查詢未實現損益
fubon account unrealized

# 查詢當日交割資訊
fubon account settlement

# 查詢昨日交割資訊
fubon account settlement --range 1d

# 查詢個股融資融券額度
fubon account margin-quota 2330

市場資料

# 即時報價
fubon market quote 2330

# 個股資訊
fubon market ticker 2330

# 盤中 K 線(預設 5 分鐘)
fubon market candles 2330
fubon market candles 2330 --timeframe 15

# 盤中成交明細
fubon market trades 2330 --limit 50

# 價量分布
fubon market volumes 2330

# 大盤快照
fubon market snapshot TSE

# 漲幅排行
fubon market movers TSE --direction up --change percent

# 跌幅排行
fubon market movers OTC --direction down

# 成交量排行
fubon market actives TSE --trade volume

# 歷史 K 線
fubon market history 2330 --from 2024-01-01 --to 2024-06-30
fubon market history 0050 --timeframe W --adjusted

# 52 週統計
fubon market stats 2330

# 列出所有股票代碼
fubon market tickers --type EQUITY --exchange TWSE

即時串流

# 訂閱即時成交(JSONL 串流)
fubon realtime subscribe 2330

# 訂閱聚合資料
fubon realtime subscribe 2330 --channel aggregates

# 監聽委託/成交回報(JSONL 串流)
fubon realtime callbacks

JSON 輸出格式

所有指令皆輸出一致的 JSON 結構:

{
  "success": true,
  "data": { ... }
}

錯誤時:

{
  "success": false,
  "error": "Error description"
}

串流指令(realtime subscribe, realtime callbacks)每行輸出一個 JSON 物件(JSONL 格式)。

AI 助理

fubon-cli 內置 AI 助理功能,可以通過自然語言與 CLI 互動,自動生成和執行交易指令。

設定 AI

先安裝 OpenAI 支援:

pip install fubon-cli[ai]
# 或
pip install openai

再設定 OpenAI API Key:

# 方式 1:設定 OpenAI API Key
fubon config set openai-key sk-proj-...

# 方式 2:使用環境變數
export OPENAI_API_KEY=sk-proj-...
export FUBON_AI_KEY=sk-proj-...   # 或這個

# 方式 3:查看目前配置
fubon config show

一次性查詢(fubon ask)

快速詢問 AI 並取得命令建議:

# 基本詢問
fubon ask "台積電(2330)的目前報價是多少?"

# 詢問並執行建議的命令(互動確認)
fubon ask "如何以市價買入 2330 一張?" --execute

# 縮寫
fubon ask "幫我查詢帳戶庫存" -x

# 用於 AI 代理(JSON 輸出)
fubon ask "取得 2330 的即時報價" --json-output

輸出格式(--json-output):

{
  "success": true,
  "question": "台積電(2330)的目前報價是多少?",
  "answer": "根據 Fubon Neo SDK...",
  "suggested_commands": [
    "fubon market quote 2330"
  ]
}

互動對話模式(fubon chat)

進入 AI 聊天 REPL,可連續對話並執行命令:

fubon chat

進入後的內建指令:

/run      — 執行 AI 最新建議的指令(帶確認)
/clear    — 清除對話記錄,重新開始
exit      — 離開

示例:

你 ❯ 台積電現在的股價多少?
富邦助理 ❯ 為了幫您查詢台積電(2330)的目前股價...
[AI 回覆 + 建議指令]

💡 有 1 個建議指令,輸入 /run 執行
你 ❯ /run
  1. ✦ fubon market quote 2330
  執行 [fubon market quote 2330]? (y|n): y
  ▶ fubon market quote 2330
  {
    "success": true,
    "data": {
      "symbol": "2330",
      "price": 995.0,
      ...
    }
  }

你 ❯ 幫我買 5 張零股
富邦助理 ❯ 為了買進 2330 的零股...
  ⚠ [交易] fubon stock buy 2330 50 --price 990
  執行交易指令?此操作會影響帳戶!
  [fubon stock buy 2330 50 --price 990]
  請輸入 yes 確認

AI 助理特性

  • 繁體中文對話 — 自動回應繁體中文
  • 指令生成 — 自動從回應中擷取 fubon 指令
  • 安全確認 — 交易指令(買、賣、取消等)需要明確確認
  • 完整 CLI 知識 — AI 瞭解所有 fubon 指令語法和選項
  • 多模型支援 — 預設使用 gpt-4o-mini,可切換為 gpt-4o

設定 AI 模型

# 查看目前模型
fubon config show

# 改為 GPT-4O
fubon config set ai-model gpt-4o

# 改為其他模型
fubon config set ai-model gpt-4-turbo

AI 代理整合

此 CLI 針對 AI 代理的交易流程自動化設計:

  1. 無狀態執行:每個指令都是獨立呼叫,登入憑證會儲存在 ~/.fubon-cli-session.json
  2. JSON 輸出:所有回應皆為可機器解析的 JSON。
  3. 錯誤代碼:失敗時返回非 0 狀態碼,錯誤細節在 JSON 中。
  4. 串流:即時資料以 JSONL 持續輸出,便於監控。

代理流程示例:

# 查詢持倉
positions=$(fubon account inventory)

# 取得報價
quote=$(fubon market quote 2330)

# 依代理邏輯下單
fubon stock buy 2330 1000 --price 580

# 監控成交回報
fubon realtime callbacks

授權

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fubon_cli-0.4.0.tar.gz (21.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fubon_cli-0.4.0-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

File details

Details for the file fubon_cli-0.4.0.tar.gz.

File metadata

  • Download URL: fubon_cli-0.4.0.tar.gz
  • Upload date:
  • Size: 21.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fubon_cli-0.4.0.tar.gz
Algorithm Hash digest
SHA256 52c6698f30498efbf1b61c40b83cc627421c33a19921c965d5784e8768bc1459
MD5 91860fb974609b279f45f974df6efef0
BLAKE2b-256 9db71404be1210ceac7c96b95a5793c2b04fdb6c18346355674e96fd3738bb70

See more details on using hashes here.

File details

Details for the file fubon_cli-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: fubon_cli-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 33.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fubon_cli-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fa8c3cdcf3efb63bb11cd00a68a90406d7b386a84623d7fea15e0c7940fb623
MD5 bac02acdd1eefd7be12193fc46909b51
BLAKE2b-256 6bdba9189073714485efaa0c3abfff27a364a285b3058b3bba624745e2b73f28

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 files

0.3.0

2 files

0.1.3

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page