Files
server-configs/stock_report.sh
2026-02-13 22:24:27 +08:00

53 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
# 每日美股持仓报表 - 文字版
# 时间: 每天早上 08:00
python3 << 'PYEOF'
import requests
from datetime import datetime
holdings = {
"SGOV": {"shares": 33.7072, "cost": 100.34, "name": "iShares 0-3月国债ETF"},
"BND": {"shares": 6.7219, "cost": 74.62, "name": "Vanguard 全债ETF"},
"VOO": {"shares": 3.1963, "cost": 616.32, "name": "Vanguard 标普500 ETF"},
"MSFT": {"shares": 1, "cost": 439.00, "name": "Microsoft"}
}
def get_price(code):
url = f"https://qt.gtimg.cn/q=us{code}"
try:
resp = requests.get(url, timeout=5)
if resp.status_code == 200:
data = resp.text
parts = data.split('~')
return float(parts[3])
except:
pass
return None
report = f"*美股持仓日报*\n{datetime.now().strftime('%Y-%m-%d')}\n\n"
total_cost = 0
total_value = 0
for code, info in holdings.items():
price = get_price(code)
if price:
value = info["shares"] * price
cost = info["shares"] * info["cost"]
pnl = value - cost
emoji = "+" if pnl > 0 else "-"
report += f"{emoji} *{code}* ${price:.2f}\n"
report += f" 持仓{info['shares']:.2f}股 | 盈亏 {pnl:+.2f}\n\n"
total_cost += cost
total_value += value
total_pnl = total_value - total_cost
total_emoji = "📈" if total_pnl > 0 else "📉"
report += f"{total_emoji} *总计*: ${total_value:,.2f} ({total_pnl:+.2f})"
print(report)
PYEOF