Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. // LOVE JOY PEACE PATIENCE KINDNESS GOODNESS FAITHFULNESS GENTLENESS SELF-CONTROL
  2. // @version=4
  3. // Copyright (c) 2019-present, Alex Orekhov (everget)
  4. // Updated By: JoshuaMcGowan
  5. // Date: 2-27-20
  6. // SuperTrend script may be freely distributed under the terms of the GPL-3.0 license.
  7. // Original Script Link: https://www.tradingview.com/script/VLWVV7tH-SuperTrend/#tc2782766
  8.  
  9. // Commented out to convert to strategy
  10. // study("SuperTrend", overlay=true)
  11.  
  12. strategy(title = "SuperTrend", overlay=true, currency=currency.USD, commission_value=0.075,commission_type=strategy.commission.percent, initial_capital=1000)
  13.  
  14. // Added for date selection for strategy
  15. ///////////////////////////////////////////////
  16. //* Backtesting Period Selector | Component *//
  17. ///////////////////////////////////////////////
  18.  
  19. testStartYear = input(2019, "Backtest Start Year",minval=1980)
  20. testStartMonth = input(1, "Backtest Start Month",minval=1,maxval=12)
  21. testStartDay = input(1, "Backtest Start Day",minval=1,maxval=31)
  22. testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
  23.  
  24. testStopYear = input(2020, "Backtest Stop Year",minval=1980)
  25. testStopMonth = input(12, "Backtest Stop Month",minval=1,maxval=12)
  26. testStopDay = input(31, "Backtest Stop Day",minval=1,maxval=31)
  27. testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
  28.  
  29. testPeriod() => time >= testPeriodStart and time <= testPeriodStop ? true : false
  30.  
  31. ///////////////////////////////////////////////
  32. //* Inputs and Variables *//
  33. ///////////////////////////////////////////////
  34.  
  35. length = input(title="ATR Period", type=input.integer, defval=22)
  36. mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
  37. wicks = input(title="Take Wicks into Account ?", type=input.bool, defval=true)
  38. showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)
  39. highlightState = input(title="Highlight State ?", type=input.bool, defval=true)
  40.  
  41. atr = mult * atr(length)
  42.  
  43. longStop = hl2 - atr
  44. longStopPrev = nz(longStop[1], longStop)
  45. longStop := (wicks ? low[1] : close[1]) > longStopPrev ? max(longStop, longStopPrev) : longStop
  46.  
  47. shortStop = hl2 + atr
  48. shortStopPrev = nz(shortStop[1], shortStop)
  49. shortStop := (wicks ? high[1] : close[1]) < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop
  50.  
  51. dir = 1
  52. dir := nz(dir[1], dir)
  53. dir := dir == -1 and (wicks ? high : close) > shortStopPrev ? 1 : dir == 1 and (wicks ? low : close) < longStopPrev ? -1 : dir
  54.  
  55. longColor = color.green
  56. shortColor = color.red
  57. noneColor = color.new(color.white, 100)
  58.  
  59. longStopPlot = plot(dir == 1 ? longStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
  60. buySignal = dir == 1 and dir[1] == -1
  61. plotshape(buySignal ? longStop : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor, transp=0)
  62. 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)
  63.  
  64. shortStopPlot = plot(dir == 1 ? na : shortStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
  65. sellSignal = dir == -1 and dir[1] == 1
  66. plotshape(sellSignal ? shortStop : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor, transp=0)
  67. 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)
  68.  
  69. midPricePlot = plot(ohlc4, title="midPricePlot", style=plot.style_circles, linewidth=0)
  70.  
  71. longFillColor = highlightState ? (dir == 1 ? longColor : noneColor) : noneColor
  72. shortFillColor = highlightState ? (dir == -1 ? shortColor : noneColor) : noneColor
  73. fill(midPricePlot, longStopPlot, title="Long State Filling", color=longFillColor)
  74. fill(midPricePlot, shortStopPlot, title="Short State Filling", color=shortFillColor)
  75.  
  76. alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
  77. alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
  78.  
  79. changeCond = dir != dir[1]
  80. alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")
  81.  
  82. // Debug Plots
  83. plot(dir, title='dir', transp=100)
  84.  
  85. ///////////////////////////////////////////////
  86. //* Strategy Entry & Exit Conditions *//
  87. ///////////////////////////////////////////////
  88.  
  89. // Entries
  90. if buySignal and testPeriod()
  91. stop_level = longStopPlot
  92. strategy.entry(id="Long", long=true, qty=100)
  93.  
  94. if sellSignal and testPeriod()
  95. stop_level = shortStopPlot
  96. strategy.entry(id="Short", long=false, qty=100)
  97.  
  98. // Exits
  99. strategy.close_all(when=sellSignal and
  100. (strategy.position_size > 0))
  101. strategy.close_all(when=buySignal and
  102. (strategy.position_size < 0))
  103.  
  104. strategy.close_all(when=not testPeriod())
  105.  
  106. // END //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement