Certainly! Below is a TradingView indicator written in Pine Script that automatically draws an optimal trailing path based on the Average True Range (ATR) while considering the asset's volatility.
//@version=5
indicator("ATR-Based Optimal Trailing Path", overlay=true)
// User Inputs
atrPeriod = input.int(title="ATR Period", defval=14, minval=1)
atrMultiplier = input.float(title="ATR Multiplier", defval=3.0, minval=0.1, step=0.1)
// Calculate ATR
atrValue = ta.atr(atrPeriod)
// Variables to hold trailing stop values
var float trailingStopLong = na
var float trailingStopShort = na
// Variables to track position status
var bool inLong = false
var bool inShort = false
// Entry conditions (These are placeholders; replace with your own conditions)
fastMA = ta.sma(close, 10)
slowMA = ta.sma(close, 30)
longEntry = ta.crossover(fastMA, slowMA)
shortEntry = ta.crossunder(fastMA, slowMA)
// Long Position Logic
if (longEntry)
inLong := true
inShort := false
trailingStopLong := close - (atrMultiplier * atrValue)
else if (inLong)
newStopLong = close - (atrMultiplier * atrValue)
trailingStopLong := math.max(trailingStopLong, newStopLong)
plot(trailingStopLong, title="Trailing Stop Long", color=color.green, linewidth=2)
// Exit condition
if (close <= trailingStopLong)
inLong := false
trailingStopLong := na
// Short Position Logic
if (shortEntry)
inShort := true
inLong := false
trailingStopShort := close + (atrMultiplier * atrValue)
else if (inShort)
newStopShort = close + (atrMultiplier * atrValue)
trailingStopShort := math.min(trailingStopShort, newStopShort)
plot(trailingStopShort, title="Trailing Stop Short", color=color.red, linewidth=2)
// Exit condition
if (close >= trailingStopShort)
inShort := false
trailingStopShort := na
How the Indicator Works:
- ATR Calculation: The script calculates the ATR based on the period you specify (
atrPeriod).
- Entry Signals: For demonstration purposes, it uses a simple moving average crossover system:
- Long Entry: When the 10-period SMA crosses above the 30-period SMA.
- Short Entry: When the 10-period SMA crosses below the 30-period SMA.
- Trailing Stop Calculation:
- Long Positions:
- The initial trailing stop is set at
close - (ATR Multiplier × ATR).
- As the price moves in your favor, the trailing stop moves up but never down.
- The trailing stop is the maximum of the current trailing stop and the new calculated value.
- Short Positions:
- The initial trailing stop is set at
close + (ATR Multiplier × ATR).
- As the price moves in your favor, the trailing stop moves down but never up.
- The trailing stop is the minimum of the current trailing stop and the new calculated value.
- Exit Conditions:
- Long Exit: If the close price falls below the trailing stop, the long position is exited.
- Short Exit: If the close price rises above the trailing stop, the short position is exited.
- Visualization:
- The trailing stop for long positions is plotted in green.
- The trailing stop for short positions is plotted in red.
Customization:
- ATR Period (
atrPeriod): Adjust this to change the sensitivity of the ATR calculation.
- ATR Multiplier (
atrMultiplier): A higher multiplier places the trailing stop further from the price, which can be useful in volatile markets.
Instructions to Use:
-
Add the Script to TradingView:
- Copy the script above.
- Open TradingView.
- Go to Pine Editor at the bottom of the screen.
- Paste the script into the editor.
- Click on Add to Chart.
-
Customize the Settings:
- Click on the settings icon of the indicator on your chart.
- Adjust the ATR Period and ATR Multiplier to suit your trading style and the asset's volatility.
-
Replace Entry Conditions (Optional):
- The provided entry conditions use moving average crossovers as an example.
- Replace
longEntry and shortEntry with your own strategy for generating entry signals.
Important Notes:
- Backtesting: Before using this indicator with real trades, backtest it to ensure it works well with your chosen asset and timeframe.
- Risk Management: Always use proper risk management techniques. The trailing stop helps manage risk but isn't a guarantee against losses.
- Indicator Limitations: The script assumes that you are always in either a long or short position after a signal. Modify the logic if you need different behavior.
Example Chart:
Here's how the indicator might look on a chart:
[image: ATR-Based Trailing Stop Example] open
Feel free to ask if you have any questions or need further assistance customizing the script to fit your specific needs!