xmd79

MACD Advanced

Sep 24th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 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. // © SkyRockSignals
  3.  
  4. //@version=4
  5. study("MACD Advanced",shorttitle="MACD Adv", resolution="",overlay=false,format=format.price)
  6.  
  7. // Getting inputs
  8. fast_length = input(title="Fast Length", type=input.integer, defval=12)
  9. slow_length = input(title="Slow Length", type=input.integer, defval=26)
  10. src = input(title="Source", type=input.source, defval=close)
  11. signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
  12. sma_source = input(title="Oscillator MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
  13. sma_signal = input(title="Signal Line MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
  14. // Plot colors
  15. col_macd = input(#2962FF, "MACD Line  ", input.color, group="Color Settings", inline="MACD")
  16. col_signal = input(#FF6D00, "Signal Line  ", input.color, group="Color Settings", inline="Signal")
  17. col_grow_above = input(#26A69A, "Above   Grow", input.color, group="Histogram", inline="Above")
  18. col_fall_above = input(#B2DFDB, "Fall", input.color, group="Histogram", inline="Above")
  19. col_grow_below = input(#FFCDD2, "Below Grow", input.color, group="Histogram", inline="Below")
  20. col_fall_below = input(#FF5252, "Fall", input.color, group="Histogram", inline="Below")
  21. //divergence
  22. len = input(title="Divergence Checker Period", minval=1, defval=14)
  23. lbR = input(title="Pivot Lookback Right", defval=5)
  24. lbL = input(title="Pivot Lookback Left", defval=5)
  25. rangeUpper = input(title="Max of Lookback Range", defval=60)
  26. rangeLower = input(title="Min of Lookback Range", defval=5)
  27.  
  28.  
  29. // Calculating
  30. fast_ma = sma_source == "SMA" ? sma(src, fast_length) : ema(src, fast_length)
  31. slow_ma = sma_source == "SMA" ? sma(src, slow_length) : ema(src, slow_length)
  32. macd = fast_ma - slow_ma
  33. signal = sma_signal == "SMA" ? sma(macd, signal_length) : ema(macd, signal_length)
  34.  
  35.  
  36. //divergence
  37. osc = macd
  38.  
  39.  
  40. plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
  41. phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
  42. _inRange(cond) =>
  43. bars = barssince(cond == true)
  44. rangeLower <= bars and bars <= rangeUpper
  45.  
  46. //------------------------------------------------------------------------------
  47. // Regular Bullish
  48. // Osc: Higher Low
  49.  
  50. oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
  51.  
  52. // Price: Lower Low
  53.  
  54. priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
  55. bullCond = priceLL and oscHL and plFound
  56.  
  57.  
  58. //------------------------------------------------------------------------------
  59. // Hidden Bullish
  60. // Osc: Lower Low
  61.  
  62. oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
  63.  
  64. // Price: Higher Low
  65.  
  66. priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
  67. hiddenBullCond = priceHL and oscLL and plFound
  68.  
  69. //------------------------------------------------------------------------------
  70. // Regular Bearish
  71. // Osc: Lower High
  72.  
  73. oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
  74.  
  75. // Price: Higher High
  76.  
  77. priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
  78.  
  79. bearCond = priceHH and oscLH and phFound
  80.  
  81.  
  82.  
  83. //------------------------------------------------------------------------------
  84. // Hidden Bearish
  85. // Osc: Higher High
  86.  
  87. oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
  88.  
  89. // Price: Lower High
  90.  
  91. priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
  92.  
  93. hiddenBearCond = priceLH and oscHH and phFound
  94.  
  95. crosover= crossover(macd,signal)
  96. crosunder=crossunder(macd,signal)
  97.  
  98. var bool strong_bull=false
  99. var bool bull=false
  100. var bool bear=false
  101. var bool strong_bear=false
  102.  
  103. strong_bull:=crosover and bullCond
  104.  
  105. strong_bear:=crosunder and bearCond
  106.  
  107.  
  108. bull:=crosover and not hiddenBearCond and macd<0 and signal<0
  109.  
  110. bear:=crosunder and not hiddenBullCond and macd>0 and signal>0
  111.  
  112.  
  113. hist = macd - signal
  114. plot(0)
  115. plot(macd, title="MACD", color=col_macd)
  116. plot(signal, title="Signal", color=col_signal)
  117. plotshape(bull?true: na,text="Bullish Signal",size=size.tiny,offset=0,color=color.green,textcolor=color.green,style=shape.flag,location=location.absolute)
  118. plotshape(bear?true: na,text="Bearish Signal",location=location.absolute,size=size.tiny,offset=0,color=color.red,textcolor=color.red)
  119. plotshape(strong_bull?true: na,text="Strong Bullish Signal",style=shape.flag,size=size.tiny,offset=2,color=color.green,textcolor=color.green,location=location.absolute)
  120. plotshape(strong_bear?true: na,text="Strong Bearish Signal",style=shape.flag,size=size.tiny,offset=2,color=color.red,textcolor=color.red,location=location.absolute)
  121.  
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment