Advertisement
Guest User

Untitled

a guest
Jan 25th, 2022
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. //@version=5
  2. indicator("Real Relative Strength + Volume", shorttitle = "Relative Strength", overlay = false)
  3.  
  4. // Get User's inputs -----------------------------------------------------------
  5.  
  6. // Index selection
  7.  
  8. index = input.symbol(defval = "SPY", title = "Reference for comparison", group = "Reference selection")
  9.  
  10. symbol = syminfo.tickerid
  11.  
  12. // Period's parameters
  13.  
  14. period_5M = input.int (defval = 12, title = "Intraday RS period", group = "Period's parameters")
  15. period_1D = input.int (defval = 5, title = "Daily RS period", group = "Period's parameters")
  16.  
  17. length_5M = input.int (defval = 390, title = "Intraday RV period", group = "Period's parameters")
  18. length_1D = input.int (defval = 50, title = "Daily RV period", group = "Period's parameters")
  19.  
  20. period = timeframe.isminutes ? period_5M : period_1D
  21. length = timeframe.isminutes ? length_5M : length_1D
  22.  
  23. // Smoothing's parameters
  24.  
  25. pre_smoothing_switch = input.bool (false, "", inline = "pre_smoothing_switch", group = "Smoothing's parameters")
  26. post_smoothing_switch = input.bool (false, "", inline = "post_smoothing_switch", group = "Smoothing's parameters")
  27.  
  28. pre_smoothing_value = input.int (defval = 2, minval = 2, title = "Pre-calculating smoothing", inline = "pre_smoothing_switch", group = "Smoothing's parameters")
  29. post_smoothing_value = input.int (defval = 2, minval = 2, title = "Post-calculating smoothing", inline = "post_smoothing_switch", group = "Smoothing's parameters")
  30.  
  31. pre_smoothing = pre_smoothing_switch ? pre_smoothing_value : 1
  32. post_smoothing = post_smoothing_switch ? post_smoothing_value : 1
  33.  
  34. // Graphical's parameters
  35.  
  36. sensitivity_switch = input.bool (true, "", inline = "sensitivity_value", group = "Graphical's parameters")
  37. correlation_switch = input.bool (true, "", inline = "correlation_size", group = "Graphical's parameters")
  38. level_1_switch = input.bool (true, "", inline = "level_1_value", group = "Graphical's parameters")
  39. level_2_switch = input.bool (true, "", inline = "level_2_value", group = "Graphical's parameters")
  40.  
  41. sensitivity_value = input.int (defval = 2, minval = 1, title = "Chart Sensitivity", inline = "sensitivity_value", group = "Graphical's parameters")
  42. correlation_size = input.int (defval = 3, minval = 1, title = "Correlation dots", inline = "correlation_size", group = "Graphical's parameters")
  43. level_1_value = input.float (defval = 1, minval = 0, title = "Horizontal line 1", inline = "level_1_value", group = "Graphical's parameters")
  44. level_2_value = input.float (defval = 3, minval = 0, title = "Horizontal line 2", inline = "level_2_value", group = "Graphical's parameters")
  45.  
  46. level_1 = level_1_switch and sensitivity_switch ? math.atan(level_1_value / sensitivity_value) : level_1_switch ? level_1_value : na
  47. level_2 = level_2_switch and sensitivity_switch ? math.atan(level_2_value / sensitivity_value) : level_2_switch ? level_2_value : na
  48.  
  49. // Calculations ----------------------------------------------------------------
  50.  
  51. // Requesting values
  52.  
  53. request () =>
  54. source = close
  55. change = ta.ema(ta.change(close, period), pre_smoothing)
  56. atr = ta.atr(period) [1]
  57.  
  58. [source, change, atr]
  59.  
  60. [index_source, index_change, index_atr] = request.security(index , timeframe.period, request())
  61. [symbol_source, symbol_change, symbol_atr] = request.security(symbol, timeframe.period, request())
  62.  
  63. // Calculating RS
  64.  
  65. power_index = index_change / index_atr
  66. expected_change = power_index * symbol_atr
  67. relative_strength = (symbol_change - expected_change) / symbol_atr * (volume / ta.sma(volume, length) [1])
  68.  
  69. // Calculating correlation
  70.  
  71. correlation_value = ta.correlation(symbol_source, index_source, period)
  72.  
  73. // Creating an awesome chart ---------------------------------------------------
  74.  
  75. // Relative strength
  76.  
  77. graphical_strength = ta.ema(sensitivity_switch ? math.atan(relative_strength / sensitivity_value) : relative_strength, post_smoothing)
  78.  
  79. plot (graphical_strength, style = plot.style_columns, color = graphical_strength > 0 ? color.rgb(13,183,135,50) : color.rgb(239,83,80,50))
  80.  
  81. // Horizontal levels
  82.  
  83. plot ( level_1, color = color.new(color.yellow, 60))
  84. plot (-level_1, color = color.new(color.yellow, 60))
  85. plot ( level_2, color = color.new(color.yellow, 60))
  86. plot (-level_2, color = color.new(color.yellow, 60))
  87.  
  88. // Correlation
  89.  
  90. correlation_color = color.rgb(239 - 113 * (correlation_value + 1), 83 + 50 * (correlation_value + 1), 80 + 27.5 * (correlation_value + 1))
  91.  
  92. plot(correlation_switch ? 0 : na, color = color.rgb(0,0,0), style=plot.style_circles, linewidth = correlation_size + 2)
  93. plot(correlation_switch ? 0 : na, color = correlation_color, style=plot.style_circles, linewidth = correlation_size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement