Skip to content

Add AvgEntryPrice option to StopEntryPrice for average-entry-based stops - #911

Open
hakarrr wants to merge 6 commits into
polakowo:masterfrom
hakarrr:avg-entry-stop-price
Open

hakarrr wants to merge 6 commits into
polakowo:masterfrom
hakarrr:avg-entry-stop-price

Conversation

@hakarrr

@hakarrr hakarrr commented Nov 9, 2026

Copy link
Copy Markdown

Closes #711

When a position accumulates, upon_stop_update="override" re-bases the stop on the latest entry price. For an averaged-into position, that ties the stop to the most recent (and potentially extreme) fill rather than the position's actual cost basis.

This adds StopEntryPrice.AvgEntryPrice as an explicit option that seeds the initial SL/TP stop price from the running average entry price of the current position:

  • Numba: simulate_from_signals_nb and simulate_from_signal_func_nb track the per-column average entry price (set on open/reversal, volume-weighted on increase, unchanged on partial close)
  • Rust: the same logic in simulate_from_signals_non_shared_inner and simulate_from_signals_inner
  • Existing defaults are unchanged: Close remains the default, upon_stop_update="override" still re-seeds the stop when the position increases (now from the average entry price), and "keep" retains the original stop

Tests: test_stop_entry_price_avg_entry covers TP and SL stops for accumulating long and short positions, the behavioral difference against the default Close basis, and upon_stop_update="keep". The full tests/test_portfolio.py suite passes (219 tests), and the numba and Rust engines produce identical order records on the new scenarios.

hakarrr and others added 6 commits November 9, 2026 10:06
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>

@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 PR! With vectorbt-rust 1.2.0, AvgEntryPrice silently behaves as Close: your [100, 50, 80, 90] example exits at bar 2 instead of bar 3. Please gate the new native capability and fall back to Numba (or reject it clearly) on older wheels, with a released-wheel regression test. Please also cover partial closes, reversals and cash sharing on both engines.

@hakarrr

hakarrr commented Nov 15, 2026

Copy link
Copy Markdown
Author

Could a maintainer take a look at this when you get a chance? The change adds StopEntryPrice.AvgEntryPrice so an averaged-into position seeds its stop from the running average entry price instead of the latest fill (closes #711). Happy to adjust the approach or add coverage wherever you'd like.

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.

tp_stop with accumulate gives wrong result

2 participants