Hi I'm creating a trading view indicator in pine script, and im using auto fibonacci retracement, problem is I want auto fib to use the last low/high made before the 2 moving averages crossed and then the next fib point to be after cross similar to the chart attached
https://www.tradingview.com/x/L47bSMza/
Here is my code:
study(SUBHAN 2.0
, overlay = true)
BullMA = sma(close,50) // Bullish Moving Average
BearMA = sma(close,200) // Bearish Moving Average
BullTrend = (crossover(BullMA,BearMA)) // signifying change of bullish trend when 50 above 200
BearTrend = (crossover(BearMA,BullMA))// signifying change of bearish trend when 200 above 50
plotshape(BullTrend, style=shape.triangleup, color=color.green, transp=40, text=BUY
, editable=false, location=location.belowbar, size=size.small)
plotshape(BearTrend, style=shape.triangleup, color=color.red, transp=0, text=SELL
, editable=false, location=location.abovebar, size=size.small)
plot(series=BearMA, color= color.red, linewidth=2)
plot(series=BullMA, color= color.green, linewidth=2)
//Auto Fibonacci Retracement code (for the most part stock with a few changes highlighted)
threshold_multiplier = input(title=Deviation
, type=input.float, defval=3, minval=0, inline = Pivots
)
dev_threshold = atr(10) / close * 100 * threshold_multiplier
depth = input(title=Depth
, type=input.integer, defval=10, minval=1, inline = Pivots
) // area of interest for MA
reverse = false
var extendLeft = input(false, Extend Left | Extend Right
, inline = Extend Lines
)
var extendRight = input(true, , inline = "Extend Lines")
, options = [
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")
levels = input(true, "Show Levels", inline = "Levels")
levelsFormat = input("Values",Values
, Percent
], inline = Levels
)
labelsPosition = input(Left
, Labels Position
, options = [Left
, Right
])
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
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, depth / 2, true)
[iL, pL] = pivots(low, 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) > dev_threshold
// price move is significant
id = line.new(iLast, pLast, index, price, color=color.gray, width=1, style=line.style_dashed)
[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
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
line.delete(lineLast)
lineLast := id
isHighLast := isHigh
iPrev := iLast
iLast := iL
pLast := pL
_draw_line(price, col) =>
var id = line.new(iLast, price, bar_index, price, color=col, width=1, extend=extending)
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 + '\n \n'
labelsAlignStrRight = ' ' + txt + '\n \n'
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)
_wrap(txt) =>
(
+ 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])
startPrice = reverse ? line.get_y1(lineLast) : pLast
endPrice = reverse ? pLast : line.get_y1(lineLast)
iHL = startPrice > endPrice
diff = (iHL ? -1 : 1) * abs(startPrice - endPrice)
processLevel(show, value, colorL) =>
float m = value
r = startPrice + diff * m
if show
_draw_line(r, colorL)
_draw_label(r, _label_txt(m, r), colorL)
if _crossing_level(close, r)
alert(Autofib:
+ syminfo.ticker + crossing level
+ tostring(value))
show_0 = input(true, , inline = "Level0")
, inline =
value_0 = input(0,Level0
)
color_0 = input(#a7a7a7, ", inline =
Level0")
processLevel(show_0, value_0, color_0)
show_0_382 = input(true, , inline = "Level1")
, inline =
value_0_382 = input(0.382,Level1
)
color_0_382 = input(#a7a7a7, ", inline =
Level1")
processLevel(show_0_382, value_0_382, color_0_382)
show_0_5 = input(true, , inline = "Level1")
, inline =
value_0_5 = input(0.5,Level1
)
color_0_5 = input(#a7a7a7, ", inline =
Level1")
processLevel(show_0_5, value_0_5, color_0_5)
show_0_618 = input(true, , inline = "Level2")
, inline =
value_0_618 = input(0.618,Level2
)
color_0_618 = input(#009688, ", inline =
Level2")
processLevel(show_0_618, value_0_618, color_0_618)
show_0_716 = input(true, , inline = "Level2")
, inline =
value_0_716 = input(0.716,Level2
)
color_0_716 = input(#a7a7a7, ", inline =
Level2")
processLevel(show_0_716, value_0_716, color_0_716)
show_0_786 = input(true, , inline = "Level3")
, inline =
value_0_786 = input(0.786,Level3
)
color_0_786 = input(#a7a7a7, ", inline =
Level3")
processLevel(show_0_786, value_0_786, color_0_786)
show_0_886 = input(true, , inline = "Level3")
, inline =
value_0_886 = input(0.886,Level3
)
color_0_886 = input(#ff0000, ", inline =
Level3")
processLevel(show_0_886, value_0_886, color_0_886)
show_1 = input(true, , inline = "Level4")
, inline =
value_1 = input(1,Level4
)
color_1 = input(#a7a7a7, ", inline =
Level4")
processLevel(show_1, value_1, color_1)
show_1_13 = input(true, , inline = "Level4")
, inline =
value_1_13 = input(1.13,Level4
)
color_1_13 = input(#ff0000, ", inline =
Level4")
processLevel(show_1_13, value_1_13, color_1_13)
show_neg_0_27 = input(true, , inline = "Level5")
, inline =
value_neg_0_27 = input(-0.27,Level5
)
color_neg_0_27 = input(#a7a7a7, ", inline =
Level5")
processLevel(show_neg_0_27, value_neg_0_27, color_neg_0_27)
show_neg_0_618 = input(true, , inline = "Level5")
, inline =
value_neg_0_618 = input(-0.618,Level5
)
color_neg_0_618 = input(#a7a7a7, ", inline =
Level5")
processLevel(show_neg_0_618, value_neg_0_618, color_neg_0_618)
How do I tell it for the first fib point to be before the MA crossed
Any help is apprecieted thanks