Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study(title="Fractals and Pivots R5 by JustUncleL", shorttitle="FTLPVT R5", overlay=true)
- // By: JustUncleL
- // Date: 13-Feb-2017
- // Version: R4
- //
- // Description:
- // This indicator display fractals, fractal levels, Pivot Points and HH/LL points.
- // Fractals and Pivots are only drawn after they have completed.
- //
- // references:
- // - [RS]Fractal Levels by RicardoSantos
- //
- //
- //
- ShowPivots_ = input(true)
- ShowPivotLabels = input(false)
- pvtLenL_ = input(7,minval=2,title="Pivot Length Left Hand Side")
- pvtLenR_ = input(3,minval=2,title="Pivot Length Right Hand Side")
- ShowSRLevels = input(true,title="Show Pivot S/R Level Extensions")
- ShowRenkoPvts = input(false,title="Use Renko Brick Chart Pivots instead of Candle Pivots")
- pvtLenL = ShowRenkoPvts? 2 : pvtLenL_
- pvtLenR = ShowRenkoPvts? 2 : pvtLenR_
- ShowPivots = ShowRenkoPvts? true: ShowPivots_
- //
- ShowFractals_ = input(true)
- ShowFractals = ShowRenkoPvts? false : ShowFractals_
- ShowFractalLevels_= input(false,title="Show Fractal Level Extensions")
- ShowFractalLevels = ShowRenkoPvts? false : ShowFractalLevels_
- ShowHHLL_ = input(false)
- ShowHHLL = ShowPivots or ShowRenkoPvts?false:ShowHHLL_
- maxLvlLen = input(0, title="Maximum S/R Level Extension Length")
- StarShape = input(2,minval=1,maxval=4,title="Pivot Marker: 1=Star, 2=Dot, 3=Diamond, 4=Cross")
- // Slow MA - type, source, length
- uMAf = input(false,title="Use MA Filter on Fractal Levels")
- uHHLLf = input(false,title="Use HH/LL Filter on Fractal Levels")
- // 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=8, 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=36, title="Slow MA - Length", minval=1)
- slow_ma_src = input(close, title="Slow MA - Source")
- sMAs = input(false, title="Show MA Lines")
- //
- close_ = ShowRenkoPvts? close : security(tickerid, period, close)
- open_ = ShowRenkoPvts? open : security(tickerid, period, open)
- high_ = ShowRenkoPvts? high :security(tickerid, period, high)
- low_ = ShowRenkoPvts? low : security(tickerid, period, low)
- // - /INPUTS
- //
- // --- CONSTANTS ---
- DodgerBlue = #1E90FF
- // --- /CONTANTS ---
- // - /INPUTS
- // - FUNCTIONS
- // - Completed Fractals Recognition Function
- isRegularFractal(mode) =>
- ret = mode == 1 ? high_[5] < high_[3] and high_[4] <= high_[3] and high_[3] > high_[2] and high_[3] > high_[1] :
- mode == -1 ? low_[5] > low_[3] and low_[4] >= low_[3] and low_[3] < low_[2] and low_[3] < low_[1] : false
- isRenkoPivot(mode) =>
- ret = mode == 1 ? high_[5] < high_[4] and high_[4] < high_[3] and high_[3] > high_[2] and high_[2] > high_[1] :
- mode == -1 ? low_[5] > low_[4] and low_[4] > low_[3] and low_[3] < low_[2] and low_[2] < low_[1] : false
- // - /
- //
- // - variant(type, src, len)
- // 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) =>
- ema1 = ema(src, len)
- ema2 = ema(ema1, len)
- v10 = ema1+(ema1-ema2)
- 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
- // return variant, defaults to SMA
- variant(type, src, len) =>
- 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=="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
- // - SERIES VARIABLES
- // Get High and Low Pivot Points
- pvthi_ = ShowRenkoPvts? isRenkoPivot(1) : security(tickerid, period, pivothigh(pvtLenL,pvtLenR))
- pvtlo_ = ShowRenkoPvts? isRenkoPivot(-1): security(tickerid, period, pivotlow(pvtLenL,pvtLenR))
- // Force Pivot completion.
- pvthi = ShowRenkoPvts? pvthi_ : pvthi_[1]
- pvtlo = ShowRenkoPvts? pvtlo_ : pvtlo_[1]
- //plotshape(high_,location=location.top)
- //plotshape(low_,location=location.bottom)
- // MA's
- 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)
- plot(sMAs? fast_ma_series: na, color=lime,title="Fast MA", join=true,style=circles,linewidth=2,transp=15)
- plot(sMAs? slow_ma_series: na, color=gray,title="Slow MA", join=true,style=circles,linewidth=2,transp=15)
- // Fractals
- topf = isRegularFractal(1)
- botf = isRegularFractal(-1)
- topfractals = 0.0
- topfractals := topf ? high_[3] : topfractals[1]
- botfractals = 0.0
- botfractals := botf ? low_[3] : botfractals[1]
- topfcolor = topfractals != topfractals[1] ? na : green
- botfcolor = botfractals != botfractals[1] ? na : red
- // ||-----------------------------------------------------------------------------------------------------||
- // ||--- Higher Highs, Lower Highs, Higher Lows, Lower Lows -------------------------------------------||
- higherhigh = topf == false ? false : ( valuewhen(topf == true, high_[3], 1) < valuewhen(topf == true, high_[3], 0) and
- (ShowPivots or valuewhen(topf == true, high_[3], 2) < valuewhen(topf == true, high_[3], 0)))
- lowerhigh = topf == false ? false : ( valuewhen(topf == true, high_[3], 1) > valuewhen(topf == true, high_[3], 0) and
- (ShowPivots or valuewhen(topf == true, high_[3], 2) > valuewhen(topf == true, high_[3], 0)))
- higherlow = botf == false ? false : ( valuewhen(botf == true, low_[3], 1) < valuewhen(botf == true, low_[3], 0) and
- (ShowPivots or valuewhen(botf == true, low_[3], 2) < valuewhen(botf == true, low_[3], 0)))
- lowerlow = botf == false ? false : ( valuewhen(botf == true, low_[3], 1) > valuewhen(botf == true, low_[3], 0) and
- (ShowPivots or valuewhen(botf == true, low_[3], 2) > valuewhen(botf == true, low_[3], 0)))
- // If selected Display the HH/LL above/below candle.
- plotshape(ShowHHLL ? higherhigh : na, title='HH', style=shape.square, location=location.abovebar, color=green, text="H.H", offset=-3,transp=0)
- plotshape(ShowHHLL ? lowerhigh : na, title='LH', style=shape.square, location=location.abovebar, color=green, text="L.H", offset=-3,transp=0)
- plotshape(ShowHHLL ? higherlow : na, title='HL', style=shape.square, location=location.belowbar, color=maroon, text="H.L", offset=-3,transp=0)
- plotshape(ShowHHLL ? lowerlow : na, title='LL', style=shape.square, location=location.belowbar, color=maroon, text="L.L", offset=-3,transp=0)
- // Show Fractals
- havespot = true//ShowPivots? (pvthi[pvtLenR-2] or pvtlo[pvtLenR-2]) : true
- plotshape(havespot and ShowFractals? topf :na, title='Regular Top Fractals', style=shape.triangleup, location=location.abovebar, color=green, offset=-3,transp=0,size=size.auto)
- plotshape(havespot and ShowFractals? botf :na, title='Regular Bottom Fractals', style=shape.triangledown, location=location.belowbar, color=red, offset=-3,transp=0,size=size.auto)
- // If Selected Display Pivot points
- //
- plotshape(ShowPivots and ShowPivotLabels? pvthi : na, title='HL Label', style=shape.cross, location=location.abovebar, color=green, text="P.H", offset=-pvtLenR-1,transp=0)
- plotshape(ShowPivots and ShowPivotLabels? pvtlo : na, title='LL Label', style=shape.cross, location=location.belowbar, color=maroon, text="P.L", offset=-pvtLenR-1,transp=0)
- //
- plotchar(ShowPivots and not ShowPivotLabels and StarShape==1? pvthi? high[pvtLenR+1]: na: na, title='High Pivot *', location=location.absolute, color=green, offset=-pvtLenR-1,transp=0,size=size.auto)
- plotshape(ShowPivots and not ShowPivotLabels and StarShape==3? pvthi? high[pvtLenR+1]: na: na, title="High Pivot #", location=location.absolute, color=green, style=shape.diamond, offset=-pvtLenR-1,transp=0,size=size.auto)
- plotshape(ShowPivots and not ShowPivotLabels and StarShape==4? pvthi? high[pvtLenR+1]: na: na, title='High Pivot +', location=location.absolute, color=green, style=shape.cross, offset=-pvtLenR-1,transp=0,size=size.auto)
- plot(ShowPivots and not ShowPivotLabels and StarShape==2? pvthi? high[pvtLenR+1]:na: na, color = black , style = circles, linewidth = 4, title="High Pivot ..",transp=20,offset=-pvtLenR-1)
- plot(ShowPivots and not ShowPivotLabels and StarShape==2? pvthi? high[pvtLenR+1]:na: na, color = lime , style = circles, linewidth = 3,title="High Pivot .",transp=0,offset=-pvtLenR-1)
- //
- plotchar(ShowPivots and not ShowPivotLabels and StarShape==1? pvtlo? low[pvtLenR+1]: na: na, title='Low Pivot *', location=location.absolute, color=maroon, offset=-pvtLenR-1,transp=0,size=size.auto)
- plotshape(ShowPivots and not ShowPivotLabels and StarShape==3? pvtlo? low[pvtLenR+1]: na: na, title='Low Pivot #', location=location.absolute, color=maroon, style=shape.diamond, offset=-pvtLenR-1,transp=0,size=size.auto)
- plotshape(ShowPivots and not ShowPivotLabels and StarShape==4? pvtlo? low[pvtLenR+1]: na: na, title='Low Pivot +', location=location.absolute, color=maroon, style=shape.cross, offset=-pvtLenR-1,transp=0,size=size.auto)
- plot(ShowPivots and not ShowPivotLabels and StarShape==2? pvtlo? low[pvtLenR+1]: na: na, color = black , style = circles, linewidth = 4, title="Low Pivot ..",transp=20,offset=-pvtLenR-1)
- plot(ShowPivots and not ShowPivotLabels and StarShape==2? pvtlo? low[pvtLenR+1]: na: na, color = red , style = circles, linewidth = 3,title="Low Pivot .",transp=0,offset=-pvtLenR-1)
- //
- // ||--- Higher Highs, Lower Lows on All Fractals -------------------------------------------||
- HH = topf == false ? false : ( valuewhen(topf == true, high_[3], 1) < valuewhen(topf == true, high_[3], 0) and
- (valuewhen(topf == true, high_[3], 2) < valuewhen(topf == true, high_[3], 0)))
- LL = botf == false ? false : ( valuewhen(botf == true, low_[3], 1) > valuewhen(botf == true, low_[3], 0) and
- (valuewhen(botf == true, low_[3], 2) > valuewhen(botf == true, low_[3], 0)))
- //Count How many candles for current Pivot Level, If new reset.
- topcnt = 0
- botcnt = 0
- topcnt := topf and (not uHHLLf or HH)? 0 : nz(topcnt[1])+1
- botcnt := botf and (not uHHLLf or LL)? 0 : nz(botcnt[1])+1
- topfs = 0.0
- botfs = 0.0
- topfs := topf and (not uHHLLf or HH)? high_[3] : topfs[1]
- botfs := botf and (not uHHLLf or LL)? low_[3] : botfs[1]
- topfc = (topfs != topfs[1]) or (uMAf and fast_ma_series<slow_ma_series)? na : green
- botfc = (botfs != botfs[1]) or (uMAf and fast_ma_series>slow_ma_series)? na : red
- plot(ShowFractalLevels and topcnt<maxLvlLen? topfs : na, color=topfc, transp=0, linewidth=2, offset=-3, title="Top Levels")
- plot(ShowFractalLevels and botcnt<maxLvlLen? botfs : na, color=botfc, transp=0, linewidth=2, offset=-3, title="Bottom Levels")
- //Count How many candles for current Pivot Level, If new reset.
- pHH = pvthi == false ? false : ( valuewhen(topf == true, high_[pvtLenR+1], 1) < valuewhen(topf == true, high_[pvtLenR+1], 0) and
- (valuewhen(topf == true, high_[pvtLenR+1], 2) < valuewhen(topf == true, high_[pvtLenR+1], 0)))
- pLL = pvtlo == false ? false : ( valuewhen(botf == true, low_[pvtLenR+1], 1) > valuewhen(botf == true, low_[pvtLenR+1], 0) and
- (valuewhen(botf == true, low_[pvtLenR+1], 2) > valuewhen(botf == true, low_[pvtLenR+1], 0)))
- counthi = 0
- countlo = 0
- counthi := pvthi and (not uHHLLf or pHH)? 0 : nz(counthi[1])+1
- countlo := pvtlo and (not uHHLLf or pLL)? 0 : nz(countlo[1])+1
- pvthis = 0.0
- pvtlos = 0.0
- pvthis := pvthi and (not uHHLLf or pHH)? high[pvtLenR+1] : pvthis[1]
- pvtlos := pvtlo and (not uHHLLf or pLL)? low[pvtLenR+1] : pvtlos[1]
- hipc = (pvthis != pvthis[1]) or (uMAf and fast_ma_series<slow_ma_series)? na : green
- lopc = (pvtlos != pvtlos[1]) or (uMAf and fast_ma_series>slow_ma_series)? na : red
- plot(ShowSRLevels and (maxLvlLen==0 or counthi<maxLvlLen)? pvthis : na, color=hipc, transp=0, linewidth=2, offset=-pvtLenR-1, title="Top Levels")
- plot(ShowSRLevels and (maxLvlLen==0 or countlo<maxLvlLen)? pvtlos : na, color=lopc, transp=0, linewidth=2, offset=-pvtLenR-1, title="Bottom Levels")
- //
- //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement