Forums › ProRealTime English forum › ProOrder support › Trailing Stop and Breakeven codes › Reply To: Trailing Stop and Breakeven codes
05/01/2020 at 10:45 AM
#129176
Hi @Paul — this is my preferred trailing code. Should I have ‘not onmarket’ in the entry conditions? I’ve tried it with and without but back test results are identical.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
//%trailing stop function trailingPercent = tst stepPercent = st if onmarket then trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss endif //reset the stoploss value IF NOT ONMARKET THEN newSL=0 ENDIF //manage long positions IF LONGONMARKET THEN //first move (breakeven) IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN newSL = tradeprice(1)+trailingstep ENDIF //next moves IF newSL>0 AND close-newSL>trailingstep THEN newSL = newSL+trailingstep ENDIF ENDIF //manage short positions IF SHORTONMARKET THEN //first move (breakeven) IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN newSL = tradeprice(1)-trailingstep ENDIF //next moves IF newSL>0 AND newSL-close>trailingstep THEN newSL = newSL-trailingstep ENDIF ENDIF //stop order to exit the positions IF newSL>0 THEN SELL AT newSL STOP EXITSHORT AT newSL STOP ENDIF |