Carry cost basis forward into gap-filled holdings#2476
Open
cleanjunc wants to merge 1 commit into
Open
Conversation
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
Contributor
|
Caution Review failedAn error occurred during the review process. Please try again later. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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
Holding.gapfillcarries the previous day's holding data forward for dates that have no real snapshot, but it left outcost_basis. Every synthesized day was stored withcost_basis = nil, which madeHolding#avg_costfall back to a per-holding trade query and report a different average cost, or no trend at all, on those days. This change carriescost_basisforward alongside the other values so the cost basis stays consistent across consecutive days for the same position.Root cause
Holding::ForwardCalculatorandHolding::ReverseCalculatorboth computecost_basisfor real, trade-driven dates and then call the sharedHolding.gapfillto fill the dates in between. The carry-forward ingapfillcopiedqty,price,currency, andamount, but notcost_basis, so the value was dropped on weekends, holidays, and trade gaps.Fix
Carry
cost_basisforward from the previous real holding when building a gap-filled holding. This is correct because cost basis only changes on a buy trade, and gap-filled dates have none, so the previous real day's value applies unchanged. The fix lives in the sharedgapfill, so both the forward and reverse calculators are covered in one place.cost_basis_sourceis intentionally not copied: calculator holdings never set it, and the materializer assigns the source when it persists the row.Testing
Added a test in
test/models/holding/forward_calculator_test.rbthat creates a single trade so the days after the trade date have no price of their own and are gap-filled. It asserts that a gap-filled day keeps the samecost_basisas the real trade day. The test fails before the change (the gap-filled day reportsnil) and passes after.bin/rails test test/models/holding/forward_calculator_test.rbCloses #2475