// This source code is subject to the terms of the Mozilla Public License 2.
0 at
https://mozilla.org/MPL/2.0/
// © MartyVestor
//@version=4
study(title="Martyv Smart AutoFib Retracement Kit with Logarithmic Support",
shorttitle="Martyv AutoFib", overlay=true, max_bars_back = 300, max_lines_count =
300, max_labels_count=300)
//Color Inputs
var bullCol = color.new(#2962ff, 0)
var bearCol = color.new(#9c27b0, 0)
var neutralCol = color.new(#aa00ff, 50)
var whiteCol = color.new(#ffffff, 0)
var redCol = color.new(#e91e63, 0)
//Indicators
var pLabelOn = false
var fibOn = true
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 startPrice = close
var endPrice = close
var logScale = input(true, title="Use logarithmic Scale", group="Fibonacci
Retracement Settings")
contain = input(true, "Contain Price Action (Allow Pre-valid Retrace)",
group="Fibonacci Retracement Settings")
forceFib = input(false, "Manual Fibonacci Levels (Inputs Below)", group="Fibonacci
Retracement Settings")
reverse = input(false, "Reverse", group="Fibonacci Retracement Settings")
var trendOn = false
var extendLeft = input(false, "Extend Left | Extend Right", inline = "Extend
Lines", group="Fibonacci Retracement Settings")
var extendRight = input(false, "", inline = "Extend Lines", group="Fibonacci
Retracement Settings")
var extending = extend.none
if extendLeft and extendRight
extending := extend.both
if extendLeft and not extendRight
extending := extend.left
if not extendLeft and extendRight
extending := extend.right
prices = input(true, "Show Prices", group="Fibonacci Retracement Settings")
levels = input(true, "Show Levels", inline = "Levels", group="Fibonacci Retracement
Settings")
levelsFormat = input("Values", "", options = ["Values", "Percent"], inline =
"Levels", group="Fibonacci Retracement Settings")
labelsPosition = input("Right", "Labels Position", options = ["Left", "Right"],
group="Fibonacci Retracement Settings")
/////////Fibonacci Zig Zag
showzigzag = false
fibZZlookPeriod = input(defval = 21, title="Fibonacci Zig Zag Lookback", minval =
2, maxval = 55)
//var fibZZlookPeriod = 14
var fibo_ratios = array.new_float(0)
var shownlevels = 1
if barstate.isfirst //Do this once and never again
array.push(fibo_ratios, 0.000)
array.push(fibo_ratios, 0.236)
shownlevels := shownlevels + 1
array.push(fibo_ratios, 0.382)
shownlevels := shownlevels + 1
array.push(fibo_ratios, 0.500)
shownlevels := shownlevels + 1
array.push(fibo_ratios, 0.618)
shownlevels := shownlevels + 1
array.push(fibo_ratios, 0.786)
shownlevels := shownlevels + 1
for x = 1 to 2
array.push(fibo_ratios, x)
array.push(fibo_ratios, x + 0.272)
array.push(fibo_ratios, x + 0.414)
array.push(fibo_ratios, x + 0.618)
float ph = highestbars(high, fibZZlookPeriod) == 0 ? high : na
float pl = lowestbars(low, fibZZlookPeriod) == 0 ? low : na
var dir = 0
dir := iff(ph and na(pl), 1, iff(pl and na(ph), -1, dir))
var max_array_size = 10
var zigzag = array.new_float(0) //Initialize ZigZag
var fibDir = 1
add_to_zigzag(value, bindex)=>
array.unshift(zigzag, bindex)
array.unshift(zigzag, value)
if array.size(zigzag) > max_array_size
array.pop(zigzag)
array.pop(zigzag)
update_zigzag(value, bindex)=>
if array.size(zigzag) == 0
add_to_zigzag(value, bindex)
else
if (dir == 1 and value > array.get(zigzag, 0)) or (dir == -1 and value <
array.get(zigzag, 0))
array.set(zigzag, 0, value)
array.set(zigzag, 1, bindex)
0.
Round_it(value)=> round(value / syminfo.mintick) * syminfo.mintick
dirchanged = change(dir)
if ph or pl
if dirchanged
add_to_zigzag(dir == 1 ? ph : pl, bar_index)
else
update_zigzag(dir == 1 ? ph : pl, bar_index)
if fibOn and showzigzag and array.size(zigzag) >= 4
var line zzline = na
float val = array.get(zigzag, 0)
int point = round(array.get(zigzag, 1))
if change(val) or change(point)
float val1 = array.get(zigzag, 2)
int point1 = round(array.get(zigzag, 3))
if change(val1) == 0 and change(point1) == 0
line.delete(zzline)
// ORIGINAL: zzline := line.new(x1 = point, y1 = val, x2 = point1, y2 =
val1, color = dir == 1 ? bullCol : bearCol, width = 2)
zzline := line.new(x1 = point, y1 = val, x2 = point1, y2 = val1, color
= dir == 1 ? bullCol : bearCol, width = 2, style=line.style_dashed)
var fibolines = array.new_line(0)
if array.size(zigzag) >= 6 and barstate.islast
if array.size(fibolines) > 0
for x = 0 to array.size(fibolines) - 1
line.delete(array.get(fibolines, x))
diff = array.get(zigzag, 4) - array.get(zigzag, 2)
outOfBounds = contain ? ((close > array.get(zigzag, 4) and close >
array.get(zigzag, 2)) or (close < array.get(zigzag, 4) and close <
array.get(zigzag, 2))) or ((array.get(zigzag, 0) > array.get(zigzag, 4) and
array.get(zigzag, 0) > array.get(zigzag, 2)) or (array.get(zigzag, 0) <
array.get(zigzag, 4) and array.get(zigzag, 0) < array.get(zigzag, 2))) : false
startPrice := reverse ? array.get(zigzag, outOfBounds ? 2 : 4) :
array.get(zigzag, outOfBounds ? 0 : 2)
endPrice := reverse ? array.get(zigzag, outOfBounds ? 0 : 2) :
array.get(zigzag, outOfBounds ? 2 : 4)
fibDir := startPrice > endPrice ? 1 : -1
stopit = false
for x = 0 to array.size(fibo_ratios) - 1
if (stopit and x > shownlevels) or fibOn != true
break
array.unshift(fibolines,
line.new(x1 = round(array.get(zigzag, outOfBounds ? 3 : 5)),
y1 = array.get(zigzag, 2) + diff *
array.get(fibo_ratios, x),
x2 = bar_index,
y2 = array.get(zigzag, 2) + diff *
array.get(fibo_ratios, x),
//color = neutralCol,
color = color.new(neutralCol, 100),
extend = extend.right))
lineLast := array.get(fibolines, 0)
//iLast := round(array.get(zigzag, 4))
if (dir == 1 and array.get(zigzag, 2) + diff * array.get(fibo_ratios, x) >
array.get(zigzag, 0)) or
(dir == -1 and array.get(zigzag, 2) + diff * array.get(fibo_ratios, x) <
array.get(zigzag, 0))
stopit := true
/////////////////////////////////////////////////////
/////Trend Channel Zig Zag////////
//------------------------------------------------------------------------------
extend = false
show_ext = false
show_labels = false
length = 21
//Style
upcol = bullCol
midcol = neutralCol
dncol = bearCol
//------------------------------------------------------------------------------
os = 0
src = close
n = bar_index
var float valtop = na
var float valbtm = na
//------------------------------------------------------------------------------
upper = highest(src,length)
lower = lowest(src,length)
os := src[length] > upper ? 0 : src[length] < lower ? 1 : os[1]
btm = os == 1 and os[1] != 1
top = os == 0 and os[1] != 0
//------------------------------------------------------------------------------
btm_n = valuewhen(btm,n,0)
top_n = valuewhen(top,n,0)
len = abs(btm_n - top_n)
if btm
max_diff_up = 0.
max_diff_dn = 0.
valbtm := low[length]
for i = 0 to len-1
point = low[length] + i/(len-1)*(valtop - low[length])
max_diff_up := max(max(src[length+i],open[length+i]) - point,max_diff_up)
max_diff_dn := max(point - min(src[length+i],open[length+i]),max_diff_dn)
if trendOn
line.new(n[len+length],valtop,n[length],low[length],color=midcol)
if show_ext and trendOn
line.new(n[len+length],valtop+max_diff_up,n[length],low[length]+max_diff_up
,color=upcol,style=line.style_dotted)
line.new(n[len+length],valtop-max_diff_dn,n[length],low[length]-max_diff_dn
,color=dncol,style=line.style_dotted)
if show_labels and trendOn
label.new(n[length],low[length],tostring(low[length],'#.####'),color=#00000000
,style=label.style_label_up,textcolor=dncol,textalign=text.align_left,siz
e=size.small)
if top
max_diff_up = 0.
max_diff_dn = 0.
valtop := high[length]
for i = 0 to len-1
point = high[length] + i/(len-1)*(valbtm - high[length])
max_diff_up := max(max(src[length+i],open[length+i]) - point,max_diff_up)
max_diff_dn := max(point - min(src[length+i],open[length+i]),max_diff_dn)
if trendOn
line.new(n[len+length],valbtm,n[length],high[length],color=midcol)
if show_ext and trendOn
line.new(n[len+length],valbtm+max_diff_up,n[length],high[length]
+max_diff_up
,color=upcol,style=line.style_dotted)
line.new(n[len+length],valbtm-max_diff_dn,n[length],high[length]-
max_diff_dn
,color=dncol,style=line.style_dotted)
if show_labels and trendOn
label.new(n[length],high[length],tostring(high[length],'#.####'),color=#00000000
,style=label.style_label_down,textcolor=upcol,textalign=text.align_left,s
ize=size.small)
//------------------------------------------------------------------------------
plot(btm ? low[length] : top ? high[length] : na,'Circles'
,color = btm ? dncol : upcol
,style=plot.style_circles
,offset=-length)
////////////////////////////////////////////////////////////////////////////
_draw_line(price, col, style) =>
var lstyle = ""
if style == 1
lstyle := "line.style_solid"
else if style == 2
lstyle := "line.style_dashed"
else
lstyle := "line.style_dotted"
var id = line.new(iLast, price, bar_index, price, color=col, width=1,
extend=extending, style = style == 1 ? line.style_solid : style == 2 ?
line.style_dashed : line.style_dotted)
if not na(lineLast)
line.set_xy1(id, line.get_x1(lineLast), price)
line.set_xy2(id, line.get_x2(lineLast), price)
_draw_label(price, txt, txtColor) =>
x = labelsPosition == "Left" ? line.get_x1(lineLast) : not extendRight ?
line.get_x2(lineLast) : bar_index
labelStyle = labelsPosition == "Left" ? label.style_label_right :
label.style_label_left
align = labelsPosition == "Left" ? text.align_right : text.align_left
labelsAlignStrLeft = txt
labelsAlignStrRight = ' ' + txt
labelsAlignStr = labelsPosition == "Left" ? labelsAlignStrLeft :
labelsAlignStrRight
var id = label.new(x=x, y=price, text=labelsAlignStr, textcolor=txtColor,
style=labelStyle, textalign=align, color=#00000000)
label.set_xy(id, x, price)
label.set_text(id, labelsAlignStr)
label.set_textcolor(id, txtColor)
//If the price is below $1, display 4 decimal places
_wrap(txt) =>
if abs(txt) < 1
"(" + tostring(txt, "#.####") + ")"
else if abs(txt) >= 1 and abs(txt) < 50
"(" + tostring(txt, "#.###") + ")"
else
"(" + tostring(txt, "#.##") + ")"
_label_txt(level, price) =>
l = levelsFormat == "Values" ? tostring(level) : tostring(level * 100) + "%"
(levels ? l : "") + (prices ? _wrap(price) : "")
_crossing_level(sr, r) =>
(r > sr and r < sr[1]) or (r < sr and r > sr[1])
//FIBONACCI LEVELS
var vall1 = -0.65
var vall2 = -0.618
var vall3 = -0.382
var vall4 = -0.236
var vall5 = 0.
var vall6 = 0.236
var vall6a = 0.272
var vall7 = 0.382
var vall7a = 0.414
var vall8 = 0.5
var vall9 = 0.618
var vall10 = 0.65
var vall11 = 0.786
var vall12 = 0.886
var vall13 = 1.
var vall14 = 1.272
var vall15 = 1.414
var vall16 = 1.618
var vall16a = 1.886
var vall17 = 2.
//COLORS
var coll1 = #7b1fa2 //-0.65
var coll2 = #4a148c //-0.618
var coll3 = #ab47bc //-0.382
var coll4 = #ba68c8 //-0.236
var coll5 = #9575cd //0.0
var coll6 = #673ab7 //0.236
var coll6a = #b39ddb //0.236
var coll7 = #b39ddb //0.382
var coll7a = #d1c4e9 //0.414
var coll8 = #7e57c2 //0.5
var coll9 = #673ab7 //0.618
var coll10 = #9575cd //0.65
var coll11 = #673ab7 //0.786
var coll12 = #9575cd //0.886
var coll13 = #9575cd //1.0
var coll14 = #5b9cf6 //1.272
var coll15 = #3179f5 //1.414
var coll16 = #0c3299 //1.618
var coll16a = #1848cc //1.886
var coll17 = #1848cc //2.0
processLevel(show, value, colorL, lineStyle) =>
if show
float m = value
if logScale
iHL = startPrice > endPrice ? -1 : 1
diff = startPrice > endPrice ? abs(startPrice / endPrice) :
abs(endPrice/startPrice)
r = startPrice * pow(diff, iHL * value)
_draw_line(r, colorL, lineStyle)
_draw_label(r, _label_txt(m, r), colorL)
//if _crossing_level(close, r)
//alert("Autofib: " + syminfo.ticker + " crossing level " +
tostring(value))
else
iHL = startPrice > endPrice
diff = (iHL ? -1 : 1) * abs(startPrice - endPrice)
r = startPrice + diff * m
_draw_line(r, colorL, lineStyle)
_draw_label(r, _label_txt(m, r), colorL)
//if _crossing_level(close, r)
//alert("Autofib: " + syminfo.ticker + " crossing level " +
tostring(value))
true
processFibPrice(value) =>
float m = value
if logScale
iHL = startPrice > endPrice ? -1 : 1
diff = startPrice > endPrice ? abs(startPrice / endPrice) :
abs(endPrice/startPrice)
r = startPrice * pow(diff, iHL * m)
else
iHL = startPrice > endPrice
diff = (iHL ? -1 : 1) * abs(startPrice - endPrice)
r = startPrice + diff * m
show_neg_0_65 = input(false, "", inline = "Level0", group="Fibonacci Levels")
value_neg_0_65 = input(vall1, "", inline = "Level0", group="Fibonacci Levels")
color_neg_0_65 = input(coll1, "", inline = "Level0", group="Fibonacci Levels")
y_neg_0_65 = processFibPrice(value_neg_0_65)
show_neg_0_618 = input(false, "", inline = "Level0", group="Fibonacci Levels")
value_neg_0_618 = input(vall2, "", inline = "Level0", group="Fibonacci Levels")
color_neg_0_618 = input(coll2, "", inline = "Level0", group="Fibonacci Levels")
y_neg_0_618 = processFibPrice(value_neg_0_618)
show_neg_0_382 = input(false, "", inline = "Level1", group="Fibonacci Levels")
value_neg_0_382 = input(vall3, "", inline = "Level1", group="Fibonacci Levels")
color_neg_0_382 = input(coll3, "", inline = "Level1", group="Fibonacci Levels")
y_neg_0_382 = processFibPrice(value_neg_0_382)
show_neg_0_236 = input(true, "", inline = "Level1", group="Fibonacci Levels")
value_neg_0_236 = input(vall4, "", inline = "Level1", group="Fibonacci Levels")
color_neg_0_236 = input(coll4, "", inline = "Level1", group="Fibonacci Levels")
y_neg_0_236 = processFibPrice(value_neg_0_236)
show_0 = input(true, "", inline = "Level2", group="Fibonacci Levels")
value_0 = input(vall5, "", inline = "Level2", group="Fibonacci Levels")
color_0 = input(coll5, "", inline = "Level2", group="Fibonacci Levels")
y_0 = processFibPrice(value_0)
show_0_236 = input(true, "", inline = "Level2", group="Fibonacci Levels")
value_0_236 = input(vall6, "", inline = "Level2", group="Fibonacci Levels")
color_0_236 = input(coll6, "", inline = "Level2", group="Fibonacci Levels")
y_0_236 = processFibPrice(value_0_236)
show_0_272 = input(true, "", inline = "Level3", group="Fibonacci Levels")
value_0_272 = input(vall6a, "", inline = "Level3", group="Fibonacci Levels")
color_0_272 = input(coll6a, "", inline = "Level3", group="Fibonacci Levels")
y_0_272 = processFibPrice(value_0_272)
show_0_382 = input(true, "", inline = "Level3", group="Fibonacci Levels")
value_0_382 = input(vall7, "", inline = "Level3", group="Fibonacci Levels")
color_0_382 = input(coll7, "", inline = "Level3", group="Fibonacci Levels")
y_0_382 = processFibPrice(value_0_382)
show_0_414 = input(true, "", inline = "Level4", group="Fibonacci Levels")
value_0_414 = input(vall7a, "", inline = "Level4", group="Fibonacci Levels")
color_0_414 = input(coll7a, "", inline = "Level4", group="Fibonacci Levels")
y_0_414 = processFibPrice(value_0_414)
show_0_5 = input(true, "", inline = "Level4", group="Fibonacci Levels")
value_0_5 = input(vall8, "", inline = "Level4", group="Fibonacci Levels")
color_0_5 = input(coll8, "", inline = "Level4", group="Fibonacci Levels")
y_0_5 = processFibPrice(value_0_5)
show_0_618 = input(true, "", inline = "Level5", group="Fibonacci Levels")
value_0_618 = input(vall9, "", inline = "Level5", group="Fibonacci Levels")
color_0_618 = input(coll9, "", inline = "Level5", group="Fibonacci Levels")
y_0_618 = processFibPrice(value_0_618)
show_0_65 = input(true, "", inline = "Level5", group="Fibonacci Levels")
value_0_65 = input(vall10, "", inline = "Level5", group="Fibonacci Levels")
color_0_65 = input(coll10, "", inline = "Level5", group="Fibonacci Levels")
y_0_65 = processFibPrice(value_0_65)
show_0_786 = input(true, "", inline = "Level6", group="Fibonacci Levels")
value_0_786 = input(vall11, "", inline = "Level6", group="Fibonacci Levels")
color_0_786 = input(coll11, "", inline = "Level6", group="Fibonacci Levels")
y_0_786 = processFibPrice(value_0_786)
show_0_886 = input(true, "", inline = "Level6", group="Fibonacci Levels")
value_0_886 = input(vall12, "", inline = "Level6", group="Fibonacci Levels")
color_0_886 = input(coll12, "", inline = "Level6", group="Fibonacci Levels")
y_0_886 = processFibPrice(value_0_886)
show_1 = input(true, "", inline = "Level7", group="Fibonacci Levels")
value_1 = input(vall13, "", inline = "Level7", group="Fibonacci Levels")
color_1 = input(coll13, "", inline = "Level7", group="Fibonacci Levels")
y_1 = processFibPrice(value_1)
show_1_272 = input(true, "", inline = "Level7", group="Fibonacci Levels")
value_1_272 = input(vall14, "", inline = "Level7", group="Fibonacci Levels")
color_1_272 = input(coll14, "", inline = "Level7", group="Fibonacci Levels")
y_1_272 = processFibPrice(value_1_272)
show_1_414 = input(false, "", inline = "Level8", group="Fibonacci Levels")
value_1_414 = input(vall15, "", inline = "Level8", group="Fibonacci Levels")
color_1_414 = input(coll15, "", inline = "Level8", group="Fibonacci Levels")
y_1_414 = processFibPrice(value_1_414)
show_1_618 = input(false, "", inline = "Level8", group="Fibonacci Levels")
value_1_618 = input(vall16, "", inline = "Level8", group="Fibonacci Levels")
color_1_618 = input(coll16, "", inline = "Level8", group="Fibonacci Levels")
y_1_618 = processFibPrice(value_1_618)
show_1_886 = input(false, "", inline = "Level9", group="Fibonacci Levels")
value_1_886 = input(vall16a, "", inline = "Level9", group="Fibonacci Levels")
color_1_886 = input(coll16a, "", inline = "Level9", group="Fibonacci Levels")
y_1_886 = processFibPrice(value_1_886)
show_2 = input(false, "", inline = "Level9", group="Fibonacci Levels")
value_2 = input(vall17, "", inline = "Level9", group="Fibonacci Levels")
color_2 = input(coll17, "", inline = "Level9", group="Fibonacci Levels")
y_2 = processFibPrice(value_2)
processLevel(forceFib ? show_neg_0_65 : fibDir == 1 and close > y_neg_0_618 or
fibDir == -1 and close < y_neg_0_618 ? forceFib ? show_neg_0_65 : true :
show_neg_0_65, value_neg_0_65, color_neg_0_65, 3)
processLevel(forceFib ? show_neg_0_618 : fibDir == 1 and close > y_neg_0_382 or
fibDir == -1 and close < y_neg_0_382 ? forceFib ? show_neg_0_618 : true :
show_neg_0_618, value_neg_0_618, color_neg_0_618, 3)
processLevel(forceFib ? show_neg_0_382 : fibDir == 1 and close > y_neg_0_236 or
fibDir == -1 and close < y_neg_0_236 ? forceFib ? show_neg_0_382 : true :
show_neg_0_382, value_neg_0_382, color_neg_0_382, 3)
processLevel(forceFib ? show_neg_0_236 : fibDir == 1 and close > y_0 or fibDir == -
1 and close < y_0 ? forceFib ? show_neg_0_236 : true : show_neg_0_236,
value_neg_0_236, color_neg_0_236, 3)
processLevel(forceFib ? show_0 : show_0, value_0, color_0, 1)
processLevel(forceFib ? show_0_236 : show_0_236, value_0_236, color_0_236, 1)
processLevel(forceFib ? show_0_272 : show_0_272, value_0_272, color_0_272, 3)
processLevel(forceFib ? show_0_382 : show_0_382, value_0_382, color_0_382, 1)
processLevel(forceFib ? show_0_414 : show_0_414, value_0_414, color_0_414, 3)
processLevel(forceFib ? show_0_5 : show_0_5, value_0_5, color_0_5, 2)
processLevel(forceFib ? show_0_618 : show_0_618, value_0_618, color_0_618, 1)
processLevel(forceFib ? show_0_65 : show_0_65, value_0_65, color_0_65, 3)
processLevel(forceFib ? show_0_786 : show_0_786, value_0_786, color_0_786, 1)
processLevel(forceFib ? show_0_886 : show_0_886, value_0_886, color_0_886, 3)
processLevel(forceFib ? show_1 : show_1, value_1, color_1, 1)
processLevel(forceFib ? show_1_272 : fibDir == -1 and close > y_1 or fibDir == 1
and close < y_1 ? forceFib ? show_1_272 : true : show_1_272, value_1_272,
color_1_272, 3)
processLevel(forceFib ? show_1_414 : fibDir == -1 and close > y_1_272 or fibDir ==
1 and close < y_1_272 ? forceFib ? show_1_414 : true : show_1_414, value_1_414,
color_1_414, 3)
processLevel(forceFib ? show_1_618 : fibDir == -1 and close > y_1_414 or fibDir ==
1 and close < y_1_414 ? forceFib ? show_1_618 : true : show_1_618, value_1_618,
color_1_618, 3)
processLevel(forceFib ? show_1_886 : fibDir == -1 and close > y_1_618 or fibDir ==
1 and close < y_1_618 ? forceFib ? show_1_886 : true : show_1_886, value_1_886,
color_2, 3)
processLevel(forceFib ? show_2 : fibDir == -1 and close > y_1_886 or fibDir == 1
and close < y_1_886 ? forceFib ? show_2 : true : show_2, value_2, color_2, 3)