SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@version=3
  2. // Copyright by Libertus - 2018
  3. // RSI Divergences v2.3
  4. // Free for private use
  5. study(title="Relative Strength Index - Divergences - Libertus", shorttitle="RSI Div - Lib")
  6. len = input(14, minval=1, title="RSI Length")
  7. ob = input(70, "Overbought", integer, minval=0, maxval=100)
  8. os = input(30, "Oversold", integer, minval=0, maxval=100)
  9.  
  10. // RSI code
  11. rsi = rsi(close, len)
  12. band1 = hline(ob)
  13. band0 = hline(os)
  14. plot(rsi, color=(rsi > ob or rsi < os ? red : black), transp=0)
  15. fill(band1, band0, color=purple, transp=97)
  16.  
  17. // DIVS code
  18. piv = input(false,"Hide pivots?")
  19. shrt = input(false,"Shorter labels?")
  20. xbars = input(90, "Div lookback period (bars)?", integer, minval=1)
  21. hb = abs(highestbars(rsi, xbars)) // Finds bar with highest value in last X bars
  22. lb = abs(lowestbars(rsi, xbars)) // Finds bar with lowest value in last X bars
  23.  
  24. // Defining variable values, mandatory in Pine 3
  25. max = na
  26. max_rsi = na
  27. min = na
  28. min_rsi = na
  29. pivoth = na
  30. pivotl = na
  31. divbear = na
  32. divbull = na
  33.  
  34. // If bar with lowest / highest is current bar, use it's value
  35. max := hb == 0 ? close : na(max[1]) ? close : max[1]
  36. max_rsi := hb == 0 ? rsi : na(max_rsi[1]) ? rsi : max_rsi[1]
  37. min := lb == 0 ? close : na(min[1]) ? close : min[1]
  38. min_rsi := lb == 0 ? rsi : na(min_rsi[1]) ? rsi : min_rsi[1]
  39.  
  40. // Compare high of current bar being examined with previous bar's high
  41. // If curr bar high is higher than the max bar high in the lookback window range
  42. if close > max // we have a new high
  43.     max := close // change variable "max" to use current bar's high value
  44. if rsi > max_rsi // we have a new high
  45.     max_rsi := rsi // change variable "max_rsi" to use current bar's RSI value
  46. if close < min // we have a new low
  47.     min := close // change variable "min" to use current bar's low value
  48. if rsi < min_rsi // we have a new low
  49.     min_rsi := rsi // change variable "min_rsi" to use current bar's RSI value
  50.  
  51. // Finds pivot point with at least 2 right candles with lower value
  52. pivoth := (max_rsi == max_rsi[2]) and (max_rsi[2] != max_rsi[3]) ? true : na
  53. pivotl := (min_rsi == min_rsi[2]) and (min_rsi[2] != min_rsi[3]) ? true : na
  54.  
  55. // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
  56. if (max[1] > max[2]) and (rsi[1] < max_rsi) and (rsi <= rsi[1])
  57.     divbear := true
  58. if (min[1] < min[2]) and (rsi[1] > min_rsi) and (rsi >= rsi[1])
  59.     divbull := true
  60.  
  61. // Alerts
  62. alertcondition(divbear, title='Bear div', message='Bear div')
  63. alertcondition(divbull, title='Bull div', message='Bull div')
  64.  
  65. // Plots divergences and pivots with offest
  66. // Longer labels
  67. plotshape(shrt ? na : divbear ? rsi[1] + 1 : na, location=location.absolute, style=shape.labeldown, color=red, size=size.tiny, text="Bear", textcolor=white, transp=0, offset=-1)
  68. plotshape(shrt ? na : divbull ? rsi[1] - 1 : na, location=location.absolute, style=shape.labelup, color=green, size=size.tiny, text="Bull", textcolor=white, transp=0, offset=-1)
  69. plotshape(piv ? na : shrt ? na : pivoth ? max_rsi + 1 : na, location=location.absolute, style=shape.labeldown, color=blue, size=size.tiny, text="Pivot", textcolor=white, transp=0, offset=-2)
  70. plotshape(piv ? na : shrt ? na : pivotl ? min_rsi - 1 : na, location=location.absolute, style=shape.labelup, color=blue, size=size.tiny, text="Pivot", textcolor=white, transp=0, offset=-2)
  71.  
  72. // Shorter labels
  73. plotshape(shrt ? (divbear ? rsi[1] + 3 : na) : na, location=location.absolute, style=shape.triangledown, color=red, size=size.tiny, transp=0, offset=-1)
  74. plotshape(shrt ? (divbull ? rsi[1] - 3 : na) : na, location=location.absolute, style=shape.triangleup, color=green, size=size.tiny, transp=0, offset=-1)
  75. plotshape(piv ? na : shrt ? (pivoth ? max_rsi + 3 : na) : na, location=location.absolute, style=shape.triangledown, color=blue, size=size.tiny, transp=0, offset=-2)
  76. plotshape(piv ? na : shrt ? (pivotl ? min_rsi - 3 : na) : na, location=location.absolute, style=shape.triangleup, color=blue, size=size.tiny, transp=0, offset=-2)
  77.  
  78. // Debug tools
  79. // plot(max, color=blue, linewidth=2)
  80. // plot(max_rsi, color=red, linewidth=2)
  81. // plot(hb, color=orange, linewidth=2)
  82. // plot(lb, color=purple, linewidth=1)
  83. // plot(min_rsi, color=lime, linewidth=1)
  84. // plot(min, color=black, linewidth=1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top