xmd79

RSI+ [RSX DIV CSRI]

May 14th, 2022 (edited)
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 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. // © Sanctom
  3.  
  4. //@version=4
  5. study("RSI+ [RSX DIV CSRI]", shorttitle="RS+")
  6.  
  7.  
  8. length = input(title="Length", type=input.integer, defval=14)
  9. RSXsrc = input(title="Source", type=input.source, defval=hlc3)
  10. obLevel = input(title="OB Level", type=input.integer, defval=80)
  11. osLevel = input(title="OS Level", type=input.integer, defval=25)
  12. highlightBreakouts = input(title="Highlight Overbought/Oversold Breakouts ?", type=input.bool, defval=false)
  13.  
  14. piv = input(false, "Hide pivots?")
  15. shrt = input(false, "Shorter labels?")
  16. xbars = input(28, "Div lookback period (bars)?", input.integer, minval=1)
  17.  
  18. len = input(title="RSI Period", minval=1, defval=9)
  19. src = input(title="RSI Source", defval=close)
  20. lbR = input(title="Pivot Lookback Right", defval=5)
  21. lbL = input(title="Pivot Lookback Left", defval=4)
  22. rangeUpper = input(title="Max of Lookback Range", defval=60)
  23. rangeLower = input(title="Min of Lookback Range", defval=5)
  24. plotBull = input(title="Plot Bullish", defval=true)
  25. plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
  26. plotBear = input(title="Plot Bearish", defval=true)
  27. plotHiddenBear = input(title="Plot Hidden Bearish", defval=true)
  28.  
  29. lenrsi = input(3, "RSI Length")
  30. lenupdown = input(2, "UpDown Length")
  31. lenroc = input(100, "ROC Length")
  32. updown(s) =>
  33. isEqual = s == s[1]
  34. isGrowing = s > s[1]
  35. ud = 0.0
  36. ud := isEqual ? 0 : isGrowing ? (nz(ud[1]) <= 0 ? 1 : nz(ud[1])+1) : (nz(ud[1]) >= 0 ? -1 : nz(ud[1])-1)
  37. ud
  38. rsi = rsi(src, lenrsi)
  39. updownrsi = rsi(updown(src), lenupdown)
  40. percentrank = percentrank(roc(src, 1), lenroc)
  41. crsi = avg(rsi, updownrsi, percentrank)
  42.  
  43.  
  44. bearColor = #CB0000
  45. bullColor = #008100
  46. hiddenBullColor = color.new(color.green, 80)
  47. hiddenBearColor = color.new(color.red, 80)
  48. textColor = color.white
  49. noneColor = color.new(color.white, 100)
  50.  
  51. osc = rsi(src, len)
  52.  
  53.  
  54.  
  55.  
  56. plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
  57. phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
  58.  
  59. _inRange(cond) =>
  60. bars = barssince(cond == true)
  61. rangeLower <= bars and bars <= rangeUpper
  62.  
  63. //------------------------------------------------------------------------------
  64. // Regular Bullish
  65.  
  66. // Osc: Higher Low
  67. oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
  68.  
  69. // Price: Lower Low
  70. priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
  71.  
  72. bullCond = plotBull and priceLL and oscHL and plFound
  73.  
  74.  
  75.  
  76. //------------------------------------------------------------------------------
  77. // Hidden Bullish
  78.  
  79. // Osc: Lower Low
  80. oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
  81.  
  82. // Price: Higher Low
  83. priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
  84.  
  85. hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
  86.  
  87.  
  88.  
  89. //------------------------------------------------------------------------------
  90. // Regular Bearish
  91.  
  92. // Osc: Lower High
  93. oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
  94.  
  95. // Price: Higher High
  96. priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
  97.  
  98. bearCond = plotBear and priceHH and oscLH and phFound
  99.  
  100.  
  101. //------------------------------------------------------------------------------
  102. // Hidden Bearish
  103.  
  104. // Osc: Higher High
  105. oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
  106.  
  107. // Price: Lower High
  108. priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
  109.  
  110. hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
  111.  
  112.  
  113.  
  114.  
  115. f8 = 100 * RSXsrc
  116. f10 = nz(f8[1])
  117. v8 = f8 - f10
  118.  
  119. f18 = 3 / (length + 2)
  120. f20 = 1 - f18
  121.  
  122. f28 = 0.0
  123. f28 := f20 * nz(f28[1]) + f18 * v8
  124.  
  125. f30 = 0.0
  126. f30 := f18 * f28 + f20 * nz(f30[1])
  127. vC = f28 * 1.5 - f30 * 0.5
  128.  
  129. f38 = 0.0
  130. f38 := f20 * nz(f38[1]) + f18 * vC
  131.  
  132. f40 = 0.0
  133. f40 := f18 * f38 + f20 * nz(f40[1])
  134. v10 = f38 * 1.5 - f40 * 0.5
  135.  
  136. f48 = 0.0
  137. f48 := f20 * nz(f48[1]) + f18 * v10
  138.  
  139. f50 = 0.0
  140. f50 := f18 * f48 + f20 * nz(f50[1])
  141. v14 = f48 * 1.5 - f50 * 0.5
  142.  
  143. f58 = 0.0
  144. f58 := f20 * nz(f58[1]) + f18 * abs(v8)
  145.  
  146. f60 = 0.0
  147. f60 := f18 * f58 + f20 * nz(f60[1])
  148. v18 = f58 * 1.5 - f60 * 0.5
  149.  
  150. f68 = 0.0
  151. f68 := f20 * nz(f68[1]) + f18 * v18
  152.  
  153. f70 = 0.0
  154. f70 := f18 * f68 + f20 * nz(f70[1])
  155. v1C = f68 * 1.5 - f70 * 0.5
  156.  
  157. f78 = 0.0
  158. f78 := f20 * nz(f78[1]) + f18 * v1C
  159.  
  160. f80 = 0.0
  161. f80 := f18 * f78 + f20 * nz(f80[1])
  162. v20 = f78 * 1.5 - f80 * 0.5
  163.  
  164. f88_ = 0.0
  165. f90_ = 0.0
  166.  
  167. f88 = 0.0
  168. f90_ := nz(f90_[1]) == 0 ? 1 :
  169. nz(f88[1]) <= nz(f90_[1]) ? nz(f88[1]) + 1 : nz(f90_[1]) + 1
  170. f88 := nz(f90_[1]) == 0 and length - 1 >= 5 ? length - 1 : 5
  171.  
  172. f0 = f88 >= f90_ and f8 != f10 ? 1 : 0
  173. f90 = f88 == f90_ and f0 == 0 ? 0 : f90_
  174.  
  175. v4_ = f88 < f90 and v20 > 0 ? (v14 / v20 + 1) * 50 : 50
  176. rsx = v4_ > 100 ? 100 : v4_ < 0 ? 0 : v4_
  177.  
  178. rsxColor = rsx > obLevel ? #007300 : rsx < osLevel ? #CB0000 : #787b86
  179.  
  180. transparent = color.new(color.white, 100)
  181.  
  182. obFillColor = rsx > obLevel and highlightBreakouts ? color.green : transparent
  183. osFillColor = rsx < osLevel and highlightBreakouts ? color.red : transparent
  184.  
  185.  
  186.  
  187. hb = abs(highestbars(rsx, xbars)) // Finds bar with highest value in last X bars
  188. lb = abs(lowestbars(rsx, xbars)) // Finds bar with lowest value in last X bars
  189. max = float(na)
  190. max_rsi = float(na)
  191. min = float(na)
  192. min_rsi = float(na)
  193. pivoth = bool(na)
  194. pivotl = bool(na)
  195. divbear = bool(na)
  196. divbull = bool(na)
  197.  
  198. // If bar with lowest / highest is current bar, use it's value
  199. max := hb == 0 ? close : na(max[1]) ? close : max[1]
  200. max_rsi := hb == 0 ? rsx : na(max_rsi[1]) ? rsx : max_rsi[1]
  201. min := lb == 0 ? close : na(min[1]) ? close : min[1]
  202. min_rsi := lb == 0 ? rsx : na(min_rsi[1]) ? rsx : min_rsi[1]
  203.  
  204. // Compare high of current bar being examined with previous bar's high
  205. // If curr bar high is higher than the max bar high in the lookback window range
  206. if close > max // we have a new high
  207. max := close // change variable "max" to use current bar's high value
  208. max
  209. if rsx > max_rsi // we have a new high
  210. max_rsi := rsx // change variable "max_rsi" to use current bar's RSI value
  211. max_rsi
  212. if close < min // we have a new low
  213. min := close // change variable "min" to use current bar's low value
  214. min
  215. if rsx < min_rsi // we have a new low
  216. min_rsi := rsx // change variable "min_rsi" to use current bar's RSI value
  217. min_rsi
  218.  
  219. // Finds pivot point with at least 2 right candles with lower value
  220. pivoth := max_rsi == max_rsi[2] and max_rsi[2] != max_rsi[3] ? true : na
  221. pivotl := min_rsi == min_rsi[2] and min_rsi[2] != min_rsi[3] ? true : na
  222.  
  223. // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
  224. if max[1] > max[2] and rsx[1] < max_rsi and rsx <= rsx[1]
  225. divbear := true
  226. divbear
  227. if min[1] < min[2] and rsx[1] > min_rsi and rsx >= rsx[1]
  228. divbull := true
  229. divbull
  230.  
  231. // Alerts
  232. alertcondition(divbear, title='Bear div', message='Bear div')
  233. alertcondition(divbull, title='Bull div', message='Bull div')
  234.  
  235. // Plots
  236.  
  237. plot(rsx, title="RSX", linewidth=2, color=rsxColor )
  238.  
  239. plot(osc, title="RSI", linewidth=1, color=#000000)
  240.  
  241. plot(crsi, "CRSI", linewidth=1, color=#b2b5be)
  242.  
  243.  
  244. maxLevelPlot = hline(100, title="Max Level", linestyle=hline.style_dotted, color=transparent)
  245. obLevelPlot = hline(obLevel, title="Overbought Level", linestyle=hline.style_dotted)
  246. hline(50, title="Middle Level", linestyle=hline.style_dotted)
  247. osLevelPlot = hline(osLevel, title="Oversold Level", linestyle=hline.style_dotted)
  248. minLevelPlot = hline(0, title="Min Level", linestyle=hline.style_dotted, color=transparent)
  249.  
  250. fill(obLevelPlot, osLevelPlot, color=color.white, transp=100)
  251.  
  252. fill(maxLevelPlot, obLevelPlot, color=obFillColor, transp=100)
  253. fill(minLevelPlot, osLevelPlot, color=osFillColor, transp=100)
  254.  
  255. plot(
  256. plFound ? osc[lbR] : na,
  257. offset=-lbR,
  258. title="Regular Bullish",
  259. linewidth=2,
  260. color=(bullCond ? bullColor : noneColor)
  261. )
  262.  
  263. plotshape(
  264. bullCond ? osc[lbR] : na,
  265. offset=-lbR,
  266. title="Regular Bullish Label",
  267. text=" Bull ",
  268. style=shape.labelup,
  269. location=location.absolute,
  270. color=bullColor,
  271. textcolor=textColor
  272. )
  273.  
  274. plot(
  275. plFound ? osc[lbR] : na,
  276. offset=-lbR,
  277. title="Hidden Bullish",
  278. linewidth=2,
  279. color=(hiddenBullCond ? hiddenBullColor : noneColor)
  280. )
  281.  
  282. plotshape(
  283. hiddenBullCond ? osc[lbR] : na,
  284. offset=-lbR,
  285. title="Hidden Bullish Label",
  286. text=" H Bull ",
  287. style=shape.labelup,
  288. location=location.absolute,
  289. color=#6f6f6f,
  290. textcolor=textColor
  291. )
  292.  
  293. plot(
  294. phFound ? osc[lbR] : na,
  295. offset=-lbR,
  296. title="Regular Bearish",
  297. linewidth=2,
  298. color=(bearCond ? bearColor : noneColor)
  299. )
  300.  
  301. plotshape(
  302. bearCond ? osc[lbR] : na,
  303. offset=-lbR,
  304. title="Regular Bearish Label",
  305. text=" Bear ",
  306. style=shape.labeldown,
  307. location=location.absolute,
  308. color=bearColor,
  309. textcolor=textColor
  310. )
  311.  
  312.  
  313. plot(
  314. phFound ? osc[lbR] : na,
  315. offset=-lbR,
  316. title="Hidden Bearish",
  317. linewidth=2,
  318. color=(hiddenBearCond ? hiddenBearColor : noneColor)
  319. )
  320.  
  321. plotshape(
  322. hiddenBearCond ? osc[lbR] : na,
  323. offset=-lbR,
  324. title="Hidden Bearish Label",
  325. text=" H Bear ",
  326. style=shape.labeldown,
  327. location=location.absolute,
  328. color=#6f6f6f,
  329. textcolor=textColor
  330. )
  331.  
  332. // Plots divergences and pivots with offest
  333. // Longer labels
  334. plotshape(shrt ? na : divbear ? rsx[1] + 1 : na, location=location.absolute, style=shape.labeldown, color=#BF1D00, size=size.tiny, text="Bear", textcolor=color.white, offset=-1)
  335. plotshape(shrt ? na : divbull ? rsx[1] - 1 : na, location=location.absolute, style=shape.labelup, color=#008702, size=size.tiny, text="Bull", textcolor=color.white, offset=-1)
  336. plotshape(piv ? na : shrt ? na : pivoth ? max_rsi + 1 : na, location=location.absolute, style=shape.labeldown, color=#000000, size=size.tiny, text="Pivot", textcolor=color.white, offset=-2)
  337. plotshape(piv ? na : shrt ? na : pivotl ? min_rsi - 1 : na, location=location.absolute, style=shape.labelup, color=#000000, size=size.tiny, text="Pivot", textcolor=color.white, offset=-2)
  338.  
  339. // Shorter labels
  340. plotshape(shrt ? divbear ? rsx[1] + 3 : na : na, location=location.absolute, style=shape.triangledown, color=color.red, size=size.tiny, offset=-1)
  341. plotshape(shrt ? divbull ? rsx[1] - 3 : na : na, location=location.absolute, style=shape.triangleup, color=color.green, size=size.tiny, offset=-1)
  342. plotshape(piv ? na : shrt ? pivoth ? max_rsi + 3 : na : na, location=location.absolute, style=shape.triangledown, color=color.black, size=size.tiny, offset=-2)
  343. plotshape(piv ? na : shrt ? pivotl ? min_rsi - 3 : na : na, location=location.absolute, style=shape.triangleup, color=color.black, size=size.tiny, offset=-2)
  344.  
Advertisement
Add Comment
Please, Sign In to add comment