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/

    Closing trade on the same day

    General Discussion
    3
    6
    1654
    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.
    • K
      kausality last edited by kausality

      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?

      B 1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

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

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators @kausality last edited by

          @kausality said in Closing trade on the same day:

          This is an intraday

          Use intraday data.

          1 Reply Last reply Reply Quote 0
          • K
            kausality last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • K
              kausality last edited by

              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.close(data=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)

              1 Reply Last reply Reply Quote 0
              • K
                kausality last edited by kausality

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

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors