Trailing stop on IB – Stop repositioning on DAX

Viewing 9 posts - 16 through 24 (of 24 total)
  • Author
    Posts
  • #232108 quote
    Aragorna
    Participant
    Junior

    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 STOP

    ENDIF

     

    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

    IMG_7851.jpg IMG_7851.jpg
    #232112 quote
    JS
    Participant
    Senior

    Hi 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”)

    #232115 quote
    PeterSt
    Participant
    Master
    Not many people will know about it, but this does not work :
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
    because 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.
    #232122 quote
    PeterSt
    Participant
    Master
    I 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.
    #232135 quote
    Aragorna
    Participant
    Junior
    Hi JS, sure the backtest is in profit but as you can see from the red cross, the paper mode in the same point did not close in profit but went directly in stop loss.
    #232136 quote
    Aragorna
    Participant
    Junior
    so you mean that I should not test my strategies in paper mode but only backtest them? that’s incredible for me, it does not make sense. Should I go live just trusting backtest, without testing in demo?
    #232324 quote
    KumoNoJuzza
    Participant
    New
    Hi Alessio, I use this code as trailing stop on IB:
    //trailing stop function
    trailingstart = 3 //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 moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    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 moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
    newSL = newSL-trailingstep*pipsize
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF LONGONMARKET AND newSL>0 THEN
    SELL AT newSL STOP
    ENDIF
    
    IF SHORTONMARKET AND newSL>0 THEN
    EXITSHORT AT newSL STOP
    ENDIF
    
    It 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.
    #232350 quote
    Aragorna
    Participant
    Junior
    Thanks 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?
    #232364 quote
    KumoNoJuzza
    Participant
    New
    You are welcome. I am just sharing my humble experience. In terms of margin requirement, you can check these pages: https://www.interactivebrokers.com/en/index.php?f=26662&hm=us&ex=us&rgt=0&rsk=1&pm=0&rst=101004100808 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 
Viewing 9 posts - 16 through 24 (of 24 total)
  • You must be logged in to reply to this topic.

Trailing stop on IB – Stop repositioning on DAX


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 23 replies,
has 5 voices, and was last updated by KumoNoJuzza
1 year, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/04/2023
Status: Active
Attachments: 1 files
Logo Logo
Loading...