Advertisement
JustUncleL

MA Alert v5-5

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