For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Stop Order execution price?



  • Hello,
    I am sending a stop loss order with the following code

            self.buy(exectype=bt.Order.Stop,
                     price=data.close[0] * (1 + self.params.stop_loss),
                     data=data)
    

    First of all is this the correct way to send a stop loss order? I check the execution price with the following code:

        def notify_order(self, order):
    
            if order.status in [order.Submitted]:
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                self.log('Order Submitted {}-{}'.
                         format(order.params.data._name,
                         self.getOrderType(order)))
                return
    
            if order.status in [order.Accepted]:
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                self.log('Order Accepted {}-{}'.
                         format(order.params.data._name,
                         self.getOrderType(order)))
                return
    
            # Check if an order has been completed
            # Attention: broker could reject order if not enough cash
            if order.status in [order.Completed]:
                if order.isbuy():
                    self.log(
                        'BUY EXECUTED, Price: {:.4f}, Cost: {:.4f}, '
                        'Comm:{:.4f}, Ticker:{} OrderType:{}'.format
                        (order.executed.price,
                         order.executed.value,
                         order.executed.comm,
                         order.params.data._name,
                         self.getOrderType(order)))
    
                    self.buyprice = order.executed.price
                    self.buycomm = order.executed.comm
                else:  # Sell
                    self.log('SELL EXECUTED, Price: {:.4f}, '
                             'Cost: {:.4f}, Comm:{:.4f},'
                             'Ticker:{}, OrderType:{}'.format
                             (order.executed.price,
                              order.executed.value,
                              order.executed.comm,
                              order.params.data._name,
                              self.getOrderType(order)))
    
                self.bar_executed = len(self)
    

    What I am seeing is that if this is indeed a stop loss order, the execution price is actually the stop loss trigger price, not the actual closing price on the execution bar.
    Let me give an example. Let's say I buy 100 with a stop loss of 90. Then at the few bars down the road the price is 80. I expect the stop loss order to be triggered (It is). But the execution price is still 90 instead of 80.
    Is this normal? It seems unrealistic to me that a stop loss would be executed at the trigger price. What do you think? The documentation says:

    Order.Stop. An order which is triggered at price and executed like an Order.Market order

    Or is it that order.executed.price doesn't matter?

    I also checked the execution price with

    pricein = trade.history[len(trade.history)-1].status.price
    priceout = trade.history[len(trade.history)-1].event.price
    

    priceout is still the stop loss trigger price.


  • administrators

    @emin-ozkan said in Stop Order execution price?:

    I am sending a stop loss order with the following code

    There is no such thing as a stop loss order. You are sending a Stop order.

    @emin-ozkan said in Stop Order execution price?:

    First of all is this the correct way to send a stop loss order?

    If you want to implement a stop loss policy in your code, with a buy-Stop order you would first have to sell something. That's something you don't show, so there cannot be a proper answer.

    @emin-ozkan said in Stop Order execution price?:

    What I am seeing is that if this is indeed a stop loss order, the execution price is actually the stop loss trigger price, not the actual closing price on the execution bar.

    Indeed. This is how it must be in most cases. If you think it has to be the close price of the bar, you are completely wrong and you need to rethink this through.

    @emin-ozkan said in Stop Order execution price?:

    Let me give an example. Let's say I buy 100 with a stop loss of 90. Then at the few bars down the road the price is 80. I expect the stop loss order to be triggered (It is). But the execution price is still 90 instead of 80.

    This is NOT an example. It is something you have written. It does actually contradict your sample code. Because you would need a sell-Stop to counteract a buy operation. Furthermore the range of the bars and opening prices are important. Some random prices thrown into some text is far from being a valid example.

    @emin-ozkan said in Stop Order execution price?:

    Is this normal? It seems unrealistic to me that a stop loss would be executed at the trigger price.

    Yes it is normal. No it is not unrealistic. And it would be unrealistic if it suddenly used the close price.

    Let me argue the following: your trading experience is very limited. The only thing you can apply is slippage if you think the market in which your work is highly volatile and/or non-liquid during execution of a Stop. See Docs - Slippage

    @emin-ozkan said in Stop Order execution price?:

    The documentation says:
    Order.Stop. An order which is triggered at price and executed like an Order.Market order

    As it is. As soon as the trigger price is met, the order is executed with the next tick. And this is NOT always the trigger price, but it will be in most cases.

    @emin-ozkan said in Stop Order execution price?:

    Or is it that order.executed.price doesn't matter?

    This question makes no sense. order.executed.price is the reported execution price. How can it not matter?

    @emin-ozkan said in Stop Order execution price?:

    I also checked the execution price with

    You can also try convoluted integrals and quantum physics instead of checking order.executed.price



  • @backtrader ok thanks


Log in to reply
 

});