Historical Data
Returns historical data for the specified tickers. This contains the following columns:
- Open: The opening price for the period.
- High: The highest price for the period.
- Low: The lowest price for the period.
- Close: The closing price for the period.
- Adj Close: The adjusted closing price for the period.
- Volume: The volume for the period.
- Dividends: The dividends for the period.
- Return: The return for the period.
- Cumulative Return: The cumulative return for the period.
Volatility, Excess Return and Excess Volatility are not included here. These are available as dedicated calculations in the Risk module (e.g. toolkit.risk.get_volatility, toolkit.risk.get_excess_volatility) and the Performance module (e.g. toolkit.performance.get_excess_return) instead.
If a benchmark ticker is selected, it also calculates the benchmark ticker together with the results. By default this is set to “SPY” (S&P 500 Index) but can be any ticker. This is relevant for calculations for models such as CAPM, Alpha and Beta.
Important to note is that when an api_key is included in the Toolkit initialization that the data collection defaults to FinancialModelingPrep which is a more stable source and utilises your subscription. However, if this is undesired, it can be disabled by setting enforce_source to “YahooFinance”. If data collection fails from FinancialModelingPrep it automatically reverts back to YahooFinance.
Also known as: OHLCV, price history, open high low close volume.
Historical Data in Python
get_historical_data is part of the Toolkit module of the open-source Finance Toolkit. Install it with:
pip install financetoolkit -U
Then call get_historical_data as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit("AAPL", api_key="FINANCIAL_MODELING_PREP_KEY")
toolkit.get_historical_data(period="yearly")
Which returns:
| Date | Open | High | Low | Close | Adj Close | Volume | Dividends | Return | Cumulative Return |
|---|---|---|---|---|---|---|---|---|---|
| 2013 | 19.7918 | 20.0457 | 19.7857 | 20.0364 | 17.5889 | 2.23084e+08 | 0.108929 | 0 | 1 |
| 2014 | 28.205 | 28.2825 | 27.5525 | 27.595 | 24.734 | 1.65614e+08 | 0.461429 | 0.406225 | 1.40623 |
| 2015 | 26.7525 | 26.7575 | 26.205 | 26.315 | 23.9886 | 1.63649e+08 | 0.5075 | -0.0301373 | 1.36385 |
| 2016 | 29.1625 | 29.3 | 28.8575 | 28.955 | 26.9824 | 1.22345e+08 | 0.5575 | 0.124804 | 1.53406 |
| 2017 | 42.63 | 42.6475 | 42.305 | 42.3075 | 40.0593 | 1.04e+08 | 0.615 | 0.484644 | 2.27753 |
| 2018 | 39.6325 | 39.84 | 39.12 | 39.435 | 37.9 | 1.40014e+08 | 0.705 | -0.0539019 | 2.15477 |
| 2019 | 72.4825 | 73.42 | 72.38 | 73.4125 | 71.615 | 1.00806e+08 | 0.76 | 0.889578 | 4.0716 |
| 2020 | 134.08 | 134.74 | 131.72 | 132.69 | 130.559 | 9.91166e+07 | 0.8075 | 0.823067 | 7.4228 |
| 2021 | 178.09 | 179.23 | 177.26 | 177.57 | 175.795 | 6.40623e+07 | 0.865 | 0.346482 | 9.99467 |
| 2022 | 128.41 | 129.95 | 127.43 | 129.93 | 129.378 | 7.70342e+07 | 0.91 | -0.264042 | 7.35566 |
| 2023 | 187.84 | 188.51 | 187.68 | 188.108 | 188.108 | 4.72009e+06 | 0.71 | 0.453941 | 10.6947 |
Parameters
get_historical_data accepts the following parameters:
- enforce_source (str, optional): A string containing the historical source you wish to enforce. This can be either FinancialModelingPrep or YahooFinance. Defaults to no enforcement.
- period (str): The interval at which the historical data should be returned - daily, weekly, monthly, quarterly, or yearly. Defaults to “daily”.
- return_column (str): The column to use for the return calculation. Defaults to “Adj Close”.
- include_dividends (bool): Defines whether to include dividends in the return calculation. Defaults to True.
- fill_nan (bool): Defines whether to forward fill NaN values. This defaults to True to prevent holes in the dataset. This is especially relevant for technical indicators.
- overwrite (bool): Defines whether to overwrite the existing data. If this is not enabled, the function will return the earlier retrieved data. This is done to prevent too many API calls. Defaults to False.
- rounding (int): Defines the number of decimal places to round the data to.
- show_ticker_seperation (bool, optional): A boolean representing whether to show which tickers acquired data from FinancialModelingPrep and which tickers acquired data from YahooFinance.
- show_columns (list[str], optional): A list of columns to include in the output. Valid columns are Open, High, Low, Close, Adj Close, Volume, Dividends, Return and Cumulative Return. Invalid column names are logged as warnings. If all provided columns are invalid the full dataset is returned. Defaults to None (all columns).