|
| 1 | +import io |
| 2 | +from dataclasses import dataclass, field |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | +from fluid.utils.http_client import AioHttpClient |
| 8 | + |
| 9 | +URL = ( |
| 10 | + "https://www.federalreserve.gov/datadownload/Output.aspx?" |
| 11 | + "rel=H15&series=bf17364827e38702b42a58cf8eaa3f78&lastobs=&" |
| 12 | +) |
| 13 | + |
| 14 | +maturities = [ |
| 15 | + "month_1", |
| 16 | + "month_3", |
| 17 | + "month_6", |
| 18 | + "year_1", |
| 19 | + "year_2", |
| 20 | + "year_3", |
| 21 | + "year_5", |
| 22 | + "year_7", |
| 23 | + "year_10", |
| 24 | + "year_20", |
| 25 | + "year_30", |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class FederalReserve(AioHttpClient): |
| 31 | + """Federal Reserve API client. |
| 32 | +
|
| 33 | + This class is used to fetch yield curves from the Federal Reserve at |
| 34 | + https://www.federalreserve.gov/datadownload/ |
| 35 | + """ |
| 36 | + |
| 37 | + url: str = "https://www.federalreserve.gov/datadownload/Output.aspx" |
| 38 | + default_params: dict[str, Any] = field( |
| 39 | + default_factory=lambda: { |
| 40 | + "from": "", |
| 41 | + "to": "", |
| 42 | + "lastobs": "", |
| 43 | + "filetype": "csv", |
| 44 | + "label": "include", |
| 45 | + "layout": "seriescolumn", |
| 46 | + "type": "package", |
| 47 | + } |
| 48 | + ) |
| 49 | + |
| 50 | + async def yield_curves(self, **params: Any) -> pd.DataFrame: |
| 51 | + """Get treasury constant maturities rates""" |
| 52 | + params.update(series="bf17364827e38702b42a58cf8eaa3f78", rel="H15") |
| 53 | + data = await self._get_text(params) |
| 54 | + df = pd.read_csv(data, header=5, index_col=None, parse_dates=True) |
| 55 | + df.columns = ["date"] + maturities # type: ignore |
| 56 | + df = df.set_index("date").replace("ND", np.nan) |
| 57 | + return df.dropna(axis=0, how="all").reset_index() |
| 58 | + |
| 59 | + async def ref_rates(self, **params: Any) -> pd.DataFrame: |
| 60 | + """Get policy rates |
| 61 | +
|
| 62 | + Prior to 2021-07-08 it is the rate on excess reserves (IOER rate) |
| 63 | + After 2021-07-08 it is the rate on reserve balances (IORB rate) |
| 64 | +
|
| 65 | + The IOER rate was the primary tool used by the Federal Reserve to set |
| 66 | + a floor on the federal funds rate. |
| 67 | + While the Interest rate on required reserves (IORR rate) existed, |
| 68 | + the IOER rate had a more direct impact on market rates, |
| 69 | + as banks typically held far more excess reserves than required reserves. |
| 70 | + Therefore, the IOER rate was more influential |
| 71 | + in the Fed's monetary policy implementation. |
| 72 | + """ |
| 73 | + params.update(series="c27939ee810cb2e929a920a6bd77d9f6", rel="PRATES") |
| 74 | + data = await self._get_text(params) |
| 75 | + df = pd.read_csv(data, header=5, index_col=None, parse_dates=True) |
| 76 | + ioer = df["RESBME_N.D"] |
| 77 | + iorb = df["RESBM_N.D"] |
| 78 | + rate = iorb.combine_first(ioer) |
| 79 | + return pd.DataFrame( |
| 80 | + { |
| 81 | + "date": df["Time Period"], |
| 82 | + "rate": rate, |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + async def _get_text(self, params: dict[str, Any]) -> io.StringIO: |
| 87 | + """Get parameters for the request.""" |
| 88 | + params = {**self.default_params, **params} |
| 89 | + response = await self.get(self.url, params=params, callback=True) |
| 90 | + data = await response.text() |
| 91 | + return io.StringIO(data) |
0 commit comments