Backtester
Backtester
__init__(strategy, commission=None, margin=None, cash=10000, multiplier=1)
This class is used to run a backtest on a strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
type[Strategy]
|
The strategy to be run. |
required |
commission
|
Commission | None
|
The commission to be applied to the strategy. If None, no commission will be applied. |
None
|
cash
|
float
|
The starting cash to be used in the backtest. |
10000
|
multiplier
|
float
|
The multiplier to the orders in the backtest. |
1
|
add_data(data, name=None)
Adds a data source to the backtest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataSource
|
The data source to be added. |
required |
name
|
str | None
|
The name of the data source. If None, the name will be the same as the data source. |
None
|
optimize(params, constraint=None, max_trials=100, search_type=SearchType.GRID, risk_free_rate=0.04, seed=0)
Optimizes the backtest with the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict[str, list]
|
The parameters to optimize. |
required |
constraint
|
Callable[[dict], bool] | None
|
The constraint to use for the optimization. |
None
|
max_trials
|
int
|
The maximum number of trials to run. |
100
|
search_type
|
SearchType
|
The search type to use. |
GRID
|
risk_free_rate
|
float
|
The risk free rate to use in the optimization. |
0.04
|
seed
|
int
|
The seed to use for the optimization. |
0
|
Returns:
| Type | Description |
|---|---|
tuple[float, dict]
|
The maximum sharpe ratio and the best trial. |
run(params=None)
Runs the backtest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict | None
|
The parameters to use for the backtest. If None, the default parameters will be used. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
The result of the backtest. |
Result
dataclass
This class contains the result of a backtest.
annualized_return
property
The annualized return of the backtest.
end
property
The end time of the backtest.
max_drawdown
property
The maximum drawdown of the backtest
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
The maximum drawdown in dollars and percentage, respectively. |
start
property
The start time of the backtest.
timedelta
property
The amount of time elapsed between the start and end of the backtest.
total_return
property
The total return of the backtest as a percentage.
plot_equity()
Plots the equity of the backtest.
sharpe_ratio(risk_free_rate=0.04)
The yearly sharpe ratio of the backtest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
risk_free_rate
|
float
|
The risk free rate to be used in the calculation. |
0.04
|
Returns:
| Type | Description |
|---|---|
float
|
The yearly sharpe ratio of the backtest. |