A few questions:
1. Are you getting trades showing on the chart each time?
2. Is the trailing stop drawing on the chart at least initially?
3. Are you resetting the value of trailPrice to 0 each time the trade is closed and recreating the value for each candle update?
4. Have you watched Matt's video on Trailing stops?
It's hard to see as you've given me only a little info.
// 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
// 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")
// Buy condition
if long
if TimeFilter
strategy.entry("Long", strategy.long, comment="Buy Long", when = strategy.position_size == 0)
// Exit trade if stop is hit
if (strategy.position_size > 0 and close < trailPrice) or (strategy.position_size < 0 and close > trailPrice)
strategy.close("Long")
Sorry for the delay in replying. It seems you are missing (or have changed) the part of Matt's code where he resets the trailing stop and draws it on the chart as follows:
// If long stop is hit, reset trail stop
if trailPrice != 0.0 and low <= trailPrice and trailType == "Long"
trailPrice := na
// If short stop is hit, reset trail stop
if trailPrice != 0.0 and high >= trailPrice and trailType == "Short"
trailPrice := na
// Draw data to chart
plot(trailPrice != 0 ? trailPrice : na, color=color.red, title="Trailing Stop")
What you have done is instead of checking for the trailPrice != 0.0 you are checking for strategy.position_size != 0
I am having a similar problem but i cant see where the fix is sorry. im very new and half way through mastery course. might be getting a bit ahead of myself but id really like your help.
// Calculate EMAs based on the selected timeframes for another symbol (e.g., BTCUST.P)
ema1 = request.security("BTCUSDT.P", timeframe1, ta.ema(src, length1))
ema2 = request.security("BTCUSDT.P", timeframe2, ta.ema(src, length2))
// Plot EMAs on the chart
plot(ema1, color=ema1 > ema2 ? color.green : color.red, title="EMA 1", linewidth=4)
plot(ema2, color=color.gray, title="EMA 2", linewidth=4)
longCondition = ta.crossover(ema1, ema2)
if (longCondition)
strategy.entry("Long", strategy.long)
shortCondition = ta.crossunder(ema1, ema2)
if (shortCondition)
strategy.entry("Short", strategy.short)
// Declare trailing price variable (stores our trail stop value)
var float trailPrice = na
float next_trailPrice = na
// 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(trailPrice != 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("Long")
if (strategy.position_size < 0 and close > trailPrice) or (strategy.position_size > 0 and close < trailPrice)
strategy.close("Short")