// This source code is subject to the terms of Gold State Of Mind LLC License 2.0 at www.goldstateofmindllc.com

//@version=4
study(“Elite Entry”, overlay= true, max_lines_count = 500, max_labels_count = 500)

// ══════════════════════════════════════════════════════════════════════════════════════════════════ //
//
// Fibonacci Retracement / Extention / Pivot Points and Zig Zag
// ══════════════════════════════════════════════════════════════════════════════════════════════════ //

// ———————————————————————————————- //
// -Inputs ————————————————————————————– //

tooltip_threshold = “Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot” +
“\n\nDepth affects the minimum number of bars that will be taken into account when building”

tooltip_pivot_point = “A pivot point is a technical analysis indicator used to determine the overall trend of the market over different time frames”

tooltip_zigzag = “The Zig Zag indicator is used to identify price trends then connected by straight lines that help the trader visual the price action” +
“This indicator works to eliminate confusion over small price changes or fluctuations and aims to project trend changes overtime”

group_fib_tool = “Fibonacci Tool Settings”

i_isFib = input(true , “Pick a Fibonacci Tool” , inline = “FIB” , group = group_fib_tool)
i_fibTool = input(“Pivot Points” , “             ”,
options = [“Pivot Points”, “Retracements”, “Retracements + Pivot Points”, “Extentions”, “Extentions + Pivot Points”]
, inline = “FIB” , group = group_fib_tool)

i_htf_mode = input(‘Auto’ , “Pivot Points TF”, options=[‘Auto’, ‘User Defined’] , inline = “HTF” , group = group_fib_tool, tooltip=tooltip_pivot_point)
//i_htf_user = input(‘W’ , “auto “, type=input.resolution , inline = “HTF” , group = group_fib_tool)
i_htf_user_ = input(“Weekly” , “          or User Defined”, options = [“15 Min”, “1 Hour”, “4 Hour”, “Daily”, “Weekly”, “Monthly”, “Quarterly”, “Yearly”], inline = “HTF” , group = group_fib_tool)
i_htf_user = i_htf_user_ == “15 Min” ? “15” : i_htf_user_ == “1 Hour” ? “60” : i_htf_user_ == “4 Hour” ? “240” : i_htf_user_ == “Daily” ? “D” : i_htf_user_ == “Weekly” ? “W” : i_htf_user_ == “Monthly” ? “M” : i_htf_user_ == “Quarterly” ? “3M” : “12M”

i_relevant = input(false , “Pivot Points : Only Relevant Levels | Historicals” , inline = “hPV” , group = group_fib_tool)
i_histPP = input(false , “” , inline = “hPV” , group = group_fib_tool)

i_dev_thresh = atr(10) / close * 100 * input(3, “Deviation”, type = input.float, minval = 0 , inline = “Pivots”, group = group_fib_tool, tooltip=tooltip_threshold)
i_depth = input(11 , “       Depth”, type = input.integer, minval = 1 , inline = “Pivots”, group = group_fib_tool)

i_prevPivot = input(false , “Historical Extention/Retracment Levels” , inline = “hPVT” , group = group_fib_tool)
i_histPivot = input(1 , “”, minval = 1 , inline = “hPVT” , group = group_fib_tool)

i_reverse = input(false , “Reverse Extention/Retracment Levels” , group = group_fib_tool)
i_levels = input(“Levels” , “Level Labels”, options = [“Levels”, “Prices”, “Levels + Prices”, “None”], inline = “fLines”, group = group_fib_tool)
i_extend = input(false , “Extend Lines” , inline = “fLines”, group = group_fib_tool)
i_extendL = input(“Dashed” , “”, options = [“Dashed”, “Dotted”, “Solid”] , inline = “fLines”, group = group_fib_tool)

i_zigZag = input(false , “Zig Zag  ” , inline = “ZZ” , group = group_fib_tool, tooltip = tooltip_zigzag)
i_zzColor = input(#c77a08 , “” , inline = “ZZ” , group = group_fib_tool)
i_zzStyle = input(“Solid” , “”, options = [“Dashed”, “Dotted”, “Solid”] , inline = “ZZ” , group = group_fib_tool)
i_zzWidth = input(1 , “”, minval = 1 , inline = “ZZ” , group = group_fib_tool)

i_fibTime = input(true, “Fib Time Zones” , inline = “TZ” , group = group_fib_tool)
i_tzColor = input(#404f77 , “” , inline = “TZ” , group = group_fib_tool)
i_prevPivot2 = input(false , “Historical TZs” , inline = “TZ” , group = group_fib_tool)
i_histPivot2 = input(1 , “”, minval = 1 , inline = “TZ” , group = group_fib_tool)

// ———————————————————————————————- //
// -Calculations ——————————————————————————– //

var line lineLast = na
var int iLast = 0
var int iPrev = 0
var float pLast = 0
var isHighLast = false // otherwise the last pivot is a low pivot

var iPrevPivot = 0
var pPrevPivot = 0.
var iLastPivot = 0
var pLastPivot = 0.

pivots(src, length, isHigh) =>
l2 = length * 2
c = nz(src[length])
ok = true

for i = 0 to l2
if isHigh and src[i] > c
ok := false

if not isHigh and src[i] < c
ok := false
if ok
[bar_index[length], c]
else
[int(na), float(na)]

[iH, pH] = pivots(high, i_depth / 2, true )
[iL, pL] = pivots(low , i_depth / 2, false)

calc_dev(base_price, price) => 100 * (price – base_price) / price

pivotFound(dev, isHigh, index, price) =>
if isHighLast == isHigh and not na(lineLast)
// same direction
if isHighLast ? price > pLast : price < pLast
line.set_xy2(lineLast, index, price)
[lineLast, isHighLast]
else
[line(na), bool(na)]
else // reverse the direction (or create the very first line)
if abs(dev) > i_dev_thresh
// price move is significant

// —————————————————————————————- //
[zzCol, zzWid, zzSty] = if not i_zigZag
[na, 1, line.style_dashed]
else
[i_zzColor, i_zzWidth, i_zzStyle == “Solid” ? line.style_solid : i_zzStyle == “Dotted” ? line.style_dotted : line.style_dashed]
// —————————————————————————————- //

id = line.new(iLast, pLast, index, price, color=zzCol, width=zzWid, style=zzSty)
[id, isHigh]
else
[line(na), bool(na)]

if not na(iH)
dev = calc_dev(pLast, pH)
[id, isHigh] = pivotFound(dev, true, iH, pH)

if not na(id)
if id != lineLast
// —————————————————————————————- //
iPrevPivot := line.get_x1(lineLast)
pPrevPivot := line.get_y1(lineLast)
iLastPivot := line.get_x2(lineLast)
pLastPivot := line.get_y2(lineLast)

if not i_zigZag
// —————————————————————————————- //

line.delete(lineLast)

lineLast := id
isHighLast := isHigh
iPrev := iLast
iLast := iH
pLast := pH
else
if not na(iL)
dev = calc_dev(pLast, pL)
[id, isHigh] = pivotFound(dev, false, iL, pL)

if not na(id)
if id != lineLast
// —————————————————————————————- //
iPrevPivot := line.get_x1(lineLast)
pPrevPivot := line.get_y1(lineLast)
iLastPivot := line.get_x2(lineLast)
pLastPivot := line.get_y2(lineLast)

if not i_zigZag
// —————————————————————————————- //

line.delete(lineLast)

lineLast := id
isHighLast := isHigh
iPrev := iLast
iLast := iL
pLast := pL

iStartBase = i_prevPivot ? valuewhen(change(iPrevPivot), iPrevPivot, i_histPivot ) : valuewhen(change(iPrevPivot), iPrevPivot, 0)
pStartBase = i_prevPivot ? valuewhen(change(pPrevPivot), pPrevPivot, i_histPivot ) : valuewhen(change(pPrevPivot), pPrevPivot, 0)
iEndBase = i_prevPivot ? valuewhen(change(iLastPivot), iLastPivot, i_histPivot – 1) : line.get_x2(lineLast)
pEndBase = i_prevPivot ? valuewhen(change(pLastPivot), pLastPivot, i_histPivot – 1) : line.get_y2(lineLast)
iMidPivot = i_prevPivot ? valuewhen(change(iPrevPivot), iPrevPivot, i_histPivot – 1) : line.get_x1(lineLast)
pMidPivot = i_prevPivot ? valuewhen(change(pPrevPivot), pPrevPivot, i_histPivot – 1) : line.get_y1(lineLast)

iEndBase2 = i_prevPivot2 ? valuewhen(change(iLastPivot), iLastPivot, i_histPivot2 – 1) : line.get_x2(lineLast)
pEndBase2 = i_prevPivot2 ? valuewhen(change(pLastPivot), pLastPivot, i_histPivot2 – 1) : line.get_y2(lineLast)
iMidPivot2 = i_prevPivot2 ? valuewhen(change(iPrevPivot), iPrevPivot, i_histPivot2 – 1) : line.get_x1(lineLast)
pMidPivot2 = i_prevPivot2 ? valuewhen(change(pPrevPivot), pPrevPivot, i_histPivot2 – 1) : line.get_y1(lineLast)

//——————————————————————————
// auto higher time frame code snippet from pine wizard LonesomeTheBlue

htf_auto = timeframe.period == ‘1’ ? ‘240’ :
timeframe.period == ‘3’ ? ‘240’ :
timeframe.period == ‘5’ ? ‘240’ :
timeframe.period == ’15’ ? ‘D’ :
timeframe.period == ’30’ ? ‘D’ :
timeframe.period == ’45’ ? ‘D’ :
timeframe.period == ’60’ ? ‘W’ :
timeframe.period == ‘120’ ? ‘W’ :
timeframe.period == ‘180’ ? ‘W’ :
timeframe.period == ‘240’ ? ‘W’ :
timeframe.period == ‘D’ ? ‘M’ :
timeframe.period == ‘W’ ? ‘3M’ :
’12M’
htf = i_htf_mode == ‘Auto’ ? htf_auto : i_htf_user

//——————————————————————————
// security function free price calculations

f_htf_ohlc(_htf) =>
var htf_o = 0. // higher time frame open
var htf_h = 0. // higher time frame high
var htf_l = 0. // higher time frame low
htf_c = close // higher time frame close
var htf_ox = 0. // previous higher time frame open
var htf_hx = 0. // previous higher time frame high
var htf_lx = 0. // previous higher time frame low
var htf_cx = 0. // previous higher time frame close

if change(time(_htf))
htf_ox := htf_o
htf_o := open
htf_hx := htf_h
htf_h := high
htf_lx := htf_l
htf_l := low
htf_cx := htf_c[1]
else
htf_h := high > htf_h ? high : htf_h
htf_l := low < htf_l ? low : htf_l

[htf_ox, htf_hx, htf_lx, htf_cx, htf_o, htf_h, htf_l, htf_c]

[_, htf_h1, htf_l1, htf_c1, _, _, _, _] = f_htf_ohlc(htf)

// ———————————————————————————————- //
// -Plotting ———————————————————————————— //

time_x10 = valuewhen(change(time(htf)), time, 1)
time_x11 = valuewhen(change(time(htf)), time, 0)
time_x21 = 2 * time_x11 – time_x10

f_drawLineX(_x1, _y1, _x2, _y2, _xloc, _extend, _color, _style, _width) =>
var id = line.new(_x1, _y1, _x2, _y2, _xloc, _extend, _color, _style, _width)
line.set_xy1(id, _x1, _y1)
line.set_xy2(id, _x2, _y2)
line.set_color(id, _color)

f_drawLabelX(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip) =>
var id = label.new(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip)
label.set_xy(id, _x, _y)
label.set_text(id, i_extend ? _text + ‘\n\n’ : _text)
label.set_tooltip(id, tostring(round(_y / syminfo.mintick) * syminfo.mintick))
label.set_textcolor(id, _textcolor)

f_crossingLevel(_curret, _level) =>
(_level > _curret and _level < _curret[1]) or (_level < _curret and _level > _curret[1])

var ln = array.new_line()
var lb = array.new_label()

if change(time) and array.size(ln) > 0
for i = 1 to array.size(ln)
line.delete(array.shift(ln))

if change(time) and array.size(lb) > 0
for i = 1 to array.size(lb)
label.delete(array.shift(lb))

f_drawLine(_x1, _y1, _x2, _y2, _xloc, _extend, _c, _s, _w) =>
if _x1 – bar_index < 500
array.push(ln, line.new(_x1, _y1, _x2, _y2, _xloc, _extend, _c, _s, _w))

f_drawLine2(_x1, _y1, _x2, _y2, _xloc, _extend, _c, _s, _w) =>
if _y1 > 0
array.push(ln, line.new(_x1, _y1, _x2, _y2, _xloc, _extend, _c, _s, _w))

f_drawLabel2(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip) =>
if _y > 0
array.push(lb, label.new(_x, _y, i_extend ? _text + ‘\n\n’ : _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip))

if i_fibTime
referance = round(iMidPivot2 – iEndBase2)
f_drawLine(iMidPivot2 + referance , pEndBase2, iMidPivot2 + referance , pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 , pEndBase2, iMidPivot2 , pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_dashed, 1)
f_drawLine(iMidPivot2 – referance , pEndBase2, iMidPivot2 – referance , pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_dashed, 1)
f_drawLine(iMidPivot2 – referance * 2, pEndBase2, iMidPivot2 – referance * 2, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 – referance * 3, pEndBase2, iMidPivot2 – referance * 3, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 – referance * 5, pEndBase2, iMidPivot2 – referance * 5, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 – referance * 8, pEndBase2, iMidPivot2 – referance * 8, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 – referance * 13, pEndBase2, iMidPivot2 – referance * 13, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 – referance * 21, pEndBase2, iMidPivot2 – referance * 21, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 – referance * 34, pEndBase2, iMidPivot2 – referance * 34, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 – referance * 55, pEndBase2, iMidPivot2 – referance * 55, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)
f_drawLine(iMidPivot2 – referance * 89, pEndBase2, iMidPivot2 – referance * 89, pMidPivot2, xloc.bar_index, extend.both, i_tzColor, line.style_solid , 1)

f_processLevelX(_show, _level, _color) =>
if i_isFib
pPivotDiff = abs(pMidPivot – pEndBase)
price = 0.

if i_fibTool == “Extentions” or i_fibTool == “Extentions + Pivot Points”
f_drawLineX(iStartBase, pStartBase, iMidPivot, pMidPivot, xloc.bar_index, extend.none, color.gray, line.style_dashed, 1)
f_drawLineX(iMidPivot , pMidPivot , iEndBase , pEndBase , xloc.bar_index, extend.none, color.gray, line.style_dashed, 1)

offset = abs(pMidPivot – pStartBase)
price := pEndBase < pMidPivot ? pMidPivot – pPivotDiff + (i_reverse ? -1 : 1) * offset * _level : pMidPivot + pPivotDiff – (i_reverse ? -1 : 1) * offset * _level

if _show
f_drawLineX(iMidPivot, price, bar_index, price, xloc.bar_index, i_extend ? extend.both : extend.right, _color, line.style_solid, 1)

if i_levels != “None”
f_drawLabelX(iMidPivot, price, (i_levels == “Prices” ? “” : “EXT ” + tostring(_level)) + (i_levels == “Levels + Prices” or i_levels == “Prices” ? ” (” + tostring(price, format.mintick) + “)”: “”), xloc.bar_index, yloc.price, #00000000, label.style_label_right, _color, size.small, text.align_left, tostring(price))

if f_crossingLevel(close, price)
alert(“AutoFib Extention : ” + syminfo.ticker + ” crossing level ” + tostring(_level))

if i_fibTool == “Retracements” or i_fibTool == “Retracements + Pivot Points”
f_drawLineX(iMidPivot , pMidPivot , iEndBase , pEndBase , xloc.bar_index, extend.none, color.gray, line.style_dashed, 1)

price := pEndBase < pMidPivot ? (i_reverse ? pEndBase : pMidPivot) – (i_reverse ? -1 : 1) * pPivotDiff * _level : (i_reverse ? pEndBase : pMidPivot) + (i_reverse ? -1 : 1) * pPivotDiff * _level

if _show
f_drawLineX(iMidPivot, price, bar_index, price, xloc.bar_index, i_extend ? extend.both : extend.right, _color, line.style_solid, 1)

if i_levels != “None”
f_drawLabelX(iMidPivot, price, (i_levels == “Prices” ? “” : “RET ” + tostring(_level)) + (i_levels == “Levels + Prices” or i_levels == “Prices” ? ” (” + tostring(price, format.mintick) + “)”: “”), xloc.bar_index, yloc.price, #00000000, label.style_label_right, _color, size.small, text.align_left, tostring(price))

if f_crossingLevel(close, price)
alert(“AutoFib Retracment : ” + syminfo.ticker + ” crossing level ” + tostring(_level))

if i_fibTool == “Pivot Points” or i_fibTool == “Extentions + Pivot Points” or i_fibTool == “Retracements + Pivot Points”
pivot = avg(htf_h1, htf_l1, htf_c1)
range = htf_h1 – htf_l1

if not i_histPP
f_drawLine2(time_x10, pivot, time_x21, pivot, xloc.bar_time, extend.none, color.gray, line.style_dashed, 1)

if _show and _level >= 0
pvtPoints = _level == 0 or _level == 0.382 or _level == 0.618 or _level == 1

f_drawLine2(time_x11, pivot – range * _level, time_x21, pivot – range * _level, xloc.bar_time, extend.none, i_relevant ? close < pivot ? _color : na : _color, pvtPoints ? line.style_solid : line.style_dashed, pvtPoints ? 2 : 1)
f_drawLine2(time_x11, pivot + range * _level, time_x21, pivot + range * _level, xloc.bar_time, extend.none, i_relevant ? close > pivot ? _color : na : _color, pvtPoints ? line.style_solid : line.style_dashed, pvtPoints ? 2 : 1)

if i_extend
style = i_extendL == “Solid” ? line.style_solid : i_extendL == “Dotted” ? line.style_dotted : line.style_dashed
f_drawLine2(time_x11, pivot – range * _level, time_x21, pivot – range * _level, xloc.bar_time, extend.both, i_relevant ? close < pivot ? _color : na : _color, style, 1)
f_drawLine2(time_x11, pivot + range * _level, time_x21, pivot + range * _level, xloc.bar_time, extend.both, i_relevant ? close > pivot ? _color : na : _color, style, 1)

if i_levels != “None”
if pvtPoints
if _level == 0
f_drawLabel2(time_x21, pivot, (i_levels == “Prices” ? “” : “PP”) + (i_levels == “Levels + Prices” or i_levels == “Prices” ? ” (” + tostring(pivot, format.mintick) + “)”: “”), xloc.bar_time, yloc.price, #00000000, label.style_label_left, _color, size.small, text.align_left, tostring(pivot, format.mintick))
else
f_drawLabel2(time_x21, pivot – range * _level, (i_levels == “Prices” ? “” : (_level == 0.382 ? “S1 PP ” : _level == 0.618 ? “S2 PP ” : “S3 PP “) + tostring(_level)) + (i_levels == “Levels + Prices” or i_levels == “Prices” ? ” (” + tostring(pivot – range * _level, format.mintick) + “)”: “”), xloc.bar_time, yloc.price, #00000000, label.style_label_left, i_relevant ? close < pivot ? _color : na : _color, size.small, text.align_left, tostring(pivot – range * _level, format.mintick))
f_drawLabel2(time_x21, pivot + range * _level, (i_levels == “Prices” ? “” : (_level == 0.382 ? “R1 PP ” : _level == 0.618 ? “R2 PP ” : “R3 PP “) + tostring(_level)) + (i_levels == “Levels + Prices” or i_levels == “Prices” ? ” (” + tostring(pivot + range * _level, format.mintick) + “)”: “”), xloc.bar_time, yloc.price, #00000000, label.style_label_left, i_relevant ? close > pivot ? _color : na : _color, size.small, text.align_left, tostring(pivot + range * _level, format.mintick))
else
f_drawLabel2(time_x21, pivot – range * _level, (i_levels == “Prices” ? “” : “   PP ” + tostring(_level)) + (i_levels == “Levels + Prices” or i_levels == “Prices” ? ” (” + tostring(pivot – range * _level, format.mintick) + “)”: “”), xloc.bar_time, yloc.price, #00000000, label.style_label_left, i_relevant ? close < pivot ? _color : na : _color, size.small, text.align_left, tostring(pivot – range * _level, format.mintick))
f_drawLabel2(time_x21, pivot + range * _level, (i_levels == “Prices” ? “” : “   PP ” + tostring(_level)) + (i_levels == “Levels + Prices” or i_levels == “Prices” ? ” (” + tostring(pivot + range * _level, format.mintick) + “)”: “”), xloc.bar_time, yloc.price, #00000000, label.style_label_left, i_relevant ? close > pivot ? _color : na : _color, size.small, text.align_left, tostring(pivot + range * _level, format.mintick))

if f_crossingLevel(close, pivot – range * _level)
alert(“AutoFib PivotPoints : ” + syminfo.ticker + ” crossing support level ” + tostring(_level))

if f_crossingLevel(close, pivot + range * _level)
alert(“AutoFib PivotPoints : ” + syminfo.ticker + ” crossing resistance level ” + tostring(_level))

group_fib_levels = “Fibonacci Levels”

show_0 = input(true , “”, inline = “Level0” , group = group_fib_levels)
value_0 = input(0. , “”, inline = “Level0” , group = group_fib_levels)
color_0 = input(#787b86, “”, inline = “Level0” , group = group_fib_levels)
f_processLevelX(show_0, value_0, color_0)

show_0_236 = input(true , “”, inline = “Level0” , group = group_fib_levels)
value_0_236 = input(0.236 , “”, inline = “Level0” , group = group_fib_levels)
color_0_236 = input(#f44336, “”, inline = “Level0” , group = group_fib_levels)
f_processLevelX(show_0_236, value_0_236, color_0_236)

show_0_382 = input(true , “”, inline = “Level1” , group = group_fib_levels)
value_0_382 = input(0.382 , “”, inline = “Level1” , group = group_fib_levels)
color_0_382 = input(#81c784, “”, inline = “Level1” , group = group_fib_levels)
f_processLevelX(show_0_382, value_0_382, color_0_382)

show_0_5 = input(true , “”, inline = “Level1” , group = group_fib_levels)
value_0_5 = input(0.5 , “”, inline = “Level1” , group = group_fib_levels)
color_0_5 = input(#4caf50, “”, inline = “Level1” , group = group_fib_levels)
f_processLevelX(show_0_5, value_0_5, color_0_5)

show_0_618 = input(true , “”, inline = “Level2” , group = group_fib_levels)
value_0_618 = input(0.618 , “”, inline = “Level2” , group = group_fib_levels)
color_0_618 = input(#009688, “”, inline = “Level2” , group = group_fib_levels)
f_processLevelX(show_0_618, value_0_618, color_0_618)

show_0_65 = input(false , “”, inline = “Level2” , group = group_fib_levels)
value_0_65 = input(0.65 , “”, inline = “Level2” , group = group_fib_levels)
color_0_65 = input(#009688, “”, inline = “Level2” , group = group_fib_levels)
f_processLevelX(show_0_65, value_0_65, color_0_65)

show_0_786 = input(true , “”, inline = “Level3” , group = group_fib_levels)
value_0_786 = input(0.786 , “”, inline = “Level3” , group = group_fib_levels)
color_0_786 = input(#64b5f6, “”, inline = “Level3” , group = group_fib_levels)
f_processLevelX(show_0_786, value_0_786, color_0_786)

show_1 = input(true , “”, inline = “Level3” , group = group_fib_levels)
value_1 = input(1. , “”, inline = “Level3” , group = group_fib_levels)
color_1 = input(#787b86, “”, inline = “Level3” , group = group_fib_levels)
f_processLevelX(show_1, value_1, color_1)

show_1_272 = input(false , “”, inline = “Level4” , group = group_fib_levels)
value_1_272 = input(1.272 , “”, inline = “Level4” , group = group_fib_levels)
color_1_272 = input(#81c784, “”, inline = “Level4” , group = group_fib_levels)
f_processLevelX(show_1_272, value_1_272, color_1_272)

show_1_414 = input(false , “”, inline = “Level4” , group = group_fib_levels)
value_1_414 = input(1.414 , “”, inline = “Level4” , group = group_fib_levels)
color_1_414 = input(#f44336, “”, inline = “Level4” , group = group_fib_levels)
f_processLevelX(show_1_414, value_1_414, color_1_414)

show_1_618 = input(true , “”, inline = “Level5” , group = group_fib_levels)
value_1_618 = input(1.618 , “”, inline = “Level5” , group = group_fib_levels)
color_1_618 = input(#2196f3, “”, inline = “Level5” , group = group_fib_levels)
f_processLevelX(show_1_618, value_1_618, color_1_618)

show_1_65 = input(false , “”, inline = “Level5” , group = group_fib_levels)
value_1_65 = input(1.65 , “”, inline = “Level5” , group = group_fib_levels)
color_1_65 = input(#2196f3, “”, inline = “Level5” , group = group_fib_levels)
f_processLevelX(show_1_65, value_1_65, color_1_65)

show_2_618 = input(false , “”, inline = “Level6” , group = group_fib_levels)
value_2_618 = input(2.618 , “”, inline = “Level6” , group = group_fib_levels)
color_2_618 = input(#f44336, “”, inline = “Level6” , group = group_fib_levels)
f_processLevelX(show_2_618, value_2_618, color_2_618)

show_2_65 = input(false , “”, inline = “Level6” , group = group_fib_levels)
value_2_65 = input(2.65 , “”, inline = “Level6” , group = group_fib_levels)
color_2_65 = input(#f44336, “”, inline = “Level6” , group = group_fib_levels)
f_processLevelX(show_2_65, value_2_65, color_2_65)

show_3_618 = input(false , “”, inline = “Level7” , group = group_fib_levels)
value_3_618 = input(3.618 , “”, inline = “Level7” , group = group_fib_levels)
color_3_618 = input(#9c27b0, “”, inline = “Level7” , group = group_fib_levels)
f_processLevelX(show_3_618, value_3_618, color_3_618)

show_3_65 = input(false , “”, inline = “Level7” , group = group_fib_levels)
value_3_65 = input(3.65 , “”, inline = “Level7” , group = group_fib_levels)
color_3_65 = input(#9c27b0, “”, inline = “Level7” , group = group_fib_levels)
f_processLevelX(show_3_65, value_3_65, color_3_65)

show_4_236 = input(false , “”, inline = “Level8” , group = group_fib_levels)
value_4_236 = input(4.236 , “”, inline = “Level8” , group = group_fib_levels)
color_4_236 = input(#e91e63, “”, inline = “Level8” , group = group_fib_levels)
f_processLevelX(show_4_236, value_4_236, color_4_236)

show_4_618 = input(false , “”, inline = “Level8” , group = group_fib_levels)
value_4_618 = input(4.618 , “”, inline = “Level8” , group = group_fib_levels)
color_4_618 = input(#81c784, “”, inline = “Level8” , group = group_fib_levels)
f_processLevelX(show_4_618, value_4_618, color_4_618)

show_neg_0_236 = input(false , “”, inline = “Level9” , group = group_fib_levels)
value_neg_0_236 = input(-0.236 , “”, inline = “Level9” , group = group_fib_levels)
color_neg_0_236 = input(#f44336, “”, inline = “Level9” , group = group_fib_levels)
f_processLevelX(show_neg_0_236, value_neg_0_236, color_neg_0_236)

show_neg_0_382 = input(false , “”, inline = “Level9” , group = group_fib_levels)
value_neg_0_382 = input(-0.382 , “”, inline = “Level9” , group = group_fib_levels)
color_neg_0_382 = input(#81c784, “”, inline = “Level9” , group = group_fib_levels)
f_processLevelX(show_neg_0_382, value_neg_0_382, color_neg_0_382)

show_neg_0_618 = input(true , “”, inline = “Level10”, group = group_fib_levels)
value_neg_0_618 = input(-0.618 , “”, inline = “Level10”, group = group_fib_levels)
color_neg_0_618 = input(#009688, “”, inline = “Level10”, group = group_fib_levels)
f_processLevelX(show_neg_0_618, value_neg_0_618, color_neg_0_618)

show_neg_0_65 = input(false , “”, inline = “Level10”, group = group_fib_levels)
value_neg_0_65 = input(-0.65 , “”, inline = “Level10”, group = group_fib_levels)
color_neg_0_65 = input(#009688, “”, inline = “Level10”, group = group_fib_levels)
f_processLevelX(show_neg_0_65, value_neg_0_65, color_neg_0_65)

plot(i_histPP and show_0 ? avg(htf_h1, htf_l1, htf_c1) : na, “PP” , change(time(htf)) ? na : color_0 )
plot(i_histPP and show_0_236 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_0_236 : na, “PP 0_236”, change(time(htf)) ? na : color_0_236, 1, plot.style_cross)
plot(i_histPP and show_0_236 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_0_236 : na, “PP 0_236”, change(time(htf)) ? na : color_0_236, 1, plot.style_cross)
plot(i_histPP and show_0_382 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_0_382 : na, “PP 0_382”, change(time(htf)) ? na : color_0_382)
plot(i_histPP and show_0_382 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_0_382 : na, “PP 0_382”, change(time(htf)) ? na : color_0_382)
plot(i_histPP and show_0_5 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_0_5 : na, “PP 0_5 “, change(time(htf)) ? na : color_0_5 , 1, plot.style_cross)
plot(i_histPP and show_0_5 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_0_5 : na, “PP 0_5 “, change(time(htf)) ? na : color_0_5 , 1, plot.style_cross)
plot(i_histPP and show_0_618 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_0_618 : na, “PP 0_618”, change(time(htf)) ? na : color_0_618)
plot(i_histPP and show_0_618 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_0_618 : na, “PP 0_618”, change(time(htf)) ? na : color_0_618)
plot(i_histPP and show_0_65 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_0_65 : na, “PP 0_65 “, change(time(htf)) ? na : color_0_65 , 1, plot.style_cross)
plot(i_histPP and show_0_65 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_0_65 : na, “PP 0_65 “, change(time(htf)) ? na : color_0_65 , 1, plot.style_cross)
plot(i_histPP and show_0_786 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_0_786 : na, “PP 0_786”, change(time(htf)) ? na : color_0_786, 1, plot.style_cross)
plot(i_histPP and show_0_786 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_0_786 : na, “PP 0_786”, change(time(htf)) ? na : color_0_786, 1, plot.style_cross)
plot(i_histPP and show_1 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_1 : na, “PP 1 “, change(time(htf)) ? na : color_1 )
plot(i_histPP and show_1 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_1 : na, “PP 1 “, change(time(htf)) ? na : color_1 )
plot(i_histPP and show_1_272 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_1_272 : na, “PP 1_272”, change(time(htf)) ? na : color_1_272, 1, plot.style_cross)
plot(i_histPP and show_1_272 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_1_272 : na, “PP 1_272”, change(time(htf)) ? na : color_1_272, 1, plot.style_cross)
plot(i_histPP and show_1_414 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_1_414 : na, “PP 1_414”, change(time(htf)) ? na : color_1_414, 1, plot.style_cross)
plot(i_histPP and show_1_414 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_1_414 : na, “PP 1_414”, change(time(htf)) ? na : color_1_414, 1, plot.style_cross)
plot(i_histPP and show_1_618 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_1_618 : na, “PP 1_618”, change(time(htf)) ? na : color_1_618, 1, plot.style_cross)
plot(i_histPP and show_1_618 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_1_618 : na, “PP 1_618”, change(time(htf)) ? na : color_1_618, 1, plot.style_cross)
plot(i_histPP and show_1_65 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_1_65 : na, “PP 1_65 “, change(time(htf)) ? na : color_1_65 , 1, plot.style_cross)
plot(i_histPP and show_1_65 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_1_65 : na, “PP 1_65 “, change(time(htf)) ? na : color_1_65 , 1, plot.style_cross)
plot(i_histPP and show_2_618 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_2_618 : na, “PP 2_618”, change(time(htf)) ? na : color_2_618, 1, plot.style_cross)
plot(i_histPP and show_2_618 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_2_618 : na, “PP 2_618”, change(time(htf)) ? na : color_2_618, 1, plot.style_cross)
plot(i_histPP and show_2_65 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_2_65 : na, “PP 2_65 “, change(time(htf)) ? na : color_2_65 , 1, plot.style_cross)
plot(i_histPP and show_2_65 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_2_65 : na, “PP 2_65 “, change(time(htf)) ? na : color_2_65 , 1, plot.style_cross)
plot(i_histPP and show_3_618 ? avg(htf_h1, htf_l1, htf_c1) – (htf_h1 – htf_l1) * value_3_618 : na, “PP 3_618”, change(time(htf)) ? na : color_3_618, 1, plot.style_cross)
plot(i_histPP and show_3_618 ? avg(htf_h1, htf_l1, htf_c1) + (htf_h1 – htf_l1) * value_3_618 : na, “PP 3_618”, change(time(htf)) ? na : color_3_618, 1, plot.style_cross)

// Fibonacci Retracement / Extention / Pivot Points and Zig Zag
// ══════════════════════════════════════════════════════════════════════════════════════════════════ //

// ══════════════════════════════════════════════════════════════════════════════════════════════════ //
//
// Volume / Volatility AddOns
// ══════════════════════════════════════════════════════════════════════════════════════════════════ //

// ———————————————————————————————- //
// -Inputs ————————————————————————————– //

group_vol_vol = “Volume / Volatility AddOns”
tooltip_volume_spike_sign_of_exhaustion = “Moments where\n” +
“huge volume detected : current volume is grater than the product of the theshold value and volume moving average”

tooltip_high_volatility = “Moments where\n” +
“price range of the current bar is grater than the product of the theshold value and average true range value of defined period”

tooltip_volume_weighted_colored_bars = “Volume Weighted Colored Bars\nColors bars based on the bar’s volume relative to volume moving average”

// ———————————————————————————————- //
// High Volatility —————————————————————————— //

i_hATRLb = input(true , “⚡” , inline = “ATR” , group = group_vol_vol, tooltip = tooltip_high_volatility)
i_weightedATR = atr(input(13 , “ATR : Length” , inline = “ATR” , group = group_vol_vol)) *
input(2.718 , “Mult”, minval = .1, step = .1 , inline = “ATR” , group = group_vol_vol)

// ———————————————————————————————- //
// Volume Moving Average : Base —————————————————————– //

i_vSMA = sma(nz(volume), input(89, “Volume Moving Average Length”, group = group_vol_vol))

// ———————————————————————————————- //
// Volume Spike – Sign of Exhaustion ———————————————————— //

i_vSpikeLb = input(true , “🚦” , inline = “SRS1”, group = group_vol_vol, tooltip = tooltip_volume_spike_sign_of_exhaustion)
i_vSpikeThresh = input(4.669 , “Volume Spike Theshold           ”, minval = .1, step = .1 , inline = “SRS1”, group = group_vol_vol)

// ———————————————————————————————- //
// Volume Weighted Colored Bars —————————————————————– //

i_vwcb = input(true , “” , inline = “VWC”, group = group_vol_vol, tooltip = tooltip_volume_weighted_colored_bars)
i_vwcbHighThresh = input(1.618 , “VWCB :   High “, minval = 1., step = .1 , inline = “VWC”, group = group_vol_vol)
i_vwcbLowThresh = input(0.618 , “ Low” , minval = .1, step = .1 , inline = “VWC”, group = group_vol_vol)


// ———————————————————————————————- //
// -Calculations and Plotting ——————————————————————- //

nzVolume = nz(volume)
bullCandle = close > open
bearCandle = close < open
range = abs(high – low)

// ———————————————————————————————- //
// Volume Spike – Sign of Exhaustion ———————————————————— //

exhaustVol = nzVolume > i_vSpikeThresh * i_vSMA
plotchar(i_vSpikeLb and nzVolume ? exhaustVol : na, “Exhaustion Bar”, “🚦”, location.abovebar, size=size.tiny)
alertcondition(crossover(nzVolume, i_vSMA * i_vSpikeThresh), “Volume Spikes”, “sign of exhaustion, huge volume increase detected\n{{exchange}}:{{ticker}}->\nOpen = {{open}}, Current = {{close}},\nTime = {{time}}”)

// ———————————————————————————————- //
// High Volatility —————————————————————————— //

highVolatility = range > i_weightedATR
plotchar(i_hATRLb ? highVolatility : na, “High Volatile Bar”, “⚡”, location.belowbar, size=size.tiny)
alertcondition(crossover(range, i_weightedATR), “High Volatility”, “high volatility detected\n{{exchange}}:{{ticker}}->\nOpen = {{open}}, Current = {{close}},\nTime = {{time}}”)

// ———————————————————————————————- //
// Volume Weighted Colored Bars by Kıvanç Özbilgiç ———————————————- //

vwcbCol = nzVolume > i_vSMA * i_vwcbHighThresh ?
bullCandle ? #006400 : #910000 :
nzVolume < i_vSMA * i_vwcbLowThresh ?
bearCandle ? #FF9800 : #7FFFD4 :
na

barcolor(i_vwcb and nzVolume ? vwcbCol : na, title = “Volume Weighted Colored Bars”)

// Voloume / Volatility AddOns
// ══════════════════════════════════════════════════════════════════════════════════════════════════ //

var table logo = table.new(position.bottom_right, 1, 1)
table.cell(logo, 0, 0, “☼☾ “, text_size = size.normal, text_color = color.teal)


// linear regression
src = input(defval = close, title = “Source”,group= ‘linear regression’)
len = input(defval = 100, title = “Length”, minval = 10)
devlen = input(defval = 2., title = “Deviation”, minval = 0.1, step = 0.1)
extendit = input(defval = true, title = “Extend Lines”)
showfibo = input(defval = false, title = “Show Fibonacci Levels”)
showbroken = input(defval = true, title = “Show Broken Channel”)
upcol = input(defval = color.lime, title = “Up Color”)
dncol = input(defval = color.red, title = “Down Color”)
widt = input(defval = 2, title = “Line Width”)

var fibo_ratios = array.new_float(0)
var colors = array.new_color(2)
if barstate.isfirst
array.unshift(colors, upcol)
array.unshift(colors, dncol)
array.push(fibo_ratios, 0.236)
array.push(fibo_ratios, 0.382)
array.push(fibo_ratios, 0.618)
array.push(fibo_ratios, 0.786)

 

get_channel(src, len)=>
mid = sum(src, len) / len
slope = linreg(src, len, 0) – linreg(src, len, 1)
intercept = mid – slope * floor(len / 2) + ((1 – (len % 2)) / 2) * slope
endy = intercept + slope * (len – 1)
dev = 0.0
for x = 0 to len – 1
dev := dev + pow(src[x] – (slope * (len – x) + intercept), 2)
dev := sqrt(dev/len)
[intercept, endy, dev, slope]

[y1_, y2_, dev, slope] = get_channel(src, len)

outofchannel = (slope > 0 and close < y2_ – dev * devlen) ? 0 : (slope < 0 and close > y2_ + dev * devlen) ? 2 : -1

var reglines = array.new_line(3)
var fibolines = array.new_line(4)
for x = 0 to 2
if not showbroken or outofchannel != x or nz(outofchannel[1], -1) != -1
line.delete(array.get(reglines, x))
else
line.set_color(array.get(reglines, x), color = color.blue)
line.set_width(array.get(reglines, x), width = 2)
line.set_style(array.get(reglines, x), style = line.style_dotted)
line.set_extend(array.get(reglines, x), extend = extend.none)

array.set(reglines, x,
line.new(x1 = bar_index – (len – 1),
y1 = y1_ + dev * devlen * (x – 1),
x2 = bar_index,
y2 = y2_ + dev * devlen * (x – 1),
color = array.get(colors, round(max(sign(slope), 0))),
style = x % 2 == 1 ? line.style_solid : line.style_dashed,
width = widt,
extend = extendit ? extend.right : extend.none))
if showfibo
for x = 0 to 3
line.delete(array.get(fibolines, x))
array.set(fibolines, x,
line.new(x1 = bar_index – (len – 1),
y1 = y1_ – dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x),
x2 = bar_index,
y2 = y2_ – dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x),
color = array.get(colors, round(max(sign(slope), 0))),
style = line.style_dotted,
width = widt,
extend = extendit ? extend.right : extend.none))

var label sidelab = label.new(x = bar_index – (len – 1), y = y1_, text = “S”, size = size.large)
txt = slope > 0 ? slope > slope[1] ? “⇑” : “⇗” : slope < 0 ? slope < slope[1] ? “⇓” : “⇘” : “⇒”
stl = slope > 0 ? slope > slope[1] ? label.style_label_up : label.style_label_upper_right : slope < 0 ? slope < slope[1] ? label.style_label_down : label.style_label_lower_right : label.style_label_right
label.set_style(sidelab, stl)
label.set_text(sidelab, txt)
label.set_x(sidelab, bar_index – (len – 1))
label.set_y(sidelab, slope > 0 ? y1_ – dev * devlen : slope < 0 ? y1_ + dev * devlen : y1_)
label.set_color(sidelab, slope > 0 ? upcol : slope < 0 ? dncol : color.blue)

alertcondition(outofchannel , title=’Channel Broken’, message=’Channel Broken’)