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

Delay in Orderexecution



  • Hey
    I tried to test Backtraders accuracy by execution Trades on Target Data which i actually used for machine learning purposes. -> sometimes my orders are executed with a delay of hours or even days! why?

    here the log:

    1999-02-10 03:00:00, 633
    1999-02-10 04:00:00, 634
    1999-02-10 05:00:00, 635
    1999-02-10 06:00:00, 636
    1999-02-10 07:00:00, 637
    1999-02-10 08:00:00, 638
    1999-02-10 08:00:00, _______
    1999-02-10 08:00:00, OHLC = 1.12870, 1.12970, 1.12700, 1.12790, 
    1999-02-10 08:00:00, TARGET =,1.13490
    1999-02-10 08:00:00, BUY CREATE, 1.12790, ID 6,
    1999-02-10 08:00:00, STOP =,1.12640
    1999-02-10 08:00:00, _______
    1999-02-10 09:00:00, 639
    1999-02-10 10:00:00, 640
    1999-02-10 11:00:00, 641
    1999-02-10 12:00:00, 642
    1999-02-10 13:00:00, 643
    1999-02-10 14:00:00, 644
    1999-02-10 15:00:00, 645
    1999-02-10 16:00:00, 646
    1999-02-10 17:00:00, 647
    1999-02-10 18:00:00, 648
    1999-02-10 19:00:00, 649
    1999-02-10 20:00:00, 650
    1999-02-10 21:00:00, 651
    1999-02-10 22:00:00, 652
    1999-02-10 23:00:00, 653
    1999-02-11 00:00:00, 654
    1999-02-11 01:00:00, 655
    1999-02-11 02:00:00, 656
    1999-02-11 03:00:00, 657
    1999-02-11 04:00:00, 658
    1999-02-11 05:00:00, 659
    1999-02-11 06:00:00, 660
    1999-02-11 07:00:00, 661
    1999-02-11 08:00:00, 662
    1999-02-11 09:00:00, 663
    1999-02-11 10:00:00, 664
    1999-02-11 11:00:00, 665
    1999-02-11 12:00:00, 666
    1999-02-11 13:00:00, 667
    1999-02-11 14:00:00, 668
    1999-02-11 15:00:00, 669
    1999-02-11 16:00:00, 670
    1999-02-11 17:00:00, 671
    1999-02-11 18:00:00, 672
    1999-02-11 19:00:00, BUY EXECUTED, Price_1.12790 ,ID_6, 
    1999-02-11 19:00:00, DELAY 729795.3333333334  729796.7916666666
    1999-02-11 19:00:00, 673
    1999-02-11 20:00:00, DELAY 729795.3333333334  729796.8333333334
    1999-02-11 20:00:00, Position 6 Closed , GROSS -1.50, NET -1.50
    1999-02-11 20:00:00, 674
    

    here my code:

    class TestStrategy(bt.Strategy):
        params = (("PChan4", Positive_Chan4),
                  ("NChan1", Negative_Chan1),('printlog', True),)
    
        def log(self, txt, dt=None, doprint=True):  #logs
            if self.params.printlog or doprint:
                dt = dt or f"{self.datas[0].datetime.date(0)} {self.datas[0].datetime.time(0)}"#self.datas[0].datetime.date(0)
                print('%s, %s' % (dt, txt))
    
        def __init__(self): 
            self.dataclose = self.datas[0].close
            self.order = None
            self.unexecuted_Order=False
            self.buyprice = None
            self.buycomm = None
            self.TradeID = 0
            self.sellprice = None
            self.sellcomm = None
            self.long_stop1 = None
            self.long_target4 = None 
    
            
        
        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                return
            
            if order.status in [order.Completed]:
                if order.isbuy():
                    self.log('BUY EXECUTED, Price_%.5f ,ID_%.0f, ' % (order.executed.price,self.TradeID))
                    self.buyprice = order.executed.price
                    self.buycomm = order.executed.comm
                    
                
                if  (order.executed.dt - order.created.dt) >0.5:
                    self.log("DELAY %s  %s" % (order.created.dt,order.executed.dt))
                self.TradeID +=1
                self.bar_executed = len(self)
                self.unexecuted_Order =False
            elif order.status in [ order.Canceled]:
                self.log('Order Canceled')
            elif order.status in [ order.Margin]:
                self.log('Order Margin')
            elif order.status in [order.Rejected]:
                self.log('Order Rejected')
    
    
        def notify_trade(self, trade):
            
            if not trade.isclosed:
                return
            self.log('Position %.0f Closed , GROSS %.2f, NET %.2f' %(trade.tradeid, trade.pnl, trade.pnlcomm))
        
        def next(self):
            datestring = f"{self.datas[0].datetime.date(0)} {self.datas[0].datetime.time(0)}"
            #prediction = 0
            #prediction = pred_y[X_Dates.index(datestring)]
            TrueTarget = 0
            self.log(X_Dates.index(datestring))
            TrueTarget = Y_Data[X_Dates.index(datestring)]
    
            self.long_stop1 = self.data.close[0] - self.params.NChan1 
            self.long_target4 = self.data.close[0] + self.params.PChan4 
    
            if TrueTarget != 4:
                return
    
            self.log(f"prediction = {TrueTarget}")
     
            if TrueTarget == 4 and self.unexecuted_Order==False:
    
                self.log("_______")
                self.log("OHLC = %.5f, %.5f, %.5f, %.5f, "% (self.data.open[0],self.data.high[0],self.data.low[0],self.data.close[0],))
                self.log("TARGET =,%.5f" %self.long_target4)
                self.log('BUY CREATE, %.5f, ID %.0f,' % (self.dataclose[0],self.TradeID ))
    
                self.log("STOP =,%.5f" %self.long_stop1)
                self.log("_______")
    
    
                self.order = self.buy_bracket(tradeid = self.TradeID, limitprice=self.long_target4, price=self.dataclose[0], stopprice=self.long_stop1)
    
    

    I just want to know how i can make it work without this delay ( this occours about 13 times in 2 Years Chart of Hours)



  • Limit order should be executed when the price reach the order price so bar's high > limit price. With that in mind, the first guess will be that in your case price didn't get to the limit price level very quick. Check the prices first or give more details.

    Market orders give you no delays.



  • @ab_trader arent bracket_orders the same as market orders?(but with alimitsell and a stopsell?)

    if i want to implement this with market orders, should the following work the way i want it?
    (didnt fully understood the documentation, because i was used to the MQL4 methods)

    def __init__(self):
        self.Stoploss = None
        self.Takeprofit = None
    
    def notify_trade()
        takeprofit = ...
        stoploss = ....
        if trade.isbuy():
              self.Takeprofit = self.sell(tradeid = TradeID, exectype=bt.Order.Limit, price=takeprofit_price)
              self.Stoploss = self.sell(tradeid = TradeID, exectype=bt.Order.Stop, price=stoploss_price)
    
    def next():
          ...
         
         if signal:
              self.order=self.buy(tradeid=TradeID)
          ...
    


  • Best way is not to assume but take a look on what is written in the docs - Docs - Bracket Orders:

    A bracket order isn’s a single order but it is actually made up of 3 orders. Let’s consider the long side

    • A main side buy order, usually set to be a Limit or StopLimit order
    • A low side sell order, usually set to be a Stop order to limit losses
    • A high side sell order, usually set to be a Limit order to take profit

    Also reference to the buy_bracket and sell_bracket methods is shown there under Some reference section. And this reference states that baseline order is limit.

    if i want to implement this with market orders, should the following work the way i want it?

    Not clear what you want. Earlier you wanted the bracket order, now it is simple buy.


  • administrators

    @Bayne said in Delay in Orderexecution:

    arent bracket_orders the same as market orders?(but with a limitsell and a stopsell?)

    No they are not. It's a set of 3 orders.

    @Bayne said in Delay in Orderexecution:

    if i want to implement this with market orders, should the following work the way i want it?

    You are bracketing a market position and not an order. Even if you think it is the same, it isn't, because you get no chance, for example, to cancel the market order.

    In this case you want to use OCO orders to make sure One Cancel Other

    Docs - OCO Orders - https://www.backtrader.com/docu/order-creation-execution/oco/oco/

    @ab_trader said in Delay in Orderexecution:

    Also reference to the buy_bracket and sell_bracket methods is shown there under Some reference section. And this reference states that baseline order is limit.

    Here: Docs - Bracket Orders - https://www.backtrader.com/docu/order-creation-execution/bracket/bracket/

    Limit is the baseline for a good reason (including rejection by brokers like IB or Market for bracket orders). The execution price of a Market order can fall outside of the brackets, which would render the entire construct useless.


  • administrators

    @Bayne said in Delay in Orderexecution:

    sometimes my orders are executed with a delay of hours or even days! why?

    And answering the 1st question: because that's how the stock market works.



  • @backtrader thanks for your answers.

    In my Case i can do without the aspect of Broker conditions, because I only News to Test my "strategy"...

    So with "oco" i should ne feine for my purposes, right? (executing an Order with TP and SL in the Open price of the Next Bar without any delay)


Log in to reply
 

});