// ©Julielsonvidinha


//version=5
indicator(title="[JV] Patolino", overlay=true,max_labels_count=500,max_bars_back = 4900)
show_zigzag = input(true, "Show zigzag", group="Show")
show_fib = input(true, "Show fibonacci", group="Show")
show_supertrend = input(true, "Show supertrend", group="Show")
show_premiumdiscount = input(true, "Show premium/discount line", group="Show")



// Getting inputs
atrPeriod = input(10, "ATR Length")
factor = input.float(3, "Factor", step = 0.1)
atrline = input.float(1.5, "Premium/Discount", step = 0.1)
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 and show_supertrend ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend = plot(direction > 0 and show_supertrend ? supertrend :na, "Down Trend", color = color.red, style=plot.style_linebr)

fill(bodyMiddle, upTrend, show_supertrend ? color.new(color.green, 90):na, fillgaps=false)
fill(bodyMiddle, downTrend,show_supertrend ? color.new(color.red, 90):na, fillgaps=false)

ATR = ta.atr(atrPeriod)
upatrline = supertrend + atrline*ATR
dnatrline = supertrend - atrline*ATR

plot(direction < 0 and show_premiumdiscount ? upatrline : na, "Up ATR", color = color.white, style=plot.style_linebr)
plot(direction > 0 and show_premiumdiscount ? dnatrline : na, "Dn ATR", color = color.white, style=plot.style_linebr)




colorGroupTitle = "Text Color / Label Color"

textColorH = input(title="Pivot High", defval=color.white, inline="Pivot High", group=colorGroupTitle)
labelColorH = input(title="", defval=color.red, inline="Pivot High", group=colorGroupTitle)

textColorL = input(title="Pivot Low", defval=color.white, inline="Pivot Low", group=colorGroupTitle)
labelColorL = input(title="", defval=color.green, inline="Pivot Low", group=colorGroupTitle)

turnGreen = ta.change(direction) < 0
turnRed = ta.change(direction) > 0


barsturngreen = bar_index - ta.valuewhen(turnGreen, bar_index, 0)
barsturnred = bar_index - ta.valuewhen(turnRed, bar_index, 0)
barsg = barsturngreen>0 ? barsturngreen : 1

h1 = ta.highest(high,barsg)
//plot(h1)
barsr = barsturnred>0 ? barsturnred : 1


l2 = ta.lowest(low,barsr)
//plot(l2)
barsh = bar_index - ta.valuewhen(ta.change(h1),bar_index,0)
barsl = bar_index - ta.valuewhen(ta.change(l2),bar_index,0)
barsh2 = bar_index - ta.valuewhen(ta.change(h1),bar_index,1)
barsl2 = bar_index - ta.valuewhen(ta.change(l2),bar_index,1)
h1Index = 0
l2Index = 0
var label h1label = na
var label l2label = na
drawLabel(_offset, _pivot, _style, _color, _textColor, _text) =>
if not na(_pivot)
label.new(bar_index - _offset, _pivot, _text + ": " + str.tostring(_pivot, format.mintick), style=_style, color=_color, textcolor=_textColor)


if turnRed
h1label := drawLabel(barsh, h1, label.style_label_down, labelColorH, textColorH, "Venda")
if show_zigzag
line.new(bar_index - barsh, h1, bar_index - barsl2, l2[1], width = 3, color = color.lime)

if turnGreen
l2label := drawLabel(barsl, l2, label.style_label_up, labelColorL, textColorL, "Compra")
if show_zigzag
line.new(bar_index - barsh2, h1[1], bar_index - barsl, l2, width = 3, color = color.fuchsia)


hh = ta.valuewhen(turnRed, h1, 0)
ll = ta.valuewhen(turnGreen, l2, 0)




mid_val = (hh + ll) / 2

// Plot lines for highest high and lowest low
var line hhLine = na
var line llLine = na
var line midline = na
var label fibLabelh = na
var label fibLabell = na
var label fibLabelm = na
var label fibLabel1 = na
var label fibLabel2 = na
var label fibLabel3 = na
var label fibLabel4 = na
var label fibLabel5 = na
var label fibLabel6 = na
var line fibLine1 = na
var line fibLine2 = na
var line fibLine3 = na
var line fibLine4 = na
var line fibLine5 = na
var line fibLine6 = na
if barstate.islast and show_fib
while high[h1Index] != h1[1]
h1Index := h1Index + 1

while low[l2Index] != l2[1]
l2Index := l2Index + 1

n = math.max(h1Index,l2Index)
if not na(hhLine)
line.delete(hhLine)
if not na(llLine)
line.delete(llLine)
if not na(midline)
line.delete(midline)
if not na(fibLabelh)
label.delete(fibLabelh)
if not na(fibLabell)
label.delete(fibLabell)

if not na(fibLabelm)
label.delete(fibLabelm)
if not na(fibLabel1)
label.delete(fibLabel1)
if not na(fibLabel2)
label.delete(fibLabel2)
if not na(fibLabel3)
label.delete(fibLabel3)

if not na(fibLabel4)
label.delete(fibLabel4)
if not na(fibLabel5)
label.delete(fibLabel5)

if not na(fibLabel6)
label.delete(fibLabel6)
if not na(fibLine1)
line.delete(fibLine1)
if not na(fibLine2)
line.delete(fibLine2)
if not na(fibLine3)
line.delete(fibLine3)
if not na(fibLine4)
line.delete(fibLine4)
if not na(fibLine5)
line.delete(fibLine5)
if not na(fibLine6)
line.delete(fibLine6)
hhLine := line.new(x1=bar_index-n+1, y1=hh, x2=bar_index, y2=hh, color=color.purple, width=1)
fibLabelh := label.new(x=bar_index + 10, y=hh , text="1("+str.tostring(hh )+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

llLine := line.new(x1=bar_index-n+1, y1=ll, x2=bar_index, y2=ll, color=color.purple, width=1)
fibLabell := label.new(x=bar_index + 10, y=ll , text="0("+str.tostring(ll )+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

midline := line.new(bar_index-n+1, mid_val, bar_index, mid_val, color=color.purple, width=1)
fibLabelm := label.new(x=bar_index + 10, y=mid_val , text="0.5("+str.tostring(mid_val )+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

fibLabel1 := label.new(x=bar_index + 10, y=ll + 0.236*(hh-ll), text="0.236("+str.tostring(ll + 0.236*(hh-ll),"#.####")+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

fibLine1 := line.new(x1 = bar_index-n+1,y1 = ll + 0.236*(hh-ll),x2 = bar_index,y2 = ll + 0.236*(hh-ll),color = color.purple,width = 1)

fibLabel2 := label.new(x=bar_index + 10, y=ll + 0.382*(hh-ll), text="0.382("+str.tostring(ll + 0.382*(hh-ll),"#.####")+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

fibLine2 := line.new(x1 = bar_index-n+1,y1 = ll + 0.382*(hh-ll),x2 = bar_index,y2 = ll + 0.382*(hh-ll),color = color.purple,width = 1)

fibLabel3 := label.new(x=bar_index + 10, y=ll + 0.618*(hh-ll), text="0.618("+str.tostring(ll + 0.618*(hh-ll),"#.####")+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

fibLine3 := line.new(x1 = bar_index-n+1,y1 = ll + 0.618*(hh-ll),x2 = bar_index,y2 = ll + 0.618*(hh-ll),color = color.purple,width = 1)

fibLabel4 := label.new(x=bar_index + 10, y=ll + 0.786*(hh-ll), text="0.786("+str.tostring(ll + 0.786*(hh-ll),"#.####")+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

fibLine4 := line.new(x1 = bar_index-n+1,y1 = ll + 0.786*(hh-ll),x2 = bar_index,y2 = ll + 0.786*(hh-ll),color = color.purple,width = 1)

if close>supertrend
fibLabel5 := label.new(x=bar_index + 10, y=ll + 1.618*(hh-ll), text="1.618("+str.tostring(ll + 1.618*(hh-ll),"#.####")+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

fibLine5 := line.new(x1 = bar_index-n+1,y1 = ll + 1.618*(hh-ll),x2 = bar_index,y2 = ll + 1.618*(hh-ll),color = #54dd0b,width = 1)
if close<supertrend
fibLabel6 := label.new(x=bar_index + 10, y=ll - 0.618*(hh-ll), text="-1.618("+str.tostring(ll - 0.618*(hh-ll),"#.####")+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)

fibLine6 := line.new(x1 = bar_index-n+1,y1 = ll - 0.618*(hh-ll),x2 = bar_index,y2 = ll - 0.618*(hh-ll),color = color.purple,width = 1)


//

// Variáveis de estado para verificar mudança de direção
var label buyLabel = na
var label sellLabel = na

//

lengthInput = input.int(100, title="Length", minval = 1, maxval = 5000)
sourceInput = input.source(close, title="Source")

group1 = "Channel Settings"
useUpperDevInput = input.bool(true, title="Upper Deviation", inline = "Upper Deviation", group = group1)
upperMultInput = input.float(2.0, title="", inline = "Upper Deviation", group = group1)
useLowerDevInput = input.bool(true, title="Lower Deviation", inline = "Lower Deviation", group = group1)
lowerMultInput = input.float(2.0, title="", inline = "Lower Deviation", group = group1)

group2 = "Display Settings"
showPearsonInput = input.bool(true, "Show Pearson's R", group = group2)
extendLeftInput = input.bool(false, "Extend Lines Left", group = group2)
extendRightInput = input.bool(true, "Extend Lines Right", group = group2)
extendStyle = switch
extendLeftInput and extendRightInput => extend.both
extendLeftInput => extend.left
extendRightInput => extend.right
=> extend.none

group3 = "Color Settings"
colorUpper = input.color(#2195f300, "", inline = group3, group = group3)
colorLower = input.color(#ff525200, "", inline = group3, group = group3)

calcSlope(source, length) =>
max_bars_back(source, 5000)
if not barstate.islast or length <= 1
[float(na), float(na), float(na)]
else
sumX = 0.0
sumY = 0.0
sumXSqr = 0.0
sumXY = 0.0
for i = 0 to length - 1 by 1
val = source
per = i + 1.0
sumX += per
sumY += val
sumXSqr += per * per
sumXY += val * per
slope = (length * sumXY - sumX * sumY) / (length * sumXSqr - sumX * sumX)
average = sumY / length
intercept = average - slope * sumX / length + slope
[slope, average, intercept]

[s, a, i] = calcSlope(sourceInput, lengthInput)
startPrice = i + s * (lengthInput - 1)
endPrice = i
var line baseLine = na
if na(baseLine) and not na(startPrice)
baseLine := line.new(bar_index - lengthInput + 1, startPrice, bar_index, endPrice, width=1, extend=extendStyle, color=color.new(colorLower, 0))
else
line.set_xy1(baseLine, bar_index - lengthInput + 1, startPrice)
line.set_xy2(baseLine, bar_index, endPrice)
na

calcDev(source, length, slope, average, intercept) =>
upDev = 0.0
dnDev = 0.0
stdDevAcc = 0.0
dsxx = 0.0
dsyy = 0.0
dsxy = 0.0
periods = length - 1
daY = intercept + slope * periods / 2
val = intercept
for j = 0 to periods by 1
price = high[j] - val
if price > upDev
upDev := price
price := val - low[j]
if price > dnDev
dnDev := price
price := source[j]
dxt = price - average
dyt = val - daY
price -= val
stdDevAcc += price * price
dsxx += dxt * dxt
dsyy += dyt * dyt
dsxy += dxt * dyt
val += slope
stdDev = math.sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / math.sqrt(dsxx * dsyy)
[stdDev, pearsonR, upDev, dnDev]

[stdDev, pearsonR, upDev, dnDev] = calcDev(sourceInput, lengthInput, s, a, i)
upperStartPrice = startPrice + (useUpperDevInput ? upperMultInput * stdDev : upDev)
upperEndPrice = endPrice + (useUpperDevInput ? upperMultInput * stdDev : upDev)
var line upper = na
lowerStartPrice = startPrice + (useLowerDevInput ? -lowerMultInput * stdDev : -dnDev)
lowerEndPrice = endPrice + (useLowerDevInput ? -lowerMultInput * stdDev : -dnDev)
var line lower = na
if na(upper) and not na(upperStartPrice)
upper := line.new(bar_index - lengthInput + 1, upperStartPrice, bar_index, upperEndPrice, width=1, extend=extendStyle, color=color.new(colorUpper, 0))
else
line.set_xy1(upper, bar_index - lengthInput + 1, upperStartPrice)
line.set_xy2(upper, bar_index, upperEndPrice)
na
if na(lower) and not na(lowerStartPrice)
lower := line.new(bar_index - lengthInput + 1, lowerStartPrice, bar_index, lowerEndPrice, width=1, extend=extendStyle, color=color.new(colorUpper, 0))
else
line.set_xy1(lower, bar_index - lengthInput + 1, lowerStartPrice)
line.set_xy2(lower, bar_index, lowerEndPrice)
na
linefill.new(upper, baseLine, color = colorUpper)
linefill.new(baseLine, lower, color = colorLower)

float trend = math.sign(startPrice - endPrice)

// Pearson's R
var label r = na
label.delete(r[1])
if showPearsonInput and not na(pearsonR)
r := label.new(bar_index - lengthInput + 1, lowerStartPrice, str.tostring(pearsonR, "#.################"), color = color.new(color.white, 100), textcolor=color.new(colorUpper, 0), size=size.normal, style=label.style_label_up)

//

if turnGreen
alert("SETUP PATOLINO\n\nEntry\nLeverage 20x\n\nLong: " + syminfo.ticker + "\n\nEntry: " + str.tostring(close) + "\n\nBy: julielsonvidinha" + "\nGerencie seu risco" + "\nCanal: (t.me/setupthorsinais)", alert.freq_once_per_bar_close)
if turnRed
alert("SETUP PATOLINO\n\nEntry\nLeverage 20x\n\nShort: " + syminfo.ticker + "\n\nEntry: " + str.tostring(close) + "\n\nBy: julielsonvidinha" + "\nGerencie seu risco" + "\nCanal: (t.me/setupthorsinais)", alert.freq_once_per_bar_close)
Technical Indicators

إخلاء المسؤولية