Advertisement
JustUncleL

Fractals and Pivots R2-22

Feb 16th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.74 KB | None | 0 0
  1. //@version=2
  2.  
  3. study(title="Fractals and Pivots R2-22 by JustUncleL", shorttitle="FRACTAL R2-22", overlay=true)
  4.  
  5. // By: JustUncleL
  6. // Date: 13-Feb-2017
  7. // Version: R2-22
  8. //
  9. // Description:
  10. // This indicator display fractals, fractal levels, Pivot Points and HH/LL points.
  11. // Fractals are only drawn after they have completed.
  12. //
  13. // references:
  14. // - [RS]Fractal Levels by RicardoSantos
  15. //
  16. // Modifications:
  17. // R1 - original.
  18. //
  19. //
  20. //
  21. ShowPivots = input(true)
  22. ShowPivotLabels = input(false)
  23. ShowPivotNumbers = input(false)
  24. ShowFractals = input(true)
  25. ShowFractalLevels= input(false)
  26. ShowHHLL_ = input(false)
  27. ShowHHLL = ShowPivots?false:ShowHHLL_
  28. //filterBW_ = input(false,"Show Ideal Fractals Only")
  29. StarShape = input(4,minval=1,maxval=4,title="Pivot Marker: 1=Star, 2=Dot, 3=Diamond, 4=Cross")
  30.  
  31. // Slow MA - type, source, length
  32. uMAf = input(false,title="Use MA Filter on Fractal Levels")
  33. uHHLLf = input(false,title="Use HH/LL Filter on Fractal Levels")
  34. // Use Alternate Anchor TF for MAs
  35. anchor = input(0,minval=0,maxval=240,title="Use Alternate Anchor TimeFrame (0=none)")
  36. // Fast MA - type, source, length
  37. 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)
  38. fast_ma_len_ = input(defval=8, title="Fast MA - Length", minval=1)
  39. fast_ma_src = input(close, title="Fast MA - Source")
  40. // Slow MA - type, source, length
  41. 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)
  42. slow_ma_len_ = input(defval=36, title="Slow MA - Length", minval=1)
  43. slow_ma_src = input(close, title="Slow MA - Source")
  44.  
  45. //
  46. close_ = security(ticker, period, close)
  47. open_ = security(ticker, period, open)
  48. high_ = security(ticker, period, high)
  49. low_ = security(ticker, period, low)
  50.  
  51. // - /INPUTS
  52.  
  53. //
  54. // --- CONSTANTS ---
  55. DodgerBlue = #1E90FF
  56. filterBW = ShowPivots?true: false
  57. // --- /CONTANTS ---
  58.  
  59. // - FUNCTIONS
  60.  
  61. // ||--- Fractal Recognition Functions: ---------------------------------------------------------------||
  62. // ||-----------------------------------------------------------------------------------------------------||
  63. // ||--- Fractal Recognition Functions: ---------------------------------------------------------------||
  64. isIdealFractal(mode) =>
  65. ret = mode == 1 ? high_[5] < high_[4] and high_[4] < high_[3] and high_[3] > high_[2] and high_[2] > high_[1] :
  66. mode == -1 ? low_[5] > low_[4] and low_[4] > low_[3] and low_[3] < low_[2] and low_[2] < low_[1] : false
  67.  
  68. isRegularFractal(mode) =>
  69. ret = mode == 1 ? high_[5] < high_[3] and high_[4] < high_[3] and high_[3] > high_[2] and high_[3] > high_[1] :
  70. mode == -1 ? low_[5] > low_[3] and low_[4] > low_[3] and low_[3] < low_[2] and low_[3] < low_[1] : false
  71.  
  72. // ||-----------------------------------------------------------------------------------------------------||
  73.  
  74. // Returns MA input selection variant, default to SMA if blank or typo.
  75. variant(type, src, len) =>
  76. v1 = sma(src, len) // Simple
  77. v2 = ema(src, len) // Exponential
  78. v3 = wma(src, len) // Weighted
  79. v4 = vwma(src, len) // Volume Weighted
  80. v5 = na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len // Smoothed
  81. v6 = 2 * v2 - ema(v2, len) // Double Exponential
  82. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  83. v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
  84. v11 = sma(sma(src,len),len) // Triangular
  85. // SuperSmoother filter
  86. // © 2013 John F. Ehlers
  87. a1 = exp(-1.414*3.14159 / len)
  88. b1 = 2*a1*cos(1.414*3.14159 / len)
  89. c2 = b1
  90. c3 = (-a1)*a1
  91. c1 = 1 - c2 - c3
  92. v9 = c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  93. // Zero Lag Exponential
  94. ema1 = ema(src, len)
  95. ema2 = ema(ema1, len)
  96. v10 = ema1+(ema1-ema2)
  97. // return variant, defaults to SMA if input invalid.
  98. type=="EMA"?v2 : type=="WMA"?v3 : type=="VWMA"?v4 : type=="SMMA"?v5 : type=="DEMA"?v6 : type=="TEMA"?v7 : type=="HullMA"?v8 : type=="SSMA"?v9 : type=="ZEMA"?v10 : type=="TMA"? v11: v1
  99.  
  100. // - /FUNCTIONS
  101.  
  102. // - SERIES VARIABLES
  103.  
  104. // MA's
  105. // If this is 5min or less Time Frame select EMAs
  106. mult = not isintraday or anchor==0 or interval<=0 or interval>=anchor? 1 : round(anchor/interval)>1? round(anchor/interval) : 1
  107. fast_ma_len = mult==1 ? fast_ma_len_ : fast_ma_len_*mult
  108. slow_ma_len = mult==1 ? slow_ma_len_ : slow_ma_len_*mult
  109.  
  110. fast_ma_series = variant(fast_ma_type, fast_ma_src, fast_ma_len)
  111. slow_ma_series = variant(slow_ma_type, slow_ma_src, slow_ma_len)
  112.  
  113. // Fractals
  114. filteredtopf = filterBW ? isIdealFractal(1) : isRegularFractal(1)
  115. filteredbotf = filterBW ? isIdealFractal(-1) : isRegularFractal(-1)
  116.  
  117. //plotshape(ShowFractals? filteredtopf :na, title='Filtered Top Fractals', style=shape.triangleup, location=location.abovebar, color=green, offset=-3, transp=20)
  118. //plotshape(ShowFractals? filteredbotf :na, title='Filtered Bottom Fractals', style=shape.triangledown, location=location.belowbar, color=red, offset=-3, transp=20)
  119. topfractals = filteredtopf ? high_[3] : topfractals[1]
  120. botfractals = filteredbotf ? low_[3] : botfractals[1]
  121.  
  122. topfcolor = topfractals != topfractals[1] ? na : green
  123. botfcolor = botfractals != botfractals[1] ? na : red
  124.  
  125.  
  126.  
  127. // ||-----------------------------------------------------------------------------------------------------||
  128. // ||--- Higher Highs, Lower Highs, Higher Lows, Lower Lows -------------------------------------------||
  129. higherhigh = filteredtopf == false ? false : ( valuewhen(filteredtopf == true, high_[3], 1) < valuewhen(filteredtopf == true, high_[3], 0) and
  130. (ShowPivots or valuewhen(filteredtopf == true, high_[3], 2) < valuewhen(filteredtopf == true, high_[3], 0)))
  131. lowerhigh = filteredtopf == false ? false : ( valuewhen(filteredtopf == true, high_[3], 1) > valuewhen(filteredtopf == true, high_[3], 0) and
  132. (ShowPivots or valuewhen(filteredtopf == true, high_[3], 2) > valuewhen(filteredtopf == true, high_[3], 0)))
  133. higherlow = filteredbotf == false ? false : ( valuewhen(filteredbotf == true, low_[3], 1) < valuewhen(filteredbotf == true, low_[3], 0) and
  134. (ShowPivots or valuewhen(filteredbotf == true, low_[3], 2) < valuewhen(filteredbotf == true, low_[3], 0)))
  135. lowerlow = filteredbotf == false ? false : ( valuewhen(filteredbotf == true, low_[3], 1) > valuewhen(filteredbotf == true, low_[3], 0) and
  136. (ShowPivots or valuewhen(filteredbotf == true, low_[3], 2) > valuewhen(filteredbotf == true, low_[3], 0)))
  137.  
  138. // If selected Display the HH/LL above/below candle.
  139. plotshape(ShowHHLL ? higherhigh : na, title='HH', style=shape.square, location=location.abovebar, color=maroon, text="[HH]", offset=-3,transp=0)
  140. plotshape(ShowHHLL ? lowerhigh : na, title='LH', style=shape.square, location=location.abovebar, color=maroon, text="[LH]", offset=-3,transp=0)
  141. plotshape(ShowHHLL ? higherlow : na, title='HL', style=shape.square, location=location.belowbar, color=green, text="[HL]", offset=-3,transp=0)
  142. plotshape(ShowHHLL ? lowerlow : na, title='LL', style=shape.square, location=location.belowbar, color=green, text="[LL]", offset=-3,transp=0)
  143.  
  144. // If Selected Display Pivot points
  145. plotshape(ShowPivots and ShowPivotLabels? higherhigh : na, title='HH Label', style=shape.cross, location=location.abovebar, color=maroon, text="[HH]", offset=-3,transp=0)
  146. //plotshape(ShowPivots and not ShowPivotLabels? higherhigh? high :na : na, title='Higher High+', style=shape.cross, location=location.abovebar, color=maroon, offset=-3,transp=0)
  147. plotchar(ShowPivots and not ShowPivotLabels and StarShape==1? higherhigh?high:na: na, title='HH *', location=location.abovebar, color=maroon, offset=-3,transp=0)
  148. plotshape(ShowPivots and not ShowPivotLabels and StarShape==2? higherhigh?high:na: na, title='HH .', location=location.abovebar, color=maroon, style=shape.circle, offset=-3,transp=0)
  149. plotshape(ShowPivots and not ShowPivotLabels and StarShape==3? higherhigh?high:na: na, title='HH #', location=location.abovebar, color=maroon, style=shape.diamond, offset=-3,transp=0)
  150. plotshape(ShowPivots and not ShowPivotLabels and StarShape==4? higherhigh?high:na: na, title='HH +', location=location.abovebar, color=maroon, style=shape.cross, offset=-3,transp=0)
  151. //
  152. plotshape(ShowPivots and ShowPivotLabels? lowerhigh : na, title='LH Label', style=shape.cross, location=location.abovebar, color=maroon, text="[LH]", offset=-3,transp=0)
  153. //plotshape(ShowPivots and not ShowPivotLabels? lowerhigh? high : na : na, title='Lower High+', style=shape.cross, location=location.abovebar, color=maroon, offset=-3,transp=0)
  154. plotchar(ShowPivots and not ShowPivotLabels and StarShape==1? lowerhigh?high:na: na, title='LH *', location=location.abovebar, color=maroon, offset=-3,transp=0)
  155. plotshape(ShowPivots and not ShowPivotLabels and StarShape==2? lowerhigh?high:na: na, title='LH .', location=location.abovebar, color=maroon, style=shape.circle, offset=-3,transp=0)
  156. plotshape(ShowPivots and not ShowPivotLabels and StarShape==3? lowerhigh?high:na: na, title='LH #', location=location.abovebar, color=maroon, style=shape.diamond, offset=-3,transp=0)
  157. plotshape(ShowPivots and not ShowPivotLabels and StarShape==4? lowerhigh?high:na: na, title='LH +', location=location.abovebar, color=maroon, style=shape.cross, offset=-3,transp=0)
  158. //
  159. plotshape(ShowPivots and ShowPivotLabels? higherlow : na, title='HL Label', style=shape.cross, location=location.belowbar, color=green, text="[HL]", offset=-3,transp=0)
  160. //plotshape(ShowPivots and not ShowPivotLabels? higherlow? low: na : na, title='Higher Low+', style=shape.cross, location=location.belowbar, color=green, offset=-3,transp=0)
  161. plotchar(ShowPivots and not ShowPivotLabels and StarShape==1? higherlow?low:na: na, title='HL *', location=location.belowbar, color=green, offset=-3,transp=0)
  162. plotshape(ShowPivots and not ShowPivotLabels and StarShape==2? higherlow?low:na: na, title='HL .', location=location.belowbar, color=green, style=shape.circle, offset=-3,transp=0)
  163. plotshape(ShowPivots and not ShowPivotLabels and StarShape==3? higherlow?low:na: na, title='HL #', location=location.belowbar, color=green, style=shape.diamond, offset=-3,transp=0)
  164. plotshape(ShowPivots and not ShowPivotLabels and StarShape==4? higherlow?low:na: na, title='HL +', location=location.belowbar, color=green, style=shape.cross, offset=-3,transp=0)
  165. //
  166. plotshape(ShowPivots and ShowPivotLabels? lowerlow : na, title='LL Label', style=shape.cross, location=location.belowbar, color=green, text="[LL]", offset=-3,transp=0)
  167. //plotshape(ShowPivots and not ShowPivotLabels? lowerlow? low : na : na, title='Lower Low+', style=shape.cross, location=location.belowbar, color=green, offset=-3,transp=0)
  168. plotchar(ShowPivots and not ShowPivotLabels and StarShape==1? lowerlow?low:na: na, title='LL *', location=location.belowbar, color=green, offset=-3,transp=0)
  169. plotshape(ShowPivots and not ShowPivotLabels and StarShape==2? lowerlow?low:na: na, title='LL .', location=location.belowbar, color=green, style=shape.circle, offset=-3,transp=0)
  170. plotshape(ShowPivots and not ShowPivotLabels and StarShape==3? lowerlow?low:na: na, title='LL #', location=location.belowbar, color=green, style=shape.diamond, offset=-3,transp=0)
  171. plotshape(ShowPivots and not ShowPivotLabels and StarShape==4? lowerlow?low:na: na, title='LL +', location=location.belowbar, color=green, style=shape.cross, offset=-3,transp=0)
  172.  
  173. // Plot the Stars only when spot vacant
  174.  
  175.  
  176. //
  177. // Number the Pivot candle fractal positions above or below candles
  178. plotchar(ShowPivots and ShowPivotNumbers? filteredtopf: na, title='High 1u', location=location.abovebar, color=maroon, char="1", offset=-5,transp=0)
  179. plotchar(ShowPivots and ShowPivotNumbers? filteredtopf: na, title='High 2u', location=location.abovebar, color=maroon, char="2", offset=-4,transp=0)
  180. plotchar(ShowPivots and ShowPivotNumbers ? filteredtopf: na, title='High 2d', location=location.abovebar, color=maroon, char="2", offset=-2,transp=0)
  181. plotchar(ShowPivots and ShowPivotNumbers ? filteredtopf: na, title='High 1d', location=location.abovebar, color=maroon, char="1", offset=-1,transp=0)
  182. //
  183. plotchar(ShowPivots and ShowPivotNumbers ? filteredbotf: na, title='Low 1d', location=location.belowbar, color=green, char="1", offset=-5,transp=0)
  184. plotchar(ShowPivots and ShowPivotNumbers ? filteredbotf: na, title='Low 2d', location=location.belowbar, color=green, char="2", offset=-4,transp=0)
  185. plotchar(ShowPivots and ShowPivotNumbers ? filteredbotf: na, title='Low 2u', location=location.belowbar, color=green, char="2", offset=-2,transp=0)
  186. plotchar(ShowPivots and ShowPivotNumbers ? filteredbotf: na, title='Low 1u', location=location.belowbar, color=green, char="1", offset=-1,transp=0)
  187.  
  188. // Get All fractals
  189. topf = isRegularFractal(1)
  190. botf = isRegularFractal(-1)
  191.  
  192. havespot = not ShowPivots or (ShowPivots and not(higherhigh or lowerhigh or higherlow or lowerlow))
  193. plotshape(havespot and ShowFractals? topf :na, title='Regular Top Fractals', style=shape.triangleup, location=location.abovebar, color=red, offset=-3,transp=0)
  194. plotshape(havespot and ShowFractals? botf :na, title='Regular Bottom Fractals', style=shape.triangledown, location=location.belowbar, color=green, offset=-3,transp=0)
  195.  
  196. // ||--- Higher Highs, Lower Lows on All Fractals -------------------------------------------||
  197. HH = topf == false ? false : ( valuewhen(topf == true, high_[3], 1) < valuewhen(topf == true, high_[3], 0) and
  198. (valuewhen(topf == true, high_[3], 2) < valuewhen(topf == true, high_[3], 0)))
  199. LL = botf == false ? false : ( valuewhen(botf == true, low_[3], 1) > valuewhen(botf == true, low_[3], 0) and
  200. (valuewhen(botf == true, low_[3], 2) > valuewhen(botf == true, low_[3], 0)))
  201.  
  202. topfs = topf and (not uHHLLf or HH)? high_[3] : topfs[1]
  203. botfs = botf and (not uHHLLf or LL)? low_[3] : botfs[1]
  204.  
  205. topfc = (topfs != topfs[1]) or (uMAf and fast_ma_series<slow_ma_series)? na : green
  206. botfc = (botfs != botfs[1]) or (uMAf and fast_ma_series>slow_ma_series)? na : red
  207.  
  208. plot(ShowFractalLevels ? topfs : na, color=topfc, transp=0, linewidth=2, offset=-3, title="Top Levels")
  209. plot(ShowFractalLevels ? botfs : na, color=botfc, transp=0, linewidth=2, offset=-3, title="Bottom Levels")
  210.  
  211. //
  212. //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement