#!/bin/bash # 自动更新持仓脚本 # MSFT: 每14天 | VOO: 每月1号 LOG_FILE="/root/.openclaw/workspace/auto_invest.log" DATE=$(date +%Y-%m-%d) DAY=$(date +%d) echo "=== 自动持仓更新 ===" >> "$LOG_FILE" echo "时间: $DATE" >> "$LOG_FILE" python3 << 'PYEOF' >> "$LOG_FILE" 2>&1 import requests import json from datetime import datetime NOTION_TOKEN = "ntn_c43902219395mirQBetIfYoww1qKCAF14GBRUQeDee29o2" DATABASE_ID = "2fb105ad78738175bbbde5b87cf101d9" 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 def get_notion_pages(): url = f"https://api.notion.com/v1/databases/{DATABASE_ID}/query" headers = { "Authorization": f"Bearer {NOTION_TOKEN}", "Notion-Version": "2022-06-28", "Content-Type": "application/json" } resp = requests.post(url, headers=headers, json={"page_size": 100}) return resp.json().get("results", []) def update_notion(page_id, shares, cost): url = f"https://api.notion.com/v1/pages/{page_id}" headers = { "Authorization": f"Bearer {NOTION_TOKEN}", "Notion-Version": "2022-06-28", "Content-Type": "application/json" } data = { "properties": { "持仓数量": {"number": float(shares)}, "当前价格": {"number": cost}, "当前市值": {"number": float(shares) * cost}, "盈亏金额": {"number": 0}, "盈亏百分比": {"number": 0} } } requests.patch(url, headers=headers, json=data) # 检查 MSFT 持仓 print("检查持仓...") pages = get_notion_pages() for page in pages: title = page.get("properties", {}).get("股票代码", {}).get("title", []) if title: code = title[0].get("plain_text", "") shares = page.get("properties", {}).get("持仓数量", {}).get("number", 0) print(f"{code}: {shares} 股") # 获取当前价格并计算 if code in ["MSFT", "VOO"]: price = get_price(code) if price: print(f"{code} 当前价格: ${price}") print("检查完成") PYEOF echo "完成: $(date)" >> "$LOG_FILE"