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?



  • 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.


  • administrators

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

    Then

    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.


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



  • @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.


  • administrators

    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:
          ...