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.
Sure, it would look like this but I am not sure about the syntax/use:
//trailing stop function
trailingstart = 4 //trailing will start @trailingstart points profit
trailingstep = 3 //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*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next 9 moves
For i = 1 to 9 DO
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
NEXT
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next 9 moves
For i = 1 to 9 DO
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
NEXT
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ELSIF newSL>0 THEN
SELL AT newSL LIMIT
EXITSHORT AT newSL LIMIT
ELSIF newSL>0 THEN
SELL AT Market
EXITSHORT AT MARKET
ENDIF
JSParticipant
Senior
Hi
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…
The 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?
Yes @Roberto, I just want to code a trailing stop that moves the stop 10 times at most.
There you go (not tested):
//trailing stop function
trailingstart = 4 //trailing will start @trailingstart points profit
trailingstep = 3 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
count=0
ENDIF
//manage long positions
IF LONGONMARKET AND count < 10 THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next 9 moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
IF newSL <> newSL[1] then
count = count + 1
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET AND count < 10 THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next 9 moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
IF newSL <> newSL[1] then
count = count + 1
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT Market
EXITSHORT AT MARKET
ENDIF
Thanks. I will test it tomorrow.
Hi 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
JSParticipant
Senior
I 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)
JSParticipant
Senior
Hi 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 mean I have to replace:
//stop order to exit the positions
IF newSL>0 THEN
SELL AT Market
EXITSHORT AT MARKET
ENDIF
with 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)
JSParticipant
Senior
That’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)
Hi 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!