Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. //@version=2
  2. //
  3.  
  4. study(title='DIVERGENCE_SPOTTER', shorttitle='DIV_SPOTTER++',overlay=true)
  5.  
  6. // || General Input:
  7. method = input(title='Method (0=RSI, 1=macd, 2=stoch, 3=volume, 4=acc/dist, 5=fisher, 6=cci, 7=BB %B, 8=IdealRSI, 9=snort):', type=integer, defval=9, minval=0, maxval=9)
  8. SHOW_LABEL = input(title='Show Labels', type=bool, defval=true)
  9. SHOW_CHANNEL = input(title='Show Channel', type=bool, defval=false)
  10. uHid = input(true,title="Use Hidden Divergence in Strategy")
  11. uReg = input(true,title="Use Regular Divergence in Strategy")
  12. // || RSI / STOCH / VOLUME / ACC/DIST/ FISHER/ CCI/ BB %B Input:
  13. rsi_smooth = input(title='RSI/STOCH/Volume/ACC-DIST/Fisher/cci Smooth/BB %B length:', type=integer, defval=20)
  14. // || MACD Input:
  15. macd_src = input(title='MACD Source:', type=source, defval=close)
  16. macd_fast = input(title='MACD Fast:', type=integer, defval=12)
  17. macd_slow = input(title='MACD Slow:', type=integer, defval=26)
  18. macd_smooth = input(title='MACD Smooth Signal:', type=integer, defval=9)
  19. // || Functions:
  20. f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
  21. f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
  22. f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
  23.  
  24. // ||••> START MACD FUNCTION
  25. f_macd(_src, _fast, _slow, _smooth)=>
  26. _fast_ma = sma(_src, _fast)
  27. _slow_ma = sma(_src, _slow)
  28. _macd = _fast_ma-_slow_ma
  29. _signal = ema(_macd, _smooth)
  30. _hist = _macd - _signal
  31. // ||<•• END MACD FUNCTION
  32.  
  33. // ||••> START ACC/DIST FUNCTION
  34. f_accdist(_smooth)=>_return=sma(cum(close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume), _smooth)
  35. // ||<•• END ACC/DIST FUNCTION
  36.  
  37. // ||••> START FISHER FUNCTION
  38. f_fisher(_src, _window)=>
  39. _h = highest(_src, _window)
  40. _l = lowest(_src, _window)
  41. _value0 = .66 * ((_src - _l) / max(_h - _l, .001) - .5) + .67 * nz(_value0[1])
  42. _value1 = _value0 > .99 ? .999 : _value0 < -.99 ? -.999 : _value0
  43. _fisher = .5 * log((1 + _value1) / max(1 - _value1, .001)) + .5 * nz(_fisher[1])
  44. // ||<•• END FISHER FUNCTION
  45.  
  46. // Rolling Moving Average (or Wells Wilders MA)
  47. irma(p,l) =>
  48. irma = (nz(irma[1]) * (l - 1) + p) / l
  49.  
  50. // RSI function.
  51. irsi(p, l) =>
  52. up = irma(max(change(p), 0), l)
  53. down = irma(-min(change(p), 0), l)
  54. irsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
  55. //
  56.  
  57. //
  58. // --- Start the Homodyne Discriminator Caculations
  59. //
  60. idealRSI(p) =>
  61. C1 = 0.0962
  62. C2 = 0.5769
  63. Df = 0.5
  64. C3 = (nz(Period[1])*0.075+0.54)
  65. smooth = ((hl2*4.0) + (hl2[1]*3.0) + (hl2[2]*2.0) + (hl2[3]))/10.0
  66. dDeTrend = (smooth*C1 + nz(smooth[2])*C2 - nz(smooth[4])*C2 - nz(smooth[6])*C1)*C3
  67. Q1 = (dDeTrend*C1 + nz(dDeTrend[2])*C2 - nz(dDeTrend[4])*C2 - nz(dDeTrend[6])*C1)*C3
  68. I1 = nz(dDeTrend[3])
  69. jI = (I1*C1 + nz(I1[2])*C2 - nz(I1[4])*C2 - nz(I1[6])*C1)*C3
  70. jQ = (Q1*C1 + nz(Q1[2])*C2 - nz(Q1[4])*C2 - nz(Q1[6])*C1)*C3
  71. I2_ = I1 - jQ
  72. Q2_ = Q1 + jI
  73. I2 = 0.2*I2_ + 0.8*nz(I2[1])
  74. Q2 = 0.2*Q2_ + 0.8*nz(Q2[1])
  75. Re_ = I2*nz(I2[1]) + Q2*nz(Q2[1])
  76. Im_ = I2*nz(Q2[1]) - Q2*nz(I2[1])
  77. Re = 0.2*Re_ + 0.8*nz(Re[1])
  78. Im = 0.2*Im_ + 0.8*nz(Im[1])
  79. dp_ = iff(Re!=0 and Im!=0 , 6.28318/atan(Im/Re) , 0)
  80. II = nz(Period[1])
  81. dp = max(max(min(min(dp_,1.5*II),50),0.6667*II),6)
  82. Period = dp*0.2 + nz(Period[1])*0.8
  83. SmoothPeriod = 0.33*Period + nz(SmoothPeriod[1])*0.67
  84. rsiLen = round((SmoothPeriod*Df)-1) // Get variable RSI length from discriminator
  85. idealRSI = irsi(p,rsiLen) // Generate RSI.
  86.  
  87. // --- Bollinger Band Vdub BB %B
  88. pcBB(p,l) =>
  89. basis = sma(p, l)
  90. dev = 0.1*stdev(p, l)
  91. upper = basis + dev
  92. lower = basis - dev
  93. pcBB = (p - lower)/(upper - lower)
  94.  
  95. // --- SNORT
  96. length = input(title="Length", type=integer, defval=14, minval=1, maxval=2000)
  97. src = hlc3
  98. upper = sum(volume * (change(src) <= 0 ? 0 : src), length)
  99. lower = sum(volume * (change(src) >= 0 ? 0 : src), length)
  100. mf = rsi(upper, lower)
  101.  
  102. clength = input(20, minval=1)
  103. csrc = input(close, title="Source")
  104. cma = sma(csrc, clength)
  105. cci = (csrc - cma) / (0.015 * dev(csrc, clength))
  106.  
  107. rsrc = close
  108. rlen = input(14, minval=1, title="Length")
  109. rup = rma(max(change(rsrc), 0), rlen)
  110. rdown = rma(-min(change(rsrc), 0), rlen)
  111. rsi = rdown == 0 ? 100 : rup == 0 ? 0 : 100 - (100 / (1 + rup / rdown))
  112.  
  113. snort = (((mf + rsi)/2)*5 + cci)/6
  114.  
  115. // === End of Functions.
  116.  
  117. method_high = method == 0 ? rsi(high, rsi_smooth) :
  118. method == 1 ? f_macd(macd_src, macd_fast, macd_slow, macd_smooth) :
  119. method == 2 ? stoch(close, high, low, rsi_smooth) :
  120. method == 3 ? sma(volume, rsi_smooth) :
  121. method == 4 ? f_accdist(rsi_smooth) :
  122. method == 5 ? f_fisher(high, rsi_smooth) :
  123. method == 6 ? cci(high, rsi_smooth) :
  124. method == 7 ? pcBB(close, rsi_smooth) :
  125. method == 8 ? idealRSI(close) :
  126. method == 9 ? snort :
  127. na
  128.  
  129. method_low = method == 0 ? rsi(low, rsi_smooth) :
  130. method == 1 ? f_macd(macd_src, macd_fast, macd_slow, macd_smooth) :
  131. method == 2 ? stoch(close, high, low, rsi_smooth) :
  132. method == 3 ? sma(volume, rsi_smooth) :
  133. method == 4 ? f_accdist(rsi_smooth) :
  134. method == 5 ? f_fisher(low, rsi_smooth) :
  135. method == 6 ? cci(low, rsi_smooth) :
  136. method == 7 ? pcBB(close, rsi_smooth) :
  137. method == 8 ? idealRSI(close) :
  138. method == 9 ? snort :
  139. na
  140.  
  141. fractal_top = f_fractalize(method_high) > 0 ? method_high[2] : na
  142. fractal_bot = f_fractalize(method_low) < 0 ? method_low[2] : na
  143.  
  144. high_prev = valuewhen(fractal_top, method_high[2], 1)
  145. high_price = valuewhen(fractal_top, high[2], 1)
  146. low_prev = valuewhen(fractal_bot, method_low[2], 1)
  147. low_price = valuewhen(fractal_bot, low[2], 1)
  148.  
  149. regular_bearish_div = fractal_top and high[2] > high_price and method_high[2] < high_prev
  150. hidden_bearish_div = fractal_top and high[2] < high_price and method_high[2] > high_prev
  151. regular_bullish_div = fractal_bot and low[2] < low_price and method_low[2] > low_prev
  152. hidden_bullish_div = fractal_bot and low[2] > low_price and method_low[2] < low_prev
  153.  
  154. plot(title='H F', series=fractal_top ? high[2] : na, color=(regular_bearish_div and uReg) or (hidden_bearish_div and uHid)? maroon : not SHOW_CHANNEL ? na : silver, offset=-2)
  155. plot(title='L F', series=fractal_bot ? low[2] : na, color=(regular_bullish_div and uReg) or (hidden_bullish_div and uHid) ? green : not SHOW_CHANNEL ? na : silver, offset=-2)
  156. plot(title='H D', series=fractal_top ? high[2] : na, style=circles, color=(regular_bearish_div and uReg) or (hidden_bearish_div and uHid)? maroon : not SHOW_CHANNEL ? na : silver, linewidth=3, offset=-2)
  157. plot(title='L D', series=fractal_bot ? low[2] : na, style=circles, color=(regular_bullish_div and uReg) or (hidden_bullish_div and uHid) ? green : not SHOW_CHANNEL ? na : silver, linewidth=3, offset=-2)
  158.  
  159. plotshape(title='+RBD', series=not SHOW_LABEL or not uReg? na : regular_bearish_div ? high[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=maroon, textcolor=white, offset=-2)
  160. plotshape(title='+HBD', series=not SHOW_LABEL or not uHid? na : hidden_bearish_div ? high[2] : na, text='H', style=shape.labeldown, location=location.absolute, color=maroon, textcolor=white, offset=-2)
  161. plotshape(title='-RBD', series=not SHOW_LABEL or not uReg? na : regular_bullish_div ? low[2] : na, text='R', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
  162. plotshape(title='-HBD', series=not SHOW_LABEL or not uHid? na : hidden_bullish_div ? low[2] : na, text='H', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement