Trailing stop on IB – Stop repositioning on DAX
Forums › ProRealTime English forum › ProOrder support › Trailing stop on IB – Stop repositioning on DAX
- This topic has 7 replies, 3 voices, and was last updated 3 months ago by
KumoNoJuzza.
-
-
08/04/2023 at 11:03 AM #218584
Hi,
I want to implement trailing stops in my strategies on DAX Futures (DXS and DXM) on IB. There is a limitation in terms of stop repositioning to 10 times, sometimes more but I would rather stick to 10.
I have found trailing stops codes but I am wondering how to set the numbers of iterations.
Therefore I would like to use a loop like For i=1 to 10… to take profit when stop is touched after the 10th repositioning.
Has anyone already coded this? Or maybe such topic already exists.
Thanks for your help.
08/04/2023 at 3:03 PM #218597Can you make an example?
08/04/2023 at 4:37 PM #218602Sure, it would look like this but I am not sure about the syntax/use:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748//trailing stop functiontrailingstart = 4 //trailing will start @trailingstart points profittrailingstep = 3 //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next 9 movesFor i = 1 to 9 DOIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFNEXTENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next 9 movesFor i = 1 to 9 DOIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFNEXTENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPELSIF newSL>0 THENSELL AT newSL LIMITEXITSHORT AT newSL LIMITELSIF newSL>0 THENSELL AT MarketEXITSHORT AT MARKETENDIF08/05/2023 at 8:03 AM #218614Hi
I think it’s better to use a “counter” (Step=Step+1) than a loop…
You can then just let the trailing stop do its job and stop after 10 steps…
1 user thanked author for this post.
08/06/2023 at 5:54 PM #218651The lines from 30 through the end are logically incorrect. You can’t use iterations that way. I can’t understand the goal you want to achieve.
Can you write an exemple using just text?
08/06/2023 at 9:38 PM #218652Yes @Roberto, I just want to code a trailing stop that moves the stop 10 times at most.
08/07/2023 at 5:32 PM #218692There you go (not tested):
123456789101112131415161718192021222324252627282930313233343536373839404142434445//trailing stop functiontrailingstart = 4 //trailing will start @trailingstart points profittrailingstep = 3 //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0count=0ENDIF//manage long positionsIF LONGONMARKET AND count < 10 THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next 9 movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFIF newSL <> newSL[1] thencount = count + 1ENDIFENDIF//manage short positionsIF SHORTONMARKET AND count < 10 THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next 9 movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFIF newSL <> newSL[1] thencount = count + 1ENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT MarketEXITSHORT AT MARKETENDIF08/07/2023 at 10:59 PM #218703Thanks. I will test it tomorrow.
-
AuthorPosts
Find exclusive trading pro-tools on