Skip to content

Fix target_percent short sizing ignoring fees/slippage - #919

Open
DresdenGman wants to merge 1 commit into
polakowo:masterfrom
DresdenGman:fix-665-target-percent-short
Open

DresdenGman wants to merge 1 commit into
polakowo:masterfrom
DresdenGman:fix-665-target-percent-short

Conversation

@DresdenGman

Copy link
Copy Markdown

Summary

size_type="target_percent" translates a target percentage into shares off the
pre-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_only periodic returns no longer sum up to the
combined portfolio's returns. (Full-size buys are exact because buy_nb caps
the 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 leaves

value_after = value + (position - T) * (price * q - val_price) - fixed_fees,

and requiring T * val_price / value_after = target_pct gives

T = target_pct * (value - position * val_price + position * price * q - fixed_fees)
    / (val_price * (1 - target_pct) + target_pct * price * q).

With no fees, no slippage and no fixed fees (q = 1) this reduces to the
plain translation target_pct * value / val_price, so zero-cost behavior is
unchanged. The buy side, TargetValue/Value/Amount/Percent and the
call-sequence ordering helper are untouched. The value-based form also covers
cash sharing, where value is 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):

fee slippage allocations after fix
0.0 0.0 exact (unchanged)
0.0 0.05 short entries at exactly -1.0
0.002 0.0 short entries at exactly -1.0
0.002 0.05 short entries at exactly -1.0, incl. long-to-short reversals

After the fix, asset_value / value of the long-only plus the short-only
column 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 bars
only (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

  • New TestFromOrders::test_target_percent_fees_slippage: the issue's setup
    with 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.
  • New test_execute_order_nb cases 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_value hardcoded 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.
  • Mutation check: reverting only the nb.py change 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_nb only
caps 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 in
the default flow.

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

No deployments
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.

bug: long_only and short_only periodic returns don't sum up to long_short daily returns

1 participant