//trailing stop function
trailingstart = 10 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//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>close[1] 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
//newSL = close + (1 * trailingstep)
ENDIF
//next moves
IF newSL>0 AND close<close[1] THEN
newSL = newSL-trailingstep
//newSL = close + (1 * trailingstep)
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
Hi
In this trailing stop function, what is the purpose of STOP in the last commands SELL AT newSL STOP and EXITSHORT AT newSL STOP? Is it a stop loss command or does it stop the code from continuing to run the commands after STOP if the conditions are met?
Thanks
Sachin
It is a pending STOP order.
Hi Nicholas
Thanks for clarifying.
Does the code in the below page of the blog incorporate a minimum stop distance required by the broker? e.g. a minimum stop distance of 10 points for any instrument?
https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
Thanks
Sachin
This is Nicolas’ code with the addition of distance and a delayed execution https://www.prorealcode.com/topic/problemi-con-broker-ig-riguardo-lutilizzo-di-algo-con-trailing-stop-e-trailing/#post-175071
Distance is at line 9.
Delayed execution is at line 6, just replace 0 with any number to delay execution for N bars from entry.
Dear Roberto,
this code for trailing stop works also for Interactive broker?
kind regards
Alessio
I don’t know, as I don’t use IB.
If the ProRealTime language is wholly supported, it should, but that’s just speculation.