Stop Limit Orders

Is it possible to set it to only enter with a stop limit order, to account stop slippage?


  • in oder to stop slippage*

  • ptunneyptunney Posts: 246

    There is no application between you and the market (unlike trading with an app that may have some software managed stop exit order), effectively you are the application.

    There are some simple order types listed on page 0 of the documentation (use your browser find CTRL F and type simple) which may work for you..

    Simple Stop Market Order, use 'buy' or 'sell', stop price is the price at which the market order will be triggered
    order.send(self.symbol, 'sell', 100, type='STP', stop=1125)

    Simple Stop Limit Order, use 'buy' or 'sell', stop price is the price at which the Limit order will be triggered, price is the Limit price (as above)
    order.send(self.symbol, 'buy', 100, type='STL', stop=1130,price=1129.2)

    But when writing an algo trading script there are a number of ways to achieve your goal, each potentially more effective than the previous.

    You can simply enter a limit order to exit at your desired price at the same time as your entry order. The problem with this is, in the real world you do not know if you will actually get filled (or fully filled) so..
    You can wait for the fill (on_fill) and then submit a limit order at your desired price with the same size as you were actually filled at (resolving the problem above). But then you do not know if your exit got fully filled so you could be stuck in a position again so..
    You can track your order through each step using a variable like self.status where 0 = not in a position 1 is order out waiting to be filled, 2 is filled, 3 is exit order out waiting to be filled and 4 is exit filled (or set it back to zero if you want to be able to trade again).
    If your exit is at the close then you can still occasionally not get filled on your exit so in the real world you would either manually exit or you could hedge any outstanding capital.

    So as you can see, there is no simple answer as, in real trading there are rarely simple solutions. You have to be prepared for as many eventualities as you can. Even then, you will never catch them all.

    But for most backtests, just submit your entry and exit together. Using normal order types your exit can be type='limit' and set intent='none', for live models the intent must be specified but if you set intent to exit and submit it at the same time as the opening order the simulator will object that you cannot exit a position as you are not 'in' a position yet.

    (Some of this advice would change slightly if you were holding multi day rather than trading intraday).

Sign In or Register to comment.