-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds yield curves * Adds yield curves for several countries * Adds yield curves for several countries * Adds yield curves for several countries * Adds yield curves for several countries * Adds yield curves for several countries * ycrv plots by default * Limits source choices and renames raw columns * Fix test * Fix test * lint Co-authored-by: Jeroen Bouma <jer.bouma@gmail.com> Co-authored-by: jose-donato <43375532+jose-donato@users.noreply.github.com> Co-authored-by: didierlopes.eth <dro.lopes@campus.fct.unl.pt> Co-authored-by: James Maslek <jmaslek11@gmail.com>
- Loading branch information
1 parent
26781cb
commit 4f692f2
Showing
12 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" Investing.com Model """ | ||
__docformat__ = "numpy" | ||
|
||
import logging | ||
import argparse | ||
|
||
import pandas as pd | ||
import investpy | ||
|
||
from openbb_terminal.decorators import log_start_end | ||
from openbb_terminal.helper_funcs import log_and_raise | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
COUNTRIES = investpy.bonds.get_bond_countries() | ||
|
||
|
||
def check_correct_country(country): | ||
"""Argparse type to check that correct country is inserted""" | ||
if country not in investpy.bonds.get_bond_countries(): | ||
log_and_raise( | ||
argparse.ArgumentTypeError( | ||
f"{country} is an invalid country. Choose from {', '.join(investpy.bonds.get_bond_countries())}" | ||
) | ||
) | ||
return country | ||
|
||
|
||
@log_start_end(log=logger) | ||
def get_yieldcurve(country) -> pd.DataFrame: | ||
"""Get country yield curve [Source: Investing.com] | ||
Returns | ||
------- | ||
pd.DataFrame | ||
Country yield curve | ||
""" | ||
|
||
data = investpy.bonds.get_bonds_overview(country) | ||
data.drop(columns=data.columns[0], axis=1, inplace=True) | ||
data.rename( | ||
columns={ | ||
"name": "Tenor", | ||
"last": "Current", | ||
"last_close": "Previous", | ||
"high": "High", | ||
"low": "Low", | ||
"change": "Change", | ||
"change_percentage": "% Change", | ||
}, | ||
inplace=True, | ||
) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
""" Investing.com View """ | ||
__docformat__ = "numpy" | ||
|
||
import logging | ||
import os | ||
from typing import Optional, List | ||
|
||
import matplotlib.pyplot as plt | ||
from pandas.plotting import register_matplotlib_converters | ||
|
||
from openbb_terminal.config_plot import PLOT_DPI | ||
from openbb_terminal.config_terminal import theme | ||
from openbb_terminal.decorators import log_start_end | ||
from openbb_terminal.economy import investingcom_model | ||
from openbb_terminal.helper_funcs import ( | ||
export_data, | ||
plot_autoscale, | ||
print_rich_table, | ||
) | ||
from openbb_terminal.rich_config import console | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
register_matplotlib_converters() | ||
|
||
tenors_dict = { | ||
"1M": 1 / 12, | ||
"3M": 0.25, | ||
"6M": 0.5, | ||
"9M": 0.75, | ||
"1Y": 1, | ||
"2Y": 2, | ||
"3Y": 3, | ||
"4Y": 4, | ||
"5Y": 5, | ||
"6Y": 6, | ||
"7Y": 7, | ||
"8Y": 8, | ||
"9Y": 9, | ||
"10Y": 10, | ||
"15Y": 15, | ||
"20Y": 20, | ||
"25Y": 25, | ||
"30Y": 30, | ||
"50Y": 50, | ||
} | ||
|
||
|
||
@log_start_end(log=logger) | ||
def display_yieldcurve( | ||
country: str, | ||
external_axes: Optional[List[plt.Axes]] = None, | ||
raw: bool = False, | ||
export: str = "", | ||
): | ||
"""Display yield curve. [Source: Investing.com] | ||
Parameters | ||
---------- | ||
country: str | ||
Country to display | ||
export : str | ||
Export dataframe data to csv,json,xlsx file | ||
""" | ||
|
||
df = investingcom_model.get_yieldcurve(country) | ||
df = df.replace(float("NaN"), "") | ||
|
||
if df.empty: | ||
console.print(f"[red]Yield data not found for {country.title()}[/red].\n") | ||
return | ||
if external_axes is None: | ||
_, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI) | ||
|
||
else: | ||
if len(external_axes) != 1: | ||
logger.error("Expected list of 3 axis items") | ||
console.print("[red]Expected list of 3 axis items.\n[/red]") | ||
return | ||
(ax,) = external_axes | ||
|
||
tenors = [] | ||
for i, row in df.iterrows(): | ||
t = row["Tenor"][-3:].strip() | ||
df.at[i, "Tenor"] = t | ||
if t[-1] == "M": | ||
tenors.append(int(t[:-1]) / 12) | ||
elif t[-1] == "Y": | ||
tenors.append(int(t[:-1])) | ||
|
||
ax.plot(tenors, df["Current"], "-o") | ||
ax.set_xlabel("Maturity") | ||
ax.set_ylabel("Rate (%)") | ||
theme.style_primary_axis(ax) | ||
if external_axes is None: | ||
ax.set_title(f"Yield Curve for {country.title()} ") | ||
theme.visualize_output() | ||
|
||
if raw: | ||
print_rich_table( | ||
df, | ||
headers=list(df.columns), | ||
show_index=False, | ||
title=f"{country.title()} Yield Curve", | ||
floatfmt=".3f", | ||
) | ||
console.print("") | ||
|
||
export_data( | ||
export, | ||
os.path.dirname(os.path.abspath(__file__)), | ||
"ycrv", | ||
df, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# IMPORTATION STANDARD | ||
|
||
# IMPORTATION THIRDPARTY | ||
import pandas as pd | ||
import pytest | ||
|
||
# IMPORTATION INTERNAL | ||
from openbb_terminal.economy import investingcom_model | ||
|
||
|
||
# @pytest.mark.vcr | ||
@pytest.mark.parametrize( | ||
"country", | ||
[ | ||
"united states", | ||
"portugal", | ||
"spain", | ||
"germany", | ||
], | ||
) | ||
def test_get_yieldcurve(country): | ||
result_df = investingcom_model.get_yieldcurve(country) | ||
|
||
assert isinstance(result_df, pd.DataFrame) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# IMPORTATION STANDARD | ||
# import gzip | ||
|
||
# IMPORTATION THIRDPARTY | ||
# import pandas as pd | ||
|
||
# import pytest | ||
|
||
# IMPORTATION INTERNAL | ||
from openbb_terminal.economy import investingcom_view | ||
|
||
|
||
# @pytest.mark.vcr | ||
# @pytest.mark.record_stdout | ||
def test_display_yieldcurve(): | ||
investingcom_view.display_yieldcurve(country="portugal", export="") |
1 change: 0 additions & 1 deletion
1
...ndb_view/test_show_treasuries[instruments0-maturities0-monthly-2020-01-01-2020-02-03].txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
The maturity 3y is not an option for inflation. Please choose between 5y, 7y, 10y, 20y, 30y | ||
|
1 change: 0 additions & 1 deletion
1
...db_view/test_show_treasuries[instruments1-maturities1-annually-2015-01-04-2015-01-28].txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
No data found for the combination nominal and 1m. | ||
No data found for the combination nominal and 30y. | ||
|
1 change: 0 additions & 1 deletion
1
...ondb_view/test_show_treasuries[instruments2-maturities2-weekly-2018-06-05-2018-07-06].txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
The maturity 3y is not an option for inflation. Please choose between 5y, 7y, 10y, 20y, 30y | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters