How do i identify when price action gets tight?
I am trying to write a script to identify and buy when the closes of the past 15 seconds stay between two prices. Below is the code i have so far. I am using tick data so the time between ticks can vary. Meaning (close, close and close can all have different spans of time between them. Hence why i am using time for my entry.
Any idea how to make this work? In this particular case, i am trying to buy when the close has stayed in a 5 cent range for atleast 15 seconds.
def next(self): for i, d in enumerate(self.datas): if (len(self.datas[i])>0): close = self.datas[i].close price_up2 = price[-1] +.02 price_down2 = price[-1] - .02 if not position: if close_up2c > close > close_down2c: start_time = time.time() if time.time() - start_time <= 15: return else: self.buy(data = d)
The first thing is to use the actual time reference
if time.time() - start_time <= 15:
This is the time in your PC/Laptop/Raspberry Pi, which may have nothing to do with the trading time and the speed and which the data delivers ticks/bars.
@backtrader Got it. Thanks for pointing this out