Advertisement
JustUncleL

MA Tool R6

Apr 25th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. //@version=2
  2. //
  3.  
  4. study("MA Tool R6",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=1440,title="Use Alternate Anchor TimeFrame (0=none, max=1440mins)")
  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(true,"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=50, title="PAC MA - Length", minval=1)
  22. pac_src = input(close, title="PAC MA - Source")
  23. ShowPAC = input(true)
  24. col_bars = input(true, title="Colour Alert Bars")
  25. show_alert= input(true, title="Show BUY/SELL Alerts")
  26. UseBigArrows = input(true, title="Use Big Arrows instead of BUY/SELL for Alerts")
  27. filter_alert = input(true, title="Use PAC to filter Alerts")
  28. shunt_signal = input(false,"Shunt BUY/SELL Alert to Bar Completed")
  29. shunt = shunt_signal? 1 : 0
  30. // Fast MA - type, source, length
  31. 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)
  32. fast_ma_len_ = input(defval=21, title="Fast MA - Length", minval=1)
  33. fast_ma_src = input(close, title="Fast MA - Source")
  34. // Slow MA - type, source, length
  35. 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)
  36. slow_ma_len_ = input(defval=200, title="Slow MA - Length", minval=1)
  37. slow_ma_src = input(close, title="Slow MA - Source")
  38. sKC = input(false, title="Show smoothed KC Channel")
  39. KClength = input(20,minval=2,title="KC Channel Length")
  40. KCdeviation = input(2.0,minval=0.01,title="KC Std Deviation")
  41. ATRlength = input(14,minval=2,title="ATR Length for KC")
  42. // - /INPUTS
  43.  
  44. // - FUNCTIONS
  45.  
  46. // Returns MA input selection variant, default to SMA if blank or typo.
  47. variant(type, src, len) =>
  48. v1 = sma(src, len) // Simple
  49. v2 = ema(src, len) // Exponential
  50. v3 = wma(src, len) // Weighted
  51. v4 = vwma(src, len) // Volume Weighted
  52. v5 = na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len // Smoothed
  53. v6 = 2 * v2 - ema(v2, len) // Double Exponential
  54. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  55. v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
  56. v11 = sma(sma(src,len),len) // Triangular
  57. // SuperSmoother filter
  58. // © 2013 John F. Ehlers
  59. a1 = exp(-1.414*3.14159 / len)
  60. b1 = 2*a1*cos(1.414*3.14159 / len)
  61. c2 = b1
  62. c3 = (-a1)*a1
  63. c1 = 1 - c2 - c3
  64. v9 = c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  65. // Zero Lag Exponential
  66. ema1 = ema(src, len)
  67. ema2 = ema(ema1, len)
  68. v10 = ema1+(ema1-ema2)
  69. // return variant, defaults to SMA if input invalid.
  70. 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
  71.  
  72. // - /FUNCTIONS
  73.  
  74. // - SERIES VARIABLES
  75. // MA's
  76.  
  77. // If this is 5min or less Time Frame select EMAs
  78. mult = not isintraday or anchor==0 or interval<=0 or interval>=anchor? 1 : round(anchor/interval)>1? round(anchor/interval) : 1
  79. ma_len = mult==1 ? ma_len_ : ma_len_*mult
  80. pac_len = mult==1 ? pac_len_ : pac_len_*mult
  81. fast_ma_len = mult==1 ? fast_ma_len_ : fast_ma_len_*mult
  82. slow_ma_len = mult==1 ? slow_ma_len_ : slow_ma_len_*mult
  83.  
  84.  
  85. ma_series = variant(ma_type, ma_src, ma_len)
  86. fast_ma_series = variant(fast_ma_type, fast_ma_src, fast_ma_len)
  87. slow_ma_series = variant(slow_ma_type, slow_ma_src, slow_ma_len)
  88. pacC = variant(pac_type, pac_src, pac_len)
  89. pacH = variant(pac_type, high, pac_len)
  90. pacL = variant(pac_type, low, pac_len)
  91.  
  92. KC = ema(close, KClength)
  93. ATR = security(tickerid, "D", atr(ATRlength))
  94. KCmult = not isintraday or interval<=0 or interval>=1440? 1 : round(1440/interval)>1? round(1440/interval) : 1
  95. KCtop = KC + (sqrt(ATR)/KCmult) * KCdeviation
  96. KCbot = KC - (sqrt(ATR)/KCmult) * KCdeviation
  97.  
  98. // - /SERIES VARIABLES
  99.  
  100. // Get Direction From Moving Average
  101. direction = rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : 0
  102. // Count Bars going up or going down
  103. up = direction>0 and close>ma_series? na(up[1])? 1 : up[1]+1 : 0
  104. down = direction<0 and close<ma_series? na(down[1])? 1 : down[1]+1 : 0
  105.  
  106. // - /SERIES VARIABLES
  107.  
  108. // - PLOTTING
  109. // Plot MA series and color it according too direction
  110. plot(ma_series, title="MA Plot", color=direction<0?red:direction>0?green:gray, linewidth=3, transp=0)
  111. plot(fast_ma_series, title="Fast MA Plot", color=orange, style=line, linewidth=2, transp=20)
  112. plot(slow_ma_series, title="Slow MA Plot", color=black, linewidth=3, transp=20)
  113.  
  114. plot(sKC? KC: na, color=black,linewidth=1)
  115. KCt = plot(sKC? KCtop: na, color=blue,transp=50,linewidth=1)
  116. KCb = plot(sKC? KCbot: na, color=blue,transp=50,linewidth=1)
  117. fill(KCt,KCb,color=blue,transp=90)
  118.  
  119.  
  120. // Color Bars
  121. //trendColour = gray
  122. H=plot(ShowPAC ?pacH:na, color=green, linewidth=2, title="High PAC EMA",transp=20)
  123. L=plot(ShowPAC ?pacL:na, color=red, linewidth=2, title="Low PAC EMA",transp=20)
  124. C=plot(ShowPAC ?pacC:na, color=blue, linewidth=2, title="Close PAC EMA",transp=20)
  125. fill(L,H, color=gray,transp=90,title="Fill HiLo PAC")
  126.  
  127. // Colour bars according to the close position relative to the PAC selected.
  128. bcol = iff(uHiLoPAC, close>=open? close>=pacH? lime : close<=pacL? red : aqua : close>=pacH? green : close<=pacL? maroon : blue,
  129. close>=open? direction>0? (close>=ma_series?lime:aqua) : red : direction<0? (close>=ma_series?blue:maroon) : green )
  130.  
  131. barcolor(col_bars?bcol:na, title = "Bar Colours",transp=0)
  132.  
  133. // - /PLOTTING
  134.  
  135. // - ALERTING
  136. xPacLong = crossover(close,pacH) ? 1 : crossunder(close,pacL) ? 0 : nz(xPacLong[1])
  137. xPacShort = crossover(close,pacH) ? 0 : crossunder(close,pacL) ? 1 : nz(xPacShort[1])
  138.  
  139. isup = uHiLoPAC? xPacLong : up
  140. isdown = uHiLoPAC? xPacShort : down
  141.  
  142. // Check have alert
  143. up_alert = isup and (not filter_alert or close>pacH)? na(up_alert[1]) ? 1 : up_alert[1]+1 : 0
  144. dn_alert = isdown and (not filter_alert or close<pacL)? na(dn_alert[1]) ? 1 : dn_alert[1]+1 : 0
  145.  
  146. //
  147. // show an Arrow only when alert condition is met and the bar closed.
  148. 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")
  149. 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)
  150. 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)
  151.  
  152. // generate an alert if required.
  153. alertcondition(up_alert==1 or dn_alert==1, title="MA Tool Alert", message="MA alert")
  154.  
  155.  
  156. // - /ALERTING
  157.  
  158. // /eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement