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/

    How to open several orders (all to buy or all to sell) and close them in a defined time after?

    General Discussion
    2
    5
    587
    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.
    • V
      volt111 last edited by

      Actually, the question is stated in the topic title.
      I develop a strategy that generates some signal to buy or to sell. Signals of one type may be grouped into small clusters, i.e. I can get a signal to buy in time T, then in time T+1 I will get again a signal to buy, the same I'll get in T+2 and so on. So I want to open orders in such cases and close them in a defined time (that could be specific for each order). Unfortunately, I can't find any info about this.
      I will be very appreciated if the community shows me some examples or give links to related documents.

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        First: Let's assume that you mean that you want to cancel an order, because you cannot close an order

        Then

        • Use the valid parameter in buy / sell. See Docs - Strategy

        or

        • Record the len of the Strategy or the data feed you are operating with and use cancel for the order instance when the len has increased your n number of desired bars.
        1 Reply Last reply Reply Quote 0
        • V
          volt111 last edited by

          @backtrader Thank you a lot. This is exactly what I asked.

          1 Reply Last reply Reply Quote 0
          • V
            volt111 last edited by

            @backtrader
            Help me please how to close positions in the case described above. I.e. I open a buy position self.buy(size=1) in time T, then in time T+1 I will open again a buy position self.buy(size=1), etc. Then I need in time T+X close position that was opened at time T, at time T+X+1 I'm going to close position opened at T+1, etc. How to do that? I'm a little confused in this direction.

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              If you want to remain time agnostic and be bar bound (which seems to be your T and T + 1 description)

              • Record the len of the data

                bought_at = len(self.data)
                
              • Cancel later

                new_len = len(self.data)
                if new_len > bought_at + 1:
                    ...
                
              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }