ib-portfolio
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseIB 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
- env var — default port when
IB_PORTis omitted (e.g.--portfor a Gateway container). Precedence:IB_PORT=4001flag >--port> built-in default. Set it in the shell or aIB_PORTfile..env
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参数时的默认端口(例如,Gateway容器使用--port)。优先级:IB_PORT=4001命令行参数 >--port环境变量 > 内置默认值。可在shell或IB_PORT文件中设置。.env
端口回退机制: 如果配置的端口连接失败,将自动尝试另一个端口。
如果重试成功,将在内存中记录有效的账户类型(实盘/模拟),并在本次及后续会话的所有IB skill调用中复用该设置,直到用户明确要求切换至另一种账户。
如果两个端口均连接失败,请用户确认TWS或IB Gateway已运行且已启用API访问权限。
Instructions
使用说明
Note: Ifis not installed oruvis not found, replacepyproject.tomlwithuv run pythonin all commands below.python
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
参数
- - IB port (default: 7497 for paper trading)
--port - - Specific IB account ID (optional, defaults to first account)
--account
- - IB端口(默认:模拟交易使用7497)
--port - - 指定IB账户ID(可选,默认使用第一个账户)
--account
Output
输出
Returns JSON with:
- - Whether connection succeeded
connected - - Array of positions with symbol, quantity, avg_cost, market_value, unrealized_pnl
positions
If not connected, explain that TWS/Gateway needs to be running.
返回包含以下字段的JSON:
- - 连接是否成功
connected - - 持仓数组,包含symbol(标的代码)、quantity(数量)、avg_cost(平均成本)、market_value(市值)、unrealized_pnl(未实现盈亏)
positions
如果连接失败,将提示需要运行TWS/Gateway。
Dependencies
依赖
ib-async
ib-async
Timezone
时区
All timestamps and time-based calculations must use the timezone. All JSON output must include (NY time string) and fields.
America/New_Yorkgenerated_atdata_delay所有时间戳和基于时间的计算必须使用时区。所有JSON输出必须包含(纽约时间字符串)和字段。
America/New_Yorkgenerated_atdata_delay