Dynamic trailing stop
A trailing stop a set number of ATRs back from the best price since entry; idle until a position is armed.
This indicator is a trailing stop for an open position. Once a position is armed in code, a long stop sits atr_multiplier times a Wilder-smoothed ATR below the highest high since entry, and a short stop sits the same distance above the lowest low. The trigger fires when the close reaches or passes that stop. Nothing on this chart arms a position, so with default settings it returns no stop and never fires on SPY.
How to read it
- Both outputs stay empty on this chart: the indicator only computes once a position is set in code, which the chart never does.
- For a long, the stop is tied to the highest high since entry, so it steps up with new highs; a rising ATR can still pull it down.
- A trigger needs the close, not a wick: a bar that spikes through the stop and closes back on the safe side does not fire.
What it outputs
- Stop price
- The trailing stop price once a position is armed. It is a price level, not a fire, and it is empty with no position.
- Stop hit
- True on a bar where the close reaches the stop: at or below it for a long, at or above it for a short. Always false with no armed position.
When it fires
On a bar where the close reaches the stop: at or below a long stop, at or above a short stop. That is the exit moment for the armed position. Nothing arms a position here, so it stays silent.
Settings
- atr_multiplier
- How many ATRs the stop sits back from the best price since entry. Raising it widens the stop, so it triggers later and less often; lowering it tightens the stop toward price. At zero or below the stop sits on or past the high and fires almost at once.
- atr_window
- Bars in the Wilder-smoothed ATR that sets the stop's width. Shorter windows react to the last few bars and swing the stop with each burst of range; longer windows smooth over more bars and give a steadier stop.
Pitfalls
- Inert as shown: no code path calls set_position, so the indicator returns no stop and never triggers at any setting.
- stop_price is a price level, not a fire, even though it is catalogued as a signal and drawn as markers; read stop_hit as the trigger.
- The atr_multiplier range runs from -201 to 201; zero or negative values fire on nearly every bar once a position is armed.