Advertisement
andypartridge47

RSI Learning Script

Oct 24th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /@version=5
  2. strategy(
  3. "Learning Scripts RSI",
  4. overlay=true,
  5. initial_capital=1000,
  6. default_qty_type=strategy.percent_of_equity,
  7. default_qty_value=1,
  8. commission_type=strategy.commission.percent,
  9. commission_value=0.05
  10. )
  11.  
  12. // Variables
  13. //// Inputs
  14. // 1. Get RSI Long and short variables
  15. rsiLength = input.int(14, "RSI Length", group="RSI")
  16. rsiLongThreshold = input.int(70, "RSI Long Threshold", group="RSI")
  17. rsiShortThreshold = input.int(30, "RSI Short Threshold", group="RSI")
  18.  
  19. // 2. Changed EMAs to inputs so we can change them as well
  20. fastEmaLength = input.int(12, "Fast EMA Length", group="EMA")
  21. slowEmaLength = input.int(21, "Slow EMA Length", group="EMA")
  22.  
  23. stopLossPercentage = input.float(3, "Stop Loss Percentage", group="Exits") * 0.01
  24. takeProfitPercentage = input.float(3, "Take Profit Percentage", group="Exits") * 0.01
  25.  
  26. //// Bands
  27. fastEma = ta.ema(close, fastEmaLength)
  28. slowEma = ta.ema(close, slowEmaLength)
  29.  
  30. //// Exit lines
  31. var line stopLossLine = na
  32. var line takeProfitLine = na
  33. if strategy.position_size == 0
  34. stopLossLine := na
  35. takeProfitLine := na
  36. else
  37. line.set_x2(stopLossLine, bar_index)
  38. line.set_x2(takeProfitLine, bar_index)
  39.  
  40. // Logic
  41. // 3. Get RSI and check if it is within the threshold
  42. rsi = ta.rsi(close, rsiLength)
  43. rsiWithinThreshold = rsi <= rsiLongThreshold and rsi >= rsiShortThreshold
  44.  
  45. //// Longs
  46. // 4. Check if rsi is within threshold before buys and sells
  47. if strategy.position_size == 0 and ta.crossover(fastEma, slowEma) and rsiWithinThreshold
  48. stopLoss = close * (1 - stopLossPercentage)
  49. takeProfit = close * (1 + takeProfitPercentage)
  50.  
  51. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  52. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  53.  
  54. strategy.entry("Long", strategy.long)
  55. strategy.exit("SL / TP", "Long", stop=stopLoss, limit=takeProfit)
  56.  
  57. //// Shorts
  58. // 4. Check if RSI is within threshold before buys and sells
  59. if strategy.position_size != 0 and ta.crossunder(fastEma, slowEma) and rsiWithinThreshold
  60. stopLoss = close * (1 - stopLossPercentage)
  61. takeProfit = close * (1 + takeProfitPercentage)
  62.  
  63. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  64. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  65.  
  66. strategy.entry("Short", strategy.short)
  67. strategy.exit("SL / TP", "Short", stop=stopLoss, limit=takeProfit)
  68.  
  69. // Plots
  70. bandColor = fastEma > slowEma ? color.green : color.red
  71. plot(fastEma, color=bandColor, linewidth=1)
  72. plot(slowEma, color=bandColor, linewidth=3)
  73.  
  74. // 5. Base rsi color on band color
  75. rsiColor = rsiWithinThreshold ? bandColor : color.white
  76. plot(rsi, color=rsiColor, display=display.status_line) // Display it only in the status line
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement