How to do calculations ONLY on the 2 last bars?

Viewed 39

I wrote a script that I should use barstate.islast because of the heavy calculations. But the problem is that I sometimes need also the previous candle too to check if the condition was met there or not. So what should I do? Can say like if ( barstate.islast or barstate.islast[1]).

I tried but I'm not sure if barstate.islast[1] is working or not (because backtesting it in my strategy is so hard and weird and I'm getting confused)

1 Answers

You can use the history reference operator [] on your condition.

So, something like this.

cond = ...

if barstate.islast
    another_cond = cond1 and cond[1]
Related