Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © nsadeghi
- //@version=5
- indicator("Dynamic Fibonacci Retracement", shorttitle = 'DFR', overlay = true)
- input_lookback = input.int(defval = 50, title = 'Lookback Range', minval = 5)
- input_bullColor = input.color(defval = color.new(#00e676, 40), title = 'Bull', inline = 'color')
- input_bearColor = input.color(defval = color.new(#ff5252, 40), title = 'Bear', inline = 'color')
- input_trendline = input.bool(defval = true, title = 'Use Trendline')
- // Formulate Array Structure and Pivot Conditions
- levelsArr = array.from(0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0)
- calcFib(bool bearish, float lo, float hi, float perc) =>
- float result = na
- if not bearish
- result := lo - ((lo - hi) * perc)
- else
- result := hi - ((hi - lo) * perc)
- result
- pl = ta.pivotlow(input_lookback, input_lookback)
- ph = ta.pivothigh(input_lookback, input_lookback)
- plValue = ta.valuewhen(pl, low[input_lookback], 0)
- phValue = ta.valuewhen(ph, high[input_lookback], 0)
- plSince = ta.barssince(pl) + input_lookback
- phSince = ta.barssince(ph) + input_lookback
- // Used to determine which pivot point occured last.
- calcSince(index1, index2) =>
- getSince = array.from(plSince, phSince)
- array.get(getSince, index1) > array.get(getSince, index2)
- // Push source values into arrays to dynamically adjust levels if conditions are met.
- var getLows = array.new<float>()
- var getHighs = array.new<float>()
- array.push(getLows, low), array.push(getHighs, high)
- // Clear out one of the arrays that we created above when it's specified pivot point is generated. Then push the past nth bars.
- if pl or ph
- array.clear(pl ? getLows : ph ? getHighs : na)
- for i = 0 to input_lookback
- array.push(pl ? getLows : ph ? getHighs : na, pl ? low[i] : high[i])
- // If price is below or above previous pivot values, update isBearish.
- var bool isBearish = na
- if ph
- isBearish := true
- else if pl
- isBearish := false
- if array.min(getLows) < plValue
- isBearish := true
- if array.max(getHighs) > phValue
- isBearish := false
- // Delete Previous Objects every tick
- allLines = line.all
- allLabels = label.all
- if array.size(allLines) >= array.size(levelsArr)
- for i = 0 to array.size(allLines) - 1
- line.delete(array.get(allLines, i))
- for j = 0 to array.size(allLabels) - 1
- label.delete(array.get(allLabels, j))
- // Generate Levels
- for i = 0 to array.size(levelsArr)
- currentColor = isBearish ? input_bearColor : input_bullColor
- switch
- i < 7 =>
- fibState = calcFib(isBearish, array.min(getLows), array.max(getHighs), array.get(levelsArr, i))
- line.new(isBearish ? bar_index - plSince : bar_index - phSince, fibState, bar_index, fibState, color = currentColor)
- label.new(bar_index + 4, fibState, str.tostring(array.get(levelsArr, i)), style = label.style_none, textcolor = currentColor)
- i == 7 =>
- trend_yLoc1 = isBearish ? array.max(getHighs) : array.min(getLows)
- trend_yLoc2 = isBearish ? array.min(getLows) : array.max(getHighs)
- // Draw Trendline
- if input_trendline
- 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