Advertisement
JustUncleL

Fractals and Pivots R4

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