Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(cashflow): enforce transfer flow signs

dajiaohuangf0d6765c1dc7
diff --git a/README.md b/README.md
index 9e5df8459..c0d4587fd 100644
--- a/README.md
+++ b/README.md
@@ -949,6 +949,8 @@ Beyond bars, `src/entities` ingests irregular dated cash flows (NAVs, capital
calls, coupons) and `cashflow_performance` reports XIRR / MOIC / DPI / TVPI /
TWR / Modified Dietz / MWR over them. This path is deliberately parallel to the
bar engines so a `nav` column can never reach one and get priced as a close.
+Flows use the holder's sign convention: a transfer into the portfolio is
+negative, and a transfer out is positive.
</details>
diff --git a/agent/src/entities/cashflow.py b/agent/src/entities/cashflow.py
index 5fb168a0f..961af38d0 100644
--- a/agent/src/entities/cashflow.py
+++ b/agent/src/entities/cashflow.py
@@ -69,6 +69,10 @@ KIND_DIRECTION: Mapping[str, int] = MappingProxyType(
"capital_call": -1,
"purchase": -1,
"subscription": -1,
+ # A transfer into the portfolio leaves the holder's pocket; a transfer
+ # out of the portfolio reaches it. Keep these aligned with the same
+ # holder-perspective convention as contributions and distributions.
+ "transfer_in": -1,
"fee": -1,
"expense": -1,
# Cash arriving at the holder.
@@ -78,6 +82,7 @@ KIND_DIRECTION: Mapping[str, int] = MappingProxyType(
"interest": 1,
"principal": 1,
"redemption": 1,
+ "transfer_out": 1,
"proceeds": 1,
}
)
diff --git a/agent/src/tools/cashflow_analytics_tool.py b/agent/src/tools/cashflow_analytics_tool.py
index edba480a9..c7d397dfd 100644
--- a/agent/src/tools/cashflow_analytics_tool.py
+++ b/agent/src/tools/cashflow_analytics_tool.py
@@ -84,8 +84,9 @@ class CashFlowPerformanceTool(BaseTool):
"description": (
"Inline cash flows. Amounts are from the client's point of "
"view: a contribution is NEGATIVE (cash leaves the client) "
- "and a distribution is POSITIVE. Mutually exclusive with "
- "flows_path."
+ "and a distribution is POSITIVE. A transfer into the "
+ "portfolio is NEGATIVE; a transfer out is POSITIVE. "
+ "Mutually exclusive with flows_path."
),
"items": {
"type": "object",
diff --git a/agent/tests/test_cashflow_analytics_tool.py b/agent/tests/test_cashflow_analytics_tool.py
index 0fdca22c9..364d63b50 100644
--- a/agent/tests/test_cashflow_analytics_tool.py
+++ b/agent/tests/test_cashflow_analytics_tool.py
@@ -178,6 +178,29 @@ def test_invalid_input_returns_an_error_envelope(
assert fragment in payload["error"]
+def test_transfer_into_the_portfolio_rejects_the_wrong_holder_sign() -> None:
+ """A positive portfolio inflow is not accepted as a holder cash inflow."""
+ payload = json.loads(
+ CashFlowPerformanceTool().execute(
+ valuations=[
+ {"date": "2024-01-01", "value": 100.0},
+ {"date": "2024-12-31", "value": 200.0},
+ ],
+ flows=[
+ {
+ "date": "2024-07-01",
+ "amount": 100.0,
+ "kind": "transfer_in",
+ "currency": "USD",
+ }
+ ],
+ )
+ )
+
+ assert payload["status"] == "error"
+ assert "negative (cash out)" in payload["error"]
+
+
def test_reclassifying_a_custom_kind_makes_it_external() -> None:
"""The escape hatch from the unclassified-kind error actually works."""
payload = json.loads(
diff --git a/agent/tests/test_entities_spine.py b/agent/tests/test_entities_spine.py
index d721d387b..744a1a193 100644
--- a/agent/tests/test_entities_spine.py
+++ b/agent/tests/test_entities_spine.py
@@ -129,6 +129,21 @@ def test_sign_convention_rejects_negative_distribution():
CashFlow(date=date(2024, 1, 1), amount=-500.0, kind="distribution", currency="USD")
+ ("kind", "amount", "direction"),
+ [("transfer_in", 100.0, "negative"), ("transfer_out", -100.0, "positive")],
+)
+def test_portfolio_transfer_signs_follow_holder_perspective(kind, amount, direction):
+ with pytest.raises(ValueError, match=direction):
+ CashFlow(date=date(2024, 1, 1), amount=amount, kind=kind, currency="USD")
+
+
[email protected](("kind", "amount"), [("transfer_in", -100.0), ("transfer_out", 100.0)])
+def test_portfolio_transfer_signs_accept_holder_perspective(kind, amount):
+ flow = CashFlow(date=date(2024, 1, 1), amount=amount, kind=kind, currency="USD")
+ assert flow.amount == amount
+
+
def test_sign_convention_applies_after_kind_normalization():
"""'Capital Call' must be checked as capital_call, not treated as custom."""
with pytest.raises(ValueError, match="negative"):