Advertisement
theartoftrading

TTC RSI Strategy

Jan 6th, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © ZenAndTheArtOfTrading
  3.  
  4. //@version=4
  5. strategy("TTC RSI Strategy", overlay=true, calc_on_order_fills=true)
  6.  
  7. // Get user input
  8. startDay = input(title="Start Day", type=input.integer, defval=1, minval=1, maxval=31)
  9. startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12)
  10. startYear = input(title="Start Year", type=input.integer, defval=1)
  11. rsiLen = input(title="RSI Length", type=input.integer, defval=10)
  12. rsiOB = input(title="RSI OB", type=input.float, defval=70.0)
  13. rsiOS = input(title="RSI OS", type=input.float, defval=30.0)
  14. emaLen = input(title="EMA", type=input.integer, defval=20)
  15. rr = input(title="R:R", type=input.float, defval=1.0, step=0.1)
  16. stopDistance = input(title="SL Distance", type=input.float, defval=5.0)
  17. useATR = input(title="Use ATR Stop?", type=input.bool, defval=false)
  18.  
  19. // Get indicator values
  20. rsi = rsi(close, rsiLen)
  21. atr = atr(14)
  22. ema = ema(close, emaLen)
  23.  
  24. // Custom function to convert whole numbers back into pips
  25. toPips(number) =>
  26. return = atr(14) >= 1.0 ? number : (number * syminfo.mintick) * (10 / syminfo.pointvalue)
  27. return := atr(14) >= 1.0 and atr(14) < 100.0 and syminfo.currency == "JPY" ? return / 100 : return
  28.  
  29.  
  30. // See if this bar's time happened on/after start date
  31. afterStartDate = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDay, 0, 0))
  32.  
  33. // Declare trade variables
  34. var lookingForTrade = true
  35. var inLongTrade = false
  36. var inShortTrade = false
  37. var tradeStop = 0.0
  38. var tradeTarget = 0.0
  39. var lookingForEMA = false
  40. enterLong = false
  41. enterShort = false
  42.  
  43. // Check filters
  44. filters = afterStartDate and not lookingForEMA
  45.  
  46. // Enter long trades
  47. if rsi >= rsiOB and not inLongTrade and not inShortTrade and filters
  48. enterLong := true
  49. inLongTrade := true
  50. tradeStop := low - (useATR ? atr : toPips(stopDistance))
  51. distance = close - tradeStop
  52. tradeTarget := close + (distance * rr)
  53.  
  54. // Enter short trades
  55. if rsi <= rsiOS and not inLongTrade and not inShortTrade and filters
  56. enterShort := true
  57. inShortTrade := true
  58. tradeStop := high + (useATR ? atr : toPips(stopDistance))
  59. distance = tradeStop - close
  60. tradeTarget := close - (distance * rr)
  61.  
  62. // Check trade results
  63. resultColor = color.black
  64.  
  65. // Check if long target hit
  66. if inLongTrade and high >= tradeTarget
  67. inLongTrade := false
  68. resultColor := color.lime
  69. lookingForEMA := true
  70.  
  71. // Check if long stop hit
  72. if inLongTrade and low <= tradeStop
  73. inLongTrade := false
  74. resultColor := color.maroon
  75. lookingForEMA := true
  76.  
  77. // Check if short target hit
  78. if inShortTrade and low <= tradeTarget
  79. inShortTrade := false
  80. resultColor := color.lime
  81. lookingForEMA := true
  82.  
  83. // Check if short stop hit
  84. if inShortTrade and high >= tradeStop
  85. inShortTrade := false
  86. resultColor := color.maroon
  87. lookingForEMA := true
  88.  
  89. // Check if we need to check for EMA touch
  90. if lookingForEMA and ema >= low and ema <= high
  91. lookingForEMA := false
  92.  
  93. // Draw data to chart
  94. plotshape(enterLong ? 1 : na, style=shape.triangleup, location=location.belowbar, color=color.lime, transp=0)
  95. plotshape(enterShort ? 1 : na, style=shape.triangledown, color=color.red, transp=0)
  96.  
  97. plot(ema, color=color.blue)
  98.  
  99. plot(inLongTrade or inShortTrade ? tradeTarget : na, style=plot.style_linebr, color=color.green, transp=0)
  100. plot(inLongTrade or inShortTrade ? tradeStop : na, style=plot.style_linebr, color=color.red, transp=0)
  101.  
  102. // Enter trades whenever a valid setup is detected
  103. strategy.entry(id="Long", long=strategy.long, when=enterLong)
  104. strategy.entry(id="Short", long=strategy.short, when=enterShort)
  105.  
  106. // Exit trades whenever a valid exit reason is detected
  107. strategy.exit(id="Long Exit", from_entry="Long", limit=tradeTarget, stop=tradeStop, when=strategy.position_size > 0)
  108. strategy.exit(id="Short Exit", from_entry="Short", limit=tradeTarget, stop=tradeStop, when=strategy.position_size < 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement