Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(financials): derive fiscal Q4 only for additive units, not share counts or EPS

Rudy Celeklib384e272826b
diff --git a/agent/src/tools/financial_statements_tool.py b/agent/src/tools/financial_statements_tool.py
index 2031473d1..cf4e0e324 100644
--- a/agent/src/tools/financial_statements_tool.py
+++ b/agent/src/tools/financial_statements_tool.py
@@ -553,6 +553,17 @@ def _record_sec_fact(
record["_units"][concept_name] = unit_key
+def _is_additive_unit(unit: Any) -> bool:
+ """Whether a fact in ``unit`` sums across quarters to the full year.
+
+ Currency flows do. A weighted-average share count (``shares``) is an
+ average over its own period, and a per-share figure (``USD/shares``) is a
+ ratio; quarterly EPS need not sum to the annual EPS. Subtracting three
+ quarters from the year is meaningless for both, so they are not derived.
+ """
+ return isinstance(unit, str) and unit != "shares" and "/" not in unit
+
+
def _synthesize_fiscal_q4(
frames: dict[tuple[Any, Any], dict[str, Any]],
annual_frames: dict[tuple[Any, Any], dict[str, Any]],
@@ -590,8 +601,8 @@ def _synthesize_fiscal_q4(
continue
values = {
concept: annual[concept] - sum(quarter[concept] for quarter in inside)
- for concept in annual["_units"]
- if all(concept in quarter for quarter in inside)
+ for concept, unit in annual["_units"].items()
+ if _is_additive_unit(unit) and all(concept in quarter for quarter in inside)
}
if not values:
continue
diff --git a/agent/tests/test_sec_period_frames.py b/agent/tests/test_sec_period_frames.py
index 5cfced2f4..900a3fd76 100644
--- a/agent/tests/test_sec_period_frames.py
+++ b/agent/tests/test_sec_period_frames.py
@@ -249,3 +249,45 @@ class TestSecFilingsMetricPoints:
"Revenues", 10)["points"]
ends = [p["end"] for p in points]
assert ends == sorted(ends)
+
+
+class TestDerivedQ4NonAdditiveUnits:
+ """Only additive currency flows are derived as FY - (Q1 + Q2 + Q3)."""
+
+ _QUARTERS = (
+ ("2022-09-25", "2022-12-31", "Q1", "q1", "2023-02-03"),
+ ("2023-01-01", "2023-04-01", "Q2", "q2", "2023-05-05"),
+ ("2023-04-02", "2023-07-01", "Q3", "q3", "2023-08-04"),
+ )
+
+ def _concept(self, unit, quarter_values, annual_value):
+ rows = [
+ _row(start, end, value, fy=2023, fp=fp, form="10-Q", accn=accn, filed=filed)
+ for (start, end, fp, accn, filed), value in zip(self._QUARTERS, quarter_values)
+ ]
+ rows.append(
+ _row("2022-09-25", "2023-09-30", annual_value, fy=2023, fp="FY", form="10-K",
+ accn="k23", filed="2023-11-03")
+ )
+ return {"units": {unit: rows}}
+
+ def test_share_counts_and_eps_are_not_derived(self):
+ facts = {
+ "facts": {
+ "us-gaap": {
+ "NetIncomeLoss": self._concept("USD", [30.0e9, 24.2e9, 19.9e9], 97.0e9),
+ "WeightedAverageNumberOfDilutedSharesOutstanding": self._concept(
+ "shares", [15.96e9, 15.85e9, 15.78e9], 15.81e9
+ ),
+ "EarningsPerShareDiluted": self._concept(
+ "USD/shares", [1.88, 1.52, 1.26], 6.13
+ ),
+ }
+ }
+ }
+ periods = _fetch(facts, statement="indicators", period="quarter")
+ q4 = next(p for p in periods if p["REPORT_DATE"] == "2023-09-30")
+ assert q4["DERIVED"] == "FY - (Q1 + Q2 + Q3)"
+ assert q4["NetIncomeLoss"] == 97.0e9 - (30.0e9 + 24.2e9 + 19.9e9)
+ assert "WeightedAverageNumberOfDilutedSharesOutstanding" not in q4
+ assert "EarningsPerShareDiluted" not in q4