Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(financial-rigor): report undefined three-scenario upside instead of 0% at a zero price (#1921)

Rudy Mizrahi Celekli3c9aa782304f
diff --git a/agent/src/tools/financial_rigor_tool.py b/agent/src/tools/financial_rigor_tool.py
index 984e928b1..2c8af2573 100644
--- a/agent/src/tools/financial_rigor_tool.py
+++ b/agent/src/tools/financial_rigor_tool.py
@@ -402,9 +402,15 @@ def three_scenario_valuation(
Returns:
Dict with the assumptions and a ``scenarios`` list, each carrying its
- ``future_eps``, ``target_price`` and ``upside_pct``.
+ ``future_eps``, ``target_price`` and ``upside_pct`` (``None`` when the
+ current price is not positive).
+
+ Raises:
+ ValueError: If ``years`` is negative.
"""
p, eps, shares = _exact(current_price), _exact(current_eps), _exact(shares_billion)
+ if int(years) < 0:
+ raise ValueError(f"years must be >= 0, got {years!r}")
spec = [
("bull", growth_optimistic, pe_optimistic),
("base", growth_neutral, pe_neutral),
@@ -423,14 +429,16 @@ def three_scenario_valuation(
for _ in range(int(years)):
future_eps = _CTX.multiply(future_eps, _CTX.add(Decimal("1"), g))
target_price = _CTX.multiply(future_eps, target_pe)
- upside = float(target_price - p) / float(p) * 100 if p != 0 else 0.0
+ # Upside relative to a non-positive price is undefined; report null
+ # rather than a 0% that reads as "fairly valued".
+ upside = float(target_price - p) / float(p) * 100 if p > 0 else None
scenarios.append({
"scenario": name,
"annual_growth": float(g),
"target_pe": float(target_pe),
"future_eps": float(future_eps),
"target_price": float(target_price),
- "upside_pct": round(upside, 2),
+ "upside_pct": round(upside, 2) if upside is not None else None,
})
result: dict[str, Any] = {
"current_price": float(p),
diff --git a/agent/tests/test_financial_rigor_three_scenario_undefined_upside.py b/agent/tests/test_financial_rigor_three_scenario_undefined_upside.py
new file mode 100644
index 000000000..5d8e6b6e7
--- /dev/null
+++ b/agent/tests/test_financial_rigor_three_scenario_undefined_upside.py
@@ -0,0 +1,48 @@
+"""three_scenario must not report 0% upside against a zero price, or 0 years for -3."""
+
+from __future__ import annotations
+
+import json
+
+import pytest
+
+from src.tools.financial_rigor_tool import FinancialRigorTool, three_scenario_valuation
+
+
+def _valuation(price, years=3):
+ return three_scenario_valuation(
+ current_price=price, current_eps=5, shares_billion=1,
+ growth_optimistic=0.2, growth_neutral=0.1, growth_pessimistic=0.0,
+ pe_optimistic=20, pe_neutral=15, pe_pessimistic=10, years=years,
+ )
+
+
[email protected]("price", [0, -10])
+def test_non_positive_price_reports_null_upside(price) -> None:
+ out = _valuation(price)
+ assert [s["upside_pct"] for s in out["scenarios"]] == [None, None, None]
+ assert out["scenarios"][2]["target_price"] == 50.0
+ json.dumps(out, allow_nan=False)
+
+
+def test_positive_price_keeps_its_upside() -> None:
+ out = _valuation(50)
+ assert [s["upside_pct"] for s in out["scenarios"]] == [245.6, 99.65, 0.0]
+
+
+def test_negative_years_is_an_error_envelope() -> None:
+ with pytest.raises(ValueError, match="years"):
+ _valuation(50, years=-3)
+ payload = json.loads(
+ FinancialRigorTool().execute(
+ command="three_scenario", price=50, eps=5, shares=1,
+ growth=[0.2, 0.1, 0.0], pe=[20, 15, 10], years=-3,
+ )
+ )
+ assert payload["status"] == "error"
+ assert "years" in payload["error"]
+
+
+def test_zero_years_is_still_allowed() -> None:
+ out = _valuation(50, years=0)
+ assert out["scenarios"][1]["future_eps"] == 5.0