
Cette stratégie est un système de suivi des tendances basé sur l’indicateur de la moyenne réelle de l’amplitude (ATR) pour identifier les tendances du marché en calculant dynamiquement la gamme des fluctuations des prix et en gérant les risques en combinant un mécanisme d’arrêt et de perte adapté. La stratégie utilise une méthode d’analyse à cycles multiples pour ajuster dynamiquement les conditions de déclenchement des signaux de négociation en multipliant l’ATR afin de suivre avec précision les fluctuations du marché.
Le cœur de la stratégie est basé sur le calcul dynamique de l’indicateur ATR, calculant l’amplitude réelle du marché à partir des paramètres de cycle définis (la période 10 par défaut). Construire une ligne d’orbite ascendante et descendante en utilisant le multiplicateur ATR (la période 3.0 par défaut) et déclencher un signal de transaction lorsque le prix franchit la ligne d’orbite.
Il s’agit d’une stratégie de suivi de tendance bien conçue, permettant de suivre avec précision les fluctuations du marché via les indicateurs ATR et de gérer les risques en combinaison avec un mécanisme de stop-loss. L’avantage de la stratégie réside dans sa forte adaptabilité et sa maîtrise des risques, mais il faut toujours tenir compte de l’impact de l’environnement du marché sur la performance de la stratégie.
/*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)