Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=2
- //
- study("MA Alert v3",overlay=true)
- //
- // Description: Create alert when MA changes direction
- //
- // - INPUTS
- // Medium Fast MA - type, source, length
- ma_type = input(defval="EMA", title="Alert MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
- ma_len = input(defval=8, title="Alert MA - Length", minval=1)
- ma_src = input(close, title="Alert MA - Source")
- col_bars = input(true, title="Colour Alert Bars")
- show_alert= input(false, title="Show BUY/SELL Alerts")
- // Fast MA - type, source, length
- 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)
- fast_ma_len = input(defval=36, title="Fast MA - Length", minval=1)
- fast_ma_src = input(close, title="Fast MA - Source")
- // Slow MA - type, source, length
- 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)
- slow_ma_len = input(defval=200, title="Slow MA - Length", minval=1)
- slow_ma_src = input(close, title="Slow MA - Source")
- shunt_signal = input(true)
- shunt = shunt_signal? 1 : 0
- // - /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 = 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 = 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
- ma_series = variant(ma_type, ma_src, ma_len)
- fast_ma_series = variant(fast_ma_type, fast_ma_src, fast_ma_len)
- slow_ma_series = variant(slow_ma_type, slow_ma_src, slow_ma_len)
- // - /SERIES VARIABLES
- // Get Direction From Moving Average
- direction = rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : 0
- // Count Bars going up or going down
- up = direction>0? na(up[1])? 1 : up[1]+1 : 0
- down = direction<0? na(down[1])? 1 : down[1]+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:aqua, linewidth=3, transp=0)
- plot(fast_ma_series, title="Fast MA Plot", color=gray, linewidth=3, transp=50)
- plot(slow_ma_series, title="Slow MA Plot", color=blue, linewidth=3, transp=20)
- // Color Bars
- col = direction<0 and close<ma_series ?red: close>ma_series and direction>0?green:aqua
- barcolor(col_bars?col:na)
- // - /PLOTTING
- // - ALERTING
- c_alert = up==1 or down==1
- alertcondition(c_alert, title="MA Alert", message="MA Alert")
- alertcondition(up==1, title="MA Buy Alert", message="MA Buy Alert")
- alertcondition(down==1, title="MA Sell Alert", message="MA Sell Alert")
- //
- // show an Arrow only when alert condition is met and the bar closed.
- plotshape(show_alert and down[shunt]==1?true:na,title= "MA Sell Alert Completed", location=location.abovebar, color=red, transp=0, style=shape.arrowdown, text="SELL",size=size.auto,offset=0)
- plotshape(show_alert and up[shunt]==1?true:na,title= "MA Buy Alert Completed", location=location.belowbar, color=green, transp=0, style=shape.arrowup, text="BUY",size=size.auto,offset=0)
- // - /ALERTING
- // /eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement