Skip to content

TradingAgents: Multi-Agents LLM Financial Trading Framework

fix(rating): read a free-text rating from the decision's own rating line (#1383)

Yijia-Xiaob690dc79883f
diff --git a/tests/test_rating_integrity.py b/tests/test_rating_integrity.py
index 8fcadac4a..dc3c4373d 100644
--- a/tests/test_rating_integrity.py
+++ b/tests/test_rating_integrity.py
@@ -218,3 +218,29 @@ def test_a_state_without_the_typed_rating_reads_it_from_the_decision():
assert run_rating({"final_rating": "Hold", "final_trade_decision": "**Rating**: Buy"}) == "Hold"
assert run_rating({"final_trade_decision": "**Rating**: Sell\n\nExit."}) == "Sell"
assert run_rating({}) == RATING_REVIEW
+
+
[email protected]("quoted", [
+ "Street consensus rating: Buy (28 of 35 analysts).",
+ "Moody's affirmed the credit rating: Buy-side demand for the bonds stayed firm.",
+ "Operating margin: Sell-side estimates sit below guidance.",
+])
+def test_a_rating_the_text_quotes_does_not_replace_the_decision(quoted):
+ """A free-text decision opens with its own rating line; a rating it quotes
+ as evidence, or a word merely ending in 'rating', is not the call."""
+ text = f"**Rating**: Hold\n\n**Investment Thesis**: {quoted} We wait for margins."
+ assert extract_rating(text) == "Hold"
+
+
[email protected]("quoted", [
+ "- Rating: Buy (Goldman Sachs, 12m target 180)",
+ "| Rating: Buy | Morgan Stanley |",
+ "> Rating: Buy, per the sell-side note",
+ "Street consensus rating: Buy",
+ "Consensus rating: Buy (28 of 35 analysts)",
+])
+def test_a_quoted_rating_in_a_list_table_or_quote_is_not_the_decision(quoted):
+ text = f"Our rating: Hold\n\nWhat others say:\n{quoted}\n\nWe wait for margins."
+ assert extract_rating(text) == "Hold"
diff --git a/tradingagents/agents/rating.py b/tradingagents/agents/rating.py
index 37a6db8d6..16d7689dd 100644
--- a/tradingagents/agents/rating.py
+++ b/tradingagents/agents/rating.py
@@ -32,10 +32,20 @@ RATING_REVIEW = "REVIEW"
_RATING_SET = {r.lower() for r in RATINGS_5_TIER}
# Matches "Rating: X" / "rating - X" / "Rating — **X**" — tolerates markdown
-# bold wrappers and any dash or colon a model writes as the separator.
-_RATING_LABEL_RE = re.compile(r"rating\b[^:\-\u2010-\u2015]*[:\-\u2010-\u2015][\s*]*(\w+)",
+# bold wrappers and any dash or colon a model writes as the separator. "rating"
+# must start a word, so "Operating margin: Sell-side" is not a label.
+_RATING_LABEL_RE = re.compile(r"(?<![a-z])rating\b[^:\-\u2010-\u2015]*[:\-\u2010-\u2015][\s*]*(\w+)",
re.IGNORECASE)
+# The same label opening its own line ("**Rating**: X", "## Final Rating - X",
+# "Our rating: X"): the shape the Portfolio Manager is asked to write its
+# decision in. Only emphasis and heading marks may precede it, so a list item,
+# table row or blockquote quoting someone else's rating is not one.
+_RATING_LINE_RE = re.compile(
+ r"[\s*_#]*(?:\w+\s+)?rating[^\w:\-\u2010-\u2015]*[:\-\u2010-\u2015][\s*]*(\w+)",
+ re.IGNORECASE,
+)
+
# A line presenting the scale rather than a decision ("Rating Scale: Buy, ...").
_RATING_SCALE_RE = re.compile(r"rating\s*(scale|options|legend)", re.IGNORECASE)
@@ -50,25 +60,31 @@ def extract_rating(text: str) -> str | None:
Two-pass strategy on the NFKC-normalized text (so fullwidth punctuation like
``Rating:Overweight`` is matched the same as ASCII):
- 1. An explicit "Rating: X" label (tolerant of markdown bold).
- 2. The first standalone 5-tier rating word found anywhere.
+ 1. An explicit "Rating: X" label (tolerant of markdown bold): the first one
+ opening its own line, else the last one anywhere.
+ 2. A single 5-tier rating word, when the text names only one.
"""
if not text:
return None
norm = unicodedata.normalize("NFKC", text)
- # The labelled rating, taking the last one written: a decision states its
- # rating after discussing the alternatives. Lines presenting the scale
- # itself are a legend the model echoed, not a call.
- labelled = None
+ # A decision is asked to open with its rating on its own line, so the first
+ # such line is the call; later ones may quote someone else's ("Consensus
+ # rating: Buy"). Without one, the last label anywhere wins: prose states its
+ # rating after discussing the alternatives. Lines presenting the scale itself
+ # are a legend the model echoed, not a call.
+ on_own_line = anywhere = None
for line in norm.splitlines():
if _RATING_SCALE_RE.search(line):
continue
+ m = _RATING_LINE_RE.match(line)
+ if on_own_line is None and m and m.group(1).lower() in _RATING_SET:
+ on_own_line = m.group(1).capitalize()
m = _RATING_LABEL_RE.search(line)
if m and m.group(1).lower() in _RATING_SET:
- labelled = m.group(1).capitalize()
- if labelled:
- return labelled
+ anywhere = m.group(1).capitalize()
+ if on_own_line or anywhere:
+ return on_own_line or anywhere
# No label. A single rating word in the text is the call; several are an
# argument, and picking one of them reports a direction nobody decided --