Treasury Rates
Retrieve daily, weekly, monthly, quarterly or yearly treasury data. This can be from FinancialModelingPrep or from YahooFinance. FinancialModelingPrep is by far a more extensive dataset containing daily data from 1 month to 30 years. YahooFinance only contains daily data for 5, 10 and 30 years but is a free alternative.
Also known as: US Treasury yields, yield curve, treasury rates.
Treasury Rates in Python
get_treasury_data is part of the Toolkit module of the open-source Finance Toolkit. Install it with:
pip install financetoolkit -U
Then call get_treasury_data as shown below.
from financetoolkit import Toolkit
companies = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY", start_date="2023-08-10")
companies.get_treasury_data()
Which returns:
| date | 13 Week | 5 Year | 10 Year | 30 Year |
|---|---|---|---|---|
| 2023-10-16 | 0.0533 | 0.0472 | 0.0471 | 0.0487 |
| 2023-10-17 | 0.0534 | 0.0487 | 0.0485 | 0.0495 |
| 2023-10-18 | 0.0533 | 0.0492 | 0.049 | 0.05 |
| 2023-10-19 | 0.0531 | 0.0496 | 0.0499 | 0.051 |
| 2023-10-20 | 0.053 | 0.0491 | 0.0496 | 0.0512 |
Parameters
get_treasury_data accepts the following parameters:
- period (str): The interval at which the treasury data should be returned - daily, weekly, monthly, quarterly, or yearly.
- 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.
- risk_free_rate (str | None, optional): The maturity to return as the risk free rate (‘13w’, ‘5y’, ‘10y’ or ‘30y’). Defaults to None, which uses the maturity set on the Toolkit.
- divide_ohlc_by (int | float | None, optional): A value to divide the yields by. Treasury yields are published in percent, so this defaults to 100 to return decimals.
- rounding (int | None, optional): The number of decimals to round the results to. Defaults to None, which uses the rounding set on the Toolkit.
- show_errors (bool, optional): Whether to report retrieval errors. Defaults to False.
- enforce_source (str | None, optional): Forces this specific call to use a given source, either “FinancialModelingPrep” or “YahooFinance”. This takes precedence over the source set on the Toolkit itself. Defaults to None, which falls back to the Toolkit’s own enforce_source.