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. -
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 inbuy
/sell
. See Docs - Strategy
or
- Record the
len
of the Strategy or the data feed you are operating with and usecancel
for the order instance when thelen
has increased yourn
number of desired bars.
- Use the
-
@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 positionself.buy(size=1)
in time T, then in time T+1 I will open again a buy positionself.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. -
If you want to remain time agnostic and be bar bound (which seems to be your
T
andT + 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: ...
-