Repository navigation
Fix target_percent short sizing ignoring fees/slippage - #919
Open
DresdenGman wants to merge 1 commit into
Open
DresdenGman wants to merge 1 commit into
DresdenGman wants to merge 1 commit into
Conversation
Translating a target percent into shares off the pre-trade value over-allocates the short side: the trade itself shrinks the portfolio value, so a 100% short ends up worth more than 100% of the post-trade value (e.g. allocation -1.055 with 5% slippage + 0.2% fees, -1.116 when reversing a long position), and long_only + short_only returns no longer sum up to the combined portfolio. Solve for the target value whose post-trade position equals the target percentage of the post-trade value when the order nets to a sell. With no fees/slippage the formula reduces to the plain translation, so zero-cost behavior is unchanged.
This branch has not been deployed
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
size_type="target_percent"translates a target percentage into shares off thepre-trade value, ignoring fees and slippage. On the short side this
over-allocates: the trade itself shrinks the portfolio value, so a 100% short
ends up worth more than 100% of the post-trade value. In the issue's setup
(long-only, short-only and combined portfolios running the same SMA crossover)
the short-only allocation after entry is -1.055 with 5% slippage + 0.2% fees,
and -1.116 in the combined portfolio when reversing a long position — which
is why
long_only + short_onlyperiodic returns no longer sum up to thecombined portfolio's returns. (Full-size buys are exact because
buy_nbcapsthe size by the cash limit; partial buys still over-allocate the same way and
are out of scope for this PR — see the note below.)
This PR sizes a net sell so the post-trade position equals the target
percentage of the post-trade value. With
q = (1 - slippage) * (1 - fees),selling
(position - T)shares leavesand requiring
T * val_price / value_after = target_pctgivesWith no fees, no slippage and no fixed fees (
q = 1) this reduces to theplain translation
target_pct * value / val_price, so zero-cost behavior isunchanged. The buy side,
TargetValue/Value/Amount/Percentand thecall-sequence ordering helper are untouched. The value-based form also covers
cash sharing, where
valueis the group value.Closes #665.
Verification
Reproduced the issue's four parameter combinations on current master with
local synthetic prices (same signals and sizing as the issue's script):
After the fix,
asset_value / valueof the long-only plus the short-onlycolumn equals the combined column's allocation on every bar (max deviation
9e-16), and their periodic returns sum up exactly on every bar without a
trade.
One honest caveat: the issue's literal test
(
assert_array_almost_equal(long + short, both)) still differs on trade barsonly (55 of 300 bars, max 0.0039). That residual is not a sizing defect: on a
trade bar the allocation-sum and the return-sum are two linear equations in
the single unknown (the combined column's trade size) and are generically
inconsistent — a sizing can satisfy either one on that bar, not both. The
combined portfolio pays the cost of closing one leg in the same order in which
it opens the other, while each leg pays only its own cost, so satisfying the
return-sum on a trade bar would force the allocation off target and break
every following bar. The sizing defect the maintainer diagnosed — the
allocation exceeding 100% — is fixed exactly. A near-zero-denominator guard
falls back to the previous behavior when the execution price diverges far from
the valuation price, so the exact solution is never an absurd number of shares.
Tests
TestFromOrders::test_target_percent_fees_slippage: the issue's setupwith synthetic prices; asserts the allocation sum on all bars, the return
sum on non-trade bars, and a bound on the trade-bar cost drag.
test_execute_order_nbcases with exact expectations: flat short entry(9.50660709192889 shares), long-to-short reversal (18.071679817473143
shares sold), partial sell to 50% (4.879010317745495 shares sold), and a
fixed-fees-only case (9.9 shares).
test_update_valuehardcoded order records updated to the new sell sizes(buy sizes unchanged). Verified each new sell size lands its column at the
target allocation at execution time; the remaining ~1e-4 deviation comes
from later same-bar trades by other columns under cash sharing.
nb.pychange turns both new tests red.tests/test_portfolio.py: 233 passed, 15 skipped, 0 failed (1000.21s).Zero regressions.
Note: partial buys still over-allocate by the same cost drag (e.g. a 50%
target buy lands at 0.513 with 5% slippage + 0.2% fees) because
buy_nbonlycaps the size when the buy exhausts cash. That is the same class of defect on
the other side of the trade and is left for a follow-up; likewise, exactness
assumes no binding size constraint (
max_size,size_granularity,allow_partial) and that the valuation price is the execution price, as inthe default flow.