0

I would like to set my stop-loss at the low of my entry bar.

what is the condition I need to use ?

I try

if strategy.position_size < 0
    strategy.exit(id="Close Short", stop=low[1])

but this my stop-loss change everytime

2 Answers2

0

You are recalculating low on each bar where strategy.position_size < 0.

You want to calculate low only after you've entered the position. Try:

if ta.change(strategy.position_size) 
    strategy.exit(id = "Close Short", stop = low)
mr_statler
  • 1,913
  • 2
  • 3
  • 14
0

It's because you are updating your exit order on each bar you are in a trade with low[1]. What you want to have is a constant SL price.

Store your SL target when you enter a trade in a var variable. Then use it in your exit call.

is_new_pos = (strategy.position_size[1] == 0 and strategy.position_size != 0) or ((strategy.position_size[1] * strategy.position_size) < 0)

var float sl_price = na
sl_price := is_new_pos ? low[1] : sl_price // If it is a new position update the SL price, else keep the old value

if strategy.position_size < 0
    strategy.exit(id="Close Short", stop=sl_price)
vitruvius
  • 15,740
  • 3
  • 16
  • 26