TradeMgmt

243
Library "TradeMgmt"
Trade-management primitives for the opening-range indicator family.

⚠️ WHAT IS IN HERE TODAY IS STOP RESOLUTION ONLY — the six functions that turn a
stop-mode selection into a stop price and a cascade start index. TRAILING, CASCADE
PROGRESSION, POSITION SIZING, THE TP LADDER AND tmBuildTrade ALL REMAIN HOST-SIDE.
The name is broader than the contents on purpose: the playbook's "Trade Management
externalization" roadmap entry has further slices, and a generic name gives them a
home instead of forcing a second library and a rename. The cost of that choice is
this paragraph — do not come looking here for a trailing bug.

Moved out of the host for CE10295: Pine instantiates a host function at every call
site, so this tree was counted eight times against the main body (6,784 inlined
tokens, measured 2026-08-11). Library code is exempt from that budget entirely.
Full record: plans/REFACTOR_trade_management_externalization.md.

computeStopBull(c, startIdx, hybrid, sweepRef, candleRef)
  Resolve the bull stop and its cascade start index. Resolves the effective buffer
(accounting for hybrid and the Fixed Points interaction), then delegates to
resolveStopBull or computes the Fixed Points stop inline.

⚠️ Hybrid is silently ignored when Fixed Points mode is selected — preserved
behavior, not an oversight: the fixed offset is already the whole stop there.
  Parameters:
    c (StopCtx): Per-bar context, built once per bar by the host.
    startIdx (int): IDX_* resolved from the host's stop-mode input. The host owns that
conversion; see the note on TM_STOP_* above.
    hybrid (bool): True when the hybrid checkbox for this signal type is on.
    sweepRef (float): Sweep low for the signal; na when not applicable.
    candleRef (float): Signal candle low; na when sweepRef already covers it.
  Returns: Tuple [stopPrice, cascadeStartIdx]. Stop price, or na (degenerate edge case).
cascadeStartIdx is the first cascade target index — one level above the
resolved structural stop, or the first structural level above a Fixed/Sweep
stop.

computeStopBear(c, startIdx, hybrid, sweepRef, candleRef)
  Resolve the bear stop and its cascade start index. Mirror of computeStopBull.
  Parameters:
    c (StopCtx): Per-bar context, built once per bar by the host.
    startIdx (int): IDX_* resolved from the host's stop-mode input.
    hybrid (bool): True when the hybrid checkbox for this signal type is on.
    sweepRef (float): Sweep high for the signal; na when not applicable.
    candleRef (float): Signal candle high; na when sweepRef already covers it.
  Returns: Tuple [stopPrice, cascadeStartIdx].

StopCtx
  Per-bar stop-resolution context: everything this tree used to read from host
  Fields:
    entry (series float): Execution-candle open — the trade entry price.
    atrBuf (series float): Structural-anchor buffer (raw ATR × the host's ATR multiplier).
    fixedPts (series float): Host's Fixed Points input, used by FIXED mode and by hybrid.
    orh (series float): Opening-range high.
    vah (series float): Value-area high.
    poc (series float): Point of control.
    val (series float): Value-area low.
    orl (series float): Opening-range low.

Wyłączenie odpowiedzialności

Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.