Take profit and Stop Loss ATR HL [Tcs] | ALGO
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zendrer

//@version=5
// One of the most famous SL or TP indicator developed on 3 levels, both long and short, to semplify the visualization of the target or multiple targets.

indicator(title="TCS • TP/SL ATR+HL", shorttitle= "TCS•TP/SL", overlay=true)

//•••••••••••••
//INPUT
//•••••••••••••
//ATR + LEVELS
//----------------------------------------------------------------
type = input.string(title='SL/TP', defval='ATR', options=['ATR', 'H-L'], group = "TP/SL")
atrlen = input(14, 'ATR•LEN', group = "ATR TP/SL")
smoothing = input.string(title='ATR•Smoothing', defval='WMA', options=['RMA', 'SMA', 'EMA', 'WMA'], group = "ATR TP/SL")
lines = input(true, "SHOW LEVELS", group = "ATR TP/SL")
bands = input(true, "SHOW BANDS", group = "ATR TP/SL")
labels = input(true, "SHOW LABELS", group = "ATR TP/SL")
SSLx = input(true, "", inline="1", group = "ATR TP/SL")
SSL = input.float(2.01, 'SHORT SL•Mlt', step=0.1, inline="1", group = "ATR TP/SL")
LSLx = input(true, "", inline="2", group = "ATR TP/SL")
LSL = input.float(2.01, 'LONG SL•Mlt', step=0.1, inline="2", group = "ATR TP/SL")
LTPx = input(true, "", inline="3", group = "ATR TP/SL") 
LTP = input.float(2, 'LONG TP•Mlt 1', step=0.1, inline="3", group = "ATR TP/SL")
LTP1x = input(false, "", inline="4", group = "ATR TP/SL") 
LTP1 = input.float(3, 'LONG TP•Mlt 2', step=0.1, inline="4", group = "ATR TP/SL")
LTP2x = input(false, "", inline="5", group = "ATR TP/SL") 
LTP2 = input.float(4, 'LONG TP•Mlt 3', step=0.1, inline="5", group = "ATR TP/SL") 
STPx = input(true, "", inline="6", group = "ATR TP/SL") 
STP = input.float(2, 'SHORT TP•Mlt 1', step=0.1, inline="6", group = "ATR TP/SL")
STP1x = input(false, "", inline="7", group = "ATR TP/SL") 
STP1 = input.float(3, 'SHORT TP•Mlt 2', step=0.1, inline="7", group = "ATR TP/SL")
STP2x = input(false, "", inline="8", group = "ATR TP/SL") 
STP2 = input.float(4, 'SHORT TP•Mlt 3', step=0.1, inline="8", group = "ATR TP/SL")
//----------------------------------------------------------------

//----------------------------------------------------------------
// STRUCTURE H/L
//----------------------------------------------------------------
lB  = input(20, title = "LOOKBACK", group = "HIGH-LOW TP/SL")
hx = input(true, "HIGH",  group = "HIGH-LOW TP/SL") 
lx = input(true, "LOW",  group = "HIGH-LOW TP/SL") 
rB  = 1
//----------------------------------------------------------------

//----------------------------------------------------------------
//CRYPTO RISK CALC
//----------------------------------------------------------------
risk = input(3,"ACCOUNT RISK % ", group = "ACCOUNT RISK - CRYPTO")
account = input(10000,"ACCOUNT AMOUNT ", group = "ACCOUNT RISK - CRYPTO")
fees = input(0.07,"BROKER FEES ENTRY %", group = "ACCOUNT RISK - CRYPTO")
feesex = input(0.07,"BROKER FEES EXIT %", group = "ACCOUNT RISK - CRYPTO")
//----------------------------------------------------------------

//•••••••••••••
//FUNCTION
//•••••••••••••
//ATR
//----------------------------------------------------------------
ma_function(source, atrlen) =>
    if smoothing == 'RMA'
        ta.rma(source, atrlen)
    else
        if smoothing == 'SMA'
            ta.sma(source, atrlen)
        else
            if smoothing == 'EMA'
                ta.ema(source, atrlen)
            else
                ta.wma(source, atrlen)
atr_slen = ma_function(ta.tr(true), atrlen)
//----------------------------------------------------------------

//ATR Up/Low Bands
//----------------------------------------------------------------
xLTP= LSL*LTP
xLTP1= LSL*LTP1
xLTP2= LSL*LTP2
xSTP= SSL*STP
xSTP1= SSL*STP1
xSTP2= SSL*STP2

SL_L=close - atr_slen * LSL
SL_S= atr_slen * SSL + close

TP_L1= atr_slen * xLTP + close
TP_L2= atr_slen * xLTP1 + close
TP_L3= atr_slen * xLTP2 + close

TP_S1= close - atr_slen * xSTP
TP_S2= close - atr_slen * xSTP1
TP_S3= close - atr_slen * xSTP2
//----------------------------------------------------------------

//HIGH-LOW
//----------------------------------------------------------------
hP = fixnan(ta.pivothigh(lB, rB)[1])
lP = fixnan(ta.pivotlow(lB, rB)[1])
//----------------------------------------------------------------

//SIZE CALC LONG
//----------------------------------------------------------------
changesl = (((close/SL_L)-1)*100)+fees 
accval = risk*account
relative = (changesl*accval)/risk
accquant = accval/relative
//----------------------------------------------------------------
//SIZE CALC SHORT
//----------------------------------------------------------------
changesl1 = (((SL_S/close)-1)*100)+fees 
accval1 = risk*account
relative1 = (changesl1*accval1)/risk
accquant1 = accval1/relative1
//----------------------------------------------------------------


//•••••••••••••
//PLOT
//•••••••••••••
//BANDS
//----------------------------------------------------------------
u = plot(SL_L, 'LONG SL•Mlt', color =  type == 'ATR' ?(bands ? (LSLx ? color.new(color.white, 40) : na) : na) :na , style=plot.style_stepline)
l = plot(SL_S, 'SHORT SL•Mlt', color =  type == 'ATR' ?(bands ? (SSLx ? color.new(color.white, 40) : na) : na) :na , style=plot.style_stepline)
u1 = plot(TP_L1, 'LONG TP•Mlt 1', color =  type == 'ATR' ?(bands ? (LTPx ? color.new(color.lime, 20) : na) : na) :na , style=plot.style_stepline)
u2 = plot(TP_L2, 'LONG TP•Mlt 2', color =  type == 'ATR' ?(bands ? (LTP1x ? color.new(color.lime, 40) : na) : na) :na , style=plot.style_stepline)
u3 = plot(TP_L3, 'LONG TP•Mlt 3', color =   type == 'ATR' ?(bands ? (LTP2x ? color.new(color.lime, 60) : na) : na) :na , style=plot.style_stepline)
l1 = plot(TP_S1, 'SHORT TP•Mlt 1', color =  type == 'ATR' ?(bands ? (STPx ? color.new(color.purple, 20) : na) : na) :na , style=plot.style_stepline)
l2 = plot(TP_S2, 'SHORT TP•Mlt 2', color =  type == 'ATR' ?(bands ? (STP1x ? color.new(color.purple, 40) : na) : na) :na , style=plot.style_stepline)
l3 = plot(TP_S3, 'SHORT TP•Mlt 3', color = type == 'ATR' ?(bands ? (STP2x ? color.new(color.purple, 60) : na) : na) :na , style=plot.style_stepline)
//----------------------------------------------------------------

//PLOT HL
//----------------------------------------------------------------
Hl = type == 'H-L' ? line.new(bar_index+1,hP,bar_index+9,hP, xloc = xloc.bar_index, extend = extend.none, color = lines ? ( hx ? color.new(color.lime, 0) :na) : na, style = line.style_dashed) : na
Hll = type == 'H-L' ? (labels and hx ? label.new(bar_index+9, hP, color = color.new(color.white,100) , style = label.style_label_left, text="H-SL | "+str.tostring(hP), textcolor = color.lime, textalign = text.align_left) : na) : na
line.delete(Hl[1])
label.delete(Hll[1])

Ll = type == 'H-L' ? line.new(bar_index+1,lP,bar_index+9,lP, xloc = xloc.bar_index, extend = extend.none, color =  lines ? ( lx ? color.new(color.purple, 0) :na) : na, style = line.style_dashed) : na 
Lll = type == 'H-L' ? (labels and lx ? label.new(bar_index+9, lP, color = color.new(color.white,100) , style = label.style_label_left, text="L-SL | "+str.tostring(lP), textcolor = color.purple, textalign = text.align_left) : na) : na
line.delete(Ll[1])
label.delete(Lll[1])
//plot(hP, color=ta.change(hP) ? na : color.lime,  linewidth=1, offset=-(rB+1), title="HIGH",style=plot.style_line)
//plot(lP, color=ta.change(lP) ? na : color.lime,  linewidth=1, offset=-(rB+1), title="LOW",style=plot.style_line)
//----------------------------------------------------------------
//-----
//LINE
//----------------------------------------------------------------
ul = type == 'ATR' ? line.new(bar_index+1,SL_L[1],bar_index+9,SL_L[1], xloc = xloc.bar_index, extend = extend.none, color = lines ? (LSLx ? color.new(color.white, 40) : na) : na, style = line.style_dashed) : na
ull = type == 'ATR' ? (labels and LSLx ?  label.new(bar_index+9, SL_L[1], color = color.new(color.white,100) , style = label.style_label_left, text="L-SL | "+str.tostring(LSL[1])+" | "+str.tostring(SL_L[1]), textcolor = color.gray, textalign = text.align_left) : na) : na
line.delete(ul[1])
label.delete(ull[1])

ll = type == 'ATR' ? line.new(bar_index+1,SL_S[1],bar_index+9,SL_S[1], xloc = xloc.bar_index, extend = extend.none, color = lines ? (SSLx ? color.new(color.white, 40) : na) : na, style = line.style_dashed) : na
lll = type == 'ATR' ? (labels and SSLx ? label.new(bar_index+9, SL_S[1], color =color.new(color.white,100), style = label.style_label_left, text="S-SL | "+str.tostring(SSL[1])+" | "+str.tostring(SL_S[1]), textcolor = color.gray, textalign = text.align_left) : na) : na
line.delete(ll[1])
label.delete(lll[1])

u1l = type == 'ATR' ? line.new(bar_index+1,TP_L1[1],bar_index+9,TP_L1[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (LTPx ? color.new(color.lime, 20) : na) : na, style = line.style_dashed) : na
u1ll = type == 'ATR' ? (labels and LTPx ? label.new(bar_index+9, TP_L1[1], color =color.new(color.white,100), style = label.style_label_left, text="L-TP1 | "+str.tostring(LTP[1])+" | "+str.tostring(TP_L1[1]), textcolor = color.new(color.lime, 20), textalign = text.align_left) : na) : na
line.delete(u1l[1])
label.delete(u1ll[1])

u2l = type == 'ATR' ? line.new(bar_index+1,TP_L2[1],bar_index+9,TP_L2[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (LTP1x ? color.new(color.lime, 40) : na) : na, style = line.style_dashed) : na
u2ll = type == 'ATR' ? (labels and LTP1x ? label.new(bar_index+9, TP_L2[1], color =color.new(color.white,100), style = label.style_label_left, text="L-TP2 | "+str.tostring(LTP1[1])+" | "+str.tostring(TP_L2[1]), textcolor = color.new(color.lime, 20), textalign = text.align_left) : na) : na
line.delete(u2l[1])
label.delete(u2ll[1])

u3l = type == 'ATR' ? line.new(bar_index+1,TP_L3[1],bar_index+9,TP_L3[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (LTP2x ? color.new(color.lime, 60) : na) : na, style = line.style_dashed) : na
u3ll = type == 'ATR' ? (labels and LTP2x ? label.new(bar_index+9, TP_L3[1], color =color.new(color.white,100), style = label.style_label_left, text="L-TP3 | "+str.tostring(LTP2[1])+" | "+str.tostring(TP_L3[1]), textcolor = color.new(color.lime, 20), textalign = text.align_left) : na) : na
line.delete(u3l[1])
label.delete(u3ll[1])

l1l = type == 'ATR' ? line.new(bar_index+1,TP_S1[1],bar_index+9,TP_S1[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (STPx ? color.new(color.purple, 20) : na) : na, style = line.style_dashed) : na
l1ll = type == 'ATR' ? (labels and STPx ? label.new(bar_index+9, TP_S1[1], color =color.new(color.white,100), style = label.style_label_left, text="S-TP1 | "+str.tostring(STP[1])+" | "+str.tostring(TP_S1[1]), textcolor = color.new(color.purple, 20), textalign = text.align_left) : na) : na
line.delete(l1l[1])
label.delete(l1ll[1])

l2l = type == 'ATR' ? line.new(bar_index+1,TP_S2[1],bar_index+9,TP_S2[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (STP1x ? color.new(color.purple, 40) : na) : na, style = line.style_dashed) : na
l2ll = type == 'ATR' ? (labels and STP1x ? label.new(bar_index+9, TP_S2[1], color =color.new(color.white,100), style = label.style_label_left, text="S-TP2 | "+str.tostring(STP1[1])+" | "+str.tostring(TP_S2[1]), textcolor = color.new(color.purple, 40), textalign = text.align_left) : na) : na
line.delete(l2l[1])
label.delete(l2ll[1])

l3l = type == 'ATR' ? line.new(bar_index+1,TP_S3[1],bar_index+9,TP_S3[1] , xloc = xloc.bar_index, extend = extend.none, color = lines ? (STP2x ? color.new(color.purple, 60) : na) : na, style = line.style_dashed) : na
l3ll = type == 'ATR' ? (labels and STP2x ? label.new(bar_index+9, TP_S3[1], color =color.new(color.white,100), style = label.style_label_left, text="S-TP3 | "+str.tostring(STP2[1])+" | "+str.tostring(TP_S3[1]), textcolor = color.new(color.purple, 60), textalign = text.align_left) : na) : na
line.delete(l3l[1])
label.delete(l3ll[1])

accqty = type == 'ATR' ? (labels and (LSLx or SSLx) ? label.new(bar_index+9, close[1], color =color.new(color.white,100), style = label.style_label_left, text="QTY x "+ "|LONG|" +str.tostring(accquant,"#.##") + " • |SHORT|" +str.tostring(accquant1,"#.##"), textcolor = color.new(color.white, 20), textalign = text.align_left) : na) : na
label.delete(accqty[1])

//accqty1 = type == 'ATR' ? (labels and (LSLx or SSLx) ? label.new(bar_index+9, close[1], color =color.new(color.white,100), style = label.style_label_left, text="QTY. SHORT | "+ " x " +str.tostring(accquant1), textcolor = color.new(color.white, 20), textalign = text.align_left) : na) : na
//label.delete(accqty1[1])