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 23 replies, 5 voices, and was last updated 5 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.
04/23/2024 at 1:40 PM #231846Hi Kumo, I see you’re using Coded trailing stop on IB. I’m using in papermode some systems on IB. does this coded trailing stop works for you? does it happen to you some errors when it goes to execute the stop loss ?
thank you in advance
ALessio
04/23/2024 at 2:06 PM #231849I think Roberto made a typo at the end of his trailing stop…
If newSL>0 then
Sell at newSL STOP
ExitShort at newSL STOP
EndIf
When it happens “at Market” then after the first step the newSL>0 and everything is sold “at Market”… (after the first step)
1 user thanked author for this post.
04/23/2024 at 2:37 PM #231856Well spotted JS 🙂
04/23/2024 at 2:51 PM #231859Hi Roberto,
Maybe you can help me too… 🙂
As can be read regularly on the forum, the use of “pending orders” is a bad combination between PRT and IG/IB. Often things go wrong (on the broker’s side?) with the execution of these orders, for example that the broker cannot confirm the status of the order…
This is incredibly frustrating, and you also miss a lot of transactions…
Is there nothing that can be done about this now… Are there no alternatives?
I also tried it with the new trading instructions (Set Stop Price) but unfortunately with the same outcome…
04/23/2024 at 3:16 PM #231860you mean I have to replace:
//stop order to exit the positionsIF newSL>0 THENSELL AT MarketEXITSHORT AT MARKETENDIFwith this:If newSL>0 then
Sell at newSL STOP
ExitShort at newSL STOP
EndIf
correct?
sorry I don’t understand what you mean with:
When it happens “at Market” then after the first step the newSL>0 and everything is sold “at Market”… (after the first step)
04/23/2024 at 3:29 PM #231861That’s right Aragorna, you have to replace that part…
What I meant is when the newSL>0 then in Roberto’s code everything is sold “at Market”…
If newSL>0 then
Sell at Market
…
Of course, that’s not the intention because we want to trail with a STOP order… (Trailing Stop)
04/23/2024 at 6:28 PM #231865Hi Roberto,
Maybe you can help me too… 🙂
As can be read regularly on the forum, the use of “pending orders” is a bad combination between PRT and IG/IB. Often things go wrong (on the broker’s side?) with the execution of these orders, for example that the broker cannot confirm the status of the order…
This is incredibly frustrating, and you also miss a lot of transactions…
Is there nothing that can be done about this now… Are there no alternatives?
I also tried it with the new trading instructions (Set Stop Price) but unfortunately with the same outcome…
You are right, there’s not much you (as well as anyone else) can do to change this, but to stick to the broker’s minimum distance requirements and adding some extra points to accomodate volatility changes. I trade mainly DAX, for which IG usually require 6 pips; since it’s not possible to know when that distance will rise, I use a minimun distance of 10 pips. Now I rarely experience such nuisances on my demo account and I never experienced any on my real account.
If IG would enable users to know such kind of requirements, as well as spread etc…, it would be highly welcome by all of us!
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on