Skip to content

Commit 19e0a50

Browse files
committed
feat(strategy): match TradingView big-money sizing, margin-call and trailing-stop timing
Extend the backtest broker emulator to reproduce TradingView's exact behaviour at large equity and on trailing exits. Verified on the BINANCE:BTCUSDT 30m wild corpus (entry, exit and plot all match TV). Big-money entry sizing and margin gate: from 1e7 account-currency units of order money upward TV re-judges the floor-sized quantity through a tick-grid margin gate instead of a plain float comparison. Add _judge_money_entry / _gate_entry_lots / _ceil_to_grid / _price_has_odd_f32_offset / _default_entry_budget and wire them into entry(), order(), deferred-qty resolution and the creation-time and fill-time margin checks (the fill-time check switches to an integer-tick comparison at >=1e7 equity as well). Margin-call trigger and cover at >=1e7 equity: fire on an integer-tick comparison of truncated equity against the required margin rounded to a tick -- half-up below 1e10 margin ticks, nearest multiple of ten ticks above -- and size the cover from the resulting tick-shadow shortfall; the sub-1e10 float path keeps its 2^-26 snap. Close-time margin check: TV evaluates margin at every bar close and books the liquidation on that bar at the close price. process_deferred_margin_call now runs the deferred open-price check first, then a whole-contract close check, so a position that only breaches margin at the close is trimmed on the right bar and price instead of one bar late at the next open. Reversal nofill keeps its closing leg: a market entry whose big-money sizing judges nofill no longer cancels a reversal outright -- TV keeps the order alive as a pure closing leg (the opposite position closes at the next open, the opening leg stays suppressed and re-issues a bar later). Trailing-stop two-phase intrabar walk: split _process_trailing_stop so the open tick and the legs up to the second extreme run before the intrabar margin-call checkpoints and the closing (extreme -> close) leg resumes after them, matching TV's chronology when a partial margin call at the adverse extreme precedes the trailing fill. Add the favorable-extreme checkpoint the >=1e7 trigger can trip. Trailing re-issue semantics: a re-issue with changed trailing parameters is a cancel+replace that drops the carried water mark and re-arms from the issue bar's close (_seed_trail_at_issue fold_extreme), while an identical re-issue still inherits the ratcheted stop. Truncate a fractional trail_offset tick count to whole ticks the way TV does.
1 parent bc786f1 commit 19e0a50

1 file changed

Lines changed: 532 additions & 120 deletions

File tree

0 commit comments

Comments
 (0)