持仓导入完成: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:
@@ -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.xls(TSV格式,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__':
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
#!/usr/bin/env python3
|
||||
"""stale_detector.py — 检查所有策略,标记价格偏离/过期的策略
|
||||
|
||||
读取 decisions.json 的扁平列表。自选策略和持仓策略分开判断。
|
||||
可被 cron no_agent 模式调用:stdout 注入到后续 LLM 分析。
|
||||
|
||||
输出格式:
|
||||
[FLAG] [自选/持仓] 股票名(代码) 价XX | 买入A~B | 问题
|
||||
|
||||
用法:
|
||||
python3 stale_detector.py
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
|
||||
|
||||
def fetch_prices(codes):
|
||||
import urllib.request
|
||||
if not codes:
|
||||
return {}
|
||||
symbols, code_map = [], {}
|
||||
for c in codes:
|
||||
c = str(c).strip()
|
||||
p = "sh" if (len(c) == 6 and c[0] in "569") else "sz" if len(c) == 6 else "hk"
|
||||
sym = f"{p}{c}"
|
||||
symbols.append(sym)
|
||||
code_map[sym] = c
|
||||
try:
|
||||
req = urllib.request.Request(
|
||||
f"http://qt.gtimg.cn/q={','.join(symbols)}",
|
||||
headers={"User-Agent": "curl/7.81"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
text = r.read().decode("gbk")
|
||||
except Exception as e:
|
||||
print(f"FETCH_FAIL: {e}", file=sys.stderr)
|
||||
return {}
|
||||
|
||||
results = {}
|
||||
for line in text.strip().split("\n"):
|
||||
if "=" not in line:
|
||||
continue
|
||||
try:
|
||||
raw = line.split("=", 1)[1].strip().strip('"').strip(";")
|
||||
fld = raw.split("~")
|
||||
if len(fld) < 6:
|
||||
continue
|
||||
sym = line.split("=", 1)[0].strip().lstrip("v_")
|
||||
oc = code_map.get(sym)
|
||||
if not oc:
|
||||
continue
|
||||
p = float(fld[3]) if fld[3] else 0
|
||||
c = fld[32] if len(fld) > 32 else "0"
|
||||
results[oc] = (p, c)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
decisions_list = json.load(open(DECISIONS_PATH))
|
||||
if not isinstance(decisions_list, list):
|
||||
decisions_list = decisions_list.get("decisions", []) if isinstance(decisions_list, dict) else []
|
||||
|
||||
# 只保留有买入区的条目,排除已关闭的(inactive/closed)
|
||||
EXCLUDED_STATUSES = ("closed", "inactive")
|
||||
to_check = [d for d in decisions_list if (d.get("entry_low") is not None or d.get("entry_high") is not None) and d.get("status") not in EXCLUDED_STATUSES]
|
||||
if not to_check:
|
||||
print("[SILENT] 无需要检查的策略")
|
||||
return 0
|
||||
|
||||
# ----- 组合级监测:读取总仓位 + 弱势比例 -----
|
||||
position_pct = 0
|
||||
cash = 0
|
||||
total_assets = 0
|
||||
try:
|
||||
with open(PORTFOLIO_PATH) as f:
|
||||
pf = json.load(f)
|
||||
position_pct = pf.get("position_pct", 0)
|
||||
cash = pf.get("cash", 0)
|
||||
total_assets = pf.get("total_assets", 0)
|
||||
except Exception:
|
||||
pass
|
||||
# 统计持仓策略中弱势/深套的比例
|
||||
weak_count = 0
|
||||
holding_count = 0
|
||||
for d in decisions_list:
|
||||
if d.get("type") == "持仓策略" and d.get("status") not in ("closed", "inactive"):
|
||||
holding_count += 1
|
||||
cat = d.get("stock_category", "")
|
||||
if cat in ("弱势", "深套"):
|
||||
weak_count += 1
|
||||
weak_ratio = (weak_count / holding_count * 100) if holding_count > 0 else 0
|
||||
|
||||
prices = fetch_prices([d["code"] for d in to_check])
|
||||
now = datetime.now(timezone.utc).astimezone()
|
||||
found = 0
|
||||
|
||||
for d in to_check:
|
||||
code = d["code"]
|
||||
name = d.get("name", code)
|
||||
el = d.get("entry_low")
|
||||
eh = d.get("entry_high")
|
||||
sl = d.get("stop_loss")
|
||||
tp = d.get("take_profit")
|
||||
ts = d.get("created_at") or d.get("timestamp") or d.get("updated_at", "")
|
||||
is_wl = "自选" in (d.get("type", ""))
|
||||
|
||||
pi = prices.get(code)
|
||||
if not pi:
|
||||
continue
|
||||
price, chg = pi
|
||||
if price <= 0:
|
||||
continue
|
||||
|
||||
issues, flags = [], []
|
||||
tag = "[自选]" if is_wl else "[持仓]"
|
||||
|
||||
# -- 偏离 --
|
||||
if is_wl and el and eh:
|
||||
# 读取 timing_signal 判断策略有效性(timing_signal 字段优先,fallback to action)
|
||||
current_str = d.get("current", "") or ""
|
||||
timing_signal = d.get("timing_signal", "") or current_str
|
||||
has_nonbuy_signal = any(kw in timing_signal for kw in [
|
||||
"等企稳再入", "等企稳", "弱势持有", "观望",
|
||||
"不建议买入", "谨慎买入",
|
||||
])
|
||||
|
||||
# 直接计算 R/R(不依赖文本匹配)
|
||||
rr_invalid = False
|
||||
if sl and sl > 0 and tp and tp > 0 and price > sl:
|
||||
rr = (tp - price) / (price - sl)
|
||||
if rr < 1.5:
|
||||
rr_invalid = True
|
||||
# 也检查 tp 是否接近或低于成本(微盈/浮亏止盈)
|
||||
cost = d.get("cost", 0)
|
||||
if cost and cost > 0 and tp <= cost * 1.05:
|
||||
rr_invalid = True
|
||||
|
||||
strategy_deficient = has_nonbuy_signal or rr_invalid
|
||||
# 对自选无止盈位的也标记(策略不完整)
|
||||
if not tp or tp == 0:
|
||||
strategy_deficient = True
|
||||
|
||||
if el <= price <= eh:
|
||||
flags.append("[WL_IN]")
|
||||
if strategy_deficient:
|
||||
flags.append("[STRATEGY_STALE]")
|
||||
prefix = "⚠️仓位挤占 " if position_pct > 80 else ""
|
||||
issues.append(f"[STRATEGY_STALE] {prefix}价{price:.2f}在买入区{el}~{eh}但策略不完整({'RR='+f'{rr:.2f}<1.5' if rr_invalid else '无止盈位' if not tp else '非买入信号'}),买入区需重评")
|
||||
else:
|
||||
prefix = "⚠️仓位挤占 " if position_pct > 80 else ""
|
||||
issues.append(f"[PUSH] {prefix}价{price:.2f}入买入区{el}~{eh}")
|
||||
elif price > eh * 1.35:
|
||||
flags.append("[WL_HIGH]")
|
||||
issues.append(f"价{price:.2f}高出买入区+{((price/eh)-1)*100:.0f}%,买入区需重评")
|
||||
elif price > eh * 1.20:
|
||||
flags.append("[WL_DRIFT]")
|
||||
issues.append(f"价{price:.2f}高于买入区+{((price/eh)-1)*100:.0f}%")
|
||||
elif not is_wl and eh:
|
||||
dp = (price / eh - 1) * 100
|
||||
if dp > 35:
|
||||
flags.append("[SEVERE]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
elif dp > 20:
|
||||
flags.append("[DRIFT]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
elif dp > 10:
|
||||
flags.append("[WARN]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
# 持仓在买入区内但 R/R 不达标
|
||||
if el and sl and sl > 0 and tp and tp > 0 and price > sl:
|
||||
if el <= price <= eh:
|
||||
rr = (tp - price) / (price - sl)
|
||||
if rr < 1.5:
|
||||
flags.append("[RR_WARN]")
|
||||
issues.append(f"买入区内RR仅{rr:.2f}<1.5,策略需重评")
|
||||
|
||||
# -- 距止损/止盈(仅持仓) --
|
||||
if not is_wl:
|
||||
if sl and sl > 0:
|
||||
dsl = (price / sl - 1) * 100
|
||||
if dsl < 5:
|
||||
flags.append("[NEAR_SL]")
|
||||
issues.append(f"距止损仅{dsl:.1f}%")
|
||||
if tp and tp > 0:
|
||||
dtp = (tp / price - 1) * 100
|
||||
if dtp < 5:
|
||||
# 成本基准校验:止盈标记只有在盈利≥5%时才有效
|
||||
cost_check = True
|
||||
cost = d.get("cost")
|
||||
if cost and cost > 0 and price < cost * 1.05:
|
||||
cost_check = False
|
||||
if cost_check:
|
||||
flags.append("[NEAR_TP]")
|
||||
issues.append(f"距止盈仅{dtp:.1f}%")
|
||||
|
||||
# -- 过期 --
|
||||
stale_limit = 30 if is_wl else 14
|
||||
if ts:
|
||||
try:
|
||||
ud = datetime.fromisoformat(ts)
|
||||
if ud.tzinfo is None:
|
||||
ud = ud.replace(tzinfo=timezone.utc)
|
||||
days = (now - ud).days
|
||||
if days > stale_limit:
|
||||
flags.append("[STALE]")
|
||||
issues.append(f"{days}天未更新(>{stale_limit})")
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
if issues:
|
||||
print(f"{' '.join(flags)} {tag} {name}({code}) 价{price:.2f}{chg} | 买入{el}~{eh} | {'; '.join(issues)}")
|
||||
found += 1
|
||||
|
||||
if found == 0:
|
||||
print("[SILENT] 所有策略正常")
|
||||
|
||||
# ----- 组合级警报 -----
|
||||
portfolio_alerts = 0
|
||||
if holding_count > 0:
|
||||
if weak_ratio > 40:
|
||||
print(f"\n[PORTFOLIO_WEAK] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count})!仓位{position_pct:.1f}% → 建议系统性减仓")
|
||||
portfolio_alerts += 1
|
||||
elif weak_ratio > 30:
|
||||
print(f"\n[PORTFOLIO_WEAK_MILD] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count}),仓位{position_pct:.1f}%,关注")
|
||||
portfolio_alerts += 1
|
||||
if position_pct > 80 and holding_count > 0:
|
||||
# 仓位过满提醒
|
||||
print(f"[PORTFOLIO_FULL] 总仓位{position_pct:.1f}% > 80%,现金{cash:.0f}({cash/total_assets*100:.1f}%)")
|
||||
portfolio_alerts += 1
|
||||
if portfolio_alerts > 0:
|
||||
found += portfolio_alerts
|
||||
|
||||
return found
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user