Advertisement
xmd79

Adaptive RSI/Stochastic

Jan 13th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 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. // © peacefulLizard50262
  3.  
  4. //@version=5
  5. indicator("Adaptive RSI/Stochastic")
  6. import lastguru/DominantCycle/2 as d
  7.  
  8. bes(float source = close, float length = 9)=>
  9. alpha = 2 / (length + 1)
  10. var float smoothed = na
  11. smoothed := alpha * source + (1 - alpha) * nz(smoothed[1])
  12.  
  13. rsi_count()=>
  14. high_length = d.mamaPeriod(high, 2, 4096)
  15. low_length = d.mamaPeriod(low, 2, 4096)
  16. high_filtered = bes(high, high_length)
  17. low_filtered = bes(low, low_length)
  18. change_high = ta.change(high_filtered)
  19. change_low = -ta.change(low_filtered)
  20. // Variables
  21. upMoves = ta.change(high_filtered) > 0 ? change_high : 0
  22. downMoves = ta.change(low_filtered) < 0 ? change_low : 0
  23. // Up/down move averages
  24. upMovesLength = d.mamaPeriod(upMoves, 2, 4096)
  25. downMovesLength = d.mamaPeriod(downMoves, 2, 4096)
  26. upAvg = bes(upMoves, upMovesLength)
  27. downAvg = bes(downMoves, downMovesLength)
  28. // RSI calculation
  29. rsi = downAvg != 0 ? 100 - (100 / (1 + upAvg / downAvg)) : 0
  30.  
  31. rsi_standard()=>
  32. close_length = d.mamaPeriod(close, 2, 4096)
  33. close_filtered = bes(close, close_length)
  34. up = math.max(ta.change(close_filtered), 0)
  35. down = -math.min(ta.change(close_filtered), 0)
  36. up_length = d.mamaPeriod(up, 2, 4096)
  37. down_length = d.mamaPeriod(down, 2, 4096)
  38. up_filtered = bes(up, up_length)
  39. down_filtered = bes(down, down_length)
  40. rsi = down_filtered == 0 ? 100 : 100 - (100 / (1 + up_filtered / down_filtered))
  41.  
  42. stochastic(length, smoothing)=>
  43. close_length = d.mamaPeriod(close, 2, 4096)
  44. high_length = d.mamaPeriod(high, 2, 4096)
  45. low_length = d.mamaPeriod(low, 2, 4096)
  46. close_filtered = bes(close, close_length)
  47. high_filtered = bes(high, high_length)
  48. low_filtered = bes(low, low_length)
  49.  
  50. stochastic = ta.sma(100 * (close_filtered - ta.lowest(low_filtered, length)) / (ta.highest(high_filtered, length) - ta.lowest(low_filtered, length)), smoothing)
  51. stochastic_length = d.mamaPeriod(stochastic, 2, 4096)
  52. stochastic_d = bes(stochastic, stochastic_length)
  53. [stochastic, stochastic_d]
  54.  
  55. rsi(select)=>
  56. switch select
  57. "Standard RSI" => rsi_standard()
  58. "Count RSI" => rsi_count()
  59. "Stochastic" => na
  60.  
  61. select = input.string("Standard RSI", "Select Style", ["Standard RSI","Count RSI","Stochastic"])
  62. smoothing = input.int(1, "Smoothing", 1)
  63. length = input.int(14, "Length", 1, tooltip = "Only avalible for Stochastic.")
  64.  
  65.  
  66. rsi = bes(rsi(select), smoothing)
  67. rsi_length = d.mamaPeriod(rsi, 2, 4096)
  68. rsi_smooth = bes(rsi, rsi_length)
  69. [stochastic, stochastic_d] = stochastic(length, smoothing)
  70.  
  71. colour = color.from_gradient(select == "Stochastic" ? stochastic : rsi, 0, 100, color.new(color.navy, 80), color.new(color.purple, 80))
  72.  
  73. up_line = hline(20)
  74. hline(50)
  75. dn_line = hline(80)
  76. fill(up_line, dn_line, color = colour)
  77. plot(select == "Stochastic" ? stochastic : rsi, "RSI", color.blue)
  78. plot(select == "Stochastic" ? stochastic_d : rsi_smooth, "MA", color.orange)
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement