ABC finder
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © melodicfish

//@version=5
indicator("ABC finder",shorttitle="ABC", overlay=true)
t_col=input(color.new(color.gray,25),title="Trend Line Color",inline="6")
tl_col=input(color.new(color.white,100),title="Trade Line Color",inline="6")
pl_colL=input(color.new(color.yellow,0),title="Long Setup Line Color",inline="5")
pl_colS=input(color.new(#ff933b, 0),title="Short Setup Line Color",inline="5")
lab_colL=input(color.new(color.green,0),title="Label Color Long Setup",inline="4")
lab_colS=input(color.new(color.red,0),title="Label Color Short Setup",inline="4")

tarBox_col=input(color.new(color.green,55),title="Target Box Color",inline="7")
stopBox_col=input(color.new(color.red,55),title="Stop Box Color",inline="7")

pvl_col=color.new(color.blue,100)
barsR=0
barsL=input(10,title="Bars Back",tooltip="Number of bars left of the Pivot")
tl_wth=input(3,title="Trade Line Width",inline="1")
extTradeL=input(false,title="Extend Trade Line to Right")
pl_wth=input(6,title="ABC Line Width",inline="1")
show=input(false,title="Show Fib Lines")
useFibSetups=input(false,title="Use Fib Based Trade Management")
var lastPivotType=0
var phIndex=0
var plIndex=0
var ph=0.0
var pl=0.0
var highp=0.0
var lowp=0.0
var lastLine=line.new(0,0,0,0)

var patL1L=line.new(0,0,0,0,color=pl_colL,width=pl_wth)
var patL2L=line.new(0,0,0,0,color=pl_colL,width=pl_wth)
var patL3L=line.new(0,0,0,0,color=pl_colL,width=pl_wth)
var patL1S=line.new(0,0,0,0,color=pl_colS,width=pl_wth)
var patL2S=line.new(0,0,0,0,color=pl_colS,width=pl_wth)
var patL3S=line.new(0,0,0,0,color=pl_colS,width=pl_wth)

var tradeLineL=line.new(0,0,0,0,extend=extTradeL?extend.right:extend.none,color=tl_col,width=tl_wth)
var tradeLineS=line.new(0,0,0,0,extend=extTradeL?extend.right:extend.none,color=tl_col,width=tl_wth)

var l0=line.new(0,0,0,0,extend=extend.right,color=pvl_col,width=1)
var l1=line.new(0,0,0,0,extend=extend.right,color=pvl_col,width=1)
var l2=line.new(0,0,0,0,extend=extend.right,color=pvl_col,width=1)
var l3=line.new(0,0,0,0,extend=extend.right,color=pvl_col,width=1)
var l4=line.new(0,0,0,0,extend=extend.right,color=pvl_col,width=1)

var lines= array.new_line(5)
array.set(lines,0,l0)
array.set(lines,1,l1)
array.set(lines,2,l2)
array.set(lines,3,l3)
array.set(lines,4,l4)

ph:=ta.pivothigh(high,barsL,barsR)
pl:= ta.pivotlow(low,barsL,barsR)

//Update the holding variables
if ph>0
    highp:=ph
if pl>0
    lowp:=pl
    
// Get the new index
phIndex:=ph?bar_index-barsR:phIndex[1]
plIndex:=pl?bar_index-barsR:plIndex[1]

// Update direction type
lastPivotType:= ph?1:pl?-1:barstate.isfirst?0:lastPivotType[1]


//create new line set xy2 and get end of prevous line
if lastPivotType!=lastPivotType[1] and lastPivotType==1
    line.set_x1(array.get(lines,4),line.get_x2(array.get(lines,1))-1)
    line.set_y1(array.get(lines,4),line.get_y2(array.get(lines,1)))
    line.set_x2(array.get(lines,4),line.get_x2(array.get(lines,1))+1)
    line.set_y2(array.get(lines,4),line.get_y2(array.get(lines,1)))

    lastLine:=line.new(line.get_x1(lastLine),line.get_y1(lastLine),phIndex,high[bar_index-phIndex])

    line.set_x1(array.get(lines,1),line.get_x1(lastLine))
    line.set_y1(array.get(lines,1),line.get_y1(lastLine))
    line.set_x2(array.get(lines,1),line.get_x1(lastLine)+1)
    line.set_y2(array.get(lines,1),line.get_y1(lastLine))
    line.set_x1(array.get(lines,0),line.get_x2(lastLine))
    line.set_y1(array.get(lines,0),line.get_y2(lastLine))
    line.set_x2(array.get(lines,0),line.get_x2(lastLine)+1)
    line.set_y2(array.get(lines,0),line.get_y2(lastLine))
    line.set_color(lastLine,t_col)


 //create new line set xy1 and get end of prevous line
if lastPivotType!=lastPivotType[1] and lastPivotType==-1
    line.set_x1(array.get(lines,3),line.get_x2(array.get(lines,2))-1)
    line.set_y1(array.get(lines,3),line.get_y2(array.get(lines,2)))
    line.set_x2(array.get(lines,3),line.get_x2(array.get(lines,2))+1)
    line.set_y2(array.get(lines,3),line.get_y2(array.get(lines,2)))

    lastLine:=line.new(plIndex,low[bar_index-plIndex],line.get_x2(lastLine),line.get_y2(lastLine))
    line.set_x1(array.get(lines,0),line.get_x1(lastLine))
    line.set_y1(array.get(lines,0),line.get_y1(lastLine))
    line.set_x2(array.get(lines,0),line.get_x1(lastLine)+1)
    line.set_y2(array.get(lines,0),line.get_y1(lastLine))
    line.set_x1(array.get(lines,2),line.get_x2(lastLine))
    line.set_y1(array.get(lines,2),line.get_y2(lastLine))
    line.set_x2(array.get(lines,2),line.get_x2(lastLine)+1)
    line.set_y2(array.get(lines,2),line.get_y2(lastLine))
    line.set_color(lastLine,t_col)


// Update Trend Down Line  
if lastPivotType==lastPivotType[1] and lastPivotType==-1 and pl<lowp[1]
    line.set_x1(lastLine[1],plIndex)
    line.set_y1(lastLine[1],low[bar_index-plIndex])
    line.set_color(lastLine[1],t_col)
    
    line.set_x1(array.get(lines,0),line.get_x1(lastLine))
    line.set_y1(array.get(lines,0),line.get_y1(lastLine))
    line.set_x2(array.get(lines,0),line.get_x1(lastLine)+1)
    line.set_y2(array.get(lines,0),line.get_y1(lastLine))

    
// Update Trend Up Line 
if lastPivotType==lastPivotType[1] and lastPivotType==1 and ph>highp[1]

    line.set_x2(lastLine[1],phIndex)
    line.set_y2(lastLine[1],high[bar_index-phIndex])
    line.set_color(lastLine[1],t_col)
    
    line.set_x1(array.get(lines,0),line.get_x2(lastLine))
    line.set_y1(array.get(lines,0),line.get_y2(lastLine))
    line.set_x2(array.get(lines,0),line.get_x2(lastLine)+1)
    line.set_y2(array.get(lines,0),line.get_y2(lastLine))

var s_lab=label.new(0,0,text="Short Below Here", color=lab_colS,style=label.style_label_lower_right,textcolor=color.white)
var l_lab=label.new(0,0,text="Long Above Here", color=lab_colL,style=label.style_label_upper_right,textcolor=color.white)
var lpl=0.0
var spl=0.0
var long=false
var short=false
var current=0
var currentIndex=0
var boxStart=0
var boxEnd=0
var patTop=0.0
var patBottom=0.0

//Draw Long
if barstate.isconfirmed and line.get_y1(array.get(lines,3))> line.get_y1(array.get(lines,2)) and line.get_y1(array.get(lines,2))> line.get_y1(array.get(lines,1)) and line.get_y1(array.get(lines,1))> line.get_y1(array.get(lines,0))// and line.get_y1(array.get(lines,1))>line.get_y1(array.get(lines,4)) 
    
    line.set_xy1(patL1L,line.get_x1(array.get(lines,3)),line.get_y1(array.get(lines,3)))
    line.set_xy2(patL1L,line.get_x1(array.get(lines,1)),line.get_y1(array.get(lines,1)))
    
    line.set_xy1(patL2L,line.get_x1(array.get(lines,1)),line.get_y1(array.get(lines,1)))
    line.set_xy2(patL2L,line.get_x1(array.get(lines,2)),line.get_y1(array.get(lines,2)))

    line.set_xy1(patL3L,line.get_x1(array.get(lines,2)),line.get_y1(array.get(lines,2)))
    line.set_xy2(patL3L,line.get_x1(array.get(lines,0)),line.get_y1(array.get(lines,0)))

    line.set_xy1(tradeLineL,line.get_x1(array.get(lines,1)),line.get_y1(array.get(lines,1)))
    line.set_xy2(tradeLineL,line.get_x1(array.get(lines,0))+15,line.get_y1(array.get(lines,1)))
    label.set_xy(l_lab,line.get_x1(tradeLineL),line.get_y1(tradeLineL))
    label.set_text(l_lab,"Long Above "+str.tostring(line.get_y1(tradeLineL)))
    lpl:=line.get_y1(tradeLineL)
    if lpl[1]!=lpl
        alert("LONG SETUP DETECTED Long Above: "+ str.tostring(line.get_y1(tradeLineL)),alert.freq_once_per_bar)
        current:=1
        currentIndex:=bar_index
    boxStart:=line.get_x1(tradeLineL)
    boxEnd:=line.get_x2(tradeLineL)
    patTop:=line.get_y1(patL1L)
    patBottom:=line.get_y2(patL1L) //line.get_y2(patL3L)

// Draw Short 
if barstate.isconfirmed and line.get_y1(array.get(lines,4))< line.get_y1(array.get(lines,2)) and line.get_y1(array.get(lines,1))< line.get_y1(array.get(lines,2)) and line.get_y1(array.get(lines,2))< line.get_y1(array.get(lines,0)) and line.get_y1(array.get(lines,1))>line.get_y1(array.get(lines,4)) 

    line.set_xy1(patL1S,line.get_x1(array.get(lines,4)),line.get_y1(array.get(lines,4)))
    line.set_xy2(patL1S,line.get_x1(array.get(lines,2)),line.get_y1(array.get(lines,2)))

    line.set_xy1(patL2S,line.get_x1(array.get(lines,2)),line.get_y1(array.get(lines,2)))
    line.set_xy2(patL2S,line.get_x1(array.get(lines,1)),line.get_y1(array.get(lines,1)))

    line.set_xy1(patL3S,line.get_x1(array.get(lines,1)),line.get_y1(array.get(lines,1)))
    line.set_xy2(patL3S,line.get_x1(array.get(lines,0)),line.get_y1(array.get(lines,0)))

    line.set_xy1(tradeLineS,line.get_x1(array.get(lines,2)),line.get_y1(array.get(lines,2)))
    line.set_xy2(tradeLineS,line.get_x1(array.get(lines,0))+15,line.get_y1(array.get(lines,2)))
    label.set_xy(s_lab,line.get_x1(tradeLineS),line.get_y1(tradeLineS))
    label.set_text(s_lab,"Short Below "+str.tostring(line.get_y1(tradeLineS)))
    spl:=line.get_y1(tradeLineS)
    if spl[1]!=spl
        alert("Short SETUP DETECTED Short Below: "+ str.tostring(line.get_y1(tradeLineS)),alert.freq_once_per_bar)
        current:=-1
        currentIndex:=bar_index
    boxStart:=line.get_x1(tradeLineS)
    boxEnd:=line.get_x2(tradeLineS)
    patTop:=line.get_y1(patL1S)
    patBottom:= line.get_y2(patL1S)//line.get_y2(patL3S)

//*********Table********
t_percent=input(3.0,title="Target Percentage",inline="3")
s_percent=input(1.5,title="Stop Percentage",inline="3")
buff=input(0.005,title="Position Entry buffer amount",inline="0")
isVal=input(false,title="Is a Value?",inline="0",tooltip="If enabled the scirpt will interpret the value entered as a dollar value. Disabled it will be interpreted as a precentage value")

var e_txt=""
var t_txt=""
var s_txt=""

var target_col=color.silver
var stop_col=color.silver

//table.new(position, columns, rows, bgcolor, frame_color, frame_width, border_color, border_width) → series table
var t=table.new(position.bottom_right,2,3,bgcolor=#434651,frame_color=#434651,frame_width=5,border_color= #434651,border_width=1)
table.cell(t,0,0,text="Entry",text_color=color.white)
table.cell(t,0,1,text="Target",text_color=color.white)
table.cell(t,0,2,text="Stop",text_color=color.white)
var tar=0.0
var stop=0.0
var y1=0.0
if current==1
    y1:=0.0
    if isVal==true
        y1:=line.get_y1(tradeLineL)+buff
    else
        y1:=line.get_y1(tradeLineL)*(1+(buff/100))
        
    e_txt:=str.tostring(close>10?math.round(y1,2):y1)    
    tar:=y1*(1+(t_percent/100))
    stop:=y1*(1-(s_percent/100))
    t_txt:=str.tostring(close>10?math.round(tar,2):tar)
    s_txt:=str.tostring(close>10?math.round(stop,2):stop)
    target_col:=lab_colL
    stop_col:=lab_colS
    boxStart:=line.get_x1(tradeLineL)
    boxEnd:=line.get_x2(tradeLineL)
else if current==-1
    y1:=0.0
    if isVal==true
        y1:=line.get_y1(tradeLineS)-buff
    else
        y1:=line.get_y1(tradeLineS)*(1-(buff/100))
    y1:=close>10?math.round(y1,2):y1
    e_txt:=str.tostring(close>10?math.round((y1),2):y1)
    stop:=y1*(1+(s_percent/100))
    tar:=y1*(1-(t_percent/100))
    s_txt:=str.tostring(close>10?math.round(stop,2):stop)
    t_txt:=str.tostring(close>10?math.round(tar,2):tar)
    target_col:=lab_colS
    stop_col:=lab_colL
    boxStart:=line.get_x1(tradeLineS)
    boxEnd:=line.get_x2(tradeLineS)


table.cell(t,1,0,text=e_txt,text_color=color.white)
table.cell(t,1,1,text=t_txt,text_color=color.white)
table.cell(t,1,2,text=s_txt,text_color=color.white)

// Set cell Colors
table.cell_set_bgcolor(t,0,0,target_col)
table.cell_set_bgcolor(t,1,0,target_col)
table.cell_set_bgcolor(t,0,1,target_col)
table.cell_set_bgcolor(t,1,1,target_col)
table.cell_set_bgcolor(t,0,2,stop_col)
table.cell_set_bgcolor(t,1,2,stop_col)

var tarLine=line.new(0,0,0,0)
var stopLine=line.new(0,0,0,0)
var entryLine=line.new(0,0,0,0,color=color.white)

var targetBox=box.new(0,0,0,0,bgcolor=tarBox_col,border_color=color.white)
var stopBox=box.new(0,0,0,0,bgcolor=stopBox_col,border_color=color.white)

if current==1
    box.set_left(targetBox,boxStart)
    box.set_top(targetBox,tar)
    box.set_right(targetBox,boxStart+25)
    box.set_bottom(targetBox,y1)

    box.set_left(stopBox,boxStart)
    box.set_top(stopBox,y1)
    box.set_right(stopBox,boxStart+25)
    box.set_bottom(stopBox,stop)
else if current==-1
    box.set_left(targetBox,boxStart)
    box.set_top(targetBox,y1)
    box.set_right(targetBox,boxStart+25)
    box.set_bottom(targetBox,tar)

    box.set_left(stopBox,boxStart)
    box.set_top(stopBox,stop)
    box.set_right(stopBox,boxStart+25)
    box.set_bottom(stopBox,y1)

//Fibs 

var clear_col=color.new(color.white,100)
Fib(ratio) =>
    valueRange= patTop-patBottom
    fibVal=patTop-(valueRange*ratio)

var fib1=line.new(0,0,0,0,color=color.green)    
var fib2=line.new(0,0,0,0,color=color.blue)    
var fib3=line.new(0,0,0,0,color=color.orange)  
var fibStopLine=line.new(0,0,0,0,color=color.red)    
var clear=color.new(color.white,100)


line.set_xy1(fib1,boxStart,Fib(0.382))
line.set_xy2(fib1,boxStart+25,Fib(0.382))
line.set_color(fib1,show ?color.green:clear)

line.set_xy1(fib2,boxStart,Fib(0.5))
line.set_xy2(fib2,boxStart+25,Fib(0.5))
line.set_color(fib2,show?color.blue:clear)

line.set_xy1(fib3,boxStart,Fib(0.618))
line.set_xy2(fib3,boxStart+25,Fib(0.618))
line.set_color(fib3,show?color.orange:clear)


line.set_xy1(fibStopLine,boxStart,patBottom-(Fib(0.618)-patBottom))
line.set_xy2(fibStopLine,boxStart+25,patBottom-(Fib(0.618)-patBottom))
line.set_color(fibStopLine,show?color.red:clear)

if useFibSetups==true
    if current==1
        box.set_left(targetBox,boxStart)
        box.set_top(targetBox,line.get_y1(fib1))
        box.set_right(targetBox,boxStart+25)
        box.set_bottom(targetBox,y1)

        box.set_left(stopBox,boxStart)
        box.set_top(stopBox,y1)
        box.set_right(stopBox,boxStart+25)
        box.set_bottom(stopBox,line.get_y1(fibStopLine))
    else if current==-1
        box.set_left(targetBox,boxStart)
        box.set_top(targetBox,y1)
        box.set_right(targetBox,boxStart+25)
        box.set_bottom(targetBox,line.get_y1(fib1))

        box.set_left(stopBox,boxStart)
        box.set_top(stopBox,line.get_y1(fibStopLine))
        box.set_right(stopBox,boxStart+25)
        box.set_bottom(stopBox,y1)

