execution at the next available price



  • I'm testing a strategy with multiple stocks some of which have missing data for the next day after the buy order is submitted. Is it possible to execute a buy order on the first available price (skip days with missing data)? Should be something straightforward that I'm missing... I'm using by own datafeeds (daily bars) if this matters.


  • administrators

    Orders in the daily timeframe are executed when the next "date" (ie: bar) shows up.



  • Yes, right, but how does this work with multiple stocks? Say, on day 1 I submit the buy order for stocks A and B. A has data on day 2, while B only has data on day 3. So, I want the order for A to be executed on day 2 and the order for B to be executed on day 3. But what I get is both orders being executed on day 3. I guess my ultimate question is how to make datafeeds for different stocks kind of independent of each other? Right now I feel that all datafeeds for different stocks are inner-merged on common nonmissing dates and the days that have data for some stocks but not for the others are dropped. Is this the intended feature or am I doing something wrong?


  • administrators

    The orders have an associated target (the data feed) and they will only be executed when the data feed moves forward.

    @anatur said in execution at the next available price:

    I guess my ultimate question is how to make datafeeds for different stocks kind of independent of each other?

    They are already independent.

    @anatur said in execution at the next available price:

    Right now I feel that all datafeeds for different stocks are inner-merged on common nonmissing dates and the days that have data for some stocks but not for the others are dropped

    Nothing is merged.

    @anatur said in execution at the next available price:

    Is this the intended feature or am I doing something wrong?

    Without some simple code to replicate what you are doing, nothing can be really said.



  • Thanks, good to know that it is supposed to work how I need it to work. Could you please help me to understand what I'm doing wrong then?

    That's how I feed the data. My dataframe is ohlc, with security, date, and OHLCV columns.

    for s in ohlc.security.unique().tolist():
        data_df = ohlc[ohlc.security == s].copy()
    
        data = bt.feeds.PandasData(dataname = data_df, name = s,
                                       datetime= data_df.columns.get_loc('date'),
                                       open= data_df.columns.get_loc('Price_open'), close= data_df.columns.get_loc('Price'),
                                        high = data_df.columns.get_loc('Price_high'), low= data_df.columns.get_loc('Price_low'),
                                        volume = data_df.columns.get_loc('Volume'))
        cerebro.adddata(data)
    

    The dummy strategy I'm testing is buy and hold all securities at the beginning of the period.

    class BH_strategy(bt.Strategy):
    
        def __init__(self):
            self.first_day = True
            self.stocks = self.getdatanames() 
    
        def next(self):   
            if self.first_day:
                for s in self.stocks:
                    self.order_target_percent(target=0.01, data = self.getdatabyname(s))
                    self.first_day = False
    

    The notify_order method is taken from the introduction tutorial and I didn't change anything in it:

        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, %.2f' % order.executed.price)
                elif order.issell():
                    self.log('SELL EXECUTED, %.2f' % order.executed.price)
                self.bar_executed = len(self)
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log('Order Canceled/Margin/Rejected')
            self.order = None
    

    The first date in my dataset is 2012-01-01. And I have data for some stocks on 2012-01-02. But when I run the backtester, I get all orders executed on 2012-01-03.


  • administrators

    Execution date may seem obvious to you, but see:

    • There are no data samples (the first 3 days of 2 data feeds would be enough)

    • The notify_order doesn't show when the orders have been executed and there is no log of what it has actually printed.

      An obvious question would be: how do you know when the orders were executed?

    A working example (in case there is really a bug somewhere) is the best way to go.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.