Pine Script TakeProfit & StopLoss Problem

42 Views Asked by At

In this strategy sometimes TP(%0.10) comes on the same candle I enter in but the trade closes on the next candle, how can I fix this PLEASE HELP and my tp and sl code is correct right I cant find a code for tp(%0.10) and sl(%1)

so for example here

//@version=5
strategy("BB Bot", overlay=true)

source = close
length = input.int(165, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)

basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)

upper = basis + dev
lower = basis - dev

buyEntry = ta.crossover(source, lower)
sellEntry = ta.crossunder(source, upper)

if (ta.crossover(source, lower))
    strategy.entry("BBandLE", strategy.long, stop=lower, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="Long")
else
    strategy.cancel(id="BBandLE")

if (ta.crossunder(source, upper))
    strategy.entry("BBandSE", strategy.short, stop=upper, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="Short")
else
    strategy.cancel(id="BBandSE")

//tp = 0.10
//sl = 1

//longprofit = strategy.position_avg_price * (1+tp)
//longstop = strategy.position_avg_price * (1-sl)

longprofit = strategy.position_avg_price * 0.10 / 100
longstop = strategy.position_avg_price * 1 / 100 

if strategy.position_avg_price > 0 
    strategy.exit(id = "Close", stop = longstop, limit = longprofit)
if strategy.position_avg_price < 0 
    strategy.exit(id = "Close", stop = longstop, limit = longprofit)
1

There are 1 best solutions below

0
Gu5tavo71 On

Sometimes it's not a script problem. You just have to change the setting.

Try Recalculate

enter image description here