Advertisement
JustUncleL

Fractals and Pivots R7

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