Backtest

class src.boatwright.Backtest.Backtest(strategy, data, broker, debug=False)
Parameters:
  • strategy (Strategy) – strategy

  • broker (BacktestBroker) – broker model

  • data (DataFrame) – pandas dataFrame with columns: [“datetime”, “open”, “high”, “low”, “close”, “volume”]

  • debug – boolean toggle to print debugging information

calculate_signals()

data is updated with columns for technical indicators from strategy.calculate_signals() drop self.data[0: strategy.calc_prerequisite_data_length()]. This is the range of data in which not all signals are calculated because there was not yet enough data. :return: None

notify_listeners(row)

puts a row of data in each listeners queue

register_listener(listener)

register a listener to be updated with each row of data (only for asynchronous runs)

run(skip_calculate_signals=False, verbose=False)

Execute the backtest. initialize_data() is called, updating self.data with technical indicator signals. For each row in self.data, self.strategy.step() and self.broker.step() is called triggering and processing any orders. self.data is updated with columns [aum, quote_amount, base_amount]

Parameters:
  • skip_calculate_signals (bool) – boolean toggle for skipping calculation of technical indicators with strategy.calculate_signals(); i.e. assuming backtest.data is set via some other functionality

  • verbose – boolean toggle for printing tqdm progress bar to the terminal

Variables:

self.runtime – duration in seconds to complete the backtest

Returns:

None

async run_async(skip_calculate_signals=False, verbose=False)

Asynchronous method to execute the backtest. Necessary to support asyncronous functionality (e.g. failed order response with a Strategy.OrderHandler). Execute as: asyncio.run(backtest.run_async()) Note, inlcuding asyncronous routines slows down backtest runtimes

Parameters:
  • skip_calculate_signals (bool) – boolean toggle for skipping calculation of technical indicators with strategy.calculate_signals(); i.e. assuming backtest.data is set via some other functionality

  • verbose – boolean toggle for printing tqdm progress bar to the terminal

Variables:

self.runtime – duration in seconds to complete the backtest

Returns:

None