xmd79

Stochastic RSI of Smoothed Price [Loxx]

Apr 25th, 2023 (edited)
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 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. // © loxx
  3.  
  4. //@version=5
  5. indicator("Stochastic RSI of Smoothed Price [Loxx]",
  6. overlay = false,
  7. timeframe="",
  8. timeframe_gaps = true)
  9.  
  10. greencolor = #2DD204
  11. redcolor = #D2042D
  12.  
  13. import loxx/loxxvarietyrsi/1 as lxrsi
  14. import loxx/loxxexpandedsourcetypes/4
  15.  
  16. rsimodeout(rsitype)=>
  17. rsimode = switch rsitype
  18. "RSX" => "rsi_rsx"
  19. "Regular" => "rsi_rsi"
  20. "Slow" => "rsi_slo"
  21. "Rapid" => "rsi_rap"
  22. "Harris" => "rsi_har"
  23. "Cuttler" => "rsi_cut"
  24. "Ehlers Smoothed" => "rsi_ehl"
  25. => "rsi_rsi"
  26. rsimode
  27.  
  28. variant(type, src, len) =>
  29. sig = 0.0
  30. if type == "SMA"
  31. sig := ta.sma(src, len)
  32. else if type == "EMA"
  33. sig := ta.ema(src, len)
  34. else if type == "WMA"
  35. sig := ta.wma(src, len)
  36. else if type == "RMA"
  37. sig := ta.rma(src, len)
  38. else if type == "VWMA"
  39. sig := ta.rma(src, len)
  40. sig
  41.  
  42. string SM02 = 'Fixed Levels'
  43. string SM03 = 'Floating Levels'
  44. string SM04 = 'Quantile Levels'
  45. string SM05 = 'Fixed Middle'
  46. string SM06 = 'Floating Middle'
  47. string SM07 = 'Quantile Middle'
  48.  
  49. StocPeriod = input.int(32, "Stochastic Period", group = "Stochastic Settings")
  50. StocSlowing = input.int(8, "Stochastic Slowing Period", group = "Stochastic Settings")
  51.  
  52. smthtype = input.string("Kaufman", "HAB Calc Type", options = ["AMA", "T3", "Kaufman"], group='RSI Settings')
  53. srcin = input.string("Close", "Source",
  54. options =
  55. ["Close", "Open", "High", "Low", "Median", "Typical", "Weighted", "Average", "Average Median Body", "Trend Biased", "Trend Biased (Extreme)",
  56. "HA Close", "HA Open", "HA High", "HA Low", "HA Median", "HA Typical", "HA Weighted", "HA Average", "HA Average Median Body", "HA Trend Biased", "HA Trend Biased (Extreme)",
  57. "HAB Close", "HAB Open", "HAB High", "HAB Low", "HAB Median", "HAB Typical", "HAB Weighted", "HAB Average", "HAB Average Median Body", "HAB Trend Biased", "HAB Trend Biased (Extreme)"],
  58. group='RSI Settings')
  59.  
  60.  
  61. PriceSmoothing = input.int(8, "Price Smoothing Period", group = "RSI Settings")
  62. matype = input.string("SMA", "RSI Smoothing Type", options = ["EMA", "WMA", "RMA", "SMA", "VWMA"], group = "RSI Settings")
  63.  
  64. RsiPeriod = input.int(32, "RSI Period", group = "RSI Settings")
  65. rsitype = input.string("Regular", "RSI type", options = ["RSX", "Regular", "Slow", "Rapid", "Harris", "Cuttler", "Ehlers Smoothed"], group = "RSI Settings")
  66.  
  67. MinMaxPeriod = input.int(25, "Levels Period", group = "Signal Settings")
  68. LevelUp = input.int(80, "Upper %", group = "Signal Settings")
  69. LevelDown = input.int(20, "Lower %", group = "Signal Settings")
  70. sigtype = input.string(SM03, "Signal type", options = [SM02, SM03, SM04, SM05, SM06, SM07], group = "Signal Settings")
  71.  
  72. colorBars = input.bool(true, "Color bars?", group = "UI Options")
  73. showSigs = input.bool(true, "Show Signals?", group = "UI Options")
  74.  
  75.  
  76. kfl = input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only - Fast End", group = "Moving Average Inputs")
  77. ksl = input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only - Slow End", group = "Moving Average Inputs")
  78. amafl = input.int(2, title="* Adaptive Moving Average (AMA) Only - Fast", group = "Moving Average Inputs")
  79. amasl = input.int(30, title="* Adaptive Moving Average (AMA) Only - Slow", group = "Moving Average Inputs")
  80. t3hot = input.float(.7, "* T3 Only - T3 Hot", group = "Moving Average Inputs")
  81. t3swt = input.string("T3 New", "* T3 Only - T3 Type", options = ["T3 New", "T3 Original"], group = "Moving Average Inputs")
  82.  
  83. haclose = ohlc4
  84. haopen = float(na)
  85. haopen := na(haopen[1]) ? (open + close) / 2 : (nz(haopen[1]) + nz(haclose[1])) / 2
  86. hahigh =math.max(high, math.max(haopen, haclose))
  87. halow = math.min(low, math.min(haopen, haclose))
  88. hamedian = (hahigh + halow) / 2
  89. hatypical = (hahigh + halow + haclose) / 3
  90. haweighted = (hahigh + halow + haclose + haclose)/4
  91. haaverage = (haopen + hahigh + halow + haclose)/4
  92.  
  93. sourceout(smthtype, srcin)=>
  94. src = switch srcin
  95. "Close" => close
  96. "Open" => open
  97. "High" => high
  98. "Low" => low
  99. "Median" => hl2
  100. "Typical" => hlc3
  101. "Weighted" => hlcc4
  102. "Average" => ohlc4
  103. "Average Median Body" => (open + close)/2
  104. "Trend Biased" => loxxexpandedsourcetypes.rtrendb()
  105. "Trend Biased (Extreme)" => loxxexpandedsourcetypes.rtrendbext()
  106. "HA Close" => loxxexpandedsourcetypes.haclose(haclose)
  107. "HA Open" => loxxexpandedsourcetypes.haopen(haopen)
  108. "HA High" => loxxexpandedsourcetypes.hahigh(hahigh)
  109. "HA Low" => loxxexpandedsourcetypes.halow(halow)
  110. "HA Median" => loxxexpandedsourcetypes.hamedian(hamedian)
  111. "HA Typical" => loxxexpandedsourcetypes.hatypical(hatypical)
  112. "HA Weighted" => loxxexpandedsourcetypes.haweighted(haweighted)
  113. "HA Average" => loxxexpandedsourcetypes.haaverage(haaverage)
  114. "HA Average Median Body" => loxxexpandedsourcetypes.haavemedbody(haclose, haopen)
  115. "HA Trend Biased" => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
  116. "HA Trend Biased (Extreme)" => loxxexpandedsourcetypes.hatrendbext(haclose, haopen, hahigh, halow)
  117. "HAB Close" => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl, ksl)
  118. "HAB Open" => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl, ksl)
  119. "HAB High" => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl, ksl)
  120. "HAB Low" => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
  121. "HAB Median" => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl, kfl, ksl)
  122. "HAB Typical" => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl, kfl, ksl)
  123. "HAB Weighted" => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl, kfl, ksl)
  124. "HAB Average" => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl, kfl, ksl)
  125. "HAB Average Median Body" => loxxexpandedsourcetypes.habavemedbody(smthtype, amafl, amasl, kfl, ksl)
  126. "HAB Trend Biased" => loxxexpandedsourcetypes.habtrendb(smthtype, amafl, amasl, kfl, ksl)
  127. "HAB Trend Biased (Extreme)" => loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
  128. => close
  129.  
  130. rsi = lxrsi.rsiVariety(rsimodeout(rsitype), variant(matype, sourceout(smthtype, srcin), PriceSmoothing), RsiPeriod)
  131.  
  132. stoch = ta.sma(ta.stoch(rsi, rsi, rsi, StocPeriod), StocSlowing)
  133.  
  134. float lvlup = LevelUp
  135. float lvldn = LevelDown
  136. float lvlmid = 50.
  137.  
  138. if sigtype == SM03 or sigtype == SM06
  139. fmin = ta.lowest(stoch, MinMaxPeriod)
  140. fmax = ta.highest(stoch, MinMaxPeriod)
  141. rng = fmax - fmin
  142. lvlup := fmin + LevelUp * rng / 100.0
  143. lvldn := fmin + LevelDown * rng / 100.0
  144. lvlmid := (lvlup + lvldn) * 0.5
  145.  
  146. if sigtype == SM04 or sigtype == SM07
  147. lvlup := ta.percentile_linear_interpolation(stoch, MinMaxPeriod, LevelUp)
  148. lvldn := ta.percentile_linear_interpolation(stoch, MinMaxPeriod, LevelDown)
  149. lvlmid := ta.percentile_linear_interpolation(stoch, MinMaxPeriod, (LevelUp + LevelDown) * 0.5)
  150.  
  151. state = 0
  152.  
  153. if sigtype == SM02 or sigtype == SM03 or sigtype == SM04
  154. if stoch > lvlup
  155. state := 1
  156. if stoch < lvldn
  157. state := -1
  158.  
  159. if sigtype == SM05 or sigtype == SM06 or sigtype == SM07
  160. if stoch > lvlmid
  161. state := 1
  162. if stoch < lvlmid
  163. state := -1
  164.  
  165. colorout = state == 1 ? greencolor : state == -1 ? redcolor : color.gray
  166.  
  167. plot(lvlmid, "Middle", color = color.gray)
  168. plot(lvlup, "Upper level", color = greencolor)
  169. plot(lvldn, "Lower level", color = redcolor)
  170. plot(stoch, "Stochastic", color = colorout, linewidth = 2)
  171.  
  172. barcolor(colorBars ? colorout : na)
  173.  
  174. goLong = state == 1 and state[1] != 1
  175. goShort = state == -1 and state[1] != -1
  176.  
  177. plotshape(showSigs and goLong, title = "Long", color = color.yellow, textcolor = color.yellow, text = "L", style = shape.triangleup, location = location.bottom, size = size.auto)
  178. plotshape(showSigs and goShort, title = "Short", color = color.fuchsia, textcolor = color.fuchsia, text = "S", style = shape.triangledown, location = location.top, size = size.auto)
  179.  
  180. alertcondition(goLong, title="Long", message="Stochastic RSI of Smoothed Price [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
  181. alertcondition(goShort, title="Short", message="Stochastic RSI of Smoothed Price [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")
Advertisement
Add Comment
Please, Sign In to add comment