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

Closing all open positions EOD

  • I am using an intraday strategy that requires I exit all positions by end-of day so I've incorporated the following into my code to exit a position if still open by 15:55

    import datetime
            if self.position.size > 0 or self.position.size < 0:
                if == datetime.time(15, 55):
                        self.close(exectype=bt.Order.Stop, size=self.p.size)
                elif == datetime.time(16, 0):
                        self.close(exectype=bt.Order.Stop, size=self.p.size)

    However my results indicate that positions do remain overnight into the next day as seen by the open date on Mar 11 and close on Mar 12.

    ---------------------------- TRADE ---------------------------------
    1: Data Name:                            
    2: Bar Num:                              601
    2: OPEN:                              2019-03-11 14:40:00
    3: Current date:                         2019-03-12
    3: Current time:                         14:20:00
    4: Status:                               Trade Complete
    5: Ref:                                  231
    6: PnL:                                  -2991.0

    Is there another way to close any open positions by EOD?

  • To answer my own question and simplify some code:

    import datetime
            if self.position.size != 0:
                if > datetime.time(15,50):
                        self.close(exectype=bt.Order.Stop, size=self.p.size)

  • You issue stop order, but if you want to close the position immediately, than it have to be market order.

Log in to reply