Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LOVE JOY PEACE PATIENCE KINDNESS GOODNESS FAITHFULNESS GENTLENESS SELF-CONTROL
- // @version=4
- // Copyright (c) 2019-present, Alex Orekhov (everget)
- // Updated By: JoshuaMcGowan
- // Date: 2-27-20
- // SuperTrend script may be freely distributed under the terms of the GPL-3.0 license.
- // Original Script Link: https://www.tradingview.com/script/VLWVV7tH-SuperTrend/#tc2782766
- // Commented out to convert to strategy
- // study("SuperTrend", overlay=true)
- strategy(title = "SuperTrend", overlay=true, currency=currency.USD, commission_value=0.075,commission_type=strategy.commission.percent, initial_capital=1000)
- // Added for date selection for strategy
- ///////////////////////////////////////////////
- //* Backtesting Period Selector | Component *//
- ///////////////////////////////////////////////
- testStartYear = input(2019, "Backtest Start Year",minval=1980)
- testStartMonth = input(1, "Backtest Start Month",minval=1,maxval=12)
- testStartDay = input(1, "Backtest Start Day",minval=1,maxval=31)
- testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
- testStopYear = input(2020, "Backtest Stop Year",minval=1980)
- testStopMonth = input(12, "Backtest Stop Month",minval=1,maxval=12)
- testStopDay = input(31, "Backtest Stop Day",minval=1,maxval=31)
- testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
- testPeriod() => time >= testPeriodStart and time <= testPeriodStop ? true : false
- ///////////////////////////////////////////////
- //* Inputs and Variables *//
- ///////////////////////////////////////////////
- length = input(title="ATR Period", type=input.integer, defval=22)
- mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
- wicks = input(title="Take Wicks into Account ?", type=input.bool, defval=true)
- showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)
- highlightState = input(title="Highlight State ?", type=input.bool, defval=true)
- atr = mult * atr(length)
- longStop = hl2 - atr
- longStopPrev = nz(longStop[1], longStop)
- longStop := (wicks ? low[1] : close[1]) > longStopPrev ? max(longStop, longStopPrev) : longStop
- shortStop = hl2 + atr
- shortStopPrev = nz(shortStop[1], shortStop)
- shortStop := (wicks ? high[1] : close[1]) < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop
- dir = 1
- dir := nz(dir[1], dir)
- dir := dir == -1 and (wicks ? high : close) > shortStopPrev ? 1 : dir == 1 and (wicks ? low : close) < longStopPrev ? -1 : dir
- longColor = color.green
- shortColor = color.red
- noneColor = color.new(color.white, 100)
- longStopPlot = plot(dir == 1 ? longStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
- buySignal = dir == 1 and dir[1] == -1
- plotshape(buySignal ? longStop : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor, transp=0)
- plotshape(buySignal and showLabels ? longStop : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=color.white, transp=0)
- shortStopPlot = plot(dir == 1 ? na : shortStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
- sellSignal = dir == -1 and dir[1] == 1
- plotshape(sellSignal ? shortStop : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor, transp=0)
- plotshape(sellSignal and showLabels ? shortStop : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=color.white, transp=0)
- midPricePlot = plot(ohlc4, title="midPricePlot", style=plot.style_circles, linewidth=0)
- longFillColor = highlightState ? (dir == 1 ? longColor : noneColor) : noneColor
- shortFillColor = highlightState ? (dir == -1 ? shortColor : noneColor) : noneColor
- fill(midPricePlot, longStopPlot, title="Long State Filling", color=longFillColor)
- fill(midPricePlot, shortStopPlot, title="Short State Filling", color=shortFillColor)
- alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
- alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
- changeCond = dir != dir[1]
- alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")
- // Debug Plots
- plot(dir, title='dir', transp=100)
- ///////////////////////////////////////////////
- //* Strategy Entry & Exit Conditions *//
- ///////////////////////////////////////////////
- // Entries
- if buySignal and testPeriod()
- stop_level = longStopPlot
- strategy.entry(id="Long", long=true, qty=100)
- if sellSignal and testPeriod()
- stop_level = shortStopPlot
- strategy.entry(id="Short", long=false, qty=100)
- // Exits
- strategy.close_all(when=sellSignal and
- (strategy.position_size > 0))
- strategy.close_all(when=buySignal and
- (strategy.position_size < 0))
- strategy.close_all(when=not testPeriod())
- // END //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement