For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

How do i identify when price action gets tight?

  • Hello,

    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[0], close[1] and close[2] 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[0] > close_down2c:
                        start_time = time.time()
                        if time.time() - start_time <= 15:
                   = d)

  • administrators

    The first thing is to use the actual time reference

    @sagittarius19 said in How do i identify when price action gets tight?:

    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.

    See: Docs - DateTime Management

  • @backtrader Got it. Thanks for pointing this out

Log in to reply