'SL, TP and trailing stop

Im new to coding and Pine Script and I know this is a very noob question,

However im stuck, for some reason im unable to find anything with exactly what i want,

I would like to set SL and TP based on pips, and a trailing stop based on ATR and have the "user inputs" available for them all,

this is what im trying to do and below is one of the many tries i have made, I know its a mess.

please help and thank you :)

// Calculate stops & targets

SLl = input.int(defval= syminfo.mintick -10, title="SLl", group="Long")
TPl = input.int(defval= syminfo.mintick +10, title="TPl", group="Long")
SLs = input.int(defval= syminfo.mintick +10, title="SLs", group="short")
TPs = input.int(defval= syminfo.mintick -10, title="TPl", group="short")
var stoploss1 = SLl 
var takeprofit1 = TPl
var stoploss2 = SLs 
var takeprofit2 = TPs


// Enter buy orders
if buySignal
    strategy.entry(id="Long", direction=strategy.long)
    stoploss1 := SLl
    takeprofit1 := TPl

// Enter sell orders
if sellSignal
    strategy.entry(id="Short", direction=strategy.short)
    stoploss2 := SLs
    takeprofit2 := TPs

// Manage exit orders (TP & SL)
strategy.exit(id="Long Exit", from_entry="Long", limit=takeprofit1, stop=stoploss1, when=strategy.position_size > 0)
strategy.exit(id="Short Exit", from_entry="Short", limit=takeprofit1, stop=stoploss1, when=strategy.position_size < 0)
strategy.exit(id="Long Exit", from_entry="Long", limit=takeprofit2, stop=stoploss2, when=strategy.position_size > 0)
strategy.exit(id="Short Exit", from_entry="Short", limit=takeprofit2, stop=stoploss2, when=strategy.position_size < 0)

// Draw data to chart
plotshape(buySignal, style=shape.triangleup, color=color.green, location=location.belowbar)
plotshape(sellSignal, style=shape.triangledown, color=color.red, location=location.abovebar)


plot(strategy.position_size != 0 ? stoploss1 : na, color=color.red, style=plot.style_circles)
plot(strategy.position_size != 0 ? takeprofit1 : na, color=color.green, style=plot.style_circles)
plot(strategy.position_size != 0 ? stoploss2 : na, color=color.red, style=plot.style_circles)
plot(strategy.position_size != 0 ? takeprofit2 : na, color=color.green, style=plot.style_circles)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source