Visible Fibonacci
Visible Fibonacci
0 at
https://mozilla.org/MPL/2.0/
// © TradingView
//@version=5
indicator("Visible Fibonacci", "Visible Fib", true, max_lines_count = 500)
// Visible Fibonacci
// v1, 2023.01.01
// This code was written using the recommendations from the Pine Script™ User
Manual's Style Guide:
// https://www.tradingview.com/pine-script-docs/en/v5/writing/Style_guide.html
// ————— Constants
// Colors
color GRAY = color.gray
color RED = color.red
color ORANGE = color.orange
color TEAL = color.teal
color RUBY = #e91e63
color AQUA = color.aqua
color BLUE = color.blue
color GREEN = color.green
color PURPLE = color.purple
color LIME = color.lime
color FUCHSIA = color.fuchsia
// Line Type
string TY01 = " ─ Line"
string TY02 = " ┄ Dashed line"
string TY03 = " ┉ Dotted line"
// Label placement
string LB01 = "Left"
string LB02 = "Center"
string LB03 = "Right"
string LB04 = "Top"
string LB05 = "Middle"
string LB06 = "Bottom"
// Label Size
string SZ01 = "tiny"
string SZ02 = "small"
string SZ03 = "normal"
string SZ04 = "large"
string SZ05 = "huge"
// Tool Tips
string TT_FT = "Choose from 2 calculation types:
\n\n• 'Visible Chart Range' \nCalculates the Fibonacci retracement based on the
high and low of the visible chart range.
\n\n• 'Visible Zig Zag' \nCalculates the Fibonacci retracement based on zig zag
pivots within the visible chart range."
string TT_ZZ = "Show Zig Zag lines when the 'Visible Zig Zag' display type is
enabled."
string TT_MP = "When enabled, adds a pivot in the opposite direction if a new pivot
is not found in the number of bars in the 'Pivot Length'."
string TT_PL = "Number of bars back to track pivots (ex. Highest point in 50
bars)."
string TT_ZA = "Your choices here dictate the Zig Zag line color, line width, and
line style."
string TT_LP = "Places the fib retracement on the nth pivot point back from the
last visible pivot."
// Inputs
string fibTypeInput = input.string(FT01, "Fib Calculation Type", options
= [FT01, FT02], tooltip = TT_FT)
// @function Determines a fib price based on the chart's high and low
price dependant on if the fib retracement is bullish or bearish. Converts to log
scale when dictated by user-selection.
// @param value (series float) Fib level in decimal form.
// @param hiPrice (series float) Retracement high.
// @param loPrice (series float) Retracement low.
// @param isBull (series bool) Condition determining if the retracement is
bullish.
// @returns (float) The fib price.
getPrice(series float value, series float hiPrice, series float loPrice, series
bool isBull) =>
float hiPoint = useLogScaleInput ? toLog(hiPrice) : hiPrice
float loPoint = useLogScaleInput ? toLog(loPrice) : loPrice
float fibPrice =
isBull and invertFibInput or not isBull and not invertFibInput ?
loPoint + ((hiPoint - loPoint) * value) :
hiPoint - ((hiPoint - loPoint) * value)
float result = useLogScaleInput ? fromLog(fibPrice) : fibPrice
// @function Determines a string for use within a fib label. The text
will contain the fib price/percent and/or the fib level dependant on user
selections for "Prices" and "Levels".
// @param value (series float) The fib level from one of the `level*Input`
user selections.
// @param price (series float) The corresponding price level for the fib
level.
// @returns (string) A string containing the fib `value` and/or the fib
`price`.
labelText(series float value, series float price) =>
string levelString = showPercent ? str.tostring(value * 100, format.percent) :
str.format("{0, number, #.###}", value)
string result = switch
showPricesInput and showLevelsInput => str.format("{0}({1})", levelString,
str.tostring(price, format.mintick))
showPricesInput => str.format("({0})",
str.tostring(price, format.mintick))
showLevelsInput => levelString
// @function Produces a label with the formatted time and price for the
point it is placed. Continues to update the location and text on each bar.
// @param x (series int) Bar UNIX time of the label position.
// @param y (series float) Price of the label position.
// @param color (simple color) Color for the text and label.
// @param style (series string) Label style. Accepts one of the
`label.style_*` built-in constants.
// @returns (void) Function has no return.
timeLabel(series int x, series float y, simple color color, series string style) =>
var label timeLabel = label.new(na, na, na, xloc.bar_time, yloc.price,
color.new(color, 80), style, color, tLabelSizeInput)
label.set_xy(timeLabel, x, y)
label.set_text(timeLabel, str.format("{0}\n{1,time, dd/MM/yy @ HH:mm:ss}",
str.tostring(y, format.mintick), x))
// @function Produces a line object that sets it's bar time and price on
each bar.
// @param x1 (series int) bar UNIX time of the first point of the line.
// @param y1 (series float) Price of the first point of the line.
// @param x2 (series int) bar UNIX time of the second point of the line.
// @param y2 (series float) Price of the second point of the line.
// @returns (void) Function has no return.
hiLoLine(series int x1, series float y1, series int x2, series float y2) =>
var line hiLoLine = line.new(na, na, na, na, xloc.bar_time, extend.none,
trendColorInput, lineStyle(trendStyleInput), trendWidthInput)
line.set_xy1(hiLoLine, x1, y1)
line.set_xy2(hiLoLine, x2, y2)
// @function Updates price and time of `fibLevel`s and label text.
// @param leftTime (series int) bar UNIX time of the first point of the fib
retracement.
// @param rightTime (series int) bar UNIX time of the second point of the fib
retracement.
// @param hiPrice (series float) Price of the high point of the fib
retracement.
// @param loPrice (series float) Price of the low point of the fib
retracement.
// @param isBull (series bool) Condition to determine if the retracement is
bullish.
// @returns (void) Function has no return.
setLevels(series int leftTime, series int rightTime, series float hiPrice, series
float loPrice, series bool isBull) =>
int labelTime = labelTime(leftTime, rightTime)
for fib in fibLevels
float fibPrice = getPrice(fib.level, hiPrice, loPrice, isBull)
line.set_xy1(fib.fibLine, leftTime, fibPrice)
line.set_xy2(fib.fibLine, rightTime, fibPrice)
if extendRtInput
line.set_xy1(fib.extLine, rightTime, fibPrice)
line.set_xy2(fib.extLine, chart.right_visible_bar_time, fibPrice)
if showLabels
label.set_xy(fib.fibLabel, labelTime, fibPrice)
label.set_text(fib.fibLabel, labelText(fib.level, fibPrice))
// @function Calculates Zig Zag based on pivots formed over the lookback
`length`.
// @param length (simple int) Number of bars to search for a "pivotPoint".
// @param addPivots (simple bool) Condition to search for additonal pivots,
Adds a pivot in the opposite direction if a pivot is not found in `length` bars.
Default is true.
// @returns (array<pivotPoint>) An array of "pivotPoint" objects.
zigZag(simple int length, simple bool addPivots = true) =>
var array<pivotPoint> pivots = array.new<pivotPoint>(1,
pivotPoint.new(point.new(close), point.new(close)))
updatePivot(length, pivots)
addPivot(length, pivots, addPivots)
pivots
// @function Renders a start "point" and end "point" for the `nthPivot`
back in the visible range when "Use zig zag" is enabled, and returns the 2
coordinates as a tuple.
// @param nthPivot (simple int) The nth pivot.
// @returns ([int, float, int, float]) The start point time, the start
point price, the end point time, the end point price.
findHiLo(simple int nthPivot) =>
int leftX = na
float leftY = na
int rightX = na
float rightY = na
if useZigZag
array<pivotPoint> pivotArray = zigZag(lengthInput, addPivotInput)
if barstate.islast
pivotPoint pivot = array.get(pivotArray, nthPivot)
leftX := pivot.start.x
leftY := pivot.start.y
rightX := pivot.end.x
rightY := pivot.end.y
if showZZInput
line.set_style(pivot.ln, lineStyle(trendStyleInput))
[leftX, leftY, rightX, rightY]
//#endregion
// Create fibLevel objects on first bar, populate levels array, create sortedArray
for object sorting.
if barstate.isfirst
populate(show000Input, level000Input, color000Input)
populate(show236Input, level236Input, color236Input)
populate(show382Input, level382Input, color382Input)
populate(show500Input, level500Input, color500Input)
populate(show618Input, level618Input, color618Input)
populate(show786Input, level786Input, color786Input)
populate(show100Input, level100Input, color100Input)
populate(show161Input, level161Input, color161Input)
populate(show261Input, level261Input, color261Input)
populate(show361Input, level361Input, color361Input)
populate(show423Input, level423Input, color423Input)
populate(show127Input, level127Input, color127Input)
populate(show141Input, level141Input, color141Input)
populate(show227Input, level227Input, color227Input)
populate(show241Input, level241Input, color241Input)
populate(show200Input, level200Input, color200Input)
populate(show300Input, level300Input, color300Input)
populate(show327Input, level327Input, color327Input)
populate(show341Input, level341Input, color341Input)
populate(show400Input, level400Input, color400Input)
populate(show427Input, level427Input, color427Input)
populate(show441Input, level441Input, color441Input)
populate(show461Input, level461Input, color461Input)
populate(show476Input, level476Input, color476Input)
sortedArray := array.sort_indices(levels)
if useFillInput
setBg()
// Set fibLevel properties on last bar. Draw time labels and hi/lo line.
if barstate.islast
setLevels(leftTime, rightTime, chartHigh, chartLow, isBull)
if tLabelsInput
timeLabel(hiTime, chartHigh, LIME, hiTimeStyle)
timeLabel(loTime, chartLow, FUCHSIA, loTimeStyle)
if trendLineInput and not (showZZInput and useZigZag)
hiLoLine(hiTime, chartHigh, loTime, chartLow)
//#endregion