SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@version=4
  2. study(title="MACD", shorttitle="MACD")
  3.  
  4. // Getting inputs
  5. fast_length = input(title="Fast Length", type=input.integer, defval=12)
  6. slow_length = input(title="Slow Length", type=input.integer, defval=26)
  7. src = input(title="Source", type=input.source, defval=close)
  8. signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
  9. sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
  10. sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
  11.  
  12. // Plot colors
  13. col_grow_above = #26A69A
  14. col_grow_below = #FFCDD2
  15. col_fall_above = #B2DFDB
  16. col_fall_below = #EF5350
  17. col_macd = #0094ff
  18. col_signal = #ff6a00
  19.  
  20. // Calculating
  21. fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
  22. slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
  23. macd = fast_ma - slow_ma
  24. signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
  25. hist = macd - signal
  26.  
  27. 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) ), transp=0 )
  28. plot(macd, title="MACD", color=col_macd, transp=0)
  29. plot(signal, title="Signal", color=col_signal, transp=0)
  30.  
  31.  
  32. fastMA = input(title="Fast moving average", type = input.integer, defval = 12, minval = 7)
  33. slowMA = input(title="Slow moving average", type = input.integer, defval = 26, minval = 7)
  34. signalLength = input(9,minval=1)
  35.  
  36. [currMacd,_,_] = macd(close[0], fastMA, slowMA, signalLength)
  37. [prevMacd,_,_] = macd(close[1], fastMA, slowMA, signalLength)
  38. signal2 = ema(currMacd, signalLength)
  39.  
  40. crossoverBear = cross(currMacd, signal) and currMacd < signal ? avg(currMacd, signal) : na
  41. crossoverBull = cross(currMacd, signal) and currMacd > signal ? avg(currMacd, signal) : na
  42.  
  43. plotshape(crossoverBear, title='MACD-BEAR', style=shape.triangledown, text='', location=location.abovebar, color=color.red, textcolor=color.black, size=size.tiny)
  44. plotshape(crossoverBull, title='MACD-BULL', style=shape.triangleup, text='', location=location.belowbar, color=color.green, textcolor=color.black, size=size.tiny)
  45.  
  46. alertcondition(crossoverBear, "MACD Bear", "MACD Bearish Crossover")
  47. alertcondition(crossoverBull, "MACD Bull", "MACD Bullish Crossover")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top