
Chiến lược này sử dụng sóng trung bình thực sự (ATR) làm cơ sở để theo dõi dừng lỗ (TS) để thực hiện mục đích theo dõi xu hướng bằng cách điều chỉnh động vị trí dừng lỗ. Khi giá di chuyển theo hướng thuận lợi, vị trí dừng lỗ cũng sẽ điều chỉnh theo đó, do đó, khóa lợi nhuận đã đạt được; khi giá di chuyển theo hướng bất lợi, vị trí dừng lỗ không thay đổi, và một khi giá chạm vào giá dừng lỗ, vị trí dừng lỗ sẽ bị xóa.
Chiến lược dừng chân theo dõi ATR có thể điều chỉnh vị trí dừng chân theo động lực tùy thuộc vào độ dao động của giá và có thể đạt được hiệu quả tốt trong hoạt động của xu hướng. Tuy nhiên, chiến lược này cũng có nguy cơ không thể đối phó với thị trường rung động, dừng chân quá thường xuyên và khó tránh lỗ hổng nhảy vọt. Đối với các thiếu sót trên, chiến lược có thể được tối ưu hóa và cải thiện từ các khía cạnh của phán đoán xu hướng, chiến lược dừng chân, giới hạn dừng chân tối đa.
/*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')