Date and time handling in the __init()__
Could you please advice the way of date & time handling using
__init()__. Say I have data feed and want to have the following indicator values along the history:
from the data beginning to 2000/12/31 it should be 1.0
after 2001/01/01 it should be 2.0
I've developed an indicator using
next(). It simply compares last data feed dates and dates shown, and set values accordingly . Is there a way to make the same thing in the
__init()__? It seems to me that it might be faster.
Comparison is implemented for
datetime.timeinstances and lines, but not for
datetime.date, which forbids a clean implementation of that idea with something like:
def __init__(self): self.lines.myline = 1 + (self.data.datetime >= datetime.date(2001, 1, 1))
But there is something which can be done even if not so elegant
def __init__(self): self.lines.myline = 1 + (self.data.datetime >= date2num(datetime.date(2001, 1, 1)))
Instead of directly comparing with the
datetime.dateinstance, the comparison is done against the numeric value.
@backtrader Thank you!
bt.date2num()function for transformation. Also I've found out that you rewrote
ifoperator which can be applied to
lines. That helped me also.
Edited the snippet from above to reflect that the correct usage is with
Of course using the
Iffrom backtrader allows for much more complex scenarios. The
1 + booleanin the snippet was just meant to solve the hypothetical case presented above (
1until date x,
2from that point onwards)