持仓导入完成:holding.xls→SQLite+portfolio.json全同步

1. 从 ~/stocks/holding.xls 导入25只持仓(14A/11H)
2. 同时写入 portfolio.json + SQLite holdings 表
3. stale_push_wlin 现金来源从 stale_report 改为 portfolio.json
4. portfolio.json 增加 total_assets 字段兼容 stale_detector
5. 导入脚本已规范化为 MoFin/scripts/import_holding_xls.py
   用法:python3 import_holding_xls.py [--cash 金额]
6. 全量策略重评+决策树重建立即执行

Dad下次更新holding.xls后跑:
  cd MoFin && python3 scripts/import_holding_xls.py
This commit is contained in:
知微
2026-06-24 11:21:51 +08:00
parent df4f898bc4
commit e2646c36cb
5 changed files with 414 additions and 158 deletions
+56 -52
View File
@@ -1,19 +1,16 @@
#!/usr/bin/env python3
"""
import_holding_xls.py — 从券商导出文件 holding.xls 导入持仓数据
import_holding_xls.py — 从holding.xls导入持仓到SQLite + portfolio.json
读取 /home/hmo/stocks/holding.xlsTSV格式,GBK编码)
写入 /home/hmo/web-dashboard/data/portfolio.json
触发 per_stock_reassess 全量重评(更新 decisions.json + 决策树)
用法:python3 import_holding_xls.py
用法:python3 import_holding_xls.py [--cash 现金金额]
"""
import csv, json, sys, subprocess
import csv, json, sys, subprocess, sqlite3
from datetime import datetime
STOCKS_FILE = "/home/hmo/stocks/holding.xls"
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
CASH = 80476 # 现金余额(手动更新或从其他源读取)
DB_PATH = "/home/hmo/web-dashboard/data/mofin.db"
CASH = 80476 # default cash
def clean_cell(v):
@@ -26,6 +23,12 @@ 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])
with open(STOCKS_FILE, 'r', encoding='gbk') as f:
reader = csv.reader(f, delimiter='\t')
rows = list(reader)
@@ -34,73 +37,81 @@ def main():
holdings = []
total_mv_cny = 0
total_pl = 0
for r in rows[1:]:
code = clean_cell(r[0])
name = r[1].strip()
shares = int(clean_cell(r[2]))
avail = int(clean_cell(r[3]))
price_raw = r[4].strip()
currency = 'HKD' if '港币' in price_raw or '' in r[10] else 'CNY'
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
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
holdings.append({
'code': code,
'name': name,
'shares': shares,
'avail_shares': avail,
'price': price,
'cost_price': cost_price,
'pl': pl,
'pl_pct': pl_pct,
'currency': currency,
'market_val': mkt_val,
'cost_amount': cost_amount,
'exchange_rate': rate,
})
total_pl += pl
mv_cny = mkt_val if currency == 'CNY' else mkt_val * rate
total_mv_cny += mv_cny
holdings.append({
'code': code, 'name': name, 'shares': shares,
'price': price, 'cost_price': round(cost_price, 2),
'currency': currency, 'market_val': mkt_val,
'cost_amount': cost_amount, 'exchange_rate': rate,
})
pfx = 'HK$' if currency == 'HKD' else ''
print(f" {code} {name} {shares}{pfx}{price:.2f} 盈亏{pl:+,.0f}({pl_pct:+.1f}%)")
print(f" {code} {name} {shares}{pfx}{price:.2f} 成本{cost_price:.2f} 盈亏{pl:+,.0f}({pl_pct:+.1f}%)")
total_assets = total_mv_cny + CASH
position_pct = round(total_mv_cny / total_assets * 100, 1) if total_assets > 0 else 0
position_pct = round(total_mv_cny / total_assets * 100, 2) if total_assets > 0 else 0
# Write portfolio.json
portfolio = {
'holdings': holdings,
'cash': CASH,
'total_market_value': round(total_mv_cny, 2),
'total_pl': round(total_pl, 2),
'total_assets': round(total_assets, 2),
'total_pl': 0,
'position_pct': position_pct,
'updated_at': datetime.now().strftime('%Y-%m-%d %H:%M'),
'source': STOCKS_FILE,
}
with open(PORTFOLIO_PATH, 'w') as f:
json.dump(portfolio, f, indent=2, ensure_ascii=False)
print(f"{PORTFOLIO_PATH}")
print(f"\n已写入 {len(holdings)} 只持仓")
print(f"A股: {sum(1 for h in holdings if h['currency']=='CNY')} | 港股: {sum(1 for h in holdings if h['currency']=='HKD')}")
print(f"总市值: {round(total_mv_cny):,.0f}元 | 现金: {CASH:,}")
print(f"总资产: {round(total_assets):,.0f}元 | 仓位: {position_pct}%")
print(f"累计盈亏: {round(total_pl):+,}")
# 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"],
@@ -108,7 +119,7 @@ def main():
)
print(r.stdout[-500:] if len(r.stdout) > 500 else r.stdout)
# 重建决策树
# Rebuild decision trees
print("\n→ 重建决策树...")
sys.path.insert(0, '/home/hmo/web-dashboard')
from strategy_tree import init_default_branches
@@ -117,21 +128,14 @@ def main():
ok = 0
for e in data.get('decisions', []):
branches = init_default_branches(
e.get('code', ''),
e.get('name', ''),
e.get('entry_low', 0),
e.get('entry_high', 0),
e.get('stop_loss', 0),
e.get('take_profit', 0),
)
e['strategy_tree'] = {
'branches': branches,
'created_at': datetime.now().strftime('%Y-%m-%d'),
}
e.get('code', ''), e.get('name', ''),
e.get('entry_low', 0), e.get('entry_high', 0),
e.get('stop_loss', 0), e.get('take_profit', 0))
e['strategy_tree'] = {'branches': branches, 'created_at': datetime.now().strftime('%Y-%m-%d')}
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"决策树重建: {ok}/{len(data.get('decisions',[]))}")
if __name__ == '__main__':