Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study(title="MA Ribbon R5 by JustUncleL", shorttitle="MA_RIBBON", overlay = true)
- //
- // Author: JustUncleL
- // Revision: R5
- //
- //
- // Description:
- // ============
- // This indicator plots and colour codes a ribbon between two moving averages.
- // The colour of the ribbon changes with trend direction: when price in uptrend
- // ribbon colour is green; when price in downtrend ribbon colour is red.
- //
- // Options Available:
- // ------------------
- // 1) You can select between 11 different types of moving averages, each MA line
- // can be a different type:
- // - SMA = Simple Moving Average.
- // - EMA = Exponential Moving Average.
- // - WMA = Weighted Moving Average
- // - VWMA = Volume Weighted Moving Average
- // - SMMA = Smoothed Simple Moving Average.
- // - DEMA = Double Exponential Moving Average
- // - TEMA = Triple Exponential Moving Average.
- // - LAGMA = Laguerre Moving Average.
- // - HullMA = Hull Moving Average
- // - SSMA = Ehlers Super Smoother Moving average
- // - ZEMA = Near Zero Lag Exponential Moving Average.
- // - TMA = Triangular (smoothed) Simple Moving Average.
- //
- //
- // 2) Option to display coloured Candles around the Ribbon, the coulouring uses
- // the Standard candle colours:
- // - Lime = candle closed above Ribbon.
- // - Red = candle closed below Ribbon.
- // - Gray = Candle Closed inside Ribbon.
- // the Grab candles scheme:
- // - Lime = Bull candle closed above Ribbon.
- // - Green = Bear candle closed above Ribbon.
- // - Red = Bull candle closed below Ribbon.
- // - DarkRed = Bear candle closed below Ribbon.
- // - Aqua = Bull candle closed inside Ribbon.
- // - Blue = Bear candle closed inside Ribbon.
- //
- // 3) Option to base the candles on a higher time frame (HFT), this performed by increasing
- // the MA length to create equivalent lengths from the HFT. So no re-painting.
- // NOTE: The script will time out if the MA lengths get too long after resizing.
- //
- // Modifications:
- // 06-Apr-2018
- // - Change Anchor calculation to be based on Minutes for trading period
- // Daily (1440 = 1 Day), Weekly (7200 = 5Days), and Monthly (30240 = 21 Days), to
- // make it consistant across the Intraday/extraDaily time frame crossover.
- //
- // 22-Jan-2018
- // - Added optional Filter on Alerts to only allow signals in direction
- // of main trend, ie Fast MA Directional Slope.
- //
- // 20-Jan-2018 R5 Release:
- // - Added Alert Buy/Sell Entry and exits option based on candle colour:
- // - When candle changes to green or lime, then Buy signal
- // - When candle changes to red or darkred, then Sell signal.
- // - When candle changes to different color to Buy/Sell then exit signal.
- // - Added alarms for TradingView's Alarm Subsystem.
- //
- // 13-Jan-2018
- // - Corrected Anchor calculation for non-intraday charts.
- // - Added implied GPL copyright notice.
- // - Added option for both open and close outside ribbon colouring, instead of
- // just close.
- //
- // 22-Aug-2017
- // - Added Laguerre MA type.
- // - Some code tidy up.
- //
- // 11-Aug-2017 Minor updates:
- // - Changed MA Type input to options selection list.
- // - Change to Disable coloured bars by default, to allow already installed
- // scripts to colour bars first.
- //
- // 22-Jul-2017 - R4:
- // - Allow anchor option to be used for Days and Weeks.
- // - Allow option to use standard 3-tone bar colouring, or 6-tone Grab candle Colouring
- // - Fixed bug on bar colouring with some displays.
- //
- // R1 - Original
- //
- // R2 - Recoded variant function with more efficient version.
- // - Change bar colouring to Grab candle style.
- // - Added named colour constants, to include fully non-transparent, work around
- // for bar colouring transparency bug.
- //
- // R3 - Work around for transparency with drwaing bar colours.
- //
- // R4 - Added optional Alert
- //
- // -----------------------------------------------------------------------------
- // Copyright 2017,2018 JustUncleL
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // The GNU General Public License can be found here
- // <http://www.gnu.org/licenses/>.
- //
- // -----------------------------------------------------------------------------
- //
- // Use Alternate Anchor TF for MAs
- anchor = input(0,minval=0,title="Set to an Anchor TimeFrame in mins (0=none, 1D=1440, 1W=7200)")
- // Fast MA - type, length
- type1 = input(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "LAGMA", "HullMA", "ZEMA", "TMA", "SSMA"])
- len1 = input(defval=16, title="Fast MA - Length", minval=1)
- gamma1 = input(defval=0.33,title="Fast MA - Gamma for LAGMA")
- // Medium MA - type, length
- type3 = input(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "LAGMA", "HullMA", "ZEMA", "TMA", "SSMA"])
- len3 = input(defval=21, title="Medium MA - Length (1=disabled)", minval=1)
- gamma3 = input(defval=0.55,title="Medium MA - Gamma for LAGMA")
- // Slow MA - type, length
- type2 = input(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "LAGMA", "HullMA", "ZEMA", "TMA", "SSMA"])
- len2 = input(defval=26, title="Slow MA - Length", minval=2)
- gamma2 = input(defval=0.77,title="Slow MA - Gamma for LAGMA")
- //
- ma_src = input(close,title="MA Source")
- sBars = input(false,title="Show Coloured Bars")
- uGrabClr= input(false,title="Use Grab Bar 6-tone Colours, instead of Standard 3-tone")
- uOpen = input(false,title="Candles must open and close outside ribbon Colouring" )
- //
- ShowSwing = input(false,title="Show Swing Alerts")
- rFilter = input(false,title="Filter Alerts to Ribbon Colour")
- dFilter = input(false,title="Filter Alerts to Fast MA Directional Slope")
- //
- // - INPUTS END
- // Constants colours that include fully non-transparent option.
- green100 = #008000FF
- lime100 = #00FF00FF
- red100 = #FF0000FF
- blue100 = #0000FFFF
- aqua100 = #00FFFFFF
- darkred100 = #8B0000FF
- gray100 = #808080FF
- // - FUNCTIONS
- // - variant(type, src, len, gamma)
- // Returns MA input selection variant, default to SMA if blank or typo.
- // SuperSmoother filter
- // © 2013 John F. Ehlers
- variant_supersmoother(src,len) =>
- 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])
- v9
- variant_smoothed(src,len) =>
- v5 = 0.0
- v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
- v5
- variant_zerolagema(src,len) =>
- xLag = (len - 1) / 2
- xEMA = (src + (src - src[xLag]))
- v10 = ema(xEMA, len)
- v10
- variant_doubleema(src,len) =>
- v2 = ema(src, len)
- v6 = 2 * v2 - ema(v2, len)
- v6
- variant_tripleema(src,len) =>
- v2 = ema(src, len)
- v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
- v7
- //calc Laguerre
- variant_lag(p,g) =>
- L0 = 0.0
- L1 = 0.0
- L2 = 0.0
- L3 = 0.0
- L0 := (1 - g)*p+g*nz(L0[1])
- L1 := -g*L0+nz(L0[1])+g*nz(L1[1])
- L2 := -g*L1+nz(L1[1])+g*nz(L2[1])
- L3 := -g*L2+nz(L2[1])+g*nz(L3[1])
- f = (L0 + 2*L1 + 2*L2 + L3)/6
- f
- // return variant, defaults to SMA
- variant(type, src, len, g) =>
- type=="EMA" ? ema(src,len) :
- type=="WMA" ? wma(src,len):
- type=="VWMA" ? vwma(src,len) :
- type=="SMMA" ? variant_smoothed(src,len) :
- type=="DEMA" ? variant_doubleema(src,len):
- type=="TEMA" ? variant_tripleema(src,len):
- type=="LAGMA" ? variant_lag(src,g) :
- type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
- type=="SSMA" ? variant_supersmoother(src,len) :
- type=="ZEMA" ? variant_zerolagema(src,len) :
- type=="TMA" ? sma(sma(src,len),len) :
- sma(src,len)
- // - /variant
- // - FUNCTIONS END
- // Make sure we have minimum channel spread.
- Length2_ = type1==type2?(len2-len1)<1?len1+1:len2 : len2
- // If have anchor specified, calculate the base multiplier.
- mult = isintraday ? anchor==0 or interval<=0 or interval>=anchor? 1 : round(anchor/interval) : 1
- mult := isdwm? isdaily ? (anchor==0 or interval<=0 or interval>=anchor or anchor<=1440 ? 1 : round(anchor/1440)) :
- isweekly ? (anchor==0 or interval<=0 or interval>=anchor or anchor<=7200 ? 1 : round(anchor/7200)) :
- ismonthly ? (anchor==0 or interval<=0 or interval>=anchor or anchor<=30240 ? 1 : round(anchor/30240)) : 1 : mult
- // Adjust MA lengths with Anchor multiplier
- Length1 = mult==1 ? len1 : (len1*mult)
- Length2 = mult==1 ? Length2_ : (Length2_*mult)
- Length3 = mult==1 ? len3 : (len3*mult)
- //plotshape(interval,location=location.bottom)
- // Get the two MAs
- ma1 = variant(type1,ma_src, Length1, gamma1)
- ma2 = variant(type2,ma_src, Length2, gamma2)
- ma3 = Length3==1 ? na : variant(type3,ma_src, Length3, gamma3)
- fDirection = 0
- sDirection = 0
- mDirection = 0
- fDirection := hlc3 > ma1 ? 1 : hlc3 < ma1 ? -1 : nz(fDirection[1],1)
- sDirection := hlc3 > ma2 ? 1 : hlc3 < ma2 ? -1 : nz(sDirection[1],1)
- mDirection := Length3==1 ? na : hlc3 > ma3 ? 1 : hlc3 < ma3 ? -1 : nz(mDirection[1],1)
- //Plot the Ribbon
- ma1_=plot( ma1,color=fDirection==1?green:red,style=line,join=true,linewidth=1,transp=20,title="Fast MA")
- ma2_=plot( ma2,color=sDirection==1?green:red,style=line,join=true,linewidth=1,transp=20,title="Slow MA")
- ma3_=plot( ma3,color=sDirection==1?green:red,style=circles,join=true,linewidth=1,transp=20,title="Medium MA")
- fcolor = ma1>ma2?green:red
- fill(ma1_,ma2_,color=fcolor,transp=80,title="Ribbon Fill")
- // Colour bars according to the close position relative to the MA selected
- // Or Grab candle colour code bars according to the close position relative to the MA selected
- grabcol = uGrabClr? close>=open? hlc3>ma1 and hlc3>ma2 and (not uOpen or (open>ma1 and open>ma2))? lime100 :
- hlc3<ma1 and hlc3<ma2 and (not uOpen or (open<ma1 and open<ma2))? red100 : aqua100 :
- hlc3>ma1 and hlc3>ma2 and (not uOpen or (open>ma1 and open>ma2))? green100 :
- hlc3<ma1 and hlc3<ma2 and (not uOpen or (open<ma1 and open<ma2))? darkred100 : blue100 : na
- grabcol := uGrabClr? grabcol : hlc3>ma1 and hlc3>ma2 and (not uOpen or (open>ma1 and open>ma2))? lime100 :
- hlc3<ma1 and hlc3<ma2 and (not uOpen or (open<ma1 and open<ma2))? red100 : gray100
- barcolor(sBars?grabcol:na, title = "Bar Colours")
- // Generate Alert Arrows
- //
- buy = 0
- sell=0
- buyT = 0
- sellT =0
- // Generate signal by Candle Colour
- buy := grabcol==lime100 or grabcol==green100? (nz(buy[1])+1) : 0
- sell := grabcol==red100 or grabcol==darkred100? (nz(sell[1])+1) : 0
- // Trend Filter
- buyT := buy==0? 0 : (rFilter and ma1<ma2) or (dFilter and falling(ma1,2))? 0 : nz(buyT[1])+1
- sellT := sell==0? 0 : (rFilter and ma1>ma2) or (dFilter and rising(ma1,2))? 0 : nz(sellT[1])+1
- // Exit conditions
- exitbuy = nz(buyT[1])>0 and buyT==0
- exitsell = nz(sellT[1])>0 and sellT==0
- //
- plotarrow(ShowSwing and buyT==1 ?1:na, title="BUY Swing Arrow", colorup=lime, maxheight=60, minheight=50, transp=20)
- plotarrow(ShowSwing and sellT==1 ?-1:na, title="SELL Swing Arrow", colordown=red, maxheight=60, minheight=50, transp=20)
- //
- plotshape(ShowSwing and exitbuy, title='BUY Exit', style=shape.xcross, location=location.belowbar, color=gray, text="Exit\nBuy", offset=0,transp=0)
- plotshape(ShowSwing and exitsell, title='Sell Exit', style=shape.xcross, location=location.abovebar, color=gray, text="Exit\nSell", offset=0,transp=0)
- // Generate Alarms
- alertcondition(buyT==1,title="BUY Alert",message="BUY")
- alertcondition(sellT==1,title="SELL Alert",message="SELL")
- alertcondition(exitbuy,title="BUY Exit Alert",message="ExitBuy")
- alertcondition(exitsell,title="SELL Exit Alert",message="ExitSell")
- //eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement