Building a Trading Backtesting System — Part 2: Crafting the backbone
6 min readFeb 28, 2024
Since we’re leveraging backtrader, our primary aim is to adopt an evolutionary approach in designing and implementing the system.
Initially, we establish a Backtesting
class containing the initialization of needed collaborators and arun
method responsible of executing the entire backtesting process flow:
import backtrader as bt
class Backtesting:
def __init__(self, initial_cash):
self._cerebro = bt.Cerebro()
self._cerebro.broker = bt.brokers.BackBroker()
self._cerebro.broker.setcash(initial_cash)
def run(self):
print('Starting Portfolio Value: %.2f' % self._cerebro.broker.getvalue())
self._cerebro.run()
print('Final Portfolio Value: %.2f' % self._cerebro.broker.getvalue())
if __name__ == "__main__":
backtesting = Backtesting(100000)
backtesting.run()
Running this main code will produce the following output in the console:
Starting Portfolio Value: 100000.00
Final Portfolio Value: 100000.00
As of now, no strategy or data has been incorporated, resulting in the expected initial value as the final outcome.