Advertisement
JustUncleL

Fractals and Pivots R7.2 by JustUncleL

Aug 2nd, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. //@version=3
  2. study(title="Fractals and Pivots R7.2 by JustUncleL", shorttitle="FTLPVT R7.2", overlay=true)
  3.  
  4. // By: JustUncleL
  5. // Date: 13-Feb-2017
  6. // Version: R7.1
  7. //
  8. // Description:
  9. // This indicator display fractals, fractal levels, Pivot Points and HH/LL points.
  10. // Fractals are only drawn after they have completed.
  11. //
  12. // references:
  13. // - [RS]Fractal Levels by RicardoSantos
  14. //
  15. // Modifications:
  16. // R1 - original.
  17. //
  18. //
  19. //
  20.  
  21.  
  22. // === INPUTS
  23. //
  24. ShowFractals = input(true)
  25. ShowFractalLevels= input(false)
  26. ShowHHLL = input(false)
  27. filterBW = input(true,"Show Pivots Only")
  28. uRenko = input(false, title="Use Renko Style Fractals (open/close for high/low)")
  29. maxLvlLen = input(0, title="Maximum Fractal Level Extension Length (0=No limit)")
  30. //StarShape = input(4,minval=1,maxval=4,title="Pivot Marker: 1=Star, 2=Dot, 3=Diamond, 4=Cross")
  31.  
  32. uMAf = input(false,title="Use MA Filter on Fractal Levels")
  33. // Fast MA - type, source, length
  34. 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)
  35. fast_ma_len = input(defval=21, title="Fast MA - Length", minval=1)
  36. fast_ma_src = input(close, title="Fast MA - Source")
  37. // Slow MA - type, source, length
  38. 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)
  39. slow_ma_len = input(defval=36, title="Slow MA - Length", minval=1)
  40. slow_ma_src = input(close, title="Slow MA - Source")
  41. sMAs = input(false, title="Show MA Lines")
  42.  
  43. //
  44. close_ = security(tickerid, period, close)
  45. open_ = security(tickerid, period, open)
  46. high_ = uRenko? max(close_,open_) :security(tickerid, period, high)
  47. low_ = uRenko? min(close_,open_) :security(tickerid, period, low)
  48.  
  49. // - /INPUTS
  50.  
  51. //
  52. // --- CONSTANTS ---
  53. DodgerBlue = #1E90FF
  54. // --- /CONTANTS ---
  55.  
  56. // - FUNCTIONS
  57.  
  58. // ||--- Completed Fractals Recognition Functions: -----------------------------------------------------||
  59. isIdealFractal(mode) =>
  60. ret = mode == 1 ? high_[5] < high_[4] and high_[4] <= high_[3] and high_[3] > high_[2] and high_[2] > high_[1] :
  61. mode == -1 ? low_[5] > low_[4] and low_[4] >= low_[3] and low_[3] < low_[2] and low_[2] < low_[1] : false
  62.  
  63. isRegularFractal(mode) =>
  64. ret = mode == 1 ? high_[5] < high_[3] and high_[4] <= high_[3] and high_[3] > high_[2] and high_[3] > high_[1] :
  65. mode == -1 ? low_[5] > low_[3] and low_[4] >= low_[3] and low_[3] < low_[2] and low_[3] < low_[1] : false
  66.  
  67. // ||-----------------------------------------------------------------------------------------------------||
  68.  
  69. // - variant(type, src, len)
  70. // Returns MA input selection variant, default to SMA if blank or typo.
  71.  
  72. // SuperSmoother filter
  73. // © 2013 John F. Ehlers
  74. variant_supersmoother(src,len) =>
  75. a1 = exp(-1.414*3.14159 / len)
  76. b1 = 2*a1*cos(1.414*3.14159 / len)
  77. c2 = b1
  78. c3 = (-a1)*a1
  79. c1 = 1 - c2 - c3
  80. v9 = 0.0
  81. v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  82. v9
  83.  
  84. variant_smoothed(src,len) =>
  85. v5 = 0.0
  86. v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
  87. v5
  88.  
  89. variant_zerolagema(src,len) =>
  90. ema1 = ema(src, len)
  91. ema2 = ema(ema1, len)
  92. v10 = ema1+(ema1-ema2)
  93. v10
  94.  
  95. variant_doubleema(src,len) =>
  96. v2 = ema(src, len)
  97. v6 = 2 * v2 - ema(v2, len)
  98. v6
  99.  
  100. variant_tripleema(src,len) =>
  101. v2 = ema(src, len)
  102. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  103. v7
  104.  
  105. // return variant, defaults to SMA
  106. variant(type, src, len) =>
  107. type=="EMA" ? ema(src,len) :
  108. type=="WMA" ? wma(src,len):
  109. type=="VWMA" ? vwma(src,len) :
  110. type=="SMMA" ? variant_smoothed(src,len) :
  111. type=="DEMA" ? variant_doubleema(src,len):
  112. type=="TEMA" ? variant_tripleema(src,len):
  113. type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
  114. type=="SSMA" ? variant_supersmoother(src,len) :
  115. type=="ZEMA" ? variant_zerolagema(src,len) :
  116. type=="TMA" ? sma(sma(src,len),len) : sma(src,len)
  117.  
  118. // - /variant
  119.  
  120. // - /FUNCTIONS
  121.  
  122. // - SERIES VARIABLES
  123.  
  124. // MA's
  125. fast_ma_series = variant(fast_ma_type, fast_ma_src, fast_ma_len)
  126. slow_ma_series = variant(slow_ma_type, slow_ma_src, slow_ma_len)
  127.  
  128. plot(sMAs? fast_ma_series: na, color=lime,title="Fast MA", join=true,style=circles,linewidth=2,transp=15)
  129. plot(sMAs? slow_ma_series: na, color=gray,title="Slow MA", join=true,style=circles,linewidth=2,transp=15)
  130.  
  131. // Fractals
  132. filteredtopf = filterBW ? isIdealFractal(1) : isRegularFractal(1)
  133. filteredbotf = filterBW ? isIdealFractal(-1) : isRegularFractal(-1)
  134. filteredtopf := (uMAf and fast_ma_series<slow_ma_series)? false : filteredtopf
  135. filteredbotf := (uMAf and fast_ma_series>slow_ma_series)? false : filteredbotf
  136.  
  137. plotshape(ShowFractals? filteredtopf :na, title='Filtered Top Fractals', style=shape.triangleup, location=location.abovebar, color=green, offset=-3, transp=20)
  138. plotshape(ShowFractals? filteredbotf :na, title='Filtered Bottom Fractals', style=shape.triangledown, location=location.belowbar, color=red, offset=-3, transp=20)
  139. topfractals = 0.0
  140. botfractals = 0.0
  141. topfractals := filteredtopf ? high_[3] : topfractals[1]
  142. botfractals := filteredbotf ? low_[3] : botfractals[1]
  143.  
  144. topfcolor = topfractals != topfractals[1] ? na : green
  145. botfcolor = botfractals != botfractals[1] ? na : red
  146.  
  147.  
  148.  
  149. // ||-----------------------------------------------------------------------------------------------------||
  150. // ||--- Higher Highs, Lower Highs, Higher Lows, Lower Lows -------------------------------------------||
  151. higherhigh = filteredtopf == false ? false : ( valuewhen(filteredtopf == true, high_[3], 1) < valuewhen(filteredtopf == true, high_[3], 0) and
  152. (filterBW or valuewhen(filteredtopf == true, high_[3], 2) < valuewhen(filteredtopf == true, high_[3], 0)))
  153. lowerhigh = filteredtopf == false ? false : ( valuewhen(filteredtopf == true, high_[3], 1) > valuewhen(filteredtopf == true, high_[3], 0) and
  154. (filterBW or valuewhen(filteredtopf == true, high_[3], 2) > valuewhen(filteredtopf == true, high_[3], 0)))
  155. higherlow = filteredbotf == false ? false : ( valuewhen(filteredbotf == true, low_[3], 1) < valuewhen(filteredbotf == true, low_[3], 0) and
  156. (filterBW or valuewhen(filteredbotf == true, low_[3], 2) < valuewhen(filteredbotf == true, low_[3], 0)))
  157. lowerlow = filteredbotf == false ? false : ( valuewhen(filteredbotf == true, low_[3], 1) > valuewhen(filteredbotf == true, low_[3], 0) and
  158. (filterBW or valuewhen(filteredbotf == true, low_[3], 2) > valuewhen(filteredbotf == true, low_[3], 0)))
  159.  
  160. // If selected Display the HH/LL above/below candle.
  161. plotshape(ShowHHLL ? higherhigh : na, title='HH', style=shape.square, location=location.abovebar, color=black, text="HH", offset=-3,transp=0)
  162. plotshape(ShowHHLL ? lowerhigh : na, title='LH', style=shape.square, location=location.abovebar, color=black, text="LH", offset=-3,transp=0)
  163. plotshape(ShowHHLL ? higherlow : na, title='HL', style=shape.square, location=location.belowbar, color=black, text="HL", offset=-3,transp=0)
  164. plotshape(ShowHHLL ? lowerlow : na, title='LL', style=shape.square, location=location.belowbar, color=black, text="LL", offset=-3,transp=0)
  165.  
  166. topf = filteredtopf
  167. botf = filteredbotf
  168.  
  169.  
  170. //Count How many candles for current Pivot Level, If new reset.
  171. topcnt = 0
  172. botcnt = 0
  173. topcnt := topf ? 0 : nz(topcnt[1])+1
  174. botcnt := botf ? 0 : nz(botcnt[1])+1
  175.  
  176. topfs = 0.0
  177. botfs = 0.0
  178. topfs := topf ? high_[3] : topfs[1]
  179. botfs := botf ? low_[3] : botfs[1]
  180.  
  181. topfc = (topfs != topfs[1]) ? na : green
  182. botfc = (botfs != botfs[1]) ? na : red
  183. // Build The Fractal Level lines, fill in the gaps.
  184. plot(ShowFractalLevels and (topcnt<=3)? topfs : na, color=topfc, transp=0, linewidth=1, offset=-2, title="Top Levels -2")
  185. plot(ShowFractalLevels and (botcnt<=3)? botfs : na, color=botfc, transp=0, linewidth=1, offset=-2, title="Bottom Levels -2")
  186. plot(ShowFractalLevels and (topcnt<=5)? topfs : na, color=topfc, transp=0, linewidth=1, offset=-1, title="Top Levels -1")
  187. plot(ShowFractalLevels and (botcnt<=5)? botfs : na, color=botfc, transp=0, linewidth=1, offset=-1, title="Bottom Levels -1")
  188. plot(ShowFractalLevels and (topcnt<=7)? topfs : na, color=topfc, transp=0, linewidth=1, offset=0, title="Top Levels 0")
  189. plot(ShowFractalLevels and (botcnt<=7)? botfs : na, color=botfc, transp=0, linewidth=1, offset=0, title="Bottom Levels 0")
  190. plot(ShowFractalLevels and (maxLvlLen==0 or topcnt<maxLvlLen)? topfs : na, color=topfc, transp=0, linewidth=1, offset=1, title="Top Levels 1")
  191. plot(ShowFractalLevels and (maxLvlLen==0 or botcnt<maxLvlLen)? botfs : na, color=botfc, transp=0, linewidth=1, offset=1, title="Bottom Levels 1")
  192.  
  193. //
  194. //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement