Advertisement
JustUncleL

Fractals and Pivots R5

Apr 30th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.30 KB | None | 0 0
  1. //@version=3
  2.  
  3. study(title="Fractals and Pivots R5 by JustUncleL", shorttitle="FTLPVT R5", overlay=true)
  4.  
  5. // By: JustUncleL
  6. // Date: 13-Feb-2017
  7. // Version: R4
  8. //
  9. // Description:
  10. // This indicator display fractals, fractal levels, Pivot Points and HH/LL points.
  11. // Fractals and Pivots are only drawn after they have completed.
  12. //
  13. // references:
  14. // - [RS]Fractal Levels by RicardoSantos
  15. //
  16. //
  17. //
  18.  
  19. ShowPivots_ = input(true)
  20. ShowPivotLabels = input(false)
  21. pvtLenL_ = input(7,minval=2,title="Pivot Length Left Hand Side")
  22. pvtLenR_ = input(3,minval=2,title="Pivot Length Right Hand Side")
  23. ShowSRLevels = input(true,title="Show Pivot S/R Level Extensions")
  24. ShowRenkoPvts = input(false,title="Use Renko Brick Chart Pivots instead of Candle Pivots")
  25. pvtLenL = ShowRenkoPvts? 2 : pvtLenL_
  26. pvtLenR = ShowRenkoPvts? 2 : pvtLenR_
  27. ShowPivots = ShowRenkoPvts? true: ShowPivots_
  28. //
  29. ShowFractals_ = input(true)
  30. ShowFractals = ShowRenkoPvts? false : ShowFractals_
  31. ShowFractalLevels_= input(false,title="Show Fractal Level Extensions")
  32. ShowFractalLevels = ShowRenkoPvts? false : ShowFractalLevels_
  33. ShowHHLL_ = input(false)
  34. ShowHHLL = ShowPivots or ShowRenkoPvts?false:ShowHHLL_
  35. maxLvlLen = input(0, title="Maximum S/R Level Extension Length")
  36. StarShape = input(2,minval=1,maxval=4,title="Pivot Marker: 1=Star, 2=Dot, 3=Diamond, 4=Cross")
  37. // Slow MA - type, source, length
  38. uMAf = input(false,title="Use MA Filter on Fractal Levels")
  39. uHHLLf = input(false,title="Use HH/LL Filter on Fractal Levels")
  40. // Fast MA - type, source, length
  41. 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)
  42. fast_ma_len = input(defval=8, title="Fast MA - Length", minval=1)
  43. fast_ma_src = input(close, title="Fast MA - Source")
  44. // Slow MA - type, source, length
  45. 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)
  46. slow_ma_len = input(defval=36, title="Slow MA - Length", minval=1)
  47. slow_ma_src = input(close, title="Slow MA - Source")
  48. sMAs = input(false, title="Show MA Lines")
  49.  
  50. //
  51.  
  52. close_ = ShowRenkoPvts? close : security(tickerid, period, close)
  53. open_ = ShowRenkoPvts? open : security(tickerid, period, open)
  54. high_ = ShowRenkoPvts? high :security(tickerid, period, high)
  55. low_ = ShowRenkoPvts? low : security(tickerid, period, low)
  56.  
  57. // - /INPUTS
  58.  
  59. //
  60. // --- CONSTANTS ---
  61. DodgerBlue = #1E90FF
  62. // --- /CONTANTS ---
  63.  
  64.  
  65.  
  66.  
  67. // - /INPUTS
  68.  
  69. // - FUNCTIONS
  70.  
  71. // - Completed Fractals Recognition Function
  72. isRegularFractal(mode) =>
  73. ret = mode == 1 ? high_[5] < high_[3] and high_[4] <= high_[3] and high_[3] > high_[2] and high_[3] > high_[1] :
  74. mode == -1 ? low_[5] > low_[3] and low_[4] >= low_[3] and low_[3] < low_[2] and low_[3] < low_[1] : false
  75.  
  76. isRenkoPivot(mode) =>
  77. ret = mode == 1 ? high_[5] < high_[4] and high_[4] < high_[3] and high_[3] > high_[2] and high_[2] > high_[1] :
  78. mode == -1 ? low_[5] > low_[4] and low_[4] > low_[3] and low_[3] < low_[2] and low_[2] < low_[1] : false
  79.  
  80. // - /
  81.  
  82. //
  83. // - variant(type, src, len)
  84. // Returns MA input selection variant, default to SMA if blank or typo.
  85.  
  86. // SuperSmoother filter
  87. // © 2013 John F. Ehlers
  88. variant_supersmoother(src,len) =>
  89. a1 = exp(-1.414*3.14159 / len)
  90. b1 = 2*a1*cos(1.414*3.14159 / len)
  91. c2 = b1
  92. c3 = (-a1)*a1
  93. c1 = 1 - c2 - c3
  94. v9 = 0.0
  95. v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  96. v9
  97.  
  98. variant_smoothed(src,len) =>
  99. v5 = 0.0
  100. v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
  101. v5
  102.  
  103. variant_zerolagema(src,len) =>
  104. ema1 = ema(src, len)
  105. ema2 = ema(ema1, len)
  106. v10 = ema1+(ema1-ema2)
  107. v10
  108.  
  109. variant_doubleema(src,len) =>
  110. v2 = ema(src, len)
  111. v6 = 2 * v2 - ema(v2, len)
  112. v6
  113.  
  114. variant_tripleema(src,len) =>
  115. v2 = ema(src, len)
  116. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  117. v7
  118.  
  119. // return variant, defaults to SMA
  120. variant(type, src, len) =>
  121. type=="EMA" ? ema(src,len) :
  122. type=="WMA" ? wma(src,len):
  123. type=="VWMA" ? vwma(src,len) :
  124. type=="SMMA" ? variant_smoothed(src,len) :
  125. type=="DEMA" ? variant_doubleema(src,len):
  126. type=="TEMA" ? variant_tripleema(src,len):
  127. type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
  128. type=="SSMA" ? variant_supersmoother(src,len) :
  129. type=="ZEMA" ? variant_zerolagema(src,len) :
  130. type=="TMA" ? sma(sma(src,len),len) : sma(src,len)
  131.  
  132. // - /variant
  133.  
  134. // - /FUNCTIONS
  135.  
  136. // - SERIES VARIABLES
  137.  
  138. // Get High and Low Pivot Points
  139. pvthi_ = ShowRenkoPvts? isRenkoPivot(1) : security(tickerid, period, pivothigh(pvtLenL,pvtLenR))
  140. pvtlo_ = ShowRenkoPvts? isRenkoPivot(-1): security(tickerid, period, pivotlow(pvtLenL,pvtLenR))
  141. // Force Pivot completion.
  142. pvthi = ShowRenkoPvts? pvthi_ : pvthi_[1]
  143. pvtlo = ShowRenkoPvts? pvtlo_ : pvtlo_[1]
  144.  
  145. //plotshape(high_,location=location.top)
  146. //plotshape(low_,location=location.bottom)
  147. // MA's
  148. fast_ma_series = variant(fast_ma_type, fast_ma_src, fast_ma_len)
  149. slow_ma_series = variant(slow_ma_type, slow_ma_src, slow_ma_len)
  150.  
  151. plot(sMAs? fast_ma_series: na, color=lime,title="Fast MA", join=true,style=circles,linewidth=2,transp=15)
  152. plot(sMAs? slow_ma_series: na, color=gray,title="Slow MA", join=true,style=circles,linewidth=2,transp=15)
  153.  
  154. // Fractals
  155. topf = isRegularFractal(1)
  156. botf = isRegularFractal(-1)
  157.  
  158. topfractals = 0.0
  159. topfractals := topf ? high_[3] : topfractals[1]
  160. botfractals = 0.0
  161. botfractals := botf ? low_[3] : botfractals[1]
  162.  
  163. topfcolor = topfractals != topfractals[1] ? na : green
  164. botfcolor = botfractals != botfractals[1] ? na : red
  165.  
  166.  
  167.  
  168. // ||-----------------------------------------------------------------------------------------------------||
  169. // ||--- Higher Highs, Lower Highs, Higher Lows, Lower Lows -------------------------------------------||
  170. higherhigh = topf == false ? false : ( valuewhen(topf == true, high_[3], 1) < valuewhen(topf == true, high_[3], 0) and
  171. (ShowPivots or valuewhen(topf == true, high_[3], 2) < valuewhen(topf == true, high_[3], 0)))
  172. lowerhigh = topf == false ? false : ( valuewhen(topf == true, high_[3], 1) > valuewhen(topf == true, high_[3], 0) and
  173. (ShowPivots or valuewhen(topf == true, high_[3], 2) > valuewhen(topf == true, high_[3], 0)))
  174. higherlow = botf == false ? false : ( valuewhen(botf == true, low_[3], 1) < valuewhen(botf == true, low_[3], 0) and
  175. (ShowPivots or valuewhen(botf == true, low_[3], 2) < valuewhen(botf == true, low_[3], 0)))
  176. lowerlow = botf == false ? false : ( valuewhen(botf == true, low_[3], 1) > valuewhen(botf == true, low_[3], 0) and
  177. (ShowPivots or valuewhen(botf == true, low_[3], 2) > valuewhen(botf == true, low_[3], 0)))
  178.  
  179. // If selected Display the HH/LL above/below candle.
  180. plotshape(ShowHHLL ? higherhigh : na, title='HH', style=shape.square, location=location.abovebar, color=green, text="H.H", offset=-3,transp=0)
  181. plotshape(ShowHHLL ? lowerhigh : na, title='LH', style=shape.square, location=location.abovebar, color=green, text="L.H", offset=-3,transp=0)
  182. plotshape(ShowHHLL ? higherlow : na, title='HL', style=shape.square, location=location.belowbar, color=maroon, text="H.L", offset=-3,transp=0)
  183. plotshape(ShowHHLL ? lowerlow : na, title='LL', style=shape.square, location=location.belowbar, color=maroon, text="L.L", offset=-3,transp=0)
  184.  
  185. // Show Fractals
  186. havespot = true//ShowPivots? (pvthi[pvtLenR-2] or pvtlo[pvtLenR-2]) : true
  187. 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)
  188. 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)
  189.  
  190. // If Selected Display Pivot points
  191. //
  192. 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)
  193. 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)
  194.  
  195. //
  196. 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)
  197. 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)
  198. 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)
  199. 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)
  200. 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)
  201.  
  202. //
  203. 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)
  204. 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)
  205. 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)
  206. 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)
  207. 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)
  208. //
  209.  
  210. // ||--- Higher Highs, Lower Lows on All Fractals -------------------------------------------||
  211. HH = topf == false ? false : ( valuewhen(topf == true, high_[3], 1) < valuewhen(topf == true, high_[3], 0) and
  212. (valuewhen(topf == true, high_[3], 2) < valuewhen(topf == true, high_[3], 0)))
  213. LL = botf == false ? false : ( valuewhen(botf == true, low_[3], 1) > valuewhen(botf == true, low_[3], 0) and
  214. (valuewhen(botf == true, low_[3], 2) > valuewhen(botf == true, low_[3], 0)))
  215.  
  216. //Count How many candles for current Pivot Level, If new reset.
  217. topcnt = 0
  218. botcnt = 0
  219. topcnt := topf and (not uHHLLf or HH)? 0 : nz(topcnt[1])+1
  220. botcnt := botf and (not uHHLLf or LL)? 0 : nz(botcnt[1])+1
  221.  
  222. topfs = 0.0
  223. botfs = 0.0
  224. topfs := topf and (not uHHLLf or HH)? high_[3] : topfs[1]
  225. botfs := botf and (not uHHLLf or LL)? low_[3] : botfs[1]
  226.  
  227. topfc = (topfs != topfs[1]) or (uMAf and fast_ma_series<slow_ma_series)? na : green
  228. botfc = (botfs != botfs[1]) or (uMAf and fast_ma_series>slow_ma_series)? na : red
  229.  
  230. plot(ShowFractalLevels and topcnt<maxLvlLen? topfs : na, color=topfc, transp=0, linewidth=2, offset=-3, title="Top Levels")
  231. plot(ShowFractalLevels and botcnt<maxLvlLen? botfs : na, color=botfc, transp=0, linewidth=2, offset=-3, title="Bottom Levels")
  232.  
  233. //Count How many candles for current Pivot Level, If new reset.
  234. pHH = pvthi == false ? false : ( valuewhen(topf == true, high_[pvtLenR+1], 1) < valuewhen(topf == true, high_[pvtLenR+1], 0) and
  235. (valuewhen(topf == true, high_[pvtLenR+1], 2) < valuewhen(topf == true, high_[pvtLenR+1], 0)))
  236. pLL = pvtlo == false ? false : ( valuewhen(botf == true, low_[pvtLenR+1], 1) > valuewhen(botf == true, low_[pvtLenR+1], 0) and
  237. (valuewhen(botf == true, low_[pvtLenR+1], 2) > valuewhen(botf == true, low_[pvtLenR+1], 0)))
  238.  
  239. counthi = 0
  240. countlo = 0
  241. counthi := pvthi and (not uHHLLf or pHH)? 0 : nz(counthi[1])+1
  242. countlo := pvtlo and (not uHHLLf or pLL)? 0 : nz(countlo[1])+1
  243.  
  244. pvthis = 0.0
  245. pvtlos = 0.0
  246. pvthis := pvthi and (not uHHLLf or pHH)? high[pvtLenR+1] : pvthis[1]
  247. pvtlos := pvtlo and (not uHHLLf or pLL)? low[pvtLenR+1] : pvtlos[1]
  248.  
  249. hipc = (pvthis != pvthis[1]) or (uMAf and fast_ma_series<slow_ma_series)? na : green
  250. lopc = (pvtlos != pvtlos[1]) or (uMAf and fast_ma_series>slow_ma_series)? na : red
  251.  
  252. plot(ShowSRLevels and (maxLvlLen==0 or counthi<maxLvlLen)? pvthis : na, color=hipc, transp=0, linewidth=2, offset=-pvtLenR-1, title="Top Levels")
  253. plot(ShowSRLevels and (maxLvlLen==0 or countlo<maxLvlLen)? pvtlos : na, color=lopc, transp=0, linewidth=2, offset=-pvtLenR-1, title="Bottom Levels")
  254.  
  255. //
  256. //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement