Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update treasury command to get directly from fed #5215

Merged
merged 16 commits into from
Jul 14, 2023
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions openbb_terminal/core/sdk/models/economy_sdk_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,9 +107,8 @@ def __init__(self):
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
Expand Down
2 changes: 2 additions & 0 deletions openbb_terminal/core/sdk/sdk_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@
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,
)


Expand Down
3 changes: 1 addition & 2 deletions openbb_terminal/core/sdk/trail_map.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 22 additions & 81 deletions openbb_terminal/economy/economy_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
econdb_model,
econdb_view,
economy_helpers,
fedreserve_model,
fedreserve_view,
finviz_model,
finviz_view,
fred_model,
Expand Down Expand Up @@ -235,10 +237,6 @@ def __init__(self, queue: Optional[List[str]] = None):
}
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"] = {
Expand All @@ -257,7 +255,10 @@ def __init__(self, queue: Optional[List[str]] = None):
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)

Expand Down Expand Up @@ -1616,9 +1617,7 @@ def call_treasury(self, other_args: List[str]):
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(
Expand All @@ -1636,22 +1635,6 @@ def call_treasury(self, other_args: List[str]):
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",
Expand All @@ -1677,65 +1660,23 @@ def call_treasury(self, other_args: List[str]):
)
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

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,
)

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,
)
console.print(",".join(fedreserve_model.all_mat))
return None

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,
)
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,
)

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]):
Expand Down Expand Up @@ -2459,7 +2400,7 @@ def call_eval(self, other_args):
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]:
Expand Down
82 changes: 82 additions & 0 deletions openbb_terminal/economy/fedreserve_model.py
Original file line number Diff line number Diff line change
@@ -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]
Loading
Loading