Advertisement
xmd79

Dynamic Fibonacci Retracement

Jan 17th, 2023
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © nsadeghi
  3.  
  4. //@version=5
  5. indicator("Dynamic Fibonacci Retracement", shorttitle = 'DFR', overlay = true)
  6.  
  7. input_lookback = input.int(defval = 50, title = 'Lookback Range', minval = 5)
  8. input_bullColor = input.color(defval = color.new(#00e676, 40), title = 'Bull', inline = 'color')
  9. input_bearColor = input.color(defval = color.new(#ff5252, 40), title = 'Bear', inline = 'color')
  10. input_trendline = input.bool(defval = true, title = 'Use Trendline')
  11.  
  12. // Formulate Array Structure and Pivot Conditions
  13. levelsArr = array.from(0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0)
  14.  
  15. calcFib(bool bearish, float lo, float hi, float perc) =>
  16. float result = na
  17. if not bearish
  18. result := lo - ((lo - hi) * perc)
  19. else
  20. result := hi - ((hi - lo) * perc)
  21. result
  22.  
  23. pl = ta.pivotlow(input_lookback, input_lookback)
  24. ph = ta.pivothigh(input_lookback, input_lookback)
  25.  
  26. plValue = ta.valuewhen(pl, low[input_lookback], 0)
  27. phValue = ta.valuewhen(ph, high[input_lookback], 0)
  28.  
  29. plSince = ta.barssince(pl) + input_lookback
  30. phSince = ta.barssince(ph) + input_lookback
  31.  
  32. // Used to determine which pivot point occured last.
  33. calcSince(index1, index2) =>
  34. getSince = array.from(plSince, phSince)
  35. array.get(getSince, index1) > array.get(getSince, index2)
  36.  
  37. // Push source values into arrays to dynamically adjust levels if conditions are met.
  38. var getLows = array.new<float>()
  39. var getHighs = array.new<float>()
  40. array.push(getLows, low), array.push(getHighs, high)
  41.  
  42. // Clear out one of the arrays that we created above when it's specified pivot point is generated. Then push the past nth bars.
  43. if pl or ph
  44. array.clear(pl ? getLows : ph ? getHighs : na)
  45. for i = 0 to input_lookback
  46. array.push(pl ? getLows : ph ? getHighs : na, pl ? low[i] : high[i])
  47.  
  48. // If price is below or above previous pivot values, update isBearish.
  49. var bool isBearish = na
  50. if ph
  51. isBearish := true
  52. else if pl
  53. isBearish := false
  54.  
  55. if array.min(getLows) < plValue
  56. isBearish := true
  57.  
  58. if array.max(getHighs) > phValue
  59. isBearish := false
  60.  
  61.  
  62. // Delete Previous Objects every tick
  63. allLines = line.all
  64. allLabels = label.all
  65. if array.size(allLines) >= array.size(levelsArr)
  66. for i = 0 to array.size(allLines) - 1
  67. line.delete(array.get(allLines, i))
  68. for j = 0 to array.size(allLabels) - 1
  69. label.delete(array.get(allLabels, j))
  70.  
  71. // Generate Levels
  72. for i = 0 to array.size(levelsArr)
  73. currentColor = isBearish ? input_bearColor : input_bullColor
  74. switch
  75. i < 7 =>
  76. fibState = calcFib(isBearish, array.min(getLows), array.max(getHighs), array.get(levelsArr, i))
  77. line.new(isBearish ? bar_index - plSince : bar_index - phSince, fibState, bar_index, fibState, color = currentColor)
  78. label.new(bar_index + 4, fibState, str.tostring(array.get(levelsArr, i)), style = label.style_none, textcolor = currentColor)
  79. i == 7 =>
  80. trend_yLoc1 = isBearish ? array.max(getHighs) : array.min(getLows)
  81. trend_yLoc2 = isBearish ? array.min(getLows) : array.max(getHighs)
  82. // Draw Trendline
  83. if input_trendline
  84. line.new(isBearish ? bar_index - plSince : bar_index - phSince, trend_yLoc1, bar_index, trend_yLoc2, style = line.style_dashed, color = currentColor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement