Potential bug in resampler
-
Not sure about it yet, but seems like resampler doesn't work for daily timeframe (still investigating if it is sth my code is doing or backtrader bug). Not sure if this is the cause yet, but I think I found a bug in resamplefilter.py::_BaseResampler::init():
Seems like self.componly can never be True:self.componly = (self.subdays and data._timeframe == self.p.timeframe and not (self.p.compression % data._compression) ) ... self.componly = self.componly and not self.subdays
-
Did some more investigation about it and found out that indeed it is a bug - resampler just don't work at all for timeframe == days. To fix:
The code snippet I placed in previous message:self.componly = (self.subdays and data._timeframe == self.p.timeframe and not (self.p.compression % data._compression) ) ... self.componly = self.componly and not self.subdays
should be changed to (add 'not' before self.subdays and remove the last line):
self.componly = (not self.subdays and data._timeframe == self.p.timeframe and not (self.p.compression % data._compression) )
Additionally in resamplefilter.py::Resampler::call() change this:
if consumed: self.bar.bupdate(data) # update new or existing bar data.backwards() # remove used bar # if self.bar.isopen and (onedge or (docheckover and checkbarover)) cond = self.bar.isopen() if cond: # original is and, the 2nd term must also be true if not onedge: # onedge true is sufficient if docheckover: cond = self._checkbarover(data, fromcheck=fromcheck, forcedata=forcedata)
to:
if consumed: self.bar.bupdate(data) # update new or existing bar # if self.bar.isopen and (onedge or (docheckover and checkbarover)) cond = self.bar.isopen() if cond: # original is and, the 2nd term must also be true if not onedge: # onedge true is sufficient if docheckover: cond = self._checkbarover(data, fromcheck=fromcheck, forcedata=forcedata) if consumed: data.backwards() # remove used bar
This is just a quick fix I came up with, it can probably be fixed in a much prettier way.
-
Next bug related to resampling, I found it while I tried to resample with time_frame = TimeFrame.Minutes with data compression=60 and target compression = 60*24. Problem is located in bactrader.analyzer._get_subday_cmpkey().
Lines:dtcmp = dt.replace(hour=ph, minute=pm, second=ps, microsecond=pus) dtcmp -= tadjust if extradays: dt += datetime.timedelta(days=extradays) dtkey = dtcmp return dtcmp, dtkey
should be changed to:
if extradays: dt += datetime.timedelta(days=extradays) dtcmp = dt.replace(hour=ph, minute=pm, second=ps, microsecond=pus) dtcmp -= tadjust dtkey = dtcmp return dtcmp, dtkey
Basically, the extradays adjustment was done too late.
-
@mpskowron said in Potential bug in resampler:
This is just a quick fix I came up with, it can probably be fixed in a much prettier way.
Better late than never. The fix for the compression only behavior of days (or weeks for example) is good. Alternate implementation aren't any better.
-
Available at: https://github.com/backtrader/backtrader/commit/b8dda02d83bb4bc1bef9b4b37bbca2c0062d7123 (development branch)