bt.If Help
-
I want to know how can I get this condition in the next block.
self.datas[0].datetime.date(-3) < self.datas[0].datetime.date(-2) and self.datas[0].close(-2) > self.datas[0].open(-2) and self.datas[0].close(-1) > self.datas[0].open(-1) and self.datas[0].close(0) > self.datas[0].open(0)
I tried this, but it returns a bt.If object instead of the values.
result = bt.If(bt.And(bt.And(bt.And(bt.And(self.datas[0].datetime.date(-3) < self.datas[0].datetime.date(-2)), self.datas[0].close(-2) > self.datas[0].open(-2)), self.datas[0].close(-1) > self.datas[0].open(-1)), self.datas[0].close(0) > self.datas[0].open(0)), 1, 0) print(result)
Any help regarding how to properly implement it?
-
I think you are mixing [] and (). try [] (slicing) brackets instead of () (function) brackets.
self.datas[0].close[-2] > self.datas[0].open[-2] and self.datas[0].close[-1] > self.datas[0].open[-1] and self.datas[0].close[0] > self.datas[0].open[0]
for the first part if you are comparing dates, you should slice first then check dates.
-
@Akhil-Juluru said in bt.If Help:
self.datas[0].close(-2) > self.datas[0].open(-2) and self.datas[0].close(-1) > self.datas[0].open(-1) and self.datas[0].close(0) > self.datas[0].open(0)
I'm not sure what your goal is with the date component, but you could tackle the last n periods with close over open as follows:
def __init__(self): self.close_over_open = self.datas[0].close > self.datas[0].open self.close_over_open_over_nper = bt.ind.SumN(self.close_over_open, period=3) == 3
-
@jay Thank you that was the problem! fixed it.
-
@run-out I was trying to see if the first three candles in a trading day have been green. Your method is good just implemented it! Thank you.