Skip to content

fix(portfolio): divide gross exposure by portfolio value - #903

Merged
polakowo merged 4 commits into
polakowo:masterfrom
stooq1:fix-gross-exposure-nav
Nov 11, 2026
Merged

polakowo merged 4 commits into
polakowo:masterfrom
stooq1:fix-gross-exposure-nav

Conversation

@stooq1

@stooq1 stooq1 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #799.

gross_exposure() divided abs(asset_value) by abs(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 by Portfolio.value() of the same column or group, and net_exposure() follows.

Short 1 at 100 with init_cash=100:

close value before after
100 100 1.0 1.0
50 150 1.0 0.333
150 50 1.0 3.0
100 100 1.0 1.0

Long 1 of a and short 1 of b at 100 in one group with init_cash=200, then a/b at 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_nb and gross_exposure_rs take 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 in test_gross_exposure, test_net_exposure and test_stats, and new tests cover short-only and mixed groups in both engines.

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 polakowo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

stooq1 and others added 2 commits November 7, 2026 01:26
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.

@polakowo polakowo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@polakowo
polakowo merged commit 4280699 into polakowo:master Nov 11, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why Max Gross Exposure higher than 100% in 'shortonly' mode?

3 participants