Benchmark Historical Data
Collect and align historical benchmark data with the portfolio’s data.
The following columns are included:
- Open: the opening price for the benchmark over time.
- High: the highest price for the benchmark over time.
- Low: the lowest price for the benchmark over time.
- Close: the closing price for the benchmark over time.
- Adj Close: the adjusted closing price for the benchmark over time.
- Volume: the volume of the benchmark over time.
- Dividends: the dividends of the benchmark over time.
- Returns: the returns of the benchmark over time.
- Cumulative Return: the cumulative return of the benchmark over time.
Volatility, Excess Return and Excess Volatility are no longer included here. These are available via the Risk module (e.g. toolkit.risk.get_volatility) and the Performance module (e.g. toolkit.performance.get_excess_return) instead.
This method retrieves historical benchmark data (daily, weekly, monthly, quarterly, and yearly) for the portfolio, based on a specified benchmark ticker or a mapping of portfolio tickers to their corresponding benchmark tickers. The retrieved benchmark data is then aligned with the portfolio’s historical data, ensuring that the dates of the benchmark data match the dates of the portfolio’s transactions.
The method can retrieve data for a single benchmark ticker or for multiple benchmarks depending on the portfolio tickers. The resulting benchmark data is returned in a structured DataFrame.
Benchmark Historical Data in Python
collect_benchmark_historical_data is part of the Portfolio module of the open-source Finance Toolkit. Install it with:
pip install financetoolkit -U
Then call collect_benchmark_historical_data as shown below.
from financetoolkit import Portfolio
portfolio = Portfolio(example=True, api_key="FINANCIAL_MODELING_PREP_KEY")
portfolio.collect_benchmark_historical_data()
Which returns:
| Date | Open | High | Low | Close | Adj Close | Volume | Dividends | Return | Cumulative Return |
|---|---|---|---|---|---|---|---|---|---|
| 2025-02-14 | 6115.52 | 6127.47 | 6107.62 | 6114.63 | 6114.63 | 4.33519e+09 | 0 | -0.0001 | 4.2419 |
| 2025-02-18 | 6121.6 | 6129.63 | 6099.51 | 6129.58 | 6129.58 | 4.68498e+09 | 0 | 0.0024 | 4.2523 |
| 2025-02-19 | 6117.76 | 6147.43 | 6111.15 | 6144.15 | 6144.15 | 4.56233e+09 | 0 | 0.0024 | 4.2624 |
| 2025-02-20 | 6134.5 | 6134.5 | 6084.59 | 6117.52 | 6117.52 | 4.81369e+09 | 0 | -0.0043 | 4.2439 |
| 2025-02-21 | 6114.1 | 6114.82 | 6008.56 | 6013.13 | 6013.13 | 5.43456e+09 | 0 | -0.0171 | 4.1715 |
| 2025-02-24 | 6026.69 | 6043.65 | 5977.83 | 5983.25 | 5983.25 | 4.99012e+09 | 0 | -0.005 | 4.1508 |
| 2025-02-25 | 5982.73 | 5992.65 | 5908.49 | 5955.25 | 5955.25 | 5.37469e+09 | 0 | -0.0047 | 4.1314 |
| 2025-02-26 | 5970.87 | 6009.82 | 5932.69 | 5956.06 | 5956.06 | 4.86958e+09 | 0 | 0.0001 | 4.1319 |
| 2025-02-27 | 5981.88 | 5993.69 | 5858.78 | 5861.57 | 5861.57 | 5.05768e+09 | 0 | -0.0159 | 4.0664 |
| 2025-02-28 | 5856.74 | 5959.4 | 5837.66 | 5954.5 | 5954.5 | 6.44114e+09 | 0 | 0.0159 | 4.1309 |
Parameters
collect_benchmark_historical_data accepts the following parameters:
- benchmark_ticker (str | None): The default benchmark ticker symbol to use if no per-ticker mapping is provided. If None, the default benchmark ticker is retrieved from the configuration.