
This strategy is a trend-following trading system based on the Average True Range (ATR) indicator, which identifies market trends through dynamic calculation of price volatility ranges and incorporates adaptive take-profit and stop-loss mechanisms for risk management. The strategy employs a multi-period analysis approach, using ATR multiplier to dynamically adjust trade signal triggers for precise market volatility tracking.
The core strategy is based on dynamic ATR calculations, using a period parameter (default 10) to compute market true range. An ATR multiplier (default 3.0) is used to construct upper and lower channels, triggering trading signals when price breaks through these channels. Specifically: 1. Uses SMA or standard ATR for volatility baseline calculation 2. Dynamically computes upper and lower channels as trend-following references 3. Determines trend direction through price and channel crossovers 4. Triggers trading signals at trend reversal points 5. Implements percentage-based dynamic take-profit and stop-loss system
This is a well-designed trend-following strategy that achieves precise market volatility tracking through the ATR indicator, combined with take-profit and stop-loss mechanisms for risk management. The strategy’s strengths lie in its adaptability and controlled risk, though market environment impact on strategy performance should be noted. Through the suggested optimization directions, the strategy’s stability and profitability can be further enhanced.
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-11 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Custom Buy BID Strategy", overlay=true, shorttitle="Buy BID by MR.STOCKVN")
// Cài đặt chỉ báo
Periods = input.int(title="ATR Period", defval=10)
src = input.source(hl2, title="Source")
Multiplier = input.float(title="ATR Multiplier", step=0.1, defval=3.0)
changeATR = input.bool(title="Change ATR Calculation Method?", defval=true)
showsignals = input.bool(title="Show Buy Signals?", defval=false)
highlighting = input.bool(title="Highlighter On/Off?", defval=true)
barcoloring = input.bool(title="Bar Coloring On/Off?", defval=true)
// Tính toán ATR
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
// Tính toán mức giá mua bán dựa trên ATR
up = src - (Multiplier * atr)
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + (Multiplier * atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
// Vẽ xu hướng
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_line, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
// Hiển thị tín hiệu mua
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
// Cài đặt màu cho thanh nến
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highlighter", color=longFillColor)
// Điều kiện thời gian giao dịch
FromMonth = input.int(defval=9, title="From Month", minval=1, maxval=12)
FromDay = input.int(defval=1, title="From Day", minval=1, maxval=31)
FromYear = input.int(defval=2018, title="From Year", minval=999)
ToMonth = input.int(defval=1, title="To Month", minval=1, maxval=12)
ToDay = input.int(defval=1, title="To Day", minval=1, maxval=31)
ToYear = input.int(defval=9999, title="To Year", minval=999)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
// Cửa sổ thời gian giao dịch
window() => (time >= start and time <= finish)
// Điều kiện vào lệnh Buy
longCondition = buySignal
if (longCondition)
strategy.entry("BUY", strategy.long, when=window())
// Điều kiện chốt lời và cắt lỗ có thể điều chỉnh
takeProfitPercent = input.float(5, title="Take Profit (%)") / 100
stopLossPercent = input.float(2, title="Stop Loss (%)") / 100
// Tính toán giá trị chốt lời và cắt lỗ dựa trên giá vào lệnh
if (strategy.position_size > 0)
strategy.exit("Take Profit", "BUY", limit=strategy.position_avg_price * (1 + takeProfitPercent), stop=strategy.position_avg_price * (1 - stopLossPercent))
// Màu nến theo xu hướng
buy1 = ta.barssince(buySignal)
color1 = buy1[1] < na ? color.green : na
barcolor(barcoloring ? color1 : na)