Advertisement
JustUncleL

MA Alert V4

Feb 12th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. //@version=2
  2. //
  3.  
  4. study("MA Alert v4",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. //
  17. uHiLoPAC = input(false,"Use High Low PAC instead of Colored EMA for Alerts")
  18. pac_type = input(defval="EMA", title="PAC MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
  19. pac_len = input(defval=5, title="PAC MA - Length", minval=1)
  20. pac_src = input(close, title="PAC MA - Source")
  21. ShowPAC = input(false)
  22. col_bars = input(true, title="Colour Alert Bars")
  23. show_alert= input(false, title="Show BUY/SELL Alerts")
  24. UseBigArrows = input(false, title="Use Big Arrows instead of BUY/SELL for Alerts")
  25. shunt_signal = input(false,"Shunt BUY/SELL Alert to Bar Completed")
  26. shunt = shunt_signal? 1 : 0
  27. // Fast MA - type, source, length
  28. 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)
  29. fast_ma_len = input(defval=36, title="Fast MA - Length", minval=1)
  30. fast_ma_src = input(close, title="Fast MA - Source")
  31. // Slow MA - type, source, length
  32. 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)
  33. slow_ma_len = input(defval=200, title="Slow MA - Length", minval=1)
  34. slow_ma_src = input(close, title="Slow MA - Source")
  35.  
  36. // - /INPUTS
  37.  
  38. // - FUNCTIONS
  39.  
  40. // Returns MA input selection variant, default to SMA if blank or typo.
  41. variant(type, src, len) =>
  42. v1 = sma(src, len) // Simple
  43. v2 = ema(src, len) // Exponential
  44. v3 = wma(src, len) // Weighted
  45. v4 = vwma(src, len) // Volume Weighted
  46. v5 = na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len // Smoothed
  47. v6 = 2 * v2 - ema(v2, len) // Double Exponential
  48. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  49. v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
  50. v11 = sma(sma(src,len),len) // Triangular
  51. // SuperSmoother filter
  52. // © 2013 John F. Ehlers
  53. a1 = exp(-1.414*3.14159 / len)
  54. b1 = 2*a1*cos(1.414*3.14159 / len)
  55. c2 = b1
  56. c3 = (-a1)*a1
  57. c1 = 1 - c2 - c3
  58. v9 = c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  59. // Zero Lag Exponential
  60. ema1 = ema(src, len)
  61. ema2 = ema(ema1, len)
  62. v10 = ema1+(ema1-ema2)
  63. // return variant, defaults to SMA if input invalid.
  64. 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
  65.  
  66. // - /FUNCTIONS
  67.  
  68. // - SERIES VARIABLES
  69. // MA's
  70. ma_series = variant(ma_type, ma_src, ma_len)
  71. fast_ma_series = variant(fast_ma_type, fast_ma_src, fast_ma_len)
  72. slow_ma_series = variant(slow_ma_type, slow_ma_src, slow_ma_len)
  73. pacC = variant(pac_type, pac_src, pac_len)
  74. pacH = variant(pac_type, high, pac_len)
  75. pacL = variant(pac_type, low, pac_len)
  76.  
  77. // - /SERIES VARIABLES
  78.  
  79. // Get Direction From Moving Average
  80. direction = rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : 0
  81. // Count Bars going up or going down
  82. up = direction>0 and close>ma_series? na(up[1])? 1 : up[1]+1 : 0
  83. down = direction<0 and close<ma_series? na(down[1])? 1 : down[1]+1 : 0
  84.  
  85. // - /SERIES VARIABLES
  86.  
  87. // - PLOTTING
  88. // Plot MA series and color it according too direction
  89. plot(ma_series, title="MA Plot", color=direction<0?red:direction>0?green:aqua, linewidth=3, transp=0)
  90. plot(fast_ma_series, title="Fast MA Plot", color=gray, style=line, linewidth=2, transp=50)
  91. plot(slow_ma_series, title="Slow MA Plot", color=blue, linewidth=3, transp=20)
  92.  
  93. // Color Bars
  94. trendColour = gray
  95. H=plot(ShowPAC ?pacH:na, color=trendColour, linewidth=1, title="High PAC EMA",transp=50)
  96. L=plot(ShowPAC ?pacL:na, color=trendColour, linewidth=1, title="Low PAC EMA",transp=50)
  97. C=plot(ShowPAC ?pacC:na, color=olive, linewidth=1, title="Close PAC EMA",transp=20)
  98. fill(L,H, color=trendColour,transp=90,title="Fill HiLo PAC")
  99.  
  100. // Colour bars according to the close position relative to the PAC selected.
  101. bcol = iff(uHiLoPAC, close>=pacH? blue : close<=pacL? red : gray , direction<0 and close<ma_series ?red: close>ma_series and direction>0?blue: gray)
  102. barcolor(col_bars?bcol:na, title = "Bar Colours",transp=0)
  103.  
  104. // - /PLOTTING
  105.  
  106. // - ALERTING
  107. xPacLong = crossover(close,pacH) ? 1 : crossunder(close,pacL) ? 0 : nz(xPacLong[1])
  108. xPacShort = crossover(close,pacH) ? 0 : crossunder(close,pacL) ? 1 : nz(xPacShort[1])
  109.  
  110. isup = uHiLoPAC? xPacLong : up
  111. isdown = uHiLoPAC? xPacShort : down
  112.  
  113. // Check have alert
  114. up_alert = isup ? na(up_alert[1]) ? 1 : up_alert[1]+1 : 0
  115. dn_alert = isdown ? na(dn_alert[1]) ? 1 : dn_alert[1]+1 : 0
  116.  
  117. //
  118. // show an Arrow only when alert condition is met and the bar closed.
  119. plotarrow(show_alert and UseBigArrows? up_alert[shunt]==1? 1 : dn_alert[shunt]==1? -1 : na : na, colorup=aqua, colordown=fuchsia, transp=20,minheight=10,maxheight=20, title="Big Alert Arrows")
  120. plotshape(show_alert and not UseBigArrows? up_alert[shunt]==1? true : na : na, title='Buy Arrow', location=location.belowbar, color=green, style=shape.arrowup, text="BUY", textcolor=green,transp=0)
  121. plotshape(show_alert and not UseBigArrows? dn_alert[shunt]==1? true : na : na, title='Sell Arrow', location=location.abovebar, color=red, style=shape.arrowdown, text="SELL",textcolor=red,transp=0)
  122.  
  123. // generate an alert if required.
  124. alertcondition(up_alert==1 or dn_alert==1, title="MA alert ", message="MA alert")
  125.  
  126.  
  127. // - /ALERTING
  128.  
  129. // /eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement