[Enhancement?] Resampler support for non-time-based bars (e.g. range, tick)
From https://github.com/mementum/backtrader/issues/145 to keep a reference for the future
Has there been any interest in supporting tick-based (i.e. 89-tick) or range bars (or Renko, heikin-ashi, etc.). Many platforms seem to support time-based bars only but I've been using range-based bars almost exclusively for the past few years and thought I'd ask if it were planned before I jump in and try to implement it myself. I have a script that creates OHLC range bars from tick data for back-testing but haven't yet found where similar logic would need to be inserted for "live trading" from streamed tick data from Oanda for example.
Thanks for a great platform thus far - the Oanda support was rather timely ;).
tickdata, from for example Oanda can be resampled to any larger timeframe. This was added in October 2015
The issue is not if the data is
tickdata but in which format is presented. Many times this is a pair of bid/ask prices. Following a discussion in a issue a blog post came out as to how to load bid/ask data directly into backtrader using the
The sample chooses the bid field as the price for the
tickby telling the
closecan all be found at position
In this sample the ticks are simply compressed, but nothing prevents to give the cerebro.resampledata a
timeframeargument to resample the ticks to a larger timeframe
rangebars there has been nothing. Although
backtradertries to be as time agnostic as possible, things happen with a timestamp and some parts do always expect a
datetimefield. This is possibly not a large obstacle to implement range bars, but it has to be looked into with some care.
hpsilva last edited by
To start with, congratulations for the neat framework you have here came up with. Have been fiddling around since few weeks ago with it, and i am surprised with the simplicity of use, yet powerful abilities available.
Going to the subject under discussion, I would like to give it a go and try some implementation of different bars, initially with Heikin-Ashi and later on with Renko or Range bars.
The first question would be if you have any advice on where to plug the code for those bars? After looking into backtrader docs, i wonder if Filters are the right way to do it, after looking at it's description:
'Filters transform the values provided by a data feed to deliver a different data feed' ?
Or rather one shall just previously compute the bars and just add them as a regular datafeed to cerebro? Obviously this would loose later on the live trading ability and just be useful for backtesting purposes.
As for the issue with datetime stamping observations for price based bars, i don't see an issue there, as from my pov they shall have anyhow a time-stamp associated with the time they were started/per observation, therefore comparable with regular bars, just not with a regular interval. Apart from plotting which shall be non sense, the rest should work. Does this make any sense?