Files
server-configs/add-new-positions.py

152 lines
4.6 KiB
Python
Raw Normal View History

2026-02-13 22:24:27 +08:00
#!/usr/bin/env python3
"""
添加新的美股持仓到Notion数据库
"""
import requests
import json
from datetime import datetime
# Notion API 配置
NOTION_API_KEY = "ntn_c43902219395mirQBetIfYoww1qKCAF14GBRUQeDee29o2"
DATABASE_ID = "2fb105ad-7873-8175-bbbd-e5b87cf101d9"
# 持仓数据
positions = [
{"symbol": "BND", "name": "Vanguard Total Bond Market ETF", "shares": 6.7219, "cost": 501.83, "current_price": 74.22},
{"symbol": "SGOV", "name": "iShares 0-3 Month Treasury Bond ETF", "shares": 33.7072, "cost": 3380.48, "current_price": 100.44},
{"symbol": "VOO", "name": "Vanguard S&P 500 ETF", "shares": 3.1963, "cost": 1969.46, "current_price": 636.09}
]
def calculate_pnl(shares, cost, current_price):
"""计算盈亏"""
market_value = shares * current_price
total_cost = cost
pnl_amount = market_value - total_cost
pnl_percent = (pnl_amount / total_cost * 100) if total_cost > 0 else 0
return market_value, total_cost, pnl_amount, pnl_percent
def add_position_to_notion(symbol, name, shares, cost, current_price):
"""添加持仓到Notion"""
url = "https://api.notion.com/v1/pages"
headers = {
"Authorization": f"Bearer {NOTION_API_KEY}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
}
# 计算盈亏
market_value, total_cost, pnl_amount, pnl_percent = calculate_pnl(shares, cost, current_price)
# 获取当前时间UTC
now = datetime.utcnow()
date_str = now.strftime("%Y-%m-%dT%H:%M:00Z")
data = {
"parent": {
"database_id": DATABASE_ID
},
"properties": {
"股票代码": {
"title": [
{
"text": {
"content": symbol
}
}
]
},
"公司名称": {
"rich_text": [
{
"text": {
"content": name
}
}
]
},
"持仓数量": {
"number": shares
},
"买入成本": {
"number": cost
},
"当前价格": {
"number": current_price
},
"总成本": {
"number": total_cost
},
"当前市值": {
"number": market_value
},
"盈亏金额": {
"number": round(pnl_amount, 2)
},
"盈亏百分比": {
"number": round(pnl_percent, 2)
},
"最后更新": {
"date": {
"start": date_str
}
},
"备注": {
"rich_text": [
{
"text": {
"content": "自动添加"
}
}
]
}
}
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
result = response.json()
page_id = result["id"]
print(f" ✅ 成功添加 {symbol} - 页面ID: {page_id}")
return page_id
else:
print(f" ❌ 添加失败 {symbol}: {response.status_code}")
print(f" 响应: {response.text}")
return None
def main():
print("开始添加新的美股持仓到Notion...")
print("=" * 60)
for position in positions:
symbol = position["symbol"]
name = position["name"]
shares = position["shares"]
cost = position["cost"]
current_price = position["current_price"]
print(f"\n添加 {symbol} ({name})...")
# 计算盈亏
market_value, total_cost, pnl_amount, pnl_percent = calculate_pnl(shares, cost, current_price)
print(f" 持仓数量: {shares}")
print(f" 买入成本: ${cost:.2f}")
print(f" 当前价格: ${current_price:.2f}")
print(f" 当前市值: ${market_value:.2f}")
print(f" 盈亏: ${pnl_amount:.2f} ({pnl_percent:.2f}%)")
# 添加到Notion
page_id = add_position_to_notion(symbol, name, shares, cost, current_price)
if page_id:
print(f" 📄 页面链接: https://www.notion.so/{page_id.replace('-', '')}")
print("\n" + "=" * 60)
print("所有持仓添加完成!")
print("=" * 60)
if __name__ == "__main__":
main()