fix: Portfolio NAV snapshot understates total_net_gain by ~3x (#397)#399
Merged
Conversation
… by ~3x (#397) Bug 1: In create_order_metadata_with_trade_context, the cost variable accumulated across loop iterations but the entire accumulated value was subtracted from each trade's revenue. With N trades, earlier costs were subtracted N times instead of once, systematically understating net_gain. Fix: use per-iteration trade_cost variable. Bug 2: In _create_trade_metadata_with_sell_order_and_trades, net_gain used sell_amount (total sell order) instead of trade_data['amount'] (per-trade portion), overcounting gain per trade when a sell order was split across multiple trades. Fix: use trade_data['amount']. Added 3 regression tests in tests/services/test_trade_service_net_gain.py.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #397
Fixes portfolio NAV snapshot
total_net_gainbeing understated by ~3x due to two bugs in the trade service's sell order accounting.Root Cause
Bug 1 (Primary): Accumulated cost double-counting
In
create_order_metadata_with_trade_context, when a sell order closes multiple trades, thecostvariable accumulated across loop iterations but the entire accumulated cost was subtracted from each trade's revenue:With N trades, earlier costs were subtracted N times → systematic understatement proportional to the number of overlapping trades.
Bug 2 (Secondary): Wrong amount in per-trade net_gain
In
_create_trade_metadata_with_sell_order_and_trades,sell_amount(total sell order) was used instead oftrade_data["amount"](per-trade portion) in the net_gain calculation, overcounting gain per trade when a sell order was split across trades.Fix
The
costaccumulator remains correct forposition.cost -= costbelow. Only the net_gain calculation changes.Files Changed
investing_algorithm_framework/services/trade_service/trade_service.py— Two fixes (per-iteration cost, per-trade amount)tests/services/test_trade_service_net_gain.py(new) — 3 regression testsTests
All 161 app tests + 451 service/scenario tests + 3 new regression tests pass.