trading.execution.backtester.strategy_backtester
trading.execution.backtester.strategy_backtester
StrategyBacktester
StrategyBacktester(feed: MarketDataFeed, order_api: OrderAPI, order_filler: OrderFiller, stoploss_order_manager: StoplossOrderManager, portfolio: Portfolio, strategy: Strategy, strategy_injector: StrategyInjector, results_collector: ResultsCollector, event_bus: EventBus)
Backtester that coordinates a Strategy object over OHLCV data at a high level.
Allows for iteration of data with historical data rather than a live feed.
Subscribes all handlers to an EventBus, calls lifecycle methods for the provided Strategy,
injects data into the Strategy, and returns a ExecutionData object for analysis.
Initializes StrategyBacktester & subscribes all event methods into the event bus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feed
|
MarketDataFeed
|
The feed to derive |
required |
order_api
|
OrderAPI
|
Allows the user can make |
required |
order_filler
|
OrderFiller
|
Fills any |
required |
stoploss_order_manager
|
StoplossOrderManager
|
Publishes |
required |
portfolio
|
Portfolio
|
Takes filled events, tracks positions & money. Publishes |
required |
strategy
|
Strategy
|
The user's strategy with lifecycle hooks to test. |
required |
strategy_injector
|
StrategyInjector
|
Injects snapshot events into the strategy upon them being published. |
required |
results_collector
|
ResultsCollector
|
Collects all events & computes results at the end of the backtest. |
required |
event_bus
|
EventBus
|
The tool that allows events to be published & broadcasted to multiple modules at once. |
required |
Source code in src\contango\trading\execution\backtester\strategy_backtester.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
backtest
classmethod
backtest(ohlcv_data: list[MarketDataEvent], strategy: Strategy, config: BacktesterConfig) -> ExecutionData
Runs a full backtest for a single ticker & returns the data generated from the backtest. The provided data is validated for every backtest, & will raise if in the incorrect format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ohlcv_data
|
list[MarketDataEvent]
|
The bar data for the backtest. |
required |
strategy
|
Strategy
|
The strategy subclass for the backtest (determining when to trade). |
required |
config
|
BacktesterConfig
|
The configuration for the backtest. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema is invalid or data types cannot be derived from the provided dataframe. |
Source code in src\contango\trading\execution\backtester\strategy_backtester.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |