
This strategy uses the Average True Range (ATR) as the basis for a Trailing Stop (TS), dynamically adjusting the stop-loss position to follow the trend. When the price moves in a favorable direction, the stop-loss position is adjusted accordingly to lock in profits; when the price moves in an adverse direction, the stop-loss position remains unchanged, and once the price reaches the stop-loss price, the position is closed. The key to this strategy lies in the dynamic adjustment of the stop-loss position, which can both protect profits and allow profits to expand as the trend continues.
The ATR trailing stop strategy can dynamically adjust the stop-loss position based on the magnitude of price fluctuations and can achieve good results in trending markets. However, this strategy also has risks such as inability to cope with choppy markets, excessive stop-loss frequency, and difficulty in avoiding gap openings. To address these shortcomings, the strategy can be optimized and improved in terms of trend judgment, take-profit strategies, and maximum stop-loss limits. With these adjustments, the strategy’s adaptability and profitability can hopefully be enhanced.
/*backtest
start: 2024-04-01 00:00:00
end: 2024-04-30 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Long TAP", overlay=true)
// Constants
keyValueDefault = 3.0
keyValueStep = 0.5
atrPeriodDefault = 10
// Inputs
keyValue = input.float(keyValueDefault, title="Key Value")
atrPeriod = input.int(atrPeriodDefault, title="ATR Period")
// Calculations
xATR = ta.atr(atrPeriod)
nLoss = keyValue * xATR
// Trailing Stop Calculation
var float xATRTrailingStop = 0.0
xATRTrailingStop := ta.highest(math.max(nz(xATRTrailingStop[1], 0), close - nLoss), 1)
xATRTrailingStop := ta.lowest(math.min(nz(xATRTrailingStop, 0), close + nLoss), 1)
// Position Calculation
var int pos = 0
pos := nz(pos[1], 0)
if (close[1] < nz(xATRTrailingStop, 0) and close > nz(xATRTrailingStop, 0))
pos := 1
else if (close[1] > nz(xATRTrailingStop, 0) and close < nz(xATRTrailingStop, 0))
pos := -1
// Plotting Trailing Stop
var color xcolor = na
if (pos == -1)
xcolor := color.red
else if (pos == 1)
xcolor := color.green
plot(xATRTrailingStop, color=xcolor, title="Trailing Stop")
// Buy/Sell Signals
buySignal = ta.crossover(close, xATRTrailingStop)
sellSignal = ta.crossunder(close, xATRTrailingStop)
// Strategy
if (buySignal)
strategy.entry("Long", strategy.long)
label.new(bar_index, xATRTrailingStop, text="Buy Signal", color=color.green, style=label.style_label_up, yloc=yloc.belowbar)
if (sellSignal)
strategy.entry("Short", strategy.short)
label.new(bar_index, xATRTrailingStop, text="Sell Signal", color=color.red, style=label.style_label_down, yloc=yloc.abovebar)
// Alerts
alertcondition(buySignal, title='UT BOT Buy', message='UT BOT Buy')
alertcondition(sellSignal, title='UT BOT Sell', message='UT BOT Sell')