Chiến lược này chủ yếu thực hiện một cơ chế dừng tự động, có thể tự động điều chỉnh vị trí dừng tùy theo biến động giá, để đạt được hiệu quả dừng tốt hơn. Chiến lược sử dụng chỉ số ATR để tính toán phạm vi dừng hợp lý và kết hợp với đường trung bình EMA để tạo ra tín hiệu giao dịch, mở thêm lỗ khi phá vỡ đường trung bình EMA, đồng thời theo dõi vị trí dừng bằng thuật toán dừng tự động.
Chiến lược này có ý tưởng rõ ràng và dễ hiểu, sử dụng chỉ số ATR để thiết lập phạm vi dừng tự điều chỉnh và kết hợp với EMA để tạo tín hiệu giao dịch, có thể kiểm soát tổn thất hiệu quả. Tuy nhiên, chính chiến lược này khá thụ động, có nhiều không gian tối ưu hóa, có thể xem xét thêm phán đoán xu hướng, điều chỉnh các tham số tùy theo tình trạng thị trường để chiến lược chủ động hơn. Nói chung, chiến lược này là một chiến lược dừng lỗ tốt hơn và mẫu, nhưng cần điều chỉnh tối ưu hóa theo các đặc điểm khác nhau của sản phẩm, không thể chuyển sang sử dụng phần cứng.
/*backtest
start: 2023-09-07 00:00:00
end: 2023-10-07 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy(title="UT Bot Strategy", overlay = true)
//CREDITS to HPotter for the orginal code. The guy trying to sell this as his own is a scammer lol.
// Inputs
a = input(1, title = "Key Vaule. 'This changes the sensitivity'")
c = input(10, title = "ATR Period")
h = input(false, title = "Signals from Heikin Ashi Candles")
////////////////////////////////////////////////////////////////////////////////
// BACKTESTING RANGE
// From Date Inputs
fromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
fromYear = input(defval = 2019, title = "From Year", minval = 1970)
// To Date Inputs
toDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
toMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
toYear = input(defval = 2100, title = "To Year", minval = 1970)
// Calculate start/end date and time condition
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = true
////////////////////////////////////////////////////////////////////////////////
xATR = atr(c)
nLoss = a * xATR
src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = false) : close
xATRTrailingStop = 0.0
xATRTrailingStop := iff(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
iff(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
iff(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss)))
pos = 0
pos := iff(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
iff(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue
ema = ema(src,1)
above = crossover(ema, xATRTrailingStop)
below = crossover(xATRTrailingStop, ema)
buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below
barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop
plotshape(buy, title = "Buy", text = 'Buy', style = shape.labelup, location = location.belowbar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)
plotshape(sell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red, textcolor = color.white, transp = 0, size = size.tiny)
barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)
strategy.entry("long", true, when = buy and time_cond)
strategy.entry("short", false, when = sell and time_cond)