Percent Trailing Stop Indicator
The Percent Trailing Stop Indicator is a powerful TradingView script that automates trailing stop-loss levels, generates EMA-based signals, and identifies key support and resistance levels.
Example chart using the Percent Trailing Stop Indicator
Key Features
- Dynamically adjusts trailing stop-loss levels based on price movement.
- Plots support and resistance levels for better market context.
- Generates buy and sell signals using EMA crossovers.
- Includes real-time alerts for trade opportunities and stop-loss hits.
Indicator Code
//@version=6
indicator("Percent Trailing Stop (%) - For Study Scripts", overlay=true)
// Input Parameters
trailPerc = input.float(title="Trailing Stop (%)", minval=0.0, step=0.1, defval=5) * 0.01
period = input.int(title="Period", minval=1, defval=16)
s_r = input.int(title="Support & Resistance (S&R)", minval=1, defval=16)
h_left = input.int(title="H Left", minval=1, defval=10)
h_right = input.int(title="H Right", minval=1, defval=10)
// Moving Averages to get some example trades generated
eg1 = ta.ema(close, period)
eg2 = ta.ema(close, 30)
long = ta.crossover(eg1, eg2)
short = ta.crossunder(eg1, eg2)
// Plot signals on MA cross
plotshape(long, style=shape.labelup, location=location.belowbar, color=color.green, size=size.tiny, title="Long Entry", text="LONG", textcolor=color.white) //0
plotshape(short, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.tiny, title="Short Entry", text="SHORT", textcolor=color.white) //1
///////////////
// Support and Resistance Logic
highLevel = ta.highest(high, s_r)
lowLevel = ta.lowest(low, s_r)
plot(highLevel, title="Resistance", color=color.red, linewidth=1, style=plot.style_line)
plot(lowLevel, title="Support", color=color.green, linewidth=1, style=plot.style_line)
// The Trailing Stop Code
// Detect what was last signal (long or short)
var long_short = 0
long_last = long and (nz(long_short[1]) == 0 or nz(long_short[1]) == -1)
short_last = short and (nz(long_short[1]) == 0 or nz(long_short[1]) == 1)
long_short := long_last ? 1 : short_last ? -1 : long_short[1]
// Calculate trailing stop level
var float longStopPrice = na
var float shortStopPrice = na
longStopPrice := long_short == 1 ? math.max(close * (1 - trailPerc), longStopPrice[1]) : na
shortStopPrice := long_short == -1 ? math.min(close * (1 + trailPerc), shortStopPrice[1]) : na
// Plot trailing stops
plot(long_short == 1 ? longStopPrice : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Long Trailing Stop") //2
plot(long_short == -1 ? shortStopPrice : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Short Trailing Stop") //3
// Remove first bar for SL/TP (cannot hit SL on entry bar)
longBar1 = ta.barssince(long_last)
longBar2 = longBar1 >= 1
shortBar1 = ta.barssince(short_last)
shortBar2 = shortBar1 >= 1
// Check for TSL hit during a bar
longSLhit = long_short == 1 and longBar2 and low < longStopPrice[1]
plotshape(longSLhit, style=shape.labelup, location=location.belowbar, color=color.gray, size=size.tiny, title="Long TSL", text="Long TSL", textcolor=color.white) //4
shortSLhit = long_short == -1 and shortBar2 and high > shortStopPrice[1]
plotshape(shortSLhit, style=shape.labeldown, location=location.abovebar, color=color.gray, size=size.tiny, title="Short TSL", text="Short TSL", textcolor=color.white) //5
// Reset long_short if SL/TP hit during bar
long_short := (long_short == 1 or long_short == 0) and longBar2 and longSLhit ? 0 :
(long_short == -1 or long_short == 0) and shortBar2 and shortSLhit ? 0 :
long_short
// Check if TSL has changed from last bar
longDiff = long_short == 1 and longBar2 and longStopPrice != longStopPrice[1]
shortDiff = long_short == -1 and shortBar2 and shortStopPrice != shortStopPrice[1]
// Set Alerts for Open Long/Short, SL Hit, TP Hit
alertcondition(long_last, title="Long Alarm", message="Open a Long Trade @ ${{close}}")
alertcondition(short_last, title="Short Alarm", message="Open a Short Trade @ ${{close}}")
alertcondition(longSLhit, title="Long Trailing Stop", message="Long TSL Hit @ ${{plot_2}}")
alertcondition(shortSLhit, title="Short Trailing Stop", message="Short TSL Hit @ ${{plot_3}}")
alertcondition(longDiff, title="Long TSL Move", message="Move TSL to ${{plot_2}}")
alertcondition(shortDiff, title="Short TSL Move", message="Move TSL to ${{plot_3}}")
How to Use
Follow these steps to use the indicator:
- Click the "Copy Code" button above to copy the script to your clipboard.
- Open TradingView and navigate to the Pine Script editor.
- Paste the code into the editor and click "Add to Chart".
- Customize the inputs to match your trading strategy.