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

Closing trade on the same day

  • I want to close the trade on the same day it was opened. This is an intraday square-off strategy and it is as follows:

    1. Select stocks which meet criteria X today. Create Limit orders for them. Validity is set as 1 day so if it doesn't execute the next day then cancel it.

    2. If the limit orders from the previous day were executed, I want to close all the trades by the end of the day.

    I cannot place self.close() in next() because it will close the trades the next day of their execution. So how to achieve this?

  • You may try cheat-on-close feature. Order will be executed at current bar close price.

  • administrators

    @kausality said in Closing trade on the same day:

    This is an intraday

    Use intraday data.

  • Thanks for the replies. I will go with the ab_trader method because by using intraday data, I have to keep check when the day changes and then take actions. This makes it more hectic and unsuitable for quick backtesting multiple strategies.

  • I am saving reference to open positions in the stocks using a dictionary which maps 'data' to True/False denoting open positions.

    At the start of next(), I close all the positions in 'data' which have open positions using the above dict.

        def next(self):
            for d in self.pos_map:
                if self.pos_map[d]:
                    self.pos_map[d] = False
            # more code to create orders using strategy

    I hope I am doing it right and it will close the orders at the close which were executed in the last next() cycle(the bar after whose completion this next() was called and in which my main orders were executed)

  • EDIT: Post created by mistake. As I cannot delete it, I have edited it out.

Log in to reply