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 7 months ago by KumoNoJuzza.
-
-
05/01/2024 at 12:33 AM #232108
Hi JS, sorry to bother you but I’m going crazy with my strategies for IB, trying to implement trailing stop with code. I’m not even sure that’s possible to implement it with _IB strategies. As you can see from the picture, my strategies in paper mode went in stop loss (red cross) but if I do the backtest, my “trailing” should have worked and stored the gain in profit.
the code is:
//trailing stop function
trailingstart = 30// RR 20trailing will start @trailinstart points profit-50
trailingstep = 10// 5trailing step to move the “stoploss”//reset the stoploss value
IF NOT LONGONMARKET 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 moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOPENDIF
I do not understand what I’m doing wrong. If anyone can help me or suggest a different code (for long and short) would be great!
thanks
Alessio
05/01/2024 at 3:16 AM #232112Hi Alessio,
The trailing stop looks good and should work…
(I tried your TS in IB, with a 15-minute time frame, and it just works…)
When I look at your screenshot, I see that the trailing stop has also worked here, your position has ended in the profit… (positive equity curve)
To make the TS visual in your chart, you can add this line:
GraphOnPrice newSL as “newSL” Coloured(“Green”)
05/01/2024 at 4:17 AM #232115Not many people will know about it, but this does not work :
1IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENbecause TradePrice will contain the price of the last trade and the last trade will be about the broker fee added. Alessio, you know by know – see that other topic I just replied to.
Additionally, Backtesting will not be equal to Live, and I may wonder whether PaperTrading will ever be the same as Live or Backtesting. I just don’t know but I expect another dimension or anomaly; PaperTrading generally fails vs Live anyway. I never use it because it won’t resemble IB’s Live situation at all (and way less than IG n top of it).
Switch off the Broker Fee (set to 0) to have comparable situations and let TradePrice do its work as expected. But of course add the Broker Fee costs to your backtest results (which will be a pain when you have in-trade accumulated positions).
And Alessio, In the other topic I briefly referred to a.o. PointSize. This should be PipSize in the example you gave, but maybe it is not, depending on the Instrument. So graph both for your instrument and compare them.
05/01/2024 at 7:11 AM #232122I came back here to correct myself :
Not many people will know about it, but this does not work :
because TradePrice will contain the price of the last trade and the last trade will be about the broker fee added.
I got confused by the other topic and over there wondered whether the trailing would use LastTradeIndex (I did not see this topic over here at that moment). It is lastTradeIndex which can’t be used with any trailing, because it is changed at the next Pending Order. And it was for that reason that I started to type (above) “Not many people will know this”.
But LastTradeIndex is not used here in the example in this topic, so … no problem there.05/01/2024 at 12:03 PM #23213505/01/2024 at 12:12 PM #23213605/04/2024 at 4:09 PM #232324Hi Alessio,
I use this code as trailing stop on IB:
Trailing Stop on IB1234567891011121314151617181920212223242526272829303132333435363738394041//trailing stop functiontrailingstart = 3 //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 movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF LONGONMARKET AND newSL>0 THENSELL AT newSL STOPENDIFIF SHORTONMARKET AND newSL>0 THENEXITSHORT AT newSL STOPENDIFIt works well as long as you there is liquidity, especially on MNQ and NQ. It used to work well on MYM too, but the last 3 weeks, it randomly does.
When the market lacks liquidity, your order is executed whenever possible and sometimes you end up losing money on what was set to be a winning trade.
I believe there are some issues on PRT end but I have no evidence. I use this trailing stop on 30 seconds and 1 minute Timeframes to scalp as many points as possible.
During my live experience, I noticed that if the step is too big, orders tend to fail: “Order rejected by the broker”.
At some point you have to test your algos live. On demo, I have great results. And when running my algos live I don’t get the same.
05/05/2024 at 8:12 PM #232350Thanks Kumo for sharing your experience, it’s really precious for me. I was testing trailing stop on NQXXX with starting 30K in DEMO. I have the doubt this ammount of money is not enough for margin. Probably, and that’s an idea to verify, when SELL AT newSL STOP acitvates, margin required doubles and position is not more sustainble. what do you think? for that reason I’m testing strategy on MNQ with 30K.
for what I read, Papertrading does not work well in IB, while backtest and live have close result, do you confirm ? you say papertrading good result and live less good, I dont’ know if the problem is similar. By the way, what do you think we can share experience here for IB? it’s difficult to find someone here that does trade in IB. which futures have you traded so far?
05/06/2024 at 10:08 AM #232364You are welcome. I am just sharing my humble experience.
In terms of margin requirement, you can check these pages:
https://trading.prorealtime.com/en/margin-requirements
Therefore I advise to start with a capital of 50 K €/$ to trade NQ whether on paper or live because of the overnight requirement and the additional margin to face a drawdown without being liquidated.
I confirm backtesting on IB works perfectly. Demo is not always reliable yet: orders are executed correctly but graphs displays some mistaken amounts when exiting trades.
My advice is, once you are ready with your strategy and your backest, to run it live on MNQ with a capital of 5K €/$ to understand the real behavior of your strategy and the index you are trading. Ideally you would be available to monitor your strategy live.
On manual trading when there is no counterpart to your order, you just click again and again and you are very likley to be served, whereas in the same situation your strategy will just stop.
Peter took time to share is very valuable experience in a previous thread
-
AuthorPosts
Find exclusive trading pro-tools on