xmd79

Color Changing MACD

May 23rd, 2023
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 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. // © FX365_Thailand
  3.  
  4. //@version=5
  5.  
  6. //Release note
  7. //v24.0 Released
  8.  
  9.  
  10. indicator(title="Color Changing MACD", shorttitle="Color MACD")
  11.  
  12. //User inputs
  13. fast_length = input(title="Fast Length", defval=12,group="Common")
  14. slow_length = input(title="Slow Length", defval=26)
  15. src = input(title="Source", defval=close)
  16. signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)
  17. sma_source = input.string(title="Oscillator MA Type", defval="SMA", options=["SMA", "EMA"])
  18. sma_signal = input.string(title="Signal Line MA Type", defval="SMA", options=["SMA", "EMA"])
  19. i_signal = input.bool(true,title="Show GC/DC Signals?")
  20.  
  21.  
  22. // Plot colors
  23. col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
  24. col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
  25. col_grow_above = input(color.new(color.green,20), "Above   Grow", group="Histogram", inline="Above")
  26. col_fall_above = input(color.new(color.green,80), "Fall", group="Histogram", inline="Above")
  27. col_grow_below = input(color.new(color.red,80), "Below Grow", group="Histogram", inline="Below")
  28. col_fall_below = input(color.new(color.red,20), "Fall", group="Histogram", inline="Below")
  29.  
  30.  
  31. //Logic
  32.  
  33. //MACD calculation
  34. fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
  35. slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
  36. macd = fast_ma - slow_ma
  37. signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
  38. hist = macd - signal
  39.  
  40.  
  41. //Determine colors
  42. color_macd = (macd > signal) and (hist > hist[1]) ? color.green : (macd > signal) and (hist < hist[1]) ? #9c27b0 : (macd < signal) and (hist < hist[1]) ? color.red : (macd < signal) and (hist > hist[1]) ? #9c27b0 : na
  43.  
  44.  
  45. //Plot
  46. // a=plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
  47. color_bull = color.from_gradient(value=hist, bottom_value = 0,top_value = 0.3 , top_color = color.new(color.green,80), bottom_color = color.new(color.green,20) )
  48. color_bear = color.from_gradient(value=hist, top_value = 0, bottom_value = -100, top_color = color.new(color.red,20), bottom_color = color.new(color.red,80) )
  49.  
  50. a=plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
  51. b=plot(macd, title="MACD", color=color_macd,display=display.none)
  52. c=plot(signal, title="Signal", color=color_macd,display=display.none)
  53.  
  54. //Fill
  55. //Condition
  56. bull = macd > signal
  57. bear = macd < signal
  58.  
  59. color_top = (macd > signal) and (hist > hist[1]) ? color.new(#e1bee7,80) : (macd > signal) and (hist < hist[1]) ? color.new(#9c27b0,80) : (macd < signal) and (hist < hist[1]) ? color.new(color.red,80) : (macd < signal) and (hist > hist[1]) ? color.new(#e1bee7,80) : na
  60. color_bottom = (macd > signal) and (hist > hist[1]) ? color.new(#0ef30e,0) : (macd > signal) and (hist < hist[1]) ? color.new(#9c27b0,0) : (macd < signal) and (hist < hist[1]) ? color.new(color.red,0) : (macd < signal) and (hist > hist[1]) ? color.new(#9c27b0,0) : na
  61.  
  62. fill(b,c, top_value=macd, bottom_value = signal, top_color = color_top, bottom_color = color_bottom)
  63.  
  64. //signals
  65. gc = ta.crossover(macd, signal)
  66. dc = ta.crossunder(macd, signal)
  67.  
  68. //Plot signals
  69. plotshape(i_signal ? gc : na,style= shape.triangleup, color=color.new(color.green,0),title="Golden Cross",location=location.bottom,size=size.tiny)
  70. plotshape(i_signal ? dc : na,style= shape.triangledown, color=color.new(color.red,0),title="Death Cross",location=location.top,size=size.tiny)
  71.  
  72. //alert conditions
  73. alertcondition(gc, title="Golden Cross", message="Golden Cross Occurred")
  74. alertcondition(dc, title="Death Cross", message="Death Cross Occurred")
  75.  
Advertisement
Add Comment
Please, Sign In to add comment