Advertisement
JustUncleL

Coloured MA

Mar 26th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. //@version=3
  2. //
  3.  
  4. study("Coloured MA",overlay=true)
  5.  
  6. //
  7. // Description: Colour MA when 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="MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
  16. ma_len_ = input(defval=8, title="MA - Length", minval=1)
  17. ma_src = input(close, title="MA - Source")
  18. col_bars = input(false, title="Colour Bars")
  19.  
  20. //
  21. // - /INPUTS
  22.  
  23. // - FUNCTIONS
  24.  
  25. // Returns MA input selection variant, default to SMA if blank or typo.
  26. variant(type, src, len) =>
  27. v1 = sma(src, len) // Simple
  28. v2 = ema(src, len) // Exponential
  29. v3 = wma(src, len) // Weighted
  30. v4 = vwma(src, len) // Volume Weighted
  31. v5 = 0.0
  32. v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len // Smoothed
  33. v6 = 2 * v2 - ema(v2, len) // Double Exponential
  34. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  35. v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
  36. v11 = sma(sma(src,len),len) // Triangular
  37. // SuperSmoother filter
  38. // © 2013 John F. Ehlers
  39. a1 = exp(-1.414*3.14159 / len)
  40. b1 = 2*a1*cos(1.414*3.14159 / len)
  41. c2 = b1
  42. c3 = (-a1)*a1
  43. c1 = 1 - c2 - c3
  44. v9 = 0.0
  45. v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  46. // Zero Lag Exponential
  47. ema1 = ema(src, len)
  48. ema2 = ema(ema1, len)
  49. v10 = ema1+(ema1-ema2)
  50. // return variant, defaults to SMA if input invalid.
  51. 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
  52.  
  53. // - /FUNCTIONS
  54.  
  55. // - SERIES VARIABLES
  56. // MA's
  57.  
  58. // If this is 5min or less Time Frame select EMAs
  59. mult = not isintraday or anchor==0 or interval<=0 or interval>=anchor? 1 : round(anchor/interval)>1? round(anchor/interval) : 1
  60. ma_len = mult==1 ? ma_len_ : ma_len_*mult
  61. ma_series = variant(ma_type, ma_src, ma_len)
  62.  
  63. // Get Direction From Moving Average
  64. direction = rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : 0
  65.  
  66. // - /SERIES VARIABLES
  67.  
  68. // - PLOTTING
  69. // Plot MA series and color it according too direction
  70. plot(ma_series, title="MA Plot", color=direction<0?red:direction>0?green:gray, linewidth=3, transp=0)
  71.  
  72. // Colour bars according to the close position relative to the PAC selected.
  73. bcol = col_bars? direction<0 and close<ma_series ?red: close>ma_series and direction>0?green: gray : na
  74. barcolor(bcol, title = "Bar Colours",transp=0)
  75.  
  76. // - /PLOTTING
  77.  
  78. // /eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement