Portfolio
Investment portfolio manager. Add and remove holdings, list positions, analyze allocation weights, generate rebalance suggestions against target weights, and calculate performance returns over configurable time periods. All data is stored locally in JSON files — no external API calls required.
Commands
| Command | Description | Flags |
|---|
| INLINECODE0 | Buy shares — adds to holdings and records a buy transaction | INLINECODE1 (defaults to today) |
| INLINECODE2 |
Sell shares — removes from holdings and records a sell transaction |
--quantity N (partial sell; omit to sell all) |
|
list | Display all current holdings in a formatted table |
--format table\|json\|csv |
|
analyze | Analyze portfolio allocation with value and weight percentages |
--by ticker\|sector --format table\|json |
|
rebalance | Generate buy/sell suggestions to reach target allocation weights |
--target TICKER:PCT,... --threshold PCT (default 5) |
|
performance | Calculate portfolio returns (invested, sold, current value, gain/loss) |
--period 1d\|1w\|1m\|3m\|1y\|all --format table\|json |
|
help | Show the built-in help message with all command examples | — |
Data Storage
All data is stored locally in ~/.portfolio/:
- -
holdings.json — Array of current positions, each with ticker, quantity, avg_price, and INLINECODE21 transactions.json — Array of all buy/sell transactions with type, ticker, quantity, price, and INLINECODE27
Both files are auto-created as empty JSON arrays ([]) on first use. The add command automatically merges duplicate tickers by recalculating the weighted average price.
Requirements
- - bash (4.0+)
- python3 (standard library only —
json, os, sys, datetime) - No external APIs, no pip packages, no network access needed
When to Use
- 1. Tracking purchases — When you buy stocks, ETFs, or crypto and want to record ticker, quantity, and price with automatic cost-basis averaging
- Reviewing holdings — When you need a quick snapshot of your current portfolio in table, JSON, or CSV format
- Allocation analysis — When you want to see how your portfolio is weighted across assets, with percentage breakdowns and visual bars
- Rebalancing — When you need buy/sell recommendations to reach target allocation weights (e.g.,
AAPL:40,GOOG:30,BTC:30), with configurable drift thresholds - Performance tracking — When you want to calculate total invested, total sold, current value, and gain/loss percentage over a specific time period (1 day to all-time)
Examples
CODEBLOCK0
How It Works
The script uses embedded Python (standard library only) for JSON manipulation and calculations. Bash handles argument parsing and dispatches to the appropriate Python block. When you add a position that already exists, the script recalculates the weighted average price automatically. The rebalance command compares current allocation percentages against your targets and flags any drift exceeding the threshold, recommending dollar amounts to buy or sell. The performance command filters transactions by date range and computes total invested, total sold, current portfolio value, and the resulting gain/loss.
Output
All commands print to stdout. Use --format json for machine-readable output where supported. Redirect with standard shell operators:
CODEBLOCK1
Powered by BytesAgain | bytesagain.com | hello@bytesagain.com
投资组合
投资组合管理器。可添加和移除持仓、列出头寸、分析配置权重、根据目标权重生成再平衡建议,并计算可配置时间段内的业绩回报。所有数据均存储在本地JSON文件中——无需外部API调用。
命令
| 命令 | 描述 | 标志 |
|---|
| add <代码> <数量> <价格> | 买入股票——添加到持仓并记录买入交易 | --date YYYY-MM-DD(默认为今天) |
| remove <代码> |
卖出股票——从持仓中移除并记录卖出交易 | --quantity N(部分卖出;省略则全部卖出) |
| list | 以格式化表格显示所有当前持仓 | --format table\|json\|csv |
| analyze | 分析投资组合配置,包含价值和权重百分比 | --by ticker\|sector --format table\|json |
| rebalance | 生成买入/卖出建议以达到目标配置权重 | --target 代码:百分比,... --threshold 百分比(默认5) |
| performance | 计算投资组合回报(投入、卖出、当前价值、盈亏) | --period 1d\|1w\|1m\|3m\|1y\|all --format table\|json |
| help | 显示包含所有命令示例的内置帮助信息 | — |
数据存储
所有数据均存储在本地 ~/.portfolio/ 目录中:
- - holdings.json — 当前持仓数组,每个包含代码、数量、均价和添加日期
- transactions.json — 所有买入/卖出交易数组,包含类型、代码、数量、价格和日期
两个文件在首次使用时自动创建为空JSON数组([])。add命令通过重新计算加权平均价格自动合并重复的股票代码。
系统要求
- - bash(4.0+)
- python3(仅标准库——json、os、sys、datetime)
- 无需外部API、无需pip包、无需网络访问
使用场景
- 1. 跟踪购买——当您买入股票、ETF或加密货币,并希望记录代码、数量和价格,且自动计算成本基础平均值时
- 查看持仓——当您需要以表格、JSON或CSV格式快速查看当前投资组合快照时
- 配置分析——当您想查看投资组合在各类资产中的权重分布,包含百分比分解和可视化条形图时
- 再平衡——当您需要买入/卖出建议以达到目标配置权重(例如AAPL:40,GOOG:30,BTC:30),并配有可配置的偏离阈值时
- 业绩跟踪——当您想计算特定时间段(1天至全部历史)内的总投资额、总卖出额、当前价值和盈亏百分比时
示例
bash
以150.50美元价格买入100股AAPL,购买日期为2024年1月15日
bash scripts/script.sh add AAPL 100 150.50 --date 2024-01-15
以42,000美元价格买入0.5 BTC(自动使用今天日期)
bash scripts/script.sh add BTC 0.5 42000
卖出50股AAPL(部分卖出)
bash scripts/script.sh remove AAPL --quantity 50
移除全部GOOG持仓
bash scripts/script.sh remove GOOG
以格式化表格列出持仓
bash scripts/script.sh list
以JSON格式列出持仓(便于程序化使用)
bash scripts/script.sh list --format json
以CSV格式列出持仓(便于导入电子表格)
bash scripts/script.sh list --format csv
按代码分析配置,附带可视化权重条
bash scripts/script.sh analyze --format table
以JSON格式获取配置
bash scripts/script.sh analyze --format json
生成再平衡建议,自定义目标和3%阈值
bash scripts/script.sh rebalance --target AAPL:40,GOOG:30,BTC:30 --threshold 3
等权重再平衡(不指定--target则默认所有持仓等权重)
bash scripts/script.sh rebalance
查看过去一个月的业绩
bash scripts/script.sh performance --period 1m
全部历史业绩,JSON格式
bash scripts/script.sh performance --period all --format json
工作原理
该脚本使用嵌入式Python(仅标准库)进行JSON操作和计算。Bash负责参数解析并分派到相应的Python代码块。当您add一个已存在的持仓时,脚本会自动重新计算加权平均价格。rebalance命令将当前配置百分比与目标进行比较,标记任何超过阈值的偏离,并建议买入或卖出的美元金额。performance命令按日期范围筛选交易,计算总投资额、总卖出额、当前投资组合价值以及最终的盈亏。
输出
所有命令输出到标准输出。在支持的地方使用--format json获取机器可读的输出。使用标准shell操作符重定向:
bash
bash scripts/script.sh list --format csv > portfolio.csv
bash scripts/script.sh performance --format json | jq .
由BytesAgain提供 | bytesagain.com | hello@bytesagain.com