Is backtrader event driven or vectorized?
Taewoo Kim last edited by
Sorry for the complete noob question. I am new to backtrader, but it looks promising
backtrader takes a dual approach to the problem. This is controlled with the
runonce(boolean) parameter to either a instantiation of
The default is
cerebro = Cerebro(runonce=True) # or False
cerebro = Cerebro() ... ... cerebro.run(runonce=True) # or False
This could be called a pseudo-vectorized of half-vectorized approach. Built-in operations feature a
oncemethod which calculate things in batch mode in a tight inner loop.
Data feeds are fully pre-loaded
Indicators (and sub-indicators thereof) are pre-calculated in batch-mode
Strategyinstance(s) are run step-by-step
Being the goal to offer an increase in speed, but still allow for fine grained logic in the
nextmethod of the strategy
Rough calculations indicate that it is somehow between
Drawback: Because indicators are precalculated (and therefore the buffers are preallocated), the data synchronization mechanism cannot pause the actual movement of a data feed when synchronizing the timestamps for the strategy, keeping the buffers to the final same length. This has no actual impact for backtesting but because
matplotlibexpects all things to have the same
xlength for plotting, it may not be possible to create a plot of the backtesting.
Drawback 2: The implementation of this mode prevented that some indicators can be fully defined in recursive terms with a single formula. A choice had to be made between having this or having the recursive formulas.
Nice Thing: If a user implements a custom
Indicatorand only provides a
nextmethod (intented for step-by-step, see below), the code automatically detects it and will still pre-calculate the indicator using the
nextmethod instead of the missing
oncemethod. The calculation loop will not be so tight as it could be, but users don't have to worry about implementing
This is a 100% step-by-step mode. Also named
nextbecause only the
nextmethod of the different indicators, strategies et al., play a role.
Everything is calculated one step at a time. The reason being the addition of data feeds which would be providing the data points one step at a time (not necessarily live feeds, it could have been reading out of a socket from a database connection).
cerebrois run with
preload=False(disable the preloading of data feeds) it will switch to this mode.