Advertisement
JustUncleL

MA Alert v3

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