Main Event-Driven Backtester
The `backtest` function is the main backtesting algorithm. It accepts the following parameters:
- `data`: A list of ticker symbols (default: `['SPY','IWM','QQQ','TLT','VXN']`). These tickers represent the stocks to be backtested. ** Please note that this requires multiple tickers as input (via `yfinance` Multi Column Index. If you would like to backtest just one instrument, please include a second ticker as placeholder.
- `trade_size`: The amount of capital allocated per trade (default: `1000`).
- `max_loss`: The maximum acceptable loss per trade as a fraction of the trade size (default: `0.05`). For example, 5% of trade size.
The backtest function performs the following steps:
Extracts the ticker symbols from the input data.
Initializes an empty DataFrame (`df_backtest`) to store the backtest results.
Loops through each ticker symbol.
Copies the data for the current ticker symbol and performs necessary data transformations.
Initializes variables and DataFrames used for tracking trade positions.
Calculates additional parameters and technical indicators for the current ticker symbol.
Defines the entry and exit conditions for trades based on the calculated indicators.
Loops through each date in the data.
If no position is currently open, checks if the entry conditions are met. If so, opens a long position.
If a position is open, checks if the stop-loss condition is met or if the exit conditions are met. If so, closes the position.
If none of the above conditions are met and it is the last date in the data, closes the position (flattens).
Updates trade-related variables and logs the trade details in the `df_trades` DataFrame.
Concatenates the `df_trades` DataFrame for the current ticker symbol with the overall `df_backtest` DataFrame.
Calculates the total profit and loss (`pnl_total`) and the number of trades (`trades`) for the current ticker symbol.
Plots a histogram of the profit and loss values for the overall backtest results (`df_backtest`).
After looping through all ticker symbols, the function prints a completion message and returns the `df_backtest` DataFrame.
The `sample_backtest` function is used to perform statistical sampling on the backtest results. This helps estimate the distribution of possible outcomes (worst case to best case) and helps establish statistical significance if considering a reasonable confidence interval. It accepts the following parameters:
- `df_backtest`: The DataFrame containing the backtest results.
- `iterations`: The number of iterations to perform (default: `1000`).
- `samples`: The number of samples to take in each iteration (default: `100`). -- How many trades within a given time period?
The function performs the following steps:
Initializes an empty DataFrame (`df_backtest_sample`) to store the sampled results.
Iterates through the specified number of iterations.
In each iteration, randomly samples the specified number of samples from the `df_backtest` DataFrame with replacement.
Calculates descriptive statistics (mean) for the sampled data and appends it to the `df_backtest_sample` DataFrame.
Plots a histogram of the sampled profit and loss values from the `df_backtest_sample` DataFrame.
The function returns the `df_backtest_sample` DataFrame.