Advertisement
JustUncleL

RSISMA-R2

Jan 17th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. //@version=2
  2.  
  3. strategy(title = "RSI versus SMA R2", shorttitle = "RSISMA R2", overlay = false, pyramiding = 0, calc_on_every_tick = false,
  4. default_qty_type = strategy.percent_of_equity, default_qty_value = 10, currency = currency.GBP)
  5.  
  6. // Revision: 2
  7. // Author: @JayRogers
  8. // Revisions by: R2 JustUncleL
  9. //
  10. // *** USE AT YOUR OWN RISK ***
  11. // - Nothing is perfect, and all decisions by you are on your own head. And stuff.
  12. //
  13. // Description:
  14. // - It's RSI versus a Simple Moving Average.. Not sure it really needs much more description.
  15. // - Should not repaint - Automatically offsets by 1 bar if anything other than "open" selected as RSI source.
  16. //
  17. // Revisions:
  18. // - Removed redundant function code.
  19. // - Removed Trailing Stop option (gives false and misleading results in back testing)
  20. // - Simplified the Risk management code.
  21.  
  22. // === INPUTS ===
  23. // rsi
  24. rsiSource = input(defval = open, title = "RSI Source")
  25. rsiLength = input(defval = 8, title = "RSI Length", minval = 1)
  26. // sma
  27. maLength = input(defval = 34, title = "MA Period", minval = 1)
  28. // invert trade direction
  29. tradeInvert = input(defval = false, title = "Invert Trade Direction?")
  30. // === /INPUTS ===
  31.  
  32. // === SERIES and VAR ===
  33. // rsi
  34. shunt = rsiSource == open ? 0 : 1
  35. rsiUp = rma(max(change(rsiSource[shunt]), 0), rsiLength)
  36. rsiDown = rma(-min(change(rsiSource[shunt]), 0), rsiLength)
  37. rsi = (rsiDown == 0 ? 100 : rsiUp == 0 ? 0 : 100 - (100 / (1 + rsiUp / rsiDown))) - 50 // shifted 50 points to make 0 median
  38. // sma of rsi
  39. rsiMa = sma(rsi, maLength)
  40. // self explanatory..
  41. tradeDirection = tradeInvert ? 0 <= rsiMa ? true : false : 0 >= rsiMa ? true : false
  42. // === /SERIES ===
  43.  
  44. // === PLOTTING ===
  45. barcolor(color = tradeDirection ? green : red, title = "Bar Colours")
  46. // hlines
  47. medianLine = hline(0, title = 'Median', color = #996600, linestyle = dotted, linewidth = 1)
  48. limitUp = hline(25, title = 'Limit Up', color = silver, linestyle = dotted, linewidth = 1)
  49. limitDown = hline(-25, title = 'Limit Down', color = silver, linestyle = dotted, linewidth = 1)
  50. // rsi and ma
  51. rsiLine = plot(rsi, title = 'RSI', color = purple, linewidth = 2, style = line, transp = 50)
  52. areaLine = plot(rsiMa, title = 'Area MA', color = silver, linewidth = 1, style = area, transp = 70)
  53. // === /PLOTTING ===
  54.  
  55. // === STRATEGY ===
  56. // stop loss
  57. slPoints = input(defval = 0, title = "Initial Stop Loss Points (zero to disable)", minval = 0)
  58. tpPoints = input(defval = 0, title = "Initial Target Profit Points (zero for disable)", minval = 0)
  59. // Include bar limiting algorithm
  60. ebar = input(defval = 0, title="Number of Bars for Back Testing", minval=0)
  61. dummy = input(false, title="- SET to ZERO for Daily or Longer Timeframes" )
  62. //
  63. // Calculate how many mars since last bar
  64. tdays = (timenow-time)/60000.0 // number of minutes since last bar
  65. tdays := ismonthly? tdays/1440.0/5.0/4.3/interval : isweekly? tdays/1440.0/5.0/interval : isdaily? tdays/1440.0/interval : tdays/interval // number of bars since last bar
  66. //
  67. //set up exit parameters
  68. TP = tpPoints>0?tpPoints:na
  69. SL = slPoints>0?slPoints:na
  70.  
  71. goLong() => not tradeDirection[1] and tradeDirection
  72. goShort() => tradeDirection[1] and not tradeDirection
  73.  
  74. // Make sure we are within the bar range, Set up entries and exit conditions
  75. if (ebar==0 or tdays<=ebar)
  76. strategy.entry("Buy", strategy.long, when=goLong())
  77. strategy.entry("Sell", strategy.short, when=goShort())
  78. strategy.exit("XL", from_entry = "Buy", profit = TP, loss = SL)
  79. strategy.exit("XS", from_entry = "Sell", profit = TP, loss = SL)
  80.  
  81. // === /STRATEGY ===
  82. // EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement