Pinescript - Problem using ta.valuewhen for moving my stop loss based on criteria

Viewed 16

Help

I've been trying to get my stop loss to move based on certain criteria using valuewhen, not sure it is the best way to do it.

my problem line is:

stopvalue = ta.valuewhen(hmaCloseStatus and strategy_open and dmarkHighHold, low1, 0)

This stores the previous stop loss value - great

The good - if I open a new position and the previous stop level is below the new open my code works well.

The bad - if I open the new position and previous stop level is above the new open value then the open position immediatelt closes on the next bar.

It seems I need to reset the variable to be null but na doesn't work...

In the image below I've plotted the stop loss in blue, the two red arrows show the new long position being closed by the stop loss because the spike caused by the 0 in the value when... help

enter image description here

0 Answers
Related