xmd79

RSI Exponential Smoothing (Expo)

May 22nd, 2023
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.96 KB | None | 0 0
  1. // This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
  2. // © Zeiierman
  3.  
  4. //@version=5
  5. indicator("RSI Exponential Smoothing (Expo)", overlay=true)
  6.  
  7. // ~~ Inputs {
  8. rsiperiod = input.int(14,minval=2, title="RSI Period", inline="RSI")
  9. ob = input.int(70,minval=50, maxval=100, title="Overbought", inline="obos")
  10. os = input.int(30,minval=0, maxval=50, title="Oversold", inline="obos")
  11. obosbands = input.bool(true, title="OBOS Band", inline="RSI")
  12. fibbands = input.bool(false, title="FIB Band", inline="RSI")
  13. labels = input.bool(true, title="RSI Labels", inline="RSI")
  14. // ~~ Line Colors {
  15. ema_col = input.color(color.rgb(0, 0, 255), title="EMA",inline="c")
  16. ema_ob_col = input.color(color.rgb(77, 255, 41), title="OB",inline="c")
  17. ema_os_col = input.color(color.rgb(255, 65, 47), title="OS",inline="c")
  18. ema_fib_col= input.color(color.blue, title="FIB",inline="c")
  19. //~~~}
  20. // ~~ Table Inputs {
  21. showTable = input.bool(true,title="Show Table", inline="tbl", group="Table")
  22. validity_check = input.bool(false,title="Show Validity check", inline="tbl", group="Table")
  23. TblSize = input.string(size.normal,title="",options=[size.auto,size.tiny,size.small,size.normal,size.large,size.huge],inline="tbl", group="Table")
  24. pos = input.string(position.top_right, title="",options =[position.top_right,position.top_center,
  25. position.top_left,position.bottom_right,position.bottom_center,position.bottom_left,position.middle_right,position.middle_left],inline="tbl", group="Table")
  26. textcolor = input.color(color.white, title="Text",inline="tbl_col", group="Table")
  27. bgcolor = input.color(color.new(color.blue,30), title="Bg",inline="tbl_col", group="Table")
  28. postrend = input.color(color.new(color.lime,0), title="Trend",inline="tbl_col", group="Table")
  29. negtrend = input.color(color.new(color.red,0), title="",inline="tbl_col", group="Table")
  30. ob_col = input.color(color.new(color.lime,0), title="OB/OS",inline="tbl_col", group="Table")
  31. os_col = input.color(color.new(color.red,0), title="",inline="tbl_col", group="Table")
  32. //~~~}
  33. //~~~~~~}
  34.  
  35. // ~~ EMA RSI Approx Calculation {
  36. // ~~ Exponential Smoothing Coefficient Calculation {
  37. exponential_period = 2 * rsiperiod - 1 // "exponential_period" is the length of the moving average, represented as "2 * rsiperiod - 1", where "rsiperiod" is a user-defined value.
  38. smoothing_coefficient = 2 / (exponential_period + 1) // "smoothing_coefficient" is a value used to calculate the EMA, represented as "2 / (exponential_period + 1)". This value determines the weight given to the current observation versus the previous average in the calculation.
  39. //~~~}
  40.  
  41. // ~~ Exponential Smoothing RSI Calculation {
  42. // The code calculates two running averages, "averageUp" and "averageDown", using the exponential smoothing formula with the smoothing coefficient value defined in the previous code snippet.
  43. // The "netValue" variable is then calculated as the weighted difference between the "averageUp" and "averageDown" values, with a factor determined by the user-defined "rsiperiod" value.
  44. // The "result" variable is calculated based on the sign of "netValue". If "netValue" is positive, the result is equal to the current close price plus "netValue". If "netValue" is negative, the result is equal to the close price plus a fraction of "netValue" determined by the "level" parameter
  45. emaresult(level)=>
  46. averageUp = 0.0
  47. averageDown = 0.0
  48. averageUp := close > close[1] ? smoothing_coefficient* (close - close[1]) + (1 - smoothing_coefficient) * (averageUp[1] ? averageUp[1] : 1) : (1-smoothing_coefficient) * (averageUp[1] ? averageUp[1] : 1)
  49. averageDown := close > close[1] ? (1-smoothing_coefficient) * (averageDown[1] ? averageDown[1] : 1) : smoothing_coefficient * (close[1] - close) + (1 - smoothing_coefficient) * (averageDown[1] ? averageDown[1] : 1)
  50. netValue = (rsiperiod - 1) * (averageDown * level / (100 - level) - averageUp)
  51. result = netValue >= 0 ? close + netValue : close + netValue * (100 - level) / level
  52. //~~~}
  53.  
  54. // ~~ Return EMA {
  55. ema_result = emaresult(50)
  56. // Plot the Optimal EMA based on the RSI value
  57. plot(ema_result, color=ema_col, title="Optimal EMA")
  58. //~~~}
  59. //~~~~~~}
  60.  
  61. // ~~ Calculate RSI Standard Deviation, in order to calculate the Deviation {
  62. StandardDeviation(src, period)=>
  63. mean = ta.sma(src, period)
  64. squared_diff = ((src - mean) * (src - mean))
  65. sum_squared_diff = math.sum(squared_diff, period)
  66. std_dev = math.sqrt(sum_squared_diff/period)
  67. //~~~}
  68.  
  69. // ~~ Return RSI Standard Deviation {
  70. rsi = ta.rsi(close,rsiperiod)
  71. std_dev = StandardDeviation(rsi, rsiperiod)
  72. //~~~}
  73.  
  74. // ~~ Calculate Deviation {
  75. //It calculates the difference between the RSI value of (level) and the RSI mean value, then divides that difference by the standard deviation.
  76. //This number represents how many standard deviations away the RSI Level is from its mean.
  77. dev(obos)=>
  78. deviating = (obos-ema_result)/std_dev
  79. //~~~}
  80.  
  81. // ~~ Hull Moving Average Calculation, in order to smooth the Upper OB and Lower OS lines {
  82. hma(src,len)=>
  83. wma1 = ta.wma(2 * ta.wma(src, len/2) - ta.wma(src, len), math.round(math.sqrt(0.5)))
  84. wma2 = ta.wma(2 * ta.wma(wma1, len/2) - ta.wma(wma1, len), math.round(math.sqrt(0.5)))
  85. //~~~}
  86.  
  87. // ~~ Return EMA OB/OS Lines {
  88. // Deviation refers to how far a data point is from the mean (average) of the data set. It is calculated as the difference between the data point and the mean.
  89. // Standard deviation is a measure of how much the values in a data set vary around the mean. It provides a summary of the distribution of the data.
  90. // When the deviation of a data point is multiplied by the standard deviation, it gives us a measure of how many standard deviations away from the mean the data point is.
  91. // This measure is useful in understanding the level of variation in the data set and making predictions about the data.
  92. // The deviation * stdev is used to add an offset to the EMA calculation
  93. // It returns the Upper Overbougth and Lower Oversold levels on the chart.
  94. ob_dev = dev(emaresult(ob)) // The deviation of the OB level from the mean. This number represents how many standard deviations away the OB Level is from its mean.
  95. os_dev = dev(emaresult(os)) // The deviation of the OS level from the mean. This number represents how many standard deviations away the OS Level is from its mean.
  96. upper = hma(ta.rma(ema_result + (ob_dev*std_dev),3),10) // Returns the upper OB line on the chart.
  97. lower = hma(ta.rma(ema_result + (os_dev*std_dev),3),10) // Returns the upper OS line on the chart.
  98.  
  99. // ~~ Plot Upper OB and Lower OS lines on the chart {
  100. plot(obosbands?upper:na, "Upper OB", color=ema_ob_col)
  101. plot(obosbands?lower:na, "Lower OS", color=ema_os_col)
  102. plot(obosbands?upper - ta.stdev(upper,2):na, "Upper OB", color=ema_ob_col)
  103. plot(obosbands?lower + ta.stdev(lower,2):na, "Lower OS", color=ema_os_col)
  104. //~~~}
  105. //~~~~~~}
  106.  
  107. // ~~ Calculate fib 50% from OB to 50 level and OS to 50 level {
  108. fib(src)=>
  109. fib = ema_result + (src-ema_result)*0.5
  110.  
  111. // ~~ Plot the Fib lines on the chart {
  112. plot(fibbands?fib(upper):na, "Upper Fib 50%", color=ema_fib_col)
  113. plot(fibbands?fib(lower):na, "Lower Fib 50%", color=ema_fib_col)
  114. //~~~}
  115. //~~~~~~}
  116.  
  117. // ~~ Validity Checks {
  118. // ~~ Store crossover values to check Validity {
  119. // ~~ Optimal EMA Length {
  120. emavalue = 2*rsiperiod-1
  121. ema = ta.ema(close,emavalue)
  122. //~~~}
  123. var occurrence = 0
  124. var non_occurrence = 0
  125. if (ta.crossover(rsi,50) and ta.crossover(close,ema)) or (ta.crossunder(rsi,50) and ta.crossunder(close,ema))
  126. occurrence += 1
  127. if (ta.crossover(rsi,50) and not ta.crossover(close,ema)) or (ta.crossunder(rsi,50) and not ta.crossunder(close,ema))
  128. non_occurrence += 1
  129. //~~~~~~}
  130.  
  131. // ~~ Precentage Check {
  132. //The resulting value tells you how many occurrences there are relative to the total number of events, expressed as a percentage.
  133. percent = ((math.abs(occurrence)/(occurrence + non_occurrence) * 100))
  134. //~~~}
  135.  
  136. // ~~ Inputs to Table {
  137. // ~~ Trend Direction {
  138. dir = rsi>50?true:false
  139. sign = dir == true?" > 50 ":" < 50"
  140. trend = dir == true?"Bullish":"Bearish"
  141. col = dir == true?postrend:negtrend
  142. //~~~}
  143.  
  144. // ~~ Overbought & Oversold {
  145. ob_ = rsi>ob ?true:false
  146. os_ = rsi<os?true:false
  147. obos_sign = ob_? " > " + str.tostring(ob):os_? " < " + str.tostring(os):""
  148. obos_text = ob_? "Overbought":os_? "Oversold":""
  149. obos_col = ob_?ob_col:os_?os_col:color.white
  150. //~~~}
  151. //~~~~~~}
  152.  
  153. // ~~ Table {
  154. tbl = table.new(pos, 2, 11, frame_color=chart.bg_color, frame_width=2, border_width=2, border_color=chart.bg_color)
  155. if barstate.islast and showTable
  156. tbl.cell(0, 0, text="RSI Value", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
  157. tbl.cell(0, 1, text=str.tostring(rsiperiod), text_halign=text.align_center, bgcolor=bgcolor, text_color=textcolor, text_size=TblSize)
  158. tbl.cell(1, 0, text="EMA Value", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
  159. tbl.cell(1, 1, text=str.tostring(emavalue), text_halign=text.align_center, bgcolor=bgcolor, text_color=textcolor, text_size=TblSize)
  160. tbl.cell(0, 2, text="RSI" + sign + "", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
  161. tbl.cell(1, 2, text=trend, text_halign=text.align_center, bgcolor=col, text_color=textcolor, text_size=TblSize)
  162. if (ob_) or (os_)
  163. tbl.cell(0, 3, text="RSI" + obos_sign + "", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
  164. tbl.cell(1, 3, text=obos_text, text_halign=text.align_center, bgcolor=obos_col, text_color=textcolor, text_size=TblSize)
  165. if validity_check
  166. tbl.cell(0, 4, text="Validity Check", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
  167. tbl.cell(1, 4, text=str.tostring(percent,format.percent), text_halign=text.align_center, bgcolor=color.new(color.lime,30), text_color=textcolor, text_size=TblSize)
  168. tbl.cell(0, 5, text="RSI Stdev", text_color=textcolor, bgcolor=bgcolor, text_size=TblSize)
  169. tbl.cell(1, 5, text=str.tostring(math.round(std_dev)), text_halign=text.align_center, bgcolor=color.new(color.lime,30), text_color=textcolor, text_size=TblSize)
  170.  
  171.  
  172. // ~~ Labels {
  173. labelfunc(bool_,n, line_, text_, label_col, text_col, size_)=>
  174. label l = bool_?label.new(bar_index + n,line_,text=text_,color=label_col,
  175. style=label.style_label_left,textcolor=text_col,size=size_,
  176. textalign=text.align_left):na
  177. label.delete(l[1])
  178.  
  179. labelfunc(labels,5, ema_result, "RSI 50" , color.blue, color.white, size.small)
  180. labelfunc(labels,5, upper, "RSI " + str.tostring(ob) , color.green, color.white, size.small)
  181. labelfunc(labels,5, lower, "RSI " + str.tostring(os) , color.red, color.white, size.small)
  182. labelfunc(fibbands and labels,1, fib(upper), "FIB 50 %" , color.blue, color.white, size.tiny)
  183. labelfunc(fibbands and labels,1, fib(lower), "FIB 50 %" , color.blue, color.white, size.tiny)
  184. //~~~}
  185.  
Advertisement
Add Comment
Please, Sign In to add comment