Trouble with trail offset in pinescript strategy

Viewed 14

I am having trouble getting the trailing stop to function correctly. i have a fixed stop, but want a trailing stop to trigger after a certain profit objective price is reached. the trailing stop should be the highest atr value since the target was hit. however, it seems to exit as soon as the target is hit instead. thank you in advance.

in the picture below you can see the profit objective plotted by the green line.

strategy("new trail trial", overlay=true)

//get user inputs
channelLength = input.int(defval=20)
atrLength = input.int(defval=10)
atrStopMultiplier = input.int(defval=1)
atrTpMultiplier = input.int(defval=4)

//get values
channelHigh = ta.highest(high, channelLength)
atr = ta.atr(atrLength)

//get breakouts
bullBreakout = close > channelHigh[1]

//calc stop and tp values
var t_stop = 0.0
var t_target = 0.0
longStop = close - (ta.atr(atrLength) * atrStopMultiplier)
longTarget = close + (ta.atr(atrLength) * atrTpMultiplier)
longTrail = close[1] - (ta.atr(atrLength)[1] * atrStopMultiplier)/syminfo.mintick


//detect signals
longSignal = strategy.position_size <= 0 and barstate.isconfirmed and bullBreakout and not na(channelHigh) and not na(atr)

//detect long Entries
if strategy.position_size == 0 and longSignal
    t_stop := longStop
    t_target := longTarget
    strategy.entry(id="long", direction=strategy.long)
    strategy.exit(id="long exit", from_entry="long", stop=t_stop, trail_price=longTarget, trail_offset=longTrail)

//draw data to chart
plot(strategy.position_size == 0 ? channelHigh : na, color=color.yellow, style=plot.style_linebr, linewidth=2)
plot(t_stop, color=strategy.position_size > 0 ? color.red : color.new(color.black, 100))
plot(t_target, color=strategy.position_size > 0 ? color.green : color.new(color.black, 100))
plotshape(strategy.position_size == 0 ? bullBreakout : na, color=color.green, location=location.belowbar, style=shape.triangleup, size=size.normal) 

trade example

0 Answers
Related