Movable Stop + Trail + Alert
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © coocoolikoo
//@version=5
indicator(title="movable SL and Trail + alert",shorttitle="Movable STOP + Trail + Alert",overlay=true)
y = input.price(0,'Entry',group='Lines', inline=  'Entry', confirm=true)
yBarTime = input.time(0,'',group='Lines', inline=  'Entry', confirm=true)
x = input.price(0,'Stop',group='Lines', inline=  'Stop', confirm=true)
xBarTime = input.time(0,'',group='Lines', inline=  'Stop', confirm=true)

//xTime = input.time(0,'',group='XABC', inline=  '1', confirm=true)
activetrail = input.bool(defval=false, title = "Trail Stop Loss ",confirm=false)
activehalftp = input.bool(defval=false, title = "half target ",confirm=false)
lookback = input.int(defval=10, title="trail lookback", step = 1)
RR = input.int(defval=1, title="RR", step = 1)
atr = ta.atr(14)
/////
/////////////
stopsize=(y-x)
var stop = 0.0
var tp = 0.0
var tp1 = 0.0
if time > xBarTime
    stop := ta.valuewhen(time > xBarTime,x,1)
    tp := (stopsize* RR) +y
    if activehalftp
        tp1 := (stopsize* RR/2) +y
    else
        tp1 := na
////////////////

//////////////
var trailprice = 0.0
traillong = ta.lowest(low,lookback) - atr
trailshort = ta.highest(high,lookback) + atr
// Check for trailing stop update
if time >= xBarTime and barstate.isconfirmed and activetrail and (y>x)
    // Trail long 
    if (trailprice < traillong or trailprice==0)
        trailprice := traillong

if time >= xBarTime and barstate.isconfirmed and activetrail and (y<x)
    // Trail short
    if (trailprice > trailshort or trailprice==0)
        trailprice := trailshort
////////////////
if trailprice != 0.0 and (low <= trailprice) and (y>x)
    trailprice := na

if trailprice != 0.0 and high >= trailprice and (y<x)
    trailprice := na


colorT = (x<y) and (trailprice < x) ? na : (x<y) and (trailprice > x) ? color.red : (x > y) and (trailprice > x) ? na : (x>y) and (trailprice < x) ? color.red : na
p1=plot((time > xBarTime ) ? x : na ,title="Stop Loss" ,color=color.red,style=plot.style_linebr)
p2=plot(time > yBarTime ? y : na ,title="Entry", color=color.gray,style=plot.style_linebr)
p3=plot(time > xBarTime ? tp : na ,title="Target", color=color.green,style=plot.style_linebr)
p5=plot(time > xBarTime ? tp1 : na ,title="Target", color=color.green,style=plot.style_linebr)
p4=plot((time > xBarTime) and activetrail ? trailprice : na ,title="trail stop long", color=colorT,style=plot.style_linebr)

//////ALERTS
if (y>x)
    if (low < x)
        alert("STOP REACHEAD !" + str.tostring(low), alert.freq_once_per_bar)
    if (high > tp)
        alert("TARGET REACHED !"+ str.tostring(high), alert.freq_once_per_bar)
    if activehalftp and (high > tp1)
        alert("HALF TARGET REACHED !"+ str.tostring(high), alert.freq_once_per_bar)
    if activetrail and (low < trailprice)
        alert("Price (" + str.tostring(low) + ") Hit the Trail stop.", alert.freq_once_per_bar)
if (y<x)
    if (high > x)
        alert("STOP REACHEAD !" + str.tostring(low), alert.freq_once_per_bar)
    if (low < tp)
        alert("TARGET REACHED !"+ str.tostring(high), alert.freq_once_per_bar)
    if activehalftp and (low < tp1)
        alert("HALF TARGET REACHED !"+ str.tostring(high), alert.freq_once_per_bar)
    if activetrail and (high > trailprice)
        alert("Price (" + str.tostring(high) + ") Hit the Trail stop.", alert.freq_once_per_bar)

StoplossPer = (y-x)/y*100
TargetPer   = StoplossPer*RR
///table
drawTable = input.bool(true, "On/Off Table")
Xvalue = str.format("{0,number,currency}", x)
Yvalue = str.format("{0,number,currency}", y)
TPvalue = str.format("{0,number,currency}", tp)
HTPvalue = str.format("{0,number,currency}", tp1)
SLP = str.format("{0,number,#.#}", StoplossPer)
TPP = str.format("{0,number,#.#}", TargetPer)
var testTable = table.new(position = position.top_right, columns = 2, rows = 10 , bgcolor = color.rgb(0,8,12,100), border_width = 1)
if barstate.islast and drawTable
    table.cell(table_id = testTable, column = 0, row = 0, text = "RR : " + str.tostring(RR),bgcolor=color.rgb(139, 137, 140,35),text_color=color.white)
    table.cell(table_id = testTable, column = 0, row = 2, text = "SL : " + str.tostring(Xvalue), bgcolor=color.rgb(255, 0, 0,35),text_color=color.white)
    table.cell(table_id = testTable, column = 0, row = 3, text = "E : " + str.tostring(Yvalue), bgcolor=color.rgb(139, 137, 140,35),text_color=color.white)
    table.cell(table_id = testTable, column = 0, row = 4, text = "TP : " + str.tostring(TPvalue) , bgcolor=color.rgb(66, 255, 0,35),text_color=color.white)


    if activetrail
        table.cell(table_id = testTable, column = 0, row = 1, text = "Trail on " , bgcolor=color.rgb(255, 0, 0,35),text_color=color.white)
    if activehalftp
        table.cell(table_id = testTable, column = 0, row = 5, text = "HalfTP " + str.tostring(HTPvalue), bgcolor=color.rgb(66, 255, 0,35),text_color=color.white)

////label
labelon = input.bool(defval=true, title = "Labels ",confirm=false)
SLper = ((y-x)/y)*100
TPper = SLper * RR
HalfTPper = SLper * RR / 2

if y < x
    SLper := -1 * SLper
    TPper := -1 * TPper
    HalfTPper := -1 * HalfTPper
s1 = str.format("{0,number,#.#}", SLper) // returns: 1.3
s2 = str.format("{0,number,#.#}", TPper) // returns: 1.3
s3 = str.format("{0,number,#.#}", HalfTPper) // returns: 1.3

if labelon
    lab1 = label.new(bar_index, close, text=s2+"%", style=label.style_label_left,textcolor=color.white,tooltip="TP %")
    label.set_xloc(lab1, time, xloc.bar_time)
    label.set_color(lab1, color=color.new(color.green,50))
    label.set_size(lab1, size.normal)
    label.set_y(lab1, tp)
    label.delete(lab1[1])
    ///SL
    lab2 = label.new(bar_index, close, text=s1+"%", style=label.style_label_left,textcolor=color.white,tooltip="SL %")
    label.set_xloc(lab2, time, xloc.bar_time)
    label.set_color(lab2, color=color.new(color.red,50))
    label.set_size(lab2, size.normal)
    label.set_y(lab2, x)
    label.delete(lab2[1])
    if activehalftp
        ///halftp
        lab3 = label.new(bar_index, close, text=s3+"%", style=label.style_label_left,textcolor=color.white,tooltip="HalfTP %")
        label.set_xloc(lab3, time, xloc.bar_time)
        label.set_color(lab3, color=color.new(color.green,50))
        label.set_size(lab3, size.normal)
        label.set_y(lab3, tp1)
        label.delete(lab3[1])