Hi there!
Yes I know – the topic stop loss has been covered hundreds of times in this space, but I didn’t found a code snipped matching my needs.
So I am looking for a simple code snipped which does the following:
- after entering a long/short position, the stop is placed for a long position (3 candles back low -1 tick) and for short (3 candles back high + 1 tick)
- as long as the trade is active the stop is updated accordingly (always 3 candles back +/- 1 tick)
I tried to code it by myself, but I failed – shame on me – I know 🙂
So is there anyone who solved this problem already or likes to write it?
Many thanks in advance.
Not tested:
IF MyLongConditions AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
NewSL = low[3]-1*pipsize
SELL AT NewSL STOP
ENDIF
//
IF MyShortConditions AND Not OnMarket THEN
SELLSHORT 1 CONTRACT AT MARKET
NewSL = high[3]+1*pipsize
EXITSHORT AT NewSL STOP
ENDIF
//
IF OnMarket THEN
IF PositionPerf > 0 THEN
IF LongOnMarket THEN
NewSL = max(newSL,low[3]-1*pipsize)
ELSE
NewSL = min(newSL,high[3]+1*pipsize)
ENDIF
ENDIF
IF NewSL > 0 THEN
SELL AT NewSL STOP
EXITSHORT AT NewSL STOP
ENDIF
ENDIF
Link to above added as Log 261 here …
Snippet Link Library