"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): read a dash-form trading day as a date, and mask both bounds of a ranged level
Haozhe Wub22c974e47c2
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.pyindex e3c4feded..3f7dc89bd 100644--- a/agent/src/agent/grounding.py+++ b/agent/src/agent/grounding.py@@ -214,9 +214,30 @@ _DATE_RE = re.compile(r"\b(?:19|20)\d{2}[-/]\d{1,2}[-/]\d{1,2}\b", re.ASCII)_SHORT_DATE_RE = re.compile(r"(?<![\d/])(?:0?[1-9]|1[0-2])/(?:0?[1-9]|[12]\d|3[01])(?![\d/])")+# A report writes a trading day as "08-10(一)" or "08-10盘中", and the dash form+# leaked 8 and 10 as candidate prices exactly as "8/5" once did. The dash is+# NOT symmetric with the slash, though: it also separates a range, and "目标价+# 10-20 元" must stay checkable. So the two halves are split -- a zero-padded+# month (01-09) is a formatting intent no price range imitates, while 10/11/12+# have to carry a weekday or session marker to read as a date.+_DASH_DATE_RE = re.compile(+ r"(?<![\d/-])(?:"+ r"0[1-9]-(?:0[1-9]|[12]\d|3[01])"+ r"|1[0-2]-(?:0[1-9]|[12]\d|3[01])"+ r"(?=\s*(?:[((]\s*(?:周|星期)?[一二三四五六日天]\s*[))]"+ r"|盘中|盘后|盘前|收盘|开盘|最低|最高"+ r"|\s*(?:close|open|intraday|low|high)\b))"+ r")(?![\d/-])",+ re.IGNORECASE,+)# A percentage range masks only its upper bound through the "%" tail check# below, because the sign touches the second number: "1–2%" left 1 behind# (#983). Mask the span as a whole.+# A level stated as a RANGE has the same shape: the separator touches the+# second number, so masking "目标价 10" left "-20" behind and a negative price+# matches no OHLC window at all -- a guaranteed rejection of a correct draft.+# The tail is optional, so a single-value level is unaffected.+_RANGE_TAIL = r"(?:\s*[-–—~~至到]\s*[-+]?\d[\d,]*(?:\.\d+)?)?"_PERCENT_RANGE_RE = re.compile(r"\d[\d,]*(?:\.\d+)?\s*[-–—~至]\s*\d[\d,]*(?:\.\d+)?\s*[%%]")@@ -302,7 +323,7 @@ _REFERENCE_LEVEL_RE = re.compile(r"上市以来(?:最高|最低)(?:点|位|价)?"r")"r"\s*(?:of|为|是|约|at)?\s*[::]?\s*\(?"- r"\s*" + _CURRENCY_TOKEN + r"\s*[-+]?\d[\d,]*(?:\.\d+)?",+ r"\s*" + _CURRENCY_TOKEN + r"\s*[-+]?\d[\d,]*(?:\.\d+)?" + _RANGE_TAIL,re.IGNORECASE,)# A date-anchored reference puts the historical extreme after the number:@@ -368,18 +389,19 @@ _PROSPECTIVE_LEVEL_RE = re.compile(r"(?:"# (a) comparison operator immediately before the numberr"(?:>=|<=|≥|≤|>|<|大于|小于|不低于|不高于|高于|低于)"- r"\s*" + _CURRENCY_TOKEN + r"\s*[-+]?\d[\d,]*(?:\.\d+)?"- r"|"+ r"\s*" + _CURRENCY_TOKEN + r"\s*[-+]?\d[\d,]*(?:\.\d+)?" + _RANGE_TAIL+ + r"|"# (b) a level marker introducing the numberr"(?:目标位|目标区|目标价|均值目标(?:价)?|平均目标(?:价)?|止损位?|止盈位?|触发价|触发位|触发点|上看|下看|"r"支撑(?:阶梯|位|线)?|阻力(?:位|线)?|压力位|压力线|support(?:\s+(?:level|line|zone))?|"r"resistance(?:\s+(?:level|line|zone))?|target\s+(?:price|level|zone)|trigger|stop[-\s]?loss|take[-\s]?profit)"r"\s*(?:为|是|至|到|on|at|of|=)?\s*[::]?\s*"+ _CURRENCY_TOKEN- + r"\s*[-+]?\d[\d,]*(?:\.\d+)?"- r"|"+ + r"\s*[-+]?\d[\d,]*(?:\.\d+)?" + _RANGE_TAIL+ + r"|"# (c) the number followed by a level marker- r"" + _CURRENCY_TOKEN + r"[-+]?\d[\d,]*(?:\.\d+)?\s*(?:一线|附近)?\s*(?:成为?|作为|是)?\s*"+ r"" + _CURRENCY_TOKEN + r"[-+]?\d[\d,]*(?:\.\d+)?" + _RANGE_TAIL+ + r"\s*(?:一线|附近)?\s*(?:成为?|作为|是)?\s*"r"(?:目标区|目标位|止损位|止盈位)"r"|"# (d) a conditional opener before the number, digits fencing the reach@@ -2342,6 +2364,7 @@ class GroundingLedger:masked = _LOCALIZED_DATE_RE.sub(" ", masked)masked = _DATE_RE.sub(" ", masked)masked = _SHORT_DATE_RE.sub(" ", masked)+ masked = _DASH_DATE_RE.sub(" ", masked)masked = _PERCENT_RANGE_RE.sub(" ", masked)masked = _AGGREGATE_AMOUNT_RE.sub(" ", masked)masked = _LABELLED_SCORE_RE.sub(" ", masked)diff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.pyindex f0cb34000..21f6fe3ef 100644--- a/agent/tests/test_agent_grounding.py+++ b/agent/tests/test_agent_grounding.py@@ -2160,3 +2160,47 @@ def test_in_text_decimal_survives_list_marker_mask():result = ledger.validate_final_answer(draft)assert result.valid is True, (draft, result.issues)+++def test_dash_form_trading_day_is_a_date_but_a_price_range_is_not() -> None:+ """A report writes the day as "08-10(一)"; a target still writes "10-20 元".++ The dash carries both meanings, so the mask splits them: a zero-padded+ month reads as a date on its own, October through December need a weekday+ or session marker, and a range with neither stays checkable.+ """+ extract = GroundingLedger._numbers_without_dates_or_percent++ assert extract("08-10(一) 收盘 8.20 CNY") == [8.2]+ assert extract("08-10盘中最低 8.20 CNY") == [8.2]+ assert extract("08-10 close 8.20 USD") == [8.2]+ assert extract("10-20(周一)盘中 8.20 CNY") == [8.2]++ # No date marker and no zero-padded month: an ordinary quoted range, and+ # masking it would stop checking a claim the ledger exists to check.+ assert extract("区间 8-10 元") == [8.0, 10.0]+ assert extract("跌 12-25 元") == [12.0, 25.0]+++def test_a_level_stated_as_a_range_masks_both_bounds(tmp_path: Path) -> None:+ """Masking only the lower bound left a negative upper bound behind.++ The separator touches the second number, so "目标价 10-20 元" used to+ reduce to "-20" — and a negative price is outside every OHLC window, which+ made a correct draft impossible to pass rather than merely unchecked.+ """+ extract = GroundingLedger._numbers_without_dates_or_percent++ for draft in ("目标价 10-20 元", "支撑位 8-10 元", "阻力位 12.0~13.5", "52周高 15.0-16.0"):+ assert extract(draft) == [], draft++ # A single-value level is unchanged, and an observed quote beside a ranged+ # level is still extracted and checked.+ assert extract("止损位 7.5 元") == []+ assert extract("目标价 10-20 元,现价 8.20 CNY") == [8.2]++ ledger = _screened_ledger(tmp_path)+ result = ledger.validate_final_answer(+ "000543.SZ 收盘价 8.20 CNY,目标价 10-20 元(source: tencent)"+ )+ assert result.valid is True, result.issues