Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Multi-stock backtesting, the FIFO of buy and sell orders will be invalid

    General Code/Help
    1
    2
    45
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      doublerr last edited by

      My strategy is simple, sell all stocks for the day and buy the next group of stocks.
      This works fine when the strategy has only one stock, the log is as follows
      ![]]CK67SD]IJ4]0M(3I2J(9T.png](/assets/uploads/files/1643073913032-ck67sd-ij4-0m-3i2j-9t.png)

      However, when using multiple stocks for backtesting, the sell order executed first did not refresh the cash balance, resulting in insufficient funds when buying other stocks. It looks like the order doesn't follow the first in first out rule. the log is as follows:
      RYX$_KCX$V)ULTV4I{`IUMD.png

      How can I implement this strategy?

      1. Use the closing price to sell all stocks
      2. Use the closing price to buy the next group of stocks
      1 Reply Last reply Reply Quote 0
      • D
        doublerr last edited by

        Single stock

        log
        H_XEAW4A43PY2[YTKN`K]$8.png

        code

            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 {} price:{}, size:{}, price*size:{}'.format(order.data._name, order.executed.price, order.executed.size, order.executed.price*order.executed.size))
        
                    elif order.issell():
                        self.log('sell {} price:{}, size:{}, price*size:{}'.format(order.data._name, order.executed.price, order.executed.size, order.executed.price*order.executed.size))
        
                else:
                    self.log('margin %s, %s, isbuy=%i' %
                             (order.data._name, order.getstatusname(), order.isbuy()))
        
            def next(self):
                #print(self.datas[0].datetime[0])
                print("")
                self.log('cash: {}'.format(self.broker.getcash()))
                self.log('value: {}'.format(self.broker.getvalue()))
                self.log('position: {}'.format(self.broker.getposition(self.data).size))
        
                self.close()
                self.buy()
        
        

        Multi-stock(two stock)
        log
        RYX$_KCX$V)ULTV4I{`IUMD.png

        code

            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 {} price:{}, size:{}, price*size:{}'.format(order.data._name, order.executed.price, order.executed.size, order.executed.price*order.executed.size))
        
                    elif order.issell():
                        self.log('sell {} price:{}, size:{}, price*size:{}'.format(order.data._name, order.executed.price, order.executed.size, order.executed.price*order.executed.size))
        
                else:
                    self.log('margin %s, %s, isbuy=%i' %
                             (order.data._name, order.getstatusname(), order.isbuy()))
        
            def next(self):
                #print(self.datas[0].datetime[0])
                print("")
                self.log('cash: {}'.format(self.broker.getcash()))
                self.log('value: {}'.format(self.broker.getvalue()))
                self.log('position: {}'.format(self.broker.getposition(self.data).size))
        
                for index, d in enumerate(self.datas):
                    self.close(data=d)
        
                for index, d in enumerate(self.datas):
                    if self.last_buy != d:
                        self.buy(data=d)
                        self.last_buy = d
                        break
        
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors