Advertisement
JustUncleL

MA Alert

Feb 6th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. //@version=2
  2. //
  3.  
  4. study("MA Alert",overlay=true)
  5.  
  6. //
  7. // Description: Create alert when MA changes direction
  8.  
  9. //
  10. // - INPUTS
  11.  
  12. // Medium Fast MA - type, source, length
  13. ma_type = input(defval="HullMA", title="MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
  14. ma_len = input(defval=21, title="MA - Length", minval=1)
  15. ma_src = input(close, title="MA - Source")
  16. // - /INPUTS
  17.  
  18. // - FUNCTIONS
  19.  
  20. // Returns MA input selection variant, default to SMA if blank or typo.
  21. variant(type, src, len) =>
  22. v1 = sma(src, len) // Simple
  23. v2 = ema(src, len) // Exponential
  24. v3 = wma(src, len) // Weighted
  25. v4 = vwma(src, len) // Volume Weighted
  26. v5 = na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len // Smoothed
  27. v6 = 2 * v2 - ema(v2, len) // Double Exponential
  28. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  29. v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
  30. v11 = sma(sma(src,len),len) // Triangular
  31. // SuperSmoother filter
  32. // © 2013 John F. Ehlers
  33. a1 = exp(-1.414*3.14159 / len)
  34. b1 = 2*a1*cos(1.414*3.14159 / len)
  35. c2 = b1
  36. c3 = (-a1)*a1
  37. c1 = 1 - c2 - c3
  38. v9 = c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  39. // Zero Lag Exponential
  40. ema1 = ema(src, len)
  41. ema2 = ema(ema1, len)
  42. v10 = ema1+(ema1-ema2)
  43. // return variant, defaults to SMA if input invalid.
  44. type=="EMA"?v2 : type=="WMA"?v3 : type=="VWMA"?v4 : type=="SMMA"?v5 : type=="DEMA"?v6 : type=="TEMA"?v7 : type=="HullMA"?v8 : type=="SSMA"?v9 : type=="ZEMA"?v10 : type=="TMA"? v11: v1
  45.  
  46. // - /FUNCTIONS
  47.  
  48. // - SERIES VARIABLES
  49. // MA's
  50. ma_series = variant(ma_type, ma_src, ma_len)
  51. // - /SERIES VARIABLES
  52.  
  53. // Get Direction From Moving Average
  54. direction = rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : 0
  55. // Count Bars going up or going down
  56. up = direction>0? na(up[1])? 1 : up[1]+1 : 0
  57. down = direction<0? na(down[1])? 1 : down[1]+1 : 0
  58.  
  59. // - /SERIES VARIABLES
  60.  
  61. // - PLOTTING
  62. // Plot MA series and color it according too direction
  63. plot(ma_series, title="MA Plot", color=direction<0?red:direction>0?green:aqua, linewidth=3, transp=0)
  64.  
  65. // - /PLOTTING
  66.  
  67. // - ALERTING
  68. c_alert = up==1 or down==1
  69. alertcondition(c_alert, title="MA Alert", message="MA Alert")
  70. alertcondition(up==1, title="MA Buy Alert", message="MA Buy Alert")
  71. alertcondition(down==1, title="MA Sell Alert", message="MA Sell Alert")
  72.  
  73. //
  74. // show an Arrow only when alert condition is met and the bar closed.
  75. plotshape(down[1]==1,title= "MA Sell Alert Completed", location=location.abovebar, color=red, transp=0, style=shape.arrowdown, text="SELL",size=size.auto,offset=-1)
  76. plotshape(up[1]==1,title= "MA Buy Alert Completed", location=location.belowbar, color=green, transp=0, style=shape.arrowup, text="BUY",size=size.auto,offset=-1)
  77.  
  78. // - /ALERTING
  79.  
  80. // /eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement