ib-portfolio

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

IB Portfolio

IB投资组合

Fetch current portfolio positions from Interactive Brokers.
从Interactive Brokers获取当前投资组合持仓。

IB Connection

IB连接

TWS or IB Gateway must be running locally with API enabled:
  • Paper trading — port 7497
  • Live trading — port 7496
  • IB_PORT
    env var
    — default port when
    --port
    is omitted (e.g.
    IB_PORT=4001
    for a Gateway container). Precedence:
    --port
    flag >
    IB_PORT
    > built-in default. Set it in the shell or a
    .env
    file.
Port fallback: If the configured port fails, automatically retry on the other port. If the retry succeeds, save to memory which account type worked (live/paper) and reuse it for all IB skill calls in this and future sessions — until the user explicitly asks for the other account. If both ports fail, ask the user to verify that TWS or IB Gateway is running with API access enabled.
必须在本地运行已启用API的TWS或IB Gateway:
  • 模拟交易 — 端口7497
  • 实盘交易 — 端口7496
  • IB_PORT
    环境变量
    — 当省略
    --port
    参数时的默认端口(例如,Gateway容器使用
    IB_PORT=4001
    )。优先级:
    --port
    命令行参数 >
    IB_PORT
    环境变量 > 内置默认值。可在shell或
    .env
    文件中设置。
端口回退机制: 如果配置的端口连接失败,将自动尝试另一个端口。 如果重试成功,将在内存中记录有效的账户类型(实盘/模拟),并在本次及后续会话的所有IB skill调用中复用该设置,直到用户明确要求切换至另一种账户。 如果两个端口均连接失败,请用户确认TWS或IB Gateway已运行且已启用API访问权限。

Instructions

使用说明

Note: If
uv
is not installed or
pyproject.toml
is not found, replace
uv run python
with
python
in all commands below.
bash
uv run python scripts/portfolio.py [--port PORT]
注意: 如果未安装
uv
或未找到
pyproject.toml
,请将以下所有命令中的
uv run python
替换为
python
bash
uv run python scripts/portfolio.py [--port PORT]

Arguments

参数

  • --port
    - IB port (default: 7497 for paper trading)
  • --account
    - Specific IB account ID (optional, defaults to first account)
  • --port
    - IB端口(默认:模拟交易使用7497)
  • --account
    - 指定IB账户ID(可选,默认使用第一个账户)

Output

输出

Returns JSON with:
  • connected
    - Whether connection succeeded
  • positions
    - Array of positions with symbol, quantity, avg_cost, market_value, unrealized_pnl
If not connected, explain that TWS/Gateway needs to be running.
返回包含以下字段的JSON:
  • connected
    - 连接是否成功
  • positions
    - 持仓数组,包含symbol(标的代码)、quantity(数量)、avg_cost(平均成本)、market_value(市值)、unrealized_pnl(未实现盈亏)
如果连接失败,将提示需要运行TWS/Gateway。

Dependencies

依赖

  • ib-async
  • ib-async

Timezone

时区

All timestamps and time-based calculations must use the
America/New_York
timezone. All JSON output must include
generated_at
(NY time string) and
data_delay
fields.
所有时间戳和基于时间的计算必须使用
America/New_York
时区。所有JSON输出必须包含
generated_at
(纽约时间字符串)和
data_delay
字段。