Repository navigation
fix(portfolio): divide gross exposure by portfolio value - #903
Merged
Merged
Conversation
gross_exposure() divided abs(asset_value) by abs(asset_value) + free cash. That is the portfolio value only for a long position. The free cash of a short is fixed at entry, so a fully shorted portfolio reported 1.0 at any price, and a group holding a long and a short reported a gross exposure of 1.0 and a net exposure of 0.0 on every bar. Both engines now divide by Portfolio.value() of the same column or group. gross_exposure_nb and gross_exposure_rs take the value instead of the free cash, and a value of 0 still gives 0. Fixes polakowo#799
polakowo
requested changes
Nov 6, 2026
polakowo
left a comment
Owner
There was a problem hiding this comment.
Thanks for the fix! With the pinned vectorbt-rust 1.2.0 wheel, a portfolio holding 100 in assets with NAV 200 reports 33.3% exposure instead of 50%. The existing native entry point still interprets the second argument as cash. Please preserve that contract or add capability/version gating with a safe fallback, and cover the released wheel in a regression test.
The previous commit changed the second argument of gross_exposure_nb and gross_exposure_rs from cash to the portfolio value. The released vectorbt-rust 1.2.0 wheel still reads it as cash, so with that wheel a portfolio holding 100 in assets with a value of 200 reported 1/3 instead of 0.5. Both kernels and dispatch.gross_exposure are back to their signature on master, asset_value / (asset_value + cash). Portfolio.gross_exposure passes value - abs(asset_value) as the cash, so the denominator is still the portfolio value and the native entry point is unchanged. A new parity test pins the cash contract in both engines, and the long case from the review runs in both engines next to the short-only and mixed group cases.
(cherry picked from commit b601f3c)
polakowo
approved these changes
Nov 8, 2026
polakowo
left a comment
Owner
There was a problem hiding this comment.
Thanks for the update! The existing cash contract is preserved, and exposure now uses portfolio value correctly. The released-wheel checks and all 219 portfolio tests pass.
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.
Fixes #799.
gross_exposure()dividedabs(asset_value)byabs(asset_value) + free cash. That is the portfolio value only for a long. The free cash of a short is fixed at entry, so a fully shorted portfolio reported 1.0 at any price. Both engines now divide byPortfolio.value()of the same column or group, andnet_exposure()follows.Short 1 at 100 with
init_cash=100:Long 1 of
aand short 1 ofbat 100 in one group withinit_cash=200, thena/bat 120/50 and 80/150: gross exposure was 1.0 on every bar and is now 0.630 and 1.769, net exposure was 0.0 and is now 0.259 and -0.538.gross_exposure_nbandgross_exposure_rstake the value instead of the free cash. A value of 0 still gives 0. Long-only numbers do not change. Expected values for the short column and its group are updated intest_gross_exposure,test_net_exposureandtest_stats, and new tests cover short-only and mixed groups in both engines.