Update treasury command to get directly from fed (#5215)

* Update treasury command to get directly from fed

* test

* Fix the codespell lint

* Mypy

* help text

* mypy + askobb update

* pylint now

* Fix #5190

* Fix report

* ruff life

* Fix #5172

* economy report titles

* Make the chart nicer
This commit is contained in:
James Maslek
2023-07-14 13:33:31 -04:00
committed by GitHub
parent 281799f047
commit 830159f718
18 changed files with 250 additions and 145 deletions

View File

@@ -48,7 +48,7 @@ jobs:
pip install types-pytz types-requests types-termcolor types-tabulate types-PyYAML types-python-dateutil types-setuptools types-six
- run: bandit -x ./tests -r . || true
- run: black --diff --check .
- run: codespell --ignore-words-list=vai,mis,gard,commun,statics,ro,zar,zlot,jewl,ba,buil,coo,ether,hist,hsi,mape,navagation,operatio,pres,ser,yeld,shold,ist,varian,datas,ake,creat,vie,hel,ket,toke,certi,buidl,ot,te,buda,shs,welp --quiet-level=2 --skip=./tests,.git,*.css,*.csv,*.html,*.ini,*.ipynb,*.js,*.json,*.lock,*.scss,*.txt,*.yaml,./build/pyinstaller,./website/config.toml
- run: codespell --ignore-words-list=vai,mis,gard,commun,statics,ro,zar,zlot,jewl,ba,buil,coo,ether,hist,hsi,mape,navagation,operatio,pres,ser,yeld,shold,ist,varian,datas,ake,creat,vie,hel,ket,toke,certi,buidl,ot,te,buda,shs,welp --quiet-level=2 --skip=./tests,.git,*.css,*.csv,*.html,*.ini,*.ipynb,*.js,*.json,*.lock,*.scss,*.txt,*.yaml,./build/pyinstaller,./website/config.toml -x openbb_terminal/economy/fedreserve_model.py
- run: ruff .
- run: mypy --ignore-missing-imports openbb_terminal
- run: pylint terminal.py openbb_terminal tests

View File

@@ -27,6 +27,8 @@ repos:
"--ignore-words-list=VAI,MIS,shs,gard,te,commun,ro,zar,vie,hel,jewl,zlot,ba,buil,coo,ether,hist,hsi,mape,navagation,operatio,pres,ser,yeld,shold,ist,varian,datas,ake,creat,statics,ket,toke,certi,buidl,ot",
"--quiet-level=2",
"--skip=./tests,.git,*.css,*.csv,*.html,*.ini,*.ipynb,*.js,*.json,*.lock,*.scss,*.txt,*.yaml,build/pyinstaller/*,./website/config.toml",
"-x=openbb_terminal/economy/fedreserve_model.py",
"-x=.github/workflows/linting.yml"
]
- repo: local
hooks:

View File

@@ -51,9 +51,8 @@ class EconomyRoot(Category):
`search_index`: Search indices by keyword. [Source: FinanceDatabase]\n
`spending`: General government spending provides an indication of the size\n
`spending_chart`: General government spending provides an indication of the size\n
`treasury`: Get U.S. Treasury rates [Source: EconDB]\n
`treasury`: Get treasury rates from Federal Reserve\n
`treasury_chart`: Display U.S. Treasury rates [Source: EconDB]\n
`treasury_maturities`: Get treasury maturity options [Source: EconDB]\n
`trust`: Trust in government refers to the share of people who report having confidence in\n
`trust_chart`: Trust in government refers to the share of people who report having confidence in\n
`usbonds`: Scrape data for us bonds\n
@@ -108,9 +107,8 @@ class EconomyRoot(Category):
self.search_index = lib.economy_yfinance_model.get_search_indices
self.spending = lib.economy_oecd_model.get_spending
self.spending_chart = lib.economy_oecd_view.plot_spending
self.treasury = lib.economy_econdb_model.get_treasuries
self.treasury_chart = lib.economy_econdb_view.show_treasuries
self.treasury_maturities = lib.economy_econdb_model.get_treasury_maturities
self.treasury = lib.economy_fedreserve_model.get_treasury_rates
self.treasury_chart = lib.economy_fedreserve_view.show_treasuries
self.trust = lib.economy_oecd_model.get_trust
self.trust_chart = lib.economy_oecd_view.plot_trust
self.usbonds = lib.economy_wsj_model.us_bonds

View File

@@ -338,6 +338,8 @@ from openbb_terminal.economy import (
yfinance_view as economy_yfinance_view,
oecd_model as economy_oecd_model,
oecd_view as economy_oecd_view,
fedreserve_model as economy_fedreserve_model,
fedreserve_view as economy_fedreserve_view,
)

View File

@@ -216,8 +216,7 @@ economy.revenue,economy_oecd_model.get_revenue,economy_oecd_view.plot_revenue
economy.rgdp,economy_oecd_model.get_real_gdp,economy_oecd_view.plot_real_gdp
economy.search_index,economy_yfinance_model.get_search_indices,
economy.spending,economy_oecd_model.get_spending,economy_oecd_view.plot_spending
economy.treasury,economy_econdb_model.get_treasuries,economy_econdb_view.show_treasuries
economy.treasury_maturities,economy_econdb_model.get_treasury_maturities,
economy.treasury,economy_fedreserve_model.get_treasury_rates,economy_fedreserve_view.show_treasuries
economy.trust,economy_oecd_model.get_trust,economy_oecd_view.plot_trust
economy.usbonds,economy_wsj_model.us_bonds,
economy.usdli,economy_fred_model.get_usd_liquidity,economy_fred_view.display_usd_liquidity
1 trail model view
216 economy.rgdp economy_oecd_model.get_real_gdp economy_oecd_view.plot_real_gdp
217 economy.search_index economy_yfinance_model.get_search_indices
218 economy.spending economy_oecd_model.get_spending economy_oecd_view.plot_spending
219 economy.treasury economy_econdb_model.get_treasuries economy_fedreserve_model.get_treasury_rates economy_econdb_view.show_treasuries economy_fedreserve_view.show_treasuries
economy.treasury_maturities economy_econdb_model.get_treasury_maturities
220 economy.trust economy_oecd_model.get_trust economy_oecd_view.plot_trust
221 economy.usbonds economy_wsj_model.us_bonds
222 economy.usdli economy_fred_model.get_usd_liquidity economy_fred_view.display_usd_liquidity

View File

@@ -23,6 +23,8 @@ from openbb_terminal.economy import (
econdb_model,
econdb_view,
economy_helpers,
fedreserve_model,
fedreserve_view,
finviz_model,
finviz_view,
fred_model,
@@ -235,10 +237,6 @@ class EconomyController(BaseController):
}
choices["trust"]["-c"] = "--countries"
choices["treasury"]["--type"] = {
c: {} for c in econdb_model.TREASURIES["instruments"]
}
choices["treasury"]["-t"] = "--type"
choices["macro"]["--parameters"] = {c: {} for c in econdb_model.PARAMETERS}
choices["macro"]["-p"] = "--parameters"
choices["macro"]["--countries"] = {
@@ -257,7 +255,10 @@ class EconomyController(BaseController):
c: {} for c in nasdaq_model.get_country_names()
}
choices["events"]["-c"] = "--countries"
choices["treasury"]["--maturity"] = {
c: None for c in fedreserve_model.all_mat
}
choices["treasury"]["-m"] = "--maturity"
self.choices = choices
self.completer = NestedCompleter.from_nested_dict(choices)
@@ -1616,9 +1617,7 @@ class EconomyController(BaseController):
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog="treasury",
description="Obtain any set of U.S. treasuries and plot them together. These can be a range of maturities "
"for nominal, inflation-adjusted (on long term average of inflation adjusted) and secondary "
"markets over a lengthy period. Note: 3-month and 10-year treasury yields for other countries "
description="Obtain US treasury rates. Note: 3-month and 10-year treasury yields for other countries "
"are available via the command 'macro' and parameter 'M3YD' and 'Y10YD'. [Source: EconDB / FED]",
)
parser.add_argument(
@@ -1636,22 +1635,6 @@ class EconomyController(BaseController):
action="store_true",
default=False,
)
parser.add_argument(
"--freq",
type=str,
dest="frequency",
choices=econdb_model.TREASURIES["frequencies"],
help="The frequency, this can be annually, monthly, weekly or daily",
default="monthly",
)
parser.add_argument(
"-t",
"--type",
type=str,
dest="type",
help="Choose from: nominal, inflation, average, secondary",
default="nominal",
)
parser.add_argument(
"-s",
"--start",
@@ -1677,65 +1660,23 @@ class EconomyController(BaseController):
)
if ns_parser:
maturities = list_from_str(ns_parser.maturity)
types = list_from_str(ns_parser.type)
for item in types:
if item not in econdb_model.TREASURIES["instruments"]:
print(f"{item} is not a valid instrument type.\n")
return self.queue
if ns_parser.show_maturities:
econdb_view.show_treasury_maturities()
return self.queue
console.print(",".join(fedreserve_model.all_mat))
return None
if ns_parser.maturity and ns_parser.type:
df = econdb_model.get_treasuries(
instruments=types,
maturities=maturities,
frequency=ns_parser.frequency,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
)
fedreserve_view.show_treasuries(
maturities=maturities,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
raw=ns_parser.raw,
export=ns_parser.export,
sheet_name=" ".join(ns_parser.sheet_name)
if ns_parser.sheet_name
else None,
)
if not df.empty:
cols = []
for column in df.columns:
if isinstance(column, tuple):
cols.append("_".join(column))
else:
cols.append(column)
df.columns = cols
for column in df.columns:
if column in self.DATASETS["treasury"].columns:
self.DATASETS["treasury"].drop(column, axis=1, inplace=True)
self.DATASETS["treasury"] = pd.concat(
[
self.DATASETS["treasury"],
df,
],
axis=1,
)
self.stored_datasets = (
economy_helpers.update_stored_datasets_string(self.DATASETS)
)
econdb_view.show_treasuries(
instruments=types,
maturities=maturities,
frequency=ns_parser.frequency,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
raw=ns_parser.raw,
export=ns_parser.export,
sheet_name=" ".join(ns_parser.sheet_name)
if ns_parser.sheet_name
else None,
)
self.update_runtime_choices()
if get_current_user().preferences.ENABLE_EXIT_AUTO_HELP:
self.print_help()
if get_current_user().preferences.ENABLE_EXIT_AUTO_HELP:
self.print_help()
@log_start_end(log=logger)
def call_cpi(self, other_args: List[str]):
@@ -2459,7 +2400,7 @@ class EconomyController(BaseController):
type=str,
nargs="+",
dest="query",
required="-h" not in other_args,
required="-h" not in other_args and "--help" not in other_args,
help="Query to evaluate on loaded datasets",
)
if other_args and "-" not in other_args[0][0]:

View File

@@ -0,0 +1,82 @@
from datetime import datetime
from io import BytesIO
from typing import List, Literal, Optional, Union
import numpy as np
import pandas as pd
from openbb_terminal.helper_funcs import request
maturities = Literal[
"1m",
"3m",
"6m",
"1y",
"2y",
"3y",
"5y",
"7y",
"10y",
"20y",
"30y",
]
maturityType = Union[maturities, List[maturities]]
all_mat = [
"1m",
"3m",
"6m",
"1y",
"2y",
"3y",
"5y",
"7y",
"10y",
"20y",
"30y",
]
def get_treasury_rates(
maturity: Optional[maturityType] = None,
start_date: str = "2005-01-01",
end_date: Optional[str] = datetime.now().strftime("%Y-%m-%d"),
) -> pd.DataFrame:
"""Get treasury rates from Federal Reserve
Parameters
----------
maturity : maturityType, optional
Maturity to get, by default all
start_date : str, optional
Start date of data, by default "2005-01-01"
end_date : str, optional
End date , by default today
Returns
-------
pd.DataFrame
Dataframe with date as index and maturity as columns
Examples
--------
>>> from openbb_terminal.sdk import openbb
>>> treasury_rates = openbb.economy.treasury()
"""
url = (
"https://www.federalreserve.gov/datadownload/Output.aspx?rel=H15&series=bf17364827e38702b42a58cf8eaa3f78"
"&lastobs=&from=&to=&filetype=csv&label=include&layout=seriescolumn"
)
r = request(url)
df = pd.read_csv(BytesIO(r.content), header=5, index_col=None, parse_dates=True)
df.columns = ["date"] + all_mat
df = df.replace("ND", np.nan).fillna("-").dropna(axis=0)
df = df[
(pd.to_datetime(df.date) >= pd.to_datetime(start_date))
& (pd.to_datetime(df.date) <= pd.to_datetime(end_date))
]
df[all_mat] = df[all_mat].applymap(lambda x: float(x) if x != "-" else x)
df["date"] = pd.to_datetime(df["date"])
df = df.reset_index(drop=True).set_index("date")
if not maturity:
return df
return df[maturity]

View File

@@ -0,0 +1,114 @@
import logging
import os
from datetime import datetime
from typing import List, Literal, Optional, Union
import numpy as np
from openbb_terminal import OpenBBFigure
from openbb_terminal.decorators import log_start_end
from openbb_terminal.economy import fedreserve_model
from openbb_terminal.helper_funcs import export_data, print_rich_table
from openbb_terminal.rich_config import console
logger = logging.getLogger(__name__)
maturities_types = Literal[
"1m",
"3m",
"6m",
"1y",
"2y",
"3y",
"5y",
"7y",
"10y",
"20y",
"30y",
]
maturityType = Union[maturities_types, List[maturities_types]]
@log_start_end(log=logger)
def show_treasuries(
maturities: maturityType = "1y",
start_date: str = "1900-01-01",
end_date: Optional[str] = datetime.now().strftime("%Y-%m-%d"),
raw: bool = False,
external_axes: bool = False,
export: str = "",
sheet_name: Optional[str] = None,
) -> Union[OpenBBFigure, None]:
"""Display U.S. Treasury rates [Source: EconDB]
Parameters
----------
maturities : list
Treasury maturities to display.
start_date : str
Starting date, format "YEAR-MONTH-DAY", i.e. 2010-12-31.
end_date : Optional[str]
End date, format "YEAR-MONTH-DAY", i.e. 2020-06-05.
raw : bool
Whether to display the raw output.
external_axes : bool, optional
Whether to return the figure object or not, by default False
export : str
Export data to csv,json,xlsx or png,jpg,pdf,svg file
Returns
-------
Plots the Treasury Series.
"""
treasury_data = fedreserve_model.get_treasury_rates(
maturity=maturities, start_date=start_date, end_date=end_date
)
if treasury_data.empty:
console.print("No data found matching the input.\n")
return None
# This drops any nan values so that the ful index isn't shown when not all data present
treasury_data = treasury_data.replace("-", np.nan).dropna(how="all")
title = (
"U.S. Treasuries"
if treasury_data.shape[1] > 1
else f"{maturities[0].replace('m',' Month').replace('y',' Year')} U.S. Treasury Rate"
)
fig = OpenBBFigure(
yaxis=dict(side="right", title="Yield (%)"),
title=title,
)
for col in treasury_data.columns:
fig.add_scatter(
x=treasury_data.index,
y=treasury_data[col],
mode="lines",
name=col.replace("m", " Month").replace("y", " Year"),
)
if raw:
# was a -iloc so we need to flip the index as we use head
treasury_data = treasury_data.sort_index(ascending=False)
print_rich_table(
treasury_data,
headers=list(treasury_data.columns),
show_index=True,
title="U.S. Treasuries",
export=bool(export),
)
if export:
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"treasuries_data",
treasury_data,
sheet_name,
fig,
)
return fig.show(external=raw or external_axes)

View File

@@ -1,25 +1,19 @@
parent_command:economy
usage: treasury [-m MATURITY] [--show] [--freq {annually,monthly,weekly,daily}] [-t TYPE] [-s START_DATE] [-e END_DATE]
Obtain any set of U.S. treasuries and plot them together. These can be a range of maturities for nominal, inflation-adjusted (on long term average of inflation adjusted) and secondary markets over a lengthy period. Note: 3-month
and 10-year treasury yields for other countries are available via the command 'macro' and parameter 'M3YD' and 'Y10YD'. [Source: EconDB / FED]
Obtain US treasury rates
optional arguments:
-m MATURITY, --maturity MATURITY
The preferred maturity which is dependent on the type of the treasury (default: 10y)
--show Show the maturities available for every instrument. (default: False)
--freq {annually,monthly,weekly,daily}
The frequency, this can be annually, monthly, weekly or daily (default: monthly)
-t TYPE, --type TYPE Choose from: nominal, inflation, average, secondary (default: nominal)
-s START_DATE, --start START_DATE
The start date of the data (format: YEAR-MONTH-DAY, i.e. 2010-12-31) (default: 1934-01-31)
-e END_DATE, --end END_DATE
The end date of the data (format: YEAR-DAY-MONTH, i.e. 2021-06-02) (default: 2023-06-02)
Examples:
- To plot 10-year nominal U.S. treasuries with monthly frequency: economy/treasury
- For plotting 5-year inflation-adjusted U.S. treasuries with daily frequency: economy/treasury -m 5y --freq daily -t inflation
- To view available maturities for each instrument type: economy/treasury --show
- Plot secondary market U.S. treasuries with a 2-year maturity and weekly frequency: economy/treasury -m 2y -t secondary --freq weekly
- To plot average U.S. treasuries with a 30-year maturity, monthly frequency, and specified date range: economy/treasury -m 30y -t average --freq monthly -s 2000-01-01 -e 2020-12-31
Examples:
- To plot 10-year nominal U.S. treasuries with monthly frequency: economy/treasury
- For plotting 5-year U.S. treasury rates: economy/treasury -m 5y
- To show the maturities available for every instrument: economy/treasury --show

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -527,7 +527,7 @@
"revenue": ["OECD"],
"spending": ["OECD"],
"trust": ["OECD"],
"treasury": ["EconDB"],
"treasury": ["FED"],
"fred": ["FRED"],
"index": ["YahooFinance"]
},

View File

@@ -267,16 +267,6 @@
" ycrv = None"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31fb0f86",
"metadata": {},
"outputs": [],
"source": [
"openbb.economy.treasury_maturities()"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -289,29 +279,23 @@
" 2,\n",
" vertical_spacing=0.15,\n",
" row_heights=[0.5, 0.5],\n",
" subplot_titles=(\"UST 2y-10y\", \"UST 10y-30y\", \"Nominal vs TIPS\"),\n",
" subplot_titles=(\"UST 1m-1y\", \"UST 10y-30y\", \"UST 1y-10y\"),\n",
" specs=[[{\"type\": \"xy\"}, {\"type\": \"xy\"}], [{\"colspan\": 2}, None]],\n",
")\n",
"openbb.economy.treasury_chart(\n",
" instruments=[\"nominal\"],\n",
" maturities=[\"2y\", \"10y\"],\n",
" frequency=\"daily\",\n",
" maturities=[\"1m\", \"3m\", \"6m\", \"1y\"],\n",
" start_date=\"2020-01-01\",\n",
" external_axes=True,\n",
").to_subplot(subplots, 1, 1)\n",
"\n",
"openbb.economy.treasury_chart(\n",
" instruments=[\"nominal\"],\n",
" maturities=[\"10y\", \"30y\"],\n",
" frequency=\"daily\",\n",
" maturities=[\"10y\", \"20y\", \"30y\"],\n",
" start_date=\"2020-01-01\",\n",
" external_axes=True,\n",
").to_subplot(subplots, 1, 2)\n",
"\n",
"openbb.economy.treasury_chart(\n",
" instruments=[\"nominal\", \"inflation\"],\n",
" maturities=[\"10y\"],\n",
" frequency=\"daily\",\n",
" maturities=[\"1y\",\"5y\",\"7y\", \"10y\"],\n",
" start_date=\"2020-01-01\",\n",
" external_axes=True,\n",
").to_subplot(subplots, 2, 1)\n",
@@ -657,7 +641,7 @@
"body += widgets.add_tab(\"Events\", htmlcode)\n",
"\n",
"# Yields\n",
"htmlcode = widgets.row([widgets.h(3, \"US treasuries (source: EconDB)\") + ust])\n",
"htmlcode = widgets.row([widgets.h(3, \"US treasuries (source: FED)\") + ust])\n",
"\n",
"if ycrv:\n",
" htmlcode += widgets.row(\n",

View File

@@ -213,9 +213,8 @@ class OpenBBSDK:
`search_index`: Search indices by keyword. [Source: FinanceDatabase]\n
`spending`: General government spending provides an indication of the size\n
`spending_chart`: General government spending provides an indication of the size\n
`treasury`: Get U.S. Treasury rates [Source: EconDB]\n
`treasury`: Get treasury rates from Federal Reserve\n
`treasury_chart`: Display U.S. Treasury rates [Source: EconDB]\n
`treasury_maturities`: Get treasury maturity options [Source: EconDB]\n
`trust`: Trust in government refers to the share of people who report having confidence in\n
`trust_chart`: Trust in government refers to the share of people who report having confidence in\n
`usbonds`: Scrape data for us bonds\n

View File

@@ -1189,9 +1189,8 @@ def test_call_index(mocker):
MOCK_TREASURY_DEFAULT = pd.DataFrame.from_dict(
{
"Nominal_3-month": {"2022-10-01": 3.87},
"Nominal_10-year": {"2022-10-01": 3.87},
"Long-term average_Longer than 10-year": {"2022-10-01": 1.9},
"3m": {"2022-10-01": 3.87},
"10y": {"2022-10-01": 3.87},
},
orient="index",
)
@@ -1202,13 +1201,11 @@ MOCK_TREASURY_DEFAULT = pd.DataFrame.from_dict(
"other_args, mocked_func, called_args, called_kwargs",
[
(
["-m=3m,10y", "-t=nominal,average", "-e=2022-11-04"],
"econdb_view.show_treasuries",
["-m=3m,10y", "-e=2022-11-04"],
"fedreserve_view.show_treasuries",
[],
dict(
maturities=["3m", "10y"],
instruments=["nominal", "average"],
frequency="monthly",
start_date="1934-01-31",
end_date="2022-11-04",
raw=False,
@@ -1228,7 +1225,7 @@ def test_call_treasury(mocked_func, other_args, called_args, called_kwargs, mock
mocker.patch(target=f"{path_controller}.EconomyController.update_runtime_choices")
# MOCK the econdb.get_aggregated_macro_data
mocker.patch(
target=f"{path_controller}.econdb_model.get_treasuries",
target=f"{path_controller}.fedreserve_model.get_treasury_rates",
return_value=MOCK_TREASURY_DEFAULT,
)
preferences = PreferencesModel(ENABLE_EXIT_AUTO_HELP=False)
@@ -1247,8 +1244,6 @@ def test_call_treasury(mocked_func, other_args, called_args, called_kwargs, mock
controller = economy_controller.EconomyController(queue=None)
controller.choices = {}
controller.call_treasury(other_args)
assert "treasury" in controller.DATASETS
assert not controller.DATASETS["treasury"].empty
if called_args or called_kwargs:
mock.assert_called_once_with(*called_args, **called_kwargs)
else:

View File

@@ -23,7 +23,7 @@ Country Performance:
Databases:
macro collect macro data for a country or countries
treasury obtain U.S. treasury rates [EconDB]
treasury obtain U.S. treasury rates [FED]
fred collect macro data from FRED based on a series ID [FRED]
index find and plot any (major) index on the market [YahooFinance]

View File

@@ -1,6 +1 @@
Maturities
Instrument
nominal 1m, 3m, 6m, 1y, 2y, 3y, 5y, 7y, 10y, 20y, 30y
inflation 5y, 7y, 10y, 20y, 30y
average Defined by function
secondary 4w, 3m, 6m, 1y
1m,3m,6m,1y,2y,3y,5y,7y,10y,20y,30y