Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- //
- study("Coloured MA",overlay=true)
- //
- // Description: Colour MA when changes direction
- //
- // - INPUTS
- // Use Alternate Anchor TF for MAs
- anchor = input(0,minval=0,maxval=1440,title="Use Alternate Anchor TimeFrame (0=none, max=1440mins)")
- // Medium Fast MA - type, source, length
- ma_type = input(defval="EMA", title="MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
- ma_len_ = input(defval=8, title="MA - Length", minval=1)
- ma_src = input(close, title="MA - Source")
- col_bars = input(false, title="Colour Bars")
- //
- // - /INPUTS
- // - FUNCTIONS
- // Returns MA input selection variant, default to SMA if blank or typo.
- variant(type, src, len) =>
- v1 = sma(src, len) // Simple
- v2 = ema(src, len) // Exponential
- v3 = wma(src, len) // Weighted
- v4 = vwma(src, len) // Volume Weighted
- v5 = 0.0
- v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len // Smoothed
- v6 = 2 * v2 - ema(v2, len) // Double Exponential
- v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
- v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
- v11 = sma(sma(src,len),len) // Triangular
- // SuperSmoother filter
- // © 2013 John F. Ehlers
- a1 = exp(-1.414*3.14159 / len)
- b1 = 2*a1*cos(1.414*3.14159 / len)
- c2 = b1
- c3 = (-a1)*a1
- c1 = 1 - c2 - c3
- v9 = 0.0
- v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
- // Zero Lag Exponential
- ema1 = ema(src, len)
- ema2 = ema(ema1, len)
- v10 = ema1+(ema1-ema2)
- // return variant, defaults to SMA if input invalid.
- 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
- // - /FUNCTIONS
- // - SERIES VARIABLES
- // MA's
- // If this is 5min or less Time Frame select EMAs
- mult = not isintraday or anchor==0 or interval<=0 or interval>=anchor? 1 : round(anchor/interval)>1? round(anchor/interval) : 1
- ma_len = mult==1 ? ma_len_ : ma_len_*mult
- ma_series = variant(ma_type, ma_src, ma_len)
- // Get Direction From Moving Average
- direction = rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : 0
- // - /SERIES VARIABLES
- // - PLOTTING
- // Plot MA series and color it according too direction
- plot(ma_series, title="MA Plot", color=direction<0?red:direction>0?green:gray, linewidth=3, transp=0)
- // Colour bars according to the close position relative to the PAC selected.
- bcol = col_bars? direction<0 and close<ma_series ?red: close>ma_series and direction>0?green: gray : na
- barcolor(bcol, title = "Bar Colours",transp=0)
- // - /PLOTTING
- // /eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement