0% found this document useful (0 votes)
32 views2 pages

Trailing Stops 1

This strategy implements a trailing stop that is adjusted on each bar close based on the user's selected trail source (lows/highs, close, or open price), trail method (ATR or percentage), and lookback period. On each bar close, it calculates a new trailing stop price and only updates the stop if the new price is better than the current stop. It then plots the trailing stop price on the chart and uses that price to exit any open position.

Uploaded by

toengsaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

Trailing Stops 1

This strategy implements a trailing stop that is adjusted on each bar close based on the user's selected trail source (lows/highs, close, or open price), trail method (ATR or percentage), and lookback period. On each bar close, it calculates a new trailing stop price and only updates the stop if the new price is better than the current stop. It then plots the trailing stop price on the chart and uses that price to exit any open position.

Uploaded by

toengsaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// @version=5

strategy("Trailing Stop", overlay=true)

// Get user input


tradeType = input.string(title="Trade Direction", defval="Long",
options=["Long", "Short"], confirm=true)
trailSource = input.string(title="Trail Source", defval="Lows/Highs",
options=["Lows/Highs", "Close", "Open"], confirm=true)
trailMethod = input.string(title="Trail Method", defval="ATR",
options=["ATR", "Percent"], confirm=true)
trailPercent = input.float(title="Trail Percent", defval=10, minval=0.1,
confirm=true)
swingLookback = input.int(title="Lookback", defval=7, confirm=true)
atrPeriod = input.int(title="ATR Period", defval=14, confirm=true)
atrMultiplier = input.float(title="ATR Multiplier", defval=1.0, confirm=true)
barTime = input.time(title="Bar Time", defval=timestamp("01 Jan 2021
13:30 +0000"), confirm=true)

// Enter mock trade (if bar is the one we clicked on and we have no open trades or
previous trades)
if time >= barTime and strategy.position_size == 0 and strategy.closedtrades == 0
strategy.entry("Trade", direction = (tradeType == "Long" ? strategy.long :
strategy.short))

// Declare trailing price variable (stores our trail stop value)


var float trailPrice = na
float next_trailPrice = na

// Get required trailing stop variables


atrValue = ta.atr(atrPeriod) * atrMultiplier
float swingLow = ta.lowest(low, swingLookback)
float swingHigh = ta.highest(high, swingLookback)

// Get trailing stop price


if trailMethod == "ATR" // Determine ATR trailing stop price based on price source
next_trailPrice := switch trailSource
"Close" => strategy.position_size > 0 ? close - atrValue : close +
atrValue
"Open" => strategy.position_size > 0 ? open - atrValue : open +
atrValue
=> strategy.position_size > 0 ? swingLow - atrValue : swingHigh + atrValue
else if trailMethod == "Percent" // Determine percentage trailing stop price based
on price source
float percentMulti = strategy.position_size > 0 ? (100 - trailPercent) / 100 :
(100 + trailPercent) / 100
next_trailPrice := switch trailSource
"Close" => close * percentMulti
"Open" => open * percentMulti
=> strategy.position_size > 0 ? swingLow * percentMulti : swingHigh *
percentMulti

// Check for trailing stop update


if strategy.position_size != 0 and barstate.isconfirmed
// Trail long stop ONLY IF temp trailPrice is not set or is a higher price
if (next_trailPrice > trailPrice or na(trailPrice)) and strategy.position_size
> 0
trailPrice := next_trailPrice
// Trigger alert
alert(message="Trailing Stop updated for " + syminfo.tickerid + ": " +
str.tostring(trailPrice, "#.#####"), freq=alert.freq_once_per_bar_close)
// Trail short stop ONLY IF temp trailPrice is not set or is a lower price
if (next_trailPrice < trailPrice or na(trailPrice)) and strategy.position_size
< 0
trailPrice := next_trailPrice
// Trigger alert
alert(message="Trailing Stop updated for " + syminfo.tickerid + ": " +
str.tostring(trailPrice, "#.#####"), freq=alert.freq_once_per_bar_close)

// Draw data to chart


plot(strategy.position_size != 0 ? trailPrice : na, color=color.red,
title="Trailing Stop")

// Exit trade if stop is hit


strategy.exit(id="Trail Exit", from_entry="Trade", limit=na, stop=trailPrice)

//if (strategy.position_size > 0 and close < trailPrice) or (strategy.position_size


< 0 and close > trailPrice)
// strategy.close("Trade")

You might also like