Repository navigation
fix(portfolio): stop dropping a lot when flooring to size_granularity - #894
Conversation
…ust engine Float floor division (1.0 // 0.1 == 9.0) dropped one lot from exact multiples of a fractional size_granularity in the Numba engine: 1.0 at a 0.1 lot filled 0.9, a 0.9 position closed as 0.8, and allow_partial=False rejected the order. Use np.floor(size / size_granularity), which the Rust engine already does.
|
The patch fixes the exact-multiple case from the description, but sizes that are a multiple of the lot and still land one lot short remain, because I checked out the PR head (0a572c3, Python 3.12, numpy 2.4, pandas 3, numba 0.66) and ran
The cause is If the Rust engine uses q = adj_size / size_granularity
r = np.round(q)
lots = r if abs(q - r) <= 1e-9 * max(1.0, abs(q)) else np.floor(q)
adj_size = lots * size_granularityA regression test with |
polakowo
left a comment
There was a problem hiding this comment.
Thanks for the PR! This improves parity, but size 4.3 with granularity 0.1 still fills 4.2 in both engines. Closing 4.3 leaves 0.1 open. Please include robust lot-boundary rounding in both engines here, with regression tests for exact closes and allow_partial=False.
…anularity Both engines floor through one helper that treats a size within the is_close tolerance of a lot multiple as that multiple, so 4.3 at a lot of 0.1 fills 4.3 instead of 4.2 and closing such a position leaves it flat. The result is capped at the input size.
|
Done in fb6852c. Both engines now round through one helper ( Price 10, same numbers from Numba and from Rust built from the branch:
Tests: Locally |
|
That covers it. Reading the diff: both paths snap to the nearest lot inside the One edge I would pin with a test: because the snap uses a relative tolerance, a size a hair under a lot, such as |
… a test A size within the is_close tolerance of a lot multiple counts as that multiple and the fill is capped at the requested size, so fills are lot multiples up to that tolerance. The parity test checks both engines: 0.1 * (1 - 1e-10) at a lot of 0.1 fills the requested size, 0.0999 does not trade.
|
Yes, that is deliberate, and it is now written down. The cap is the invariant I kept: a fill never exceeds the size that was asked for, and never exceeds the position being closed. A size that only reaches a lot through the tolerance therefore fills what was asked instead of being floored to zero, which is what it did before the patch. Rejecting it would turn a size that is one lot for every practical purpose into no trade at all, which is the same class of surprise as the dropped lot. One detail from running it: Done in 38ed377: the |
|
Thanks, the wording in the docstring is what I wanted: "up to the tolerance, never larger than requested" covers both of the cases I was unsure about. I re-ran the two numbers: One related thing worth a test if it is not there yet: a relative tolerance means the absolute slack grows with the lot size. At a lot of 1000 the same 1e-9 relative band is about 1e-6 shares wide, so a size that clears the tolerance at one lot size is a different absolute amount at another. Do you parametrize the tolerance test over lot sizes (0.001, 0.1, 1000), or only 0.1? |
polakowo
left a comment
There was a problem hiding this comment.
Thanks for the update! The original cases are fixed, but the tolerance grows with the lot count: size 1.0000000006 with granularity 1e-9 fills 1,000,000,000.6 lots. Please bound the snap to floating-point rounding error and add large-lot-count and genuinely sub-lot regression cases in both engines.
The snap to the next lot used is_close (relative 1e-9) on the number of lots, so the window grew with the lot count: 1.0000000006 with a granularity of 1e-9 filled 1,000,000,000.6 lots. Both engines now count a lot number as whole only within 4 machine epsilons (about 8.9e-16 relative), which covers the rounding of the size, the granularity and their quotient, and floor anything further below a multiple. 0.1 * (1 - 1e-10) with a lot of 0.1 is now a real shortfall and does not trade, so the tolerance test from the previous commit is replaced by cases at 1e9 and 1e12 lots, sub-lot sizes at lots of 0.001, 0.1 and 1000, and sizes one ulp short of a lot, in both engines.
|
Done in 9dcbd1b. The snap is now bounded by rounding error in both engines: a lot count within 4 machine epsilons of a whole number (relative, about 8.9e-16) counts as that number, anything further below is floored. Before it was Price 10, the same output from Numba and from Rust built from the branch:
The third row reverses what I wrote above on 29.10: 1e-10 of a lot is a real shortfall, so it no longer trades, and the Why 4 eps: for a size of k lots, size / granularity lands within 1 eps of k (k up to 20,000, lots from 1e-9 to 1000), and sizes the engine derives first (value / price, all cash after fees) stay within 2.4 eps. One limit remains. A position summed from many fills of one lot drifts further than that: 1000 buys of 0.1 add up to 99.9999999999986, and closing it sells 99.9 and leaves 0.1 open, as master does. Keeping closes flat up to about 9,000 fills would take about 1000 eps, a 0.2-lot window at 1e12 lots, so the bound stays at 4 eps. The new cases are |
With a fractional
size_granularity, the Numba engine fills one lot less than asked whenever the size is an exact multiple of the lot. The Rust engine fills the full size, so the same call gives different trades depending on whethervectorbt-rustis installed. vectorbt 1.2.0 / master @ 8e30b8c,from_ordersat price 10,init_cash=100:size=np.inf), lot 0.1allow_partial=Falsefrom_signalswithsize=1.0, lot 0.1, fees 0.1%, prices 100, 120, 130, 150 (enter, exit, enter, exit): Numba buys 0.9, sells 0.8, keeps 0.1 open and skips the second entry, total profit 20.81; Rust trades 1.0 four times, total profit 39.50.The cause is
adj_size // size_granularity * size_granularity(nb.py#L145, #L170, #L294). Float//is computed fromfmod, and1.0 // 0.1 == 9.0. The Rust engine uses(adj_size / size_granularity).floor()(portfolio.rs#L850, #L876, #L1042).Both engines now floor through one helper (
floor_to_granularity_nbinnb.py,floor_to_granularityinportfolio.rs). A lot count within 4 machine epsilons of a whole number (relative, about 8.9e-16, so only floating-point rounding) counts as that number, anything further below is floored, and the result is capped at the input size. The script below then prints the Rust column for both engines.The cases above are in
test_size_granularity_fractional(tests/test_portfolio.py, runs in the job withoutvectorbt-rust) andtest_from_orders_fractional_size_granularity_parity(tests/test_engine.py, Numba against Rust for lots 0.1, 0.01, 0.001). Lot boundaries:test_size_granularity_lot_boundary(4.3 and 8.1 at 0.1, 0.29 and 0.58 at 0.01, exact closes,allow_partial=False, all cash) andtest_from_orders_size_granularity_lot_boundary_parity(k lots for k = 1..1000 at 0.1, 0.01 and 0.001, bought and closed in both engines). Rounding bound:test_size_granularity_rounding_boundandtest_from_orders_size_granularity_rounding_bound_parity(1.0000000006 at a lot of 1e-9 fills 1.0, sub-lot sizes at lots of 0.001, 0.1 and 1000 do not trade, a size one ulp short of a lot still fills).Sizes that are a lot multiple but land just under it after the division (
4.3 / 0.1 == 42.99999999999999) used to lose a lot in both engines. The second commit keeps them on the lot, and 9dcbd1b bounds that snap to rounding error so a fraction of a lot is floored at any lot count (numbers in the comments below).Script