fix: 仅买入信号输出仓位+仓位计算公式(RR/大盘/品种)

This commit is contained in:
知微
2026-07-10 00:14:52 +08:00
parent 7026959f58
commit ebc3a15f73
+20 -27
View File
@@ -81,6 +81,9 @@ def collect_data(code):
def build_prompt(data):
"""构建LLM prompt,要求输出完整策略"""
cash = 321271 # 可用现金(从DB读取)
total = 952879 # 总资产
return f"""你是一个资深A股分析师。请对{data['code']} {data.get('name','')}做一个完整的九维矩阵分析,并输出策略参数。
当前数据:
@@ -92,6 +95,8 @@ PE={data.get('pe','?')} 市值={data.get('mcap','?')}亿
当前信号:{data.get('timing_signal','?')} 分类:{data.get('stock_category','?')}
原策略:{(data.get('action','') or '')[:200]}
我的总资产={total}元,可用现金={cash}元。
请严格按以下格式输出:
① 大盘×基本面 [一句话]
@@ -109,8 +114,13 @@ PE={data.get('pe','?')} 市值={data.get('mcap','?')}亿
【买入区间】最低价~最高价
【建议止损】数字
【建议止盈】数字
【建议仓位】总资产的百分之几(如8%),只写数字+%号,不要写文字描述"""
【建议仓位】只有综合结论为"买入"时才输出此项。仓位计算公式:
基础仓位按RR确定:RR<1.5→不推荐,RR1.5~3→8%RR3~5→12%RR5+→15%
大盘偏弱×0.8,大盘偏强×1.15
蓝筹/白马×1.2,成长×0.85,题材/短线×0.6
最终仓位范围:5%~20%
同时考虑:现金{cash}元足够买多少手。输出格式如"8%(约2手,XX元)"""
def parse_response(text):
"""从LLM回复中提取策略参数"""
result = {"signal": "", "entry_low": 0, "entry_high": 0, "stop_loss": 0, "take_profit": 0, "position": ""}
@@ -143,35 +153,18 @@ def parse_response(text):
nums = re.findall(r'[\d.]+', l)
if nums: result["take_profit"] = float(nums[0])
# 仓位提取百分比
for l in text.split("\n"):
if "建议仓位" in l:
nums = re.findall(r'[\d.]+', l)
pct = ""
if nums:
# 仓位:只有买入信号才需要,提取百分比数字
result["position"] = ""
if result["signal"] == "买入":
for l in text.split("\n"):
if "建议仓位" in l:
nums = re.findall(r'[\d.]+', l)
for n in nums:
f = float(n)
if 1 <= f <= 100:
pct = f"{f:.0f}%"
if 1 <= f <= 30: # 合理的仓位范围
result["position"] = f"{f:.0f}%"
break
raw = l.replace("建议仓位","").strip()
if not pct:
if "轻仓" in raw: pct = "3%"
elif "" in raw and "" in raw: pct = "5%"
elif "重仓" in raw: pct = "10%"
elif "清仓" in raw or "零仓" in raw or "不参与" in raw: pct = "0%"
else: pct = "5%"
# 信号与仓位一致性校验
if result["signal"] in ("观望", "卖出"):
pct = "0%"
elif result["signal"] == "关注":
# 关注信号仓位不超过5%
if pct and pct != "0%":
pct_num = int(pct.replace("%",""))
if pct_num > 5:
pct = "5%"
result["position"] = pct
break
break
return result