Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=2
- //
- study("MA Alert v4",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")
- //
- uHiLoPAC = input(false,"Use High Low PAC instead of Colored EMA for Alerts")
- pac_type = input(defval="EMA", title="PAC MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
- pac_len = input(defval=5, title="PAC MA - Length", minval=1)
- pac_src = input(close, title="PAC MA - Source")
- ShowPAC = input(false)
- col_bars = input(true, title="Colour Alert Bars")
- show_alert= input(false, title="Show BUY/SELL Alerts")
- UseBigArrows = input(false, title="Use Big Arrows instead of BUY/SELL for Alerts")
- shunt_signal = input(false,"Shunt BUY/SELL Alert to Bar Completed")
- shunt = shunt_signal? 1 : 0
- // 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")
- // - /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)
- pacC = variant(pac_type, pac_src, pac_len)
- pacH = variant(pac_type, high, pac_len)
- pacL = variant(pac_type, low, pac_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 and close>ma_series? na(up[1])? 1 : up[1]+1 : 0
- down = direction<0 and close<ma_series? 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, style=line, linewidth=2, transp=50)
- plot(slow_ma_series, title="Slow MA Plot", color=blue, linewidth=3, transp=20)
- // Color Bars
- trendColour = gray
- H=plot(ShowPAC ?pacH:na, color=trendColour, linewidth=1, title="High PAC EMA",transp=50)
- L=plot(ShowPAC ?pacL:na, color=trendColour, linewidth=1, title="Low PAC EMA",transp=50)
- C=plot(ShowPAC ?pacC:na, color=olive, linewidth=1, title="Close PAC EMA",transp=20)
- fill(L,H, color=trendColour,transp=90,title="Fill HiLo PAC")
- // Colour bars according to the close position relative to the PAC selected.
- 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)
- barcolor(col_bars?bcol:na, title = "Bar Colours",transp=0)
- // - /PLOTTING
- // - ALERTING
- xPacLong = crossover(close,pacH) ? 1 : crossunder(close,pacL) ? 0 : nz(xPacLong[1])
- xPacShort = crossover(close,pacH) ? 0 : crossunder(close,pacL) ? 1 : nz(xPacShort[1])
- isup = uHiLoPAC? xPacLong : up
- isdown = uHiLoPAC? xPacShort : down
- // Check have alert
- up_alert = isup ? na(up_alert[1]) ? 1 : up_alert[1]+1 : 0
- dn_alert = isdown ? na(dn_alert[1]) ? 1 : dn_alert[1]+1 : 0
- //
- // show an Arrow only when alert condition is met and the bar closed.
- plotarrow(show_alert and UseBigArrows? up_alert[shunt]==1? 1 : dn_alert[shunt]==1? -1 : na : na, colorup=aqua, colordown=fuchsia, transp=20,minheight=10,maxheight=20, title="Big Alert Arrows")
- 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)
- 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)
- // generate an alert if required.
- alertcondition(up_alert==1 or dn_alert==1, title="MA alert ", message="MA alert")
- // - /ALERTING
- // /eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement