Supertrend With Trailing
Supertrend With Trailing
0 at
https://mozilla.org/MPL/2.0/
// © KivancOzbilgic
//@version=6
strategy("SuperTrend STRATEGY", overlay=true)
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/Sell Signals ?", defval=false)
highlighting = input.bool(title="Highlighter On/Off ?", defval=true)
barcoloring = input.bool(title="Bar Coloring On/Off ?", defval=true)
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - (Multiplier * atr)
up1 = na(up[1]) ? up : up[1]
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + (Multiplier * atr)
dn1 = na(dn[1]) ? dn : dn[1]
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := na(trend[1]) ? trend : trend[1]
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
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
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute,
style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy",
location=location.absolute, style=shape.labelup, size=size.tiny,
color=color.new(color.green, 0), textcolor=color.white)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_line,
linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins",
location=location.absolute, style=shape.circle, size=size.tiny,
color=color.new(color.red, 0))
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell",
location=location.absolute, style=shape.labeldown, size=size.tiny,
color=color.new(color.red, 0), textcolor=color.white)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=1)
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)
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)
window() => (time >= start and time <= finish)
longCondition = buySignal
if (longCondition)
strategy.entry("BUY", strategy.long)
shortCondition = sellSignal
if (shortCondition)
strategy.entry("SELL", strategy.short)
buy1 = ta.barssince(buySignal)
sell1 = ta.barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)
if (longCondition)
strategy.exit("Take Profit/Stop Loss", from_entry="BUY", limit=close +
take_profit * atr, stop=close - trail_offset * atr)
if (shortCondition)
strategy.exit("Take Profit/Stop Loss", from_entry="SELL", limit=close -
take_profit * atr, stop=close + trail_offset * atr)