Skip to content

fix(orders): arm strategy.exit trailing stop via trail_price (absolute activation)#50

Merged
luisleo526 merged 1 commit into
mainfrom
fix/exit-trail-price-arming
Jun 30, 2026
Merged

fix(orders): arm strategy.exit trailing stop via trail_price (absolute activation)#50
luisleo526 merged 1 commit into
mainfrom
fix/exit-trail-price-arming

Conversation

@luisleo526

Copy link
Copy Markdown
Collaborator

Problem

The engine armed a strategy.exit trailing stop only when trail_points (an entry-relative tick offset) was set. A trail specified via trail_price (an absolute activation price level) — the other form Pine's strategy.exit accepts — was silently dropped: PendingOrder never carried trail_price, and every trail predicate (compute_exit_trail_state, the sort/classify/evaluate fill paths) keyed has_trail solely on trail_points. So strategy.exit(trail_price=close, trail_offset=...) produced no trailing exit at all.

Fix

  • Add a default-NaN trail_price to PendingOrder (default-initialized so direct constructions that never assign it can't read an indeterminate value through the trail predicates).
  • Persist trail_price in strategy_exit; initialize it to NaN in the other order constructors.
  • Arm the trail on (trail_points || trail_price) across compute_exit_trail_state, sort_exit_siblings_by_path_fill, sort_orders_by_fill_phase, classify_order_eligibility, evaluate_fill_price, and the no-trail path metric.
  • compute_exit_trail_state sets the activation level from trail_price as an absolute level (no entry-relative tick rounding); trail_points keeps precedence when both are set.

No new C-ABI export (trail_price already flows into strategy_exit from codegen; it was simply ignored).

Validation

result
legalrice2697-nse-elite match vs TV 50.0% → 99.7% (359/360, price-exact 100%)
corpus 251 excellent / 1 anomaly / 0 fail (held)
ctest 76/76 incl. extended test_resolve_exit_trail_fills (trail_price long+short; reverting the arming → 4 CHECK failures)
check_c_abi_runtime.py exit 0 (no symbol change)

🤖 Generated with Claude Code

…e activation)

The engine armed a strategy.exit trailing stop ONLY when trail_points (an
entry-relative tick offset) was set. A trail specified via trail_price (an
absolute activation price level) — the other form Pine's strategy.exit accepts
— was silently dropped: PendingOrder never carried trail_price, and every trail
predicate (compute_exit_trail_state, sort/classify/evaluate paths) keyed
has_trail solely on trail_points. So `strategy.exit(trail_price=close,
trail_offset=...)` produced no trailing exit at all.

Add a default-NaN trail_price to PendingOrder, persist it in strategy_exit, and
arm the trail on (trail_points OR trail_price) everywhere. compute_exit_trail_state
sets the activation level from trail_price as an absolute level (no entry-relative
tick rounding); trail_points keeps precedence when both are set.

legalrice2697-nse-elite (trail_price-only trailing exits): 50.0%->99.7% match
vs TradingView (359/360, price-exact 100%). Corpus held 251 excellent / 1
anomaly; ctest 76/76 incl. extended test_resolve_exit_trail_fills (trail_price
long+short cases; reverting the arming makes 4 CHECKs fail). No new C-ABI export.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@luisleo526 luisleo526 merged commit 4064c7b into main Jun 30, 2026
5 checks passed
@luisleo526 luisleo526 deleted the fix/exit-trail-price-arming branch June 30, 2026 13:26
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.

1 participant