fix: 仓位改为百分比数字(轻仓3%/中仓5%/重仓10%)

This commit is contained in:
知微
2026-07-09 23:21:26 +08:00
parent 016258bc09
commit 02268405e2
+21 -3
View File
@@ -109,7 +109,7 @@ PE={data.get('pe','?')} 市值={data.get('mcap','?')}亿
【买入区间】最低价~最高价
【建议止损】数字
【建议止盈】数字
【建议仓位】轻仓/中等仓位/重仓(并说明理由)"""
【建议仓位】总资产的百分之几(如8%),只写数字+%号,不要写文字描述"""
def parse_response(text):
"""从LLM回复中提取策略参数"""
@@ -143,10 +143,28 @@ 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:
result["position"] = l.replace("建议仓位","").strip()[:100]
nums = re.findall(r'[\d.]+', l)
pct = ""
if nums:
# 取第一个合理的百分比(1-100之间)
for n in nums:
f = float(n)
if 1 <= f <= 100:
pct = 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: pct = "0%"
else: pct = "5%"
result["position"] = pct
break
return result