Advertisement
JustUncleL

MA Alert v2

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