Hi.
I try to adjust my stoploss to my second closest supertrend to the price. But I can’t.
Does anyone have an idea?
Thank you
DEFPARAM FLATAFTER = 224500
DEFPARAM CUMULATEORDERS = False
once startt = 000000
once endt = 220000
// SUPERTREND
st1 = Supertrend[1,10]
st2 = Supertrend[2,11]
st3 = Supertrend[3,12]
// SUPERTREND CONDITIONS
stl = 0
sts = 0
IF st1 < close AND st2 < close THEN
stl = 1
ENDIF
IF st1 < close AND st3 < close THEN
stl = 2
ENDIF
IF st2 < close AND st3 < close THEN
stl = 3
ENDIF
IF st1 < close AND st2 < close AND st3 < close THEN
stl = 4
ENDIF
// SOTP LOSS CONDITIONS
IF stl = 1 AND st1 < st2 THEN
sll = st1
ELSE
sll = st2
ENDIF
IF stl = 2 AND st1 < st3 THEN
sll = st1
ELSE
sll = st3
ENDIF
IF stl = 3 AND st2 < st3 THEN
sll = st2
ELSE
sll = st3
ENDIF
IF stl = 4 THEN
sll = st2
ENDIF
// POSITIONS CONDITIONS
IF time >= startt AND time <= endt THEN
IF not onmarket AND stl >= 1 THEN
buy 1 contract at market
set stop loss (tradeprice - sll)
SET TARGET PPROFIT 45
ENDIF
I suggest computing the middle of their range like that, for line 54:
set stop loss ((tradeprice - ((st1 + st2 + st3) / 3))
There’s a major issue, TRADEPRICE is not known when BUY is executed, until the next candle.
Due to this it will retain the exit price of the prior trade or 0 the very first time making the stop loss negative.
To overcome this I’d change line 54 using the current price and saving the ST value for a later use:
MyST = (st1 + st2 + st3) / 3)
set stop loss (close - MyST)
then I would append these lines at the end, for the next candle:
If OnMarket and Not OnMarket[1] then
set stop loss (tradeprice - MyST)
Endif
This will grant you an initial stop loss, making it more accurate next candle.