import_holding_xls: 支持截图真实数字覆盖
regenerate_all 会覆盖 portfolio.json,所以导入流程改为: 1. 先更新 SQLite holdings 表 2. 再跑 regenerate_all(读 SQLite,写 decisions+portfolio) 3. 然后用真实数字覆盖 portfolio.json 汇总字段 4. 重建决策树 支持 --cash --total --mv 传入截图真实数字 用法:python3 import_holding_xls.py --cash 20230 --total 1008860 --mv 988512
This commit is contained in:
@@ -1,16 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
import_holding_xls.py — 从holding.xls导入持仓到SQLite + portfolio.json
|
||||
import_holding_xls.py — 从 holding.xls 导入持仓到全系统
|
||||
|
||||
用法:python3 import_holding_xls.py [--cash 现金金额]
|
||||
用法:
|
||||
python3 import_holding_xls.py [--cash 现金] [--total 总资产] [--mv 市值]
|
||||
|
||||
不传 --total/--mv 则从 holding.xls 计算(可能有价格时差误差)。
|
||||
建议传截图上的真实数字。
|
||||
|
||||
示例:
|
||||
python3 import_holding_xls.py --cash 20230.10 --total 1008860.62 --mv 988512.96
|
||||
"""
|
||||
import csv, json, sys, subprocess, sqlite3
|
||||
import csv, json, sys, subprocess, sqlite3, os
|
||||
from datetime import datetime
|
||||
|
||||
STOCKS_FILE = "/home/hmo/stocks/holding.xls"
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
DB_PATH = "/home/hmo/web-dashboard/data/mofin.db"
|
||||
CASH = 80476 # default cash
|
||||
|
||||
|
||||
def clean_cell(v):
|
||||
@@ -24,10 +30,18 @@ def clean_cell(v):
|
||||
|
||||
def main():
|
||||
# Parse args
|
||||
global CASH
|
||||
for i, a in enumerate(sys.argv[1:]):
|
||||
if a == '--cash' and i + 2 < len(sys.argv):
|
||||
CASH = float(sys.argv[i + 2])
|
||||
cash = 20230.10
|
||||
total_assets = 0
|
||||
market_value = 0
|
||||
|
||||
args = sys.argv[1:]
|
||||
for i, a in enumerate(args):
|
||||
if a == '--cash' and i + 1 < len(args):
|
||||
cash = float(args[i + 1])
|
||||
elif a == '--total' and i + 1 < len(args):
|
||||
total_assets = float(args[i + 1])
|
||||
elif a == '--mv' and i + 1 < len(args):
|
||||
market_value = float(args[i + 1])
|
||||
|
||||
with open(STOCKS_FILE, 'r', encoding='gbk') as f:
|
||||
reader = csv.reader(f, delimiter='\t')
|
||||
@@ -47,13 +61,11 @@ def main():
|
||||
price_str = price_raw.replace('港币', '').replace('港元', '').replace('港', '').strip()
|
||||
price = float(price_str)
|
||||
cost_price = float(clean_cell(r[5]))
|
||||
pl = float(clean_cell(r[6]))
|
||||
pl_pct = float(clean_cell(r[7])) if r[7].strip() else 0.0
|
||||
pl = float(clean_cell(r[6])) if r[6].strip() else 0
|
||||
mkt_val = float(clean_cell(r[11]))
|
||||
cost_amount = float(clean_cell(r[15])) if r[15].strip() and r[15].strip() != '--' else 0
|
||||
rate_str = clean_cell(r[16])
|
||||
rate = float(rate_str) if rate_str and rate_str != '--' else 0.866
|
||||
|
||||
rate = float(rate_str) if rate_str and rate_str != '--' else 0.8664
|
||||
mv_cny = mkt_val if currency == 'CNY' else mkt_val * rate
|
||||
total_mv_cny += mv_cny
|
||||
|
||||
@@ -64,17 +76,51 @@ def main():
|
||||
'cost_amount': cost_amount, 'exchange_rate': rate,
|
||||
})
|
||||
|
||||
pfx = 'HK$' if currency == 'HKD' else '¥'
|
||||
print(f" {code} {name} {shares}股 {pfx}{price:.2f} 成本{cost_price:.2f} 盈亏{pl:+,.0f}({pl_pct:+.1f}%)")
|
||||
# Use provided values or calculate
|
||||
if total_assets <= 0:
|
||||
total_assets = total_mv_cny + cash
|
||||
if market_value <= 0:
|
||||
market_value = round(total_mv_cny, 2)
|
||||
|
||||
position_pct = round(market_value / total_assets * 100, 2) if total_assets > 0 else 0
|
||||
|
||||
total_assets = total_mv_cny + CASH
|
||||
position_pct = round(total_mv_cny / total_assets * 100, 2) if total_assets > 0 else 0
|
||||
# Step 1: Update SQLite (regenerate_all reads from here)
|
||||
print("\n→ 更新 SQLite holdings 表...")
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
c.execute('DELETE FROM holdings')
|
||||
c.execute('DELETE FROM portfolio_summary')
|
||||
for h in holdings:
|
||||
c.execute('''
|
||||
INSERT INTO holdings (code, name, shares, cost, position_pct, added_at, is_active)
|
||||
VALUES (?, ?, ?, ?, ?, ?, 1)
|
||||
''', (h['code'], h['name'], h['shares'], h['cost_price'],
|
||||
round(h['market_val'] / total_assets * 100, 2),
|
||||
datetime.now().strftime('%Y-%m-%d')))
|
||||
c.execute('''
|
||||
INSERT INTO portfolio_summary (total_assets, stock_value, cash, position_pct, total_pnl, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
''', (round(total_assets, 2), round(market_value, 2), cash,
|
||||
position_pct, 0, datetime.now().strftime('%Y-%m-%d %H:%M')))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print(f" OK - {len(holdings)} 只持仓")
|
||||
|
||||
# Write portfolio.json
|
||||
# Step 2: Run full reassessment (reads SQLite, writes decisions.json + portfolio.json)
|
||||
print("\n→ 全量策略重评...")
|
||||
subprocess.run(
|
||||
["python3", "/home/hmo/.hermes/profiles/position-analyst/scripts/per_stock_reassess.py"],
|
||||
capture_output=True, text=True, timeout=120
|
||||
)
|
||||
print(f" 完成")
|
||||
|
||||
# Step 3: Overwrite portfolio.json with correct aggregate numbers
|
||||
# (regenerate_all writes its own format, we fix it back)
|
||||
print("\n→ 修正 portfolio.json 汇总数据...")
|
||||
portfolio = {
|
||||
'holdings': holdings,
|
||||
'cash': CASH,
|
||||
'total_market_value': round(total_mv_cny, 2),
|
||||
'cash': cash,
|
||||
'total_market_value': market_value,
|
||||
'total_assets': round(total_assets, 2),
|
||||
'total_pl': 0,
|
||||
'position_pct': position_pct,
|
||||
@@ -83,43 +129,8 @@ def main():
|
||||
}
|
||||
with open(PORTFOLIO_PATH, 'w') as f:
|
||||
json.dump(portfolio, f, indent=2, ensure_ascii=False)
|
||||
print(f" → {PORTFOLIO_PATH}")
|
||||
|
||||
# Write SQLite
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
c.execute('DELETE FROM holdings')
|
||||
c.execute('DELETE FROM portfolio_summary')
|
||||
for h in holdings:
|
||||
pos_pct = round(h['market_val'] / total_assets * 100, 2) if total_assets > 0 else 0
|
||||
c.execute('''
|
||||
INSERT INTO holdings (code, name, shares, cost, position_pct, added_at, is_active)
|
||||
VALUES (?, ?, ?, ?, ?, ?, 1)
|
||||
''', (h['code'], h['name'], h['shares'], h['cost_price'], pos_pct,
|
||||
datetime.now().strftime('%Y-%m-%d')))
|
||||
c.execute('''
|
||||
INSERT INTO portfolio_summary (total_assets, stock_value, cash, position_pct, total_pnl, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
''', (round(total_assets, 2), round(total_mv_cny, 2), CASH, position_pct, 0,
|
||||
datetime.now().strftime('%Y-%m-%d %H:%M')))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print(f" → SQLite 已更新")
|
||||
|
||||
print(f"\n统计: {len(holdings)}只持仓 | "
|
||||
f"总资产{round(total_assets):,.0f}元 | "
|
||||
f"现金{CASH:,.0f}元 | "
|
||||
f"仓位{position_pct}%")
|
||||
|
||||
# Trigger full reassessment
|
||||
print("\n→ 触发 per_stock_reassess 全量重评...")
|
||||
r = subprocess.run(
|
||||
["python3", "/home/hmo/.hermes/profiles/position-analyst/scripts/per_stock_reassess.py"],
|
||||
capture_output=True, text=True, timeout=120
|
||||
)
|
||||
print(r.stdout[-500:] if len(r.stdout) > 500 else r.stdout)
|
||||
|
||||
# Rebuild decision trees
|
||||
# Step 4: Rebuild decision trees
|
||||
print("\n→ 重建决策树...")
|
||||
sys.path.insert(0, '/home/hmo/web-dashboard')
|
||||
from strategy_tree import init_default_branches
|
||||
@@ -135,7 +146,14 @@ def main():
|
||||
ok += 1
|
||||
with open('/home/hmo/web-dashboard/data/decisions.json', 'w') as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
print(f"决策树重建: {ok}/{len(data.get('decisions',[]))}")
|
||||
|
||||
print(f"\n{'='*50}")
|
||||
print(f"导入完成:{len(holdings)}只持仓")
|
||||
print(f"总资产: {round(total_assets):,.0f}元")
|
||||
print(f"市值: {market_value:,.0f}元")
|
||||
print(f"现金: {cash:,.0f}元")
|
||||
print(f"仓位: {position_pct}%")
|
||||
print(f"决策树: {ok}/{len(data.get('decisions',[]))}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user