OPEN-SOURCE SCRIPT

cá nhân

//version=5
strategy("Demo GPT - Supertrend", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3)

// Inputs
Periods = input.int(10, title="ATR Period")
src = input.source(hl2, title="Source")
Multiplier = input.float(3.0, title="ATR Multiplier", step=0.1)
changeATR = input.bool(true, title="Change ATR Calculation Method ?")
showSignals = input.bool(true, title="Show Signals ?")
highlighting = input.bool(true, title="Highlighter On/Off ?")
emaPeriod = input.int(50, title="EMA Period")
bbLength = input.int(20, title="Bollinger Bands Length")
bbMultiplier = input.float(2.0, title="Bollinger Bands Multiplier")

// ATR Calculation
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2

// Supertrend Calculation
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

// Bollinger Bands Calculation
basis = ta.sma(close, bbLength)
deviation = ta.stdev(close, bbLength)
upperBand = basis + (bbMultiplier * deviation)
lowerBand = basis - (bbMultiplier * deviation)

// Plot Supertrend and Bollinger Bands
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_line, linewidth=2, color=color.green)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_line, linewidth=2, color=color.red)
plot(upperBand, title="Upper Band", color=color.blue, linewidth=1)
plot(lowerBand, title="Lower Band", color=color.blue, linewidth=1)
plot(basis, title="BB Basis", color=color.gray, linewidth=1)

// Buy and Sell Signals
buySignal = close > upperBand
sellSignal = close < lowerBand

if (buySignal and showSignals)
strategy.entry("Buy", strategy.long)
if (sellSignal and showSignals)
strategy.close("Buy")

// Highlighting
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.new(color.green, 90) : na) : na
shortFillColor = highlighting ? (trend == -1 ? color.new(color.red, 90) : na) : na
fill(mPlot, upPlot, title="UpTrend Highlighter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highlighter", color=shortFillColor)

// Date Range Filter
startDate = input.time(timestamp("2018-01-01 00:00"), title="Start Date")
endDate = input.time(timestamp("2069-12-31 23:59"), title="End Date")
inDateRange = (time >= startDate and time <= endDate)
if not inDateRange
strategy.close_all()
Bands and ChannelsBill Williams IndicatorsBreadth Indicators

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer