Guest User

MCB Clone

a guest
Jul 19th, 2020
2,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.35 KB | None | 0 0
  1. //@version=3
  2. //Market Cipher Update 2 - updated 8th Oct 2019
  3.  
  4. //Momentum Curves with green and red dots
  5. study(title="NotMCB", shorttitle="Not MCB")
  6. n1 = input(9, "Channel Length")
  7. n2 = input(12, "Average Length")
  8. obLevel1 = input(60, "Over Bought Level 1")
  9. obLevel2 = input(53, "Over Bought Level 2")
  10. osLevel1 = input(-60, "Over Sold Level 1")
  11. osLevel2 = input(-53, "Over Sold Level 2")
  12. osLevel3 = input(-100, "Over Sold Level 2")
  13.  
  14.  
  15. ap = hlc3
  16. esa = ema(ap, n1)
  17. d = ema(abs(ap - esa), n1)
  18. ci = (ap - esa) / (0.015 * d)
  19. tci = ema(ci, n2)
  20.  
  21. wt1 = tci
  22. wt2 = sma(wt1,3)
  23.  
  24. plot(0, color=gray, title="Zero Line")
  25. plot(obLevel1, color=red, style=3, title="Bottom")
  26. plot(osLevel1, color=green, style=3, title="Top")
  27. plot(wt1, color=#BFE4FF, style=4, title= "Lt Blue Wave")
  28. plot(wt2, color=#673ab7, style=4, title="Blue Wave", transp=40)
  29. plot(wt1-wt2, color=yellow, style=4, transp=40, title="wave1-wave2")
  30.  
  31. //green dots and crosses
  32. plotshape(crossover(wt1, wt2) and osLevel1 ? wt2 : na, title="Pos Crossover", location=location.absolute, style=shape.cross, size=size.tiny, color=#3FFF00, transp=20)
  33. plotshape(crossover(wt2, wt1) and osLevel1 ? wt1 : na, title="Neg Crossover", location=location.absolute, style=shape.cross, size=size.tiny, color=red, transp=20)
  34. plotshape(crossover(wt1, wt2) and wt2 < -59 ? wt2 : na, title="Pos Crossover", location=location.bottom, style=shape.circle, size=size.tiny, color=#3FFF00, transp=20)
  35. plotshape(crossover(wt2, wt1) and wt1 > 59 ? wt2 : na, title="Neg Crossover", location=location.top, style=shape.circle, size=size.tiny, color=red, transp=20)
  36.  
  37. //soch RSI with divergences
  38. smoothKw = input(3, minval=1)
  39. smoothDw = input(3, minval=1)
  40. lengthRSIw = input(14, minval=1)
  41. lengthStochw = input(14, minval=1)
  42. uselogw = input(true, title="Log")
  43. srcInw = input(close, type=source, title="Source")
  44. showdivsw = input(true, title="Show Divergences")
  45. showhiddenw = input(false, title="Show Hidden Divergences")
  46. showchanw = input(false, title="Show Divergences Channel")
  47.  
  48.  
  49. srcw = uselogw ? log(srcInw) : srcInw
  50. rsi1w = rsi(srcw, lengthRSIw)
  51. kkw = sma(stoch(rsi1w, rsi1w, rsi1w, lengthStochw), smoothKw)
  52. dw = sma(kkw, smoothDw)
  53. hmw = input(false, title="Use Average of both K & D")
  54. kw = hmw ? avg(kkw, dw) : kkw
  55.  
  56. aw = plot(kkw, color=blue, linewidth=1, transp=0, title="K")
  57. bw = plot(dw, color=orange, linewidth=1, transp=0, title="D")
  58. fw = kkw >= dw ? blue : orange
  59. fill(aw, bw, title="KD Fill", color=white)
  60.  
  61.  
  62. //------------------------------
  63. //@RicardoSantos' Divergence Script
  64.  
  65. f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
  66. f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
  67. f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
  68. //-------------------------
  69. fractal_top = f_fractalize(kw) > 0 ? kw[2] : na
  70. fractal_bot = f_fractalize(kw) < 0 ? kw[2] : na
  71.  
  72. high_prev = valuewhen(fractal_top, kw[2], 0)[2]
  73. high_price = valuewhen(fractal_top, high[2], 0)[2]
  74. low_prev = valuewhen(fractal_bot, kw[2], 0)[2]
  75. low_price = valuewhen(fractal_bot, low[2], 0)[2]
  76.  
  77. regular_bearish_diva = fractal_top and high[2] > high_price and kw[2] < high_prev
  78. hidden_bearish_diva = fractal_top and high[2] < high_price and kw[2] > high_prev
  79. regular_bullish_diva = fractal_bot and low[2] < low_price and kw[2] > low_prev
  80. hidden_bullish_diva = fractal_bot and low[2] > low_price and kw[2] < low_prev
  81. //-------------------------
  82. plot(showchanw?fractal_top:na, title="Top Div Channel", offset=-2, color=gray)
  83. plot(showchanw?fractal_bot:na, title="Bottom Div Channel", offset=-2, color=gray)
  84.  
  85. col1 = regular_bearish_diva ? red : hidden_bearish_diva and showhiddenw ? red : na
  86. col2 = regular_bullish_diva ? green : hidden_bullish_diva and showhiddenw ? green : na
  87. col3 = regular_bearish_diva ? red : hidden_bearish_diva and showhiddenw ? red : showchanw ? gray : na
  88. col4 = regular_bullish_diva ? green : hidden_bullish_diva and showhiddenw ? green : showchanw ? gray : na
  89.  
  90. plot(title='H F', series=showdivsw and fractal_top ? kw[2] : na, color=col1, linewidth=2, offset=-2)
  91. plot(title='L F', series=showdivsw and fractal_bot ? kw[2] : na, color=col2, linewidth=2, offset=-2)
  92. plot(title='H D', series=showdivsw and fractal_top ? kw[2] : na, style=circles, color=col3, linewidth=3, offset=-2)
  93. plot(title='L D', series=showdivsw and fractal_bot ? kw[2] : na, style=circles, color=col4, linewidth=3, offset=-2)
  94.  
  95. plotshape(title='+RBD', series=showdivsw and regular_bearish_diva ? kw[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=red, textcolor=white, offset=-2)
  96. plotshape(title='+HBD', series=showdivsw and hidden_bearish_diva and showhiddenw ? kw[2] : na, text='H', style=shape.labeldown, location=location.absolute, color=red, textcolor=white, offset=-2)
  97. plotshape(title='-RBD', series=showdivsw and regular_bullish_diva ? kw[2] : na, text='R', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
  98. plotshape(title='-HBD', series=showdivsw and hidden_bullish_diva and showhiddenw ? kw[2] : na, text='H', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
  99.  
  100.  
  101. //money flow
  102. colorRed = #ff0000
  103. colorGreen = #03ff00
  104.  
  105. ma(matype, src, length) =>
  106. if matype == "RMA"
  107. rma(src, length)
  108. else
  109. if matype == "SMA"
  110. sma(src, length)
  111. else
  112. if matype == "EMA"
  113. ema(src, length)
  114. else
  115. if matype == "WMA"
  116. wma(src, length)
  117. else
  118. if matype == "VWMA"
  119. vwma(src, length)
  120. else
  121. src
  122.  
  123. rsiMFIperiod = input(60, "RSI+MFI Period")
  124. rsiMFIMultiplier = input(190, "RSI+MFI Area multiplier")
  125. MFRSIMA = input(defval="SMA", title="MFRSIMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA"])
  126.  
  127. candleValue = (close - open) / (high - low)
  128. MVC = ma(MFRSIMA, candleValue, rsiMFIperiod)
  129. color_area = MVC > 0 ? green : red
  130.  
  131. RSIMFIplot = plot(MVC * rsiMFIMultiplier, title="RSI+MFI Area", color=color_area, transp=35)
  132. fill(RSIMFIplot, plot(0), color_area, transp=50)
  133.  
  134. //rsi
  135. //Bullish Divergence (green triangle)
  136. //Hidden Bullish Divergence (green circle)
  137. //Bearish Divergence (red triangle)
  138. //Hidden Bearish Divergence (red circle)
  139.  
  140. lend = 14
  141. bearish_div_rsi = input(60, "Min Bearish RSI", integer, minval=50, maxval=100)
  142. bullish_div_rsi = input(40, "Max Bullish RSI", integer, minval=0, maxval=50)
  143.  
  144. // RSI code
  145. rsi = rsi(close, lend)
  146. plot(rsi, color=#6DFFE1, linewidth=2, transp=0, title="RSI")
  147.  
  148. // DIVS code
  149. xbars = 60
  150. hb = abs(highestbars(rsi, xbars)) // Finds bar with highest value in last X bars
  151. lb = abs(lowestbars(rsi, xbars)) // Finds bar with lowest value in last X bars
  152.  
  153. // Defining variable values, mandatory in Pine 3
  154. max = na
  155. max_rsi = na
  156. min = na
  157. min_rsi = na
  158. bearish_div = na
  159. bullish_div = na
  160. hidden_bearish_div = na
  161. hidden_bullish_div = na
  162. div_alert = na
  163. hidden_div_alert = na
  164.  
  165. // If bar with lowest / highest is current bar, use it's value
  166. max := hb == 0 ? close : na(max[1]) ? close : max[1]
  167. max_rsi := hb == 0 ? rsi : na(max_rsi[1]) ? rsi : max_rsi[1]
  168. min := lb == 0 ? close : na(min[1]) ? close : min[1]
  169. min_rsi := lb == 0 ? rsi : na(min_rsi[1]) ? rsi : min_rsi[1]
  170.  
  171. // Compare high of current bar being examined with previous bar's high
  172. // If curr bar high is higher than the max bar high in the lookback window range
  173. if close > max // we have a new high
  174. max := close // change variable "max" to use current bar's high value
  175. if rsi > max_rsi // we have a new high
  176. max_rsi := rsi // change variable "max_rsi" to use current bar's RSI value
  177. if close < min // we have a new low
  178. min := close // change variable "min" to use current bar's low value
  179. if rsi < min_rsi // we have a new low
  180. min_rsi := rsi // change variable "min_rsi" to use current bar's RSI value
  181.  
  182. // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
  183. if (max[1] > max[2]) and (rsi[1] < max_rsi) and (rsi <= rsi[1]) and (rsi[1] >= bearish_div_rsi)
  184. bearish_div := true
  185. div_alert := true
  186. if (min[1] < min[2]) and (rsi[1] > min_rsi) and (rsi >= rsi[1]) and (rsi[1] <= bullish_div_rsi)
  187. bullish_div := true
  188. div_alert := true
  189. // Hidden divergences
  190. if (max[1] < max[2]) and (rsi[1] < max_rsi)
  191. hidden_bearish_div := true
  192. hidden_div_alert := true
  193. if (min[1] > min[2]) and (rsi[1] > min_rsi)
  194. hidden_bullish_div := true
  195. hidden_div_alert := true
  196. // Alerts
  197. alertcondition(div_alert, title='RSI Divergence', message='RSI Divergence')
  198. alertcondition(hidden_div_alert, title='Hidden RSI Divergence', message='Hidden RSI Divergence')
  199.  
  200. // Plots divergences with offest
  201. plotshape((bearish_div ? rsi[1] + 3 : na), location=location.absolute, style=shape.diamond, color=#ff0000, size=size.tiny, transp=0, offset=0, title="RSI Bear Div")
  202. plotshape((bullish_div ? rsi[1] - 3 : na), location=location.absolute, style=shape.diamond, color=#00ff01, size=size.tiny, transp=0, offset=0, title="RSI Bull Div")
  203. plotshape((hidden_bearish_div ? rsi[1] + 3 : na), location=location.absolute, style=shape.circle, color=#ff0000, size=size.tiny, transp=0, offset=0, title="RSI Bear hDiv")
  204. plotshape((hidden_bullish_div ? rsi[1] - 3 : na), location=location.absolute, style=shape.circle, color=#00ff01, size=size.tiny, transp=0, offset=0, title="RSI Bull hDiv")
  205.  
  206.  
  207. //wave divergences
  208. WTCross = cross(wt1, wt2)
  209. WTCrossUp = wt2 - wt1 <= 0
  210. WTCrossDown = wt2 - wt1 >= 0
  211. WTFractal_top = f_fractalize(wt1) > 0 and wt1[2] ? wt1[2] : na
  212. WTFractal_bot = f_fractalize(wt1) < 0 and wt1[2] ? wt1[2] : na
  213.  
  214. WTHigh_prev = valuewhen(WTFractal_top, wt1[2], 0)[2]
  215. WTHigh_price = valuewhen(WTFractal_top, high[2], 0)[2]
  216. WTLow_prev = valuewhen(WTFractal_bot, wt1, 0)[2]
  217. WTLow_price = valuewhen(WTFractal_bot, low[2], 0)[2]
  218.  
  219. WTRegular_bearish_div = WTFractal_top and high[2] > WTHigh_price and wt1[2] < WTHigh_prev
  220. WTRegular_bullish_div = WTFractal_bot and low[2] < WTLow_price and wt1[2] > WTLow_prev
  221.  
  222. bearWTSignal = WTRegular_bearish_div and WTCrossDown
  223. bullWTSignal = WTRegular_bullish_div and WTCrossUp
  224.  
  225. WTCol1 = bearWTSignal ? #ff0000 : na
  226. WTCol2 = bullWTSignal ? #00FF00EB : na
  227.  
  228. plot(series = WTFractal_top ? wt1[2] : na, title='Bearish Divergence', color=WTCol1, linewidth=5, transp=60)
  229. plot(series = WTFractal_bot ? wt1[2] : na, title='Bullish Divergence', color=WTCol2, linewidth=5, transp=60)
  230.  
  231.  
  232. //2nd wave
  233. WTFractal_topa = f_fractalize(wt2) > 0 and wt2[2] ? wt2[2] : na
  234. WTFractal_bota = f_fractalize(wt2) < 0 and wt2[2] ? wt2[2] : na
  235.  
  236. WTHigh_preva = valuewhen(WTFractal_topa, wt2[2], 0)[2]
  237. WTHigh_pricea = valuewhen(WTFractal_topa, high[2], 0)[2]
  238. WTLow_preva = valuewhen(WTFractal_bota, wt2, 0)[2]
  239. WTLow_pricea = valuewhen(WTFractal_bota, low[2], 0)[2]
  240.  
  241.  
  242. WTRegular_bearish_diva = WTFractal_topa and high[2] > WTHigh_pricea and wt2[2] < WTHigh_preva
  243. WTRegular_bullish_diva = WTFractal_bota and low[2] < WTLow_pricea and wt2[2] > WTLow_preva
  244.  
  245. bearWTSignala = WTRegular_bearish_diva and WTCrossDown
  246. bullWTSignala = WTRegular_bullish_diva and WTCrossUp
  247.  
  248. WTCol1a = bearWTSignala ? #ff0000 : na
  249. WTCol2a = bullWTSignala ? #00FF00EB : na
  250.  
  251. plot(series = WTFractal_topa ? wt2[2] : na, title='Bearish Divergence', color=WTCol1a, linewidth=5, transp=60)
  252. plot(series = WTFractal_bota ? wt2[2] : na, title='Bullish Divergence', color=WTCol2a, linewidth=5, transp=60)
Add Comment
Please, Sign In to add comment