Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. //@version=4
  2. study("SMT Divergence", overlay=true, max_bars_back = 4000)
  3.  
  4.  
  5. lb = input(5, title="Left Bars", minval=1)
  6. rb = input(5, title="Right Bars", minval=1)
  7. shownum = input(true, title="Show Divergence Number")
  8. showindis = input(false, title="Show Indicator Names")
  9. calcrsi = input(true, title="RSI")
  10. calcsmt = input(true, title="SMT")
  11. thickness = input(2, title = "Line Thickness (1-4)", minval = 1, maxval = 4)
  12.  
  13. isFractal(mode) =>
  14. ret = mode == 1 ? high[4] < high[2] and high[3] <= high[2] and high[2] >= high[1] and high[2] > high[0] :
  15. mode == -1 ? low[4] > low[2] and low[3] >= low[2] and low[2] <= low[1] and low[2] < low[0] : false
  16.  
  17. topFractal = isFractal(1)
  18. bottomFractal = isFractal(-1)
  19.  
  20. // RSI
  21. rsi = rsi(close, 14)
  22. // SMT
  23. symbol = syminfo.ticker
  24. second_symbol =
  25. (symbol == "AUDCAD") ? "NZDCAD"
  26. : (symbol == "AUDCHF") ? "NZDCHF"
  27. : (symbol == "AUDJPY") ? "NZDJPY"
  28. : (symbol == "AUDUSD") ? "NZDUSD"
  29.  
  30. : (symbol == "CADCHF") ? "USDCHF"
  31. : (symbol == "CADJPY") ? "USDJPY"
  32.  
  33. : (symbol == "EURAUD") ? "GBPAUD"
  34. : (symbol == "EURCAD") ? "GBPCAD"
  35. : (symbol == "EURCHF") ? "GBPCHF"
  36. : (symbol == "EURJPY") ? "GBPJPY"
  37. : (symbol == "EURNZD") ? "GBPNZD"
  38. : (symbol == "EURUSD") ? "GBPUSD"
  39.  
  40. : (symbol == "GBPAUD") ? "EURAUD"
  41. : (symbol == "GBPCAD") ? "EURCAD"
  42. : (symbol == "GBPCHF") ? "EURCHF"
  43. : (symbol == "GBPJPY") ? "EURJPY"
  44. : (symbol == "GBPNZD") ? "EURNZD"
  45. : (symbol == "GBPUSD") ? "EURUSD"
  46.  
  47. : (symbol == "NZDCAD") ? "AUDCAD"
  48. : (symbol == "NZDCHF") ? "AUDCHF"
  49. : (symbol == "NZDJPY") ? "AUDJPY"
  50. : (symbol == "NZDUSD") ? "AUDUSD"
  51.  
  52. : (symbol == "USDCHF") ? "CADCHF"
  53. : (symbol == "USDJPY") ? "CADJPY"
  54.  
  55. : (symbol == "USOIL") ? "UKOIL"
  56. : (symbol == "UKOIL") ? "USOIL"
  57.  
  58. : (symbol == "XAUUSD") ? "XAGUSD"
  59. : (symbol == "XAGUSD") ? "XAUUSD"
  60. : "DXY"
  61. smt = security(second_symbol, timeframe.period, close)
  62.  
  63.  
  64. float top = na
  65. float bottom = na
  66. top := pivothigh(lb, rb)
  67. bottom := pivotlow(lb, rb)
  68.  
  69. previoustop = 0, previousbottom = 0
  70. previoustop := top ? lb : nz(previoustop[1]) + 1
  71. previousbottom := bottom ? lb : nz(previousbottom[1]) + 1
  72.  
  73.  
  74.  
  75. // Positive Divergence (checking possible Lower lows(lb=0))
  76. newbottom = pivotlow(lb, 0) // check only left side
  77. emptyl = true
  78. if not na(newbottom) and newbottom < low[previousbottom] // there must not close price lower than the line between last PL and current low
  79. diff = (newbottom - low[previousbottom]) / previousbottom
  80. lline = newbottom - diff // virtual line to check there is no close price lower than it
  81. for x = 1 to previousbottom -1
  82. if close[x] < lline
  83. emptyl := false
  84. break
  85. lline := lline - diff
  86. else
  87. emptyl := false
  88.  
  89. posdivergence = 0
  90. posdivtxt = ""
  91. if emptyl and not na(newbottom)
  92. if calcrsi and rsi[previousbottom] < rsi
  93. posdivergence := 1
  94. posdivtxt := "RSI\n"
  95. if calcsmt and smt[previousbottom] < smt
  96. posdivergence := 1
  97. posdivtxt := "SMT\n"
  98.  
  99. newareal = false
  100. newareal := bottom ? false : nz(newareal[1], false)
  101. if posdivergence > 0
  102. var line divl = na
  103. var label lab = na
  104. if newareal // we remove old line until It reaches new pivot point (like animation ;)
  105. line.delete(divl)
  106. label.delete(lab)
  107. divl := line.new(bar_index - previousbottom, low[previousbottom], bar_index, low, color = color.lime, width = thickness)
  108. if shownum or showindis
  109. txt = showindis ? posdivtxt : ""
  110. txt := txt + (shownum ? tostring(posdivergence) : "")
  111. lab := label.new(bar_index, na, text=txt, color= color.lime, textcolor = color.black, style=label.style_labelup, yloc=yloc.belowbar)
  112. newareal := true
  113.  
  114.  
  115.  
  116. // Negative Divergence (checking possible higher highs(lb=0))
  117. //newtop = pivothigh(lb, 0) // check only left side
  118. newtop = pivothigh(lb, 0) // check only left side
  119. emptyh = true
  120. if not na(newtop) and newtop > high[previoustop] // there must not close price higher than the line between last PH and current high
  121. diff = (newtop - high[previoustop]) / previoustop
  122. hline = newtop - diff // virtual line to check there is no close price higher than it
  123. for x = 1 to previoustop -1
  124. if close[x] > hline
  125. emptyh := false
  126. break
  127. hline := hline - diff
  128. else
  129. emptyh := false
  130.  
  131. negdivergence = 0
  132. negdivtxt = ""
  133. if emptyh and not na(newtop)
  134. if calcrsi and rsi[previoustop] > rsi
  135. negdivergence := negdivergence + 1
  136. negdivtxt := "RSI\n"
  137. if calcsmt and smt[previoustop] > smt
  138. negdivergence := negdivergence + 1
  139. negdivtxt := "SMT\n"
  140.  
  141. newareah = false
  142. newareah := top ? false : nz(newareah[1], false)
  143. if negdivergence > 0
  144. var line divlh = na
  145. var label labh = na
  146. if newareah // we remove old line until It reaches new pivot point (like animation ;)
  147. line.delete(divlh)
  148. label.delete(labh)
  149. divlh := line.new(bar_index - previoustop, high[previoustop], bar_index, high, color = color.red, width = thickness)
  150. if shownum or showindis
  151. txt = showindis ? negdivtxt : ""
  152. txt := txt + (shownum ? tostring(negdivergence) : "")
  153. labh := label.new(bar_index, na, text=txt, color= color.red, textcolor = color.white, style=label.style_labeldown, yloc=yloc.abovebar)
  154. newareah := true
  155.  
  156.  
  157.  
  158.  
  159. alertcondition(posdivergence > 0, title='Positive Divergence', message='Positive Divergence')
  160. alertcondition(negdivergence > 0, title='Negative Divergence', message='Negative Divergence')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement