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

Ecocal improve #2225

Merged
merged 14 commits into from
Aug 4, 2022
25 changes: 15 additions & 10 deletions openbb_terminal/economy/economy_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,18 @@ def __init__(self, queue: List[str] = None):
c: None for c in econdb_model.COUNTRY_CODES
}

self.choices["ycrv"]["-c"] = {c: None for c in investingcom_model.COUNTRIES}
self.choices["ycrv"]["-c"] = {
c: None for c in investingcom_model.BOND_COUNTRIES
}
self.choices["ycrv"]["--countries"] = {
c: None for c in investingcom_model.COUNTRIES
c: None for c in investingcom_model.BOND_COUNTRIES
}

self.choices["ecocal"]["-c"] = {
c: None for c in investingcom_model.COUNTRIES
c: None for c in investingcom_model.CALENDAR_COUNTRIES
}
self.choices["ecocal"]["--countries"] = {
c: None for c in investingcom_model.COUNTRIES
c: None for c in investingcom_model.CALENDAR_COUNTRIES
}

self.choices["ecocal"]["-i"] = {
Expand Down Expand Up @@ -1021,7 +1023,9 @@ def call_ycrv(self, other_args: List[str]):

elif ns_parser.source == "investpy":

investingcom_model.check_correct_country(ns_parser.country)
investingcom_model.check_correct_country(
ns_parser.country, investingcom_model.BOND_COUNTRIES
)

investingcom_view.display_yieldcurve(
country=ns_parser.country,
Expand All @@ -1036,15 +1040,15 @@ def call_ecocal(self, other_args: List[str]):
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog="ecocal",
description="Economic calendar.",
description="Economic calendar. If no start or end dates, default is the current day.",
)
parser.add_argument(
"-c",
"--country",
action="store",
dest="country",
nargs="+",
default="united states",
default="all",
help="Display calendar for specific country.",
)
parser.add_argument(
Expand All @@ -1053,7 +1057,6 @@ def call_ecocal(self, other_args: List[str]):
action="store",
dest="importances",
choices=investingcom_model.IMPORTANCES,
default="all",
help="Event importance classified as high, medium, low or all.",
)
parser.add_argument(
Expand Down Expand Up @@ -1089,7 +1092,7 @@ def call_ecocal(self, other_args: List[str]):
other_args,
export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED,
raw=True,
limit=10,
limit=100,
)

if ns_parser:
Expand All @@ -1100,7 +1103,9 @@ def call_ecocal(self, other_args: List[str]):
if isinstance(ns_parser.categories, list):
ns_parser.categories = " ".join(ns_parser.categories)

investingcom_model.check_correct_country(ns_parser.country)
investingcom_model.check_correct_country(
ns_parser.country, investingcom_model.CALENDAR_COUNTRIES
)

investingcom_view.display_economic_calendar(
countries=ns_parser.country,
Expand Down
26 changes: 18 additions & 8 deletions openbb_terminal/economy/investingcom_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

logger = logging.getLogger(__name__)

COUNTRIES = investpy.bonds.get_bond_countries()
BOND_COUNTRIES = investpy.bonds.get_bond_countries()
CALENDAR_COUNTRIES = list(investpy.utils.constant.COUNTRY_ID_FILTERS.keys()) + ["all"]
CATEGORIES = [
"employment",
"credit",
Expand All @@ -31,13 +32,13 @@
IMPORTANCES = ["high", "medium", "low", "all"]


def check_correct_country(country):
def check_correct_country(country, countries):
"""Argparse type to check that correct country is inserted"""
if country.lower() not in investpy.bonds.get_bond_countries():
if country.lower() not in countries:
log_and_raise(
argparse.ArgumentTypeError(
f"{country} is an invalid country. Choose from \
{', '.join(investpy.bonds.get_bond_countries())}"
{', '.join(countries)}"
)
)
return country
Expand Down Expand Up @@ -114,6 +115,13 @@ def format_date(date: datetime.date) -> str:

return day + "/" + month + "/" + year

# Joint default for countries and importances
if countries == ["all"] and importances == []:
countries = CALENDAR_COUNTRIES[:-1]
importances = ["high"]
elif importances is None:
importances = ["all"]

if from_date and not to_date:
to_date_string = format_date(from_date + datetime.timedelta(days=7))
from_date_string = format_date(from_date)
Expand All @@ -124,9 +132,8 @@ def format_date(date: datetime.date) -> str:
from_date_string = format_date(from_date)
to_date_string = format_date(to_date)
else:
today = datetime.date.today()
from_date_string = format_date(today)
to_date_string = format_date(today + datetime.timedelta(days=7))
from_date_string = None
to_date_string = None

# Get user time zone in GMT offset format
user_time_zone = pytz.timezone(helper_funcs.get_user_timezone())
Expand Down Expand Up @@ -163,8 +170,11 @@ def format_date(date: datetime.date) -> str:

if not data.empty:
data.drop(columns=data.columns[0], axis=1, inplace=True)
data.sort_values(by="date", inplace=True)
data.drop_duplicates(keep="first", inplace=True)
data["date"] = data["date"].apply(
lambda date: date[-4:] + "-" + date[3:5] + "-" + date[:2]
)
data.sort_values(by=data.columns[0], inplace=True)

if importances:
if importances == ["all"]:
Expand Down
11 changes: 10 additions & 1 deletion openbb_terminal/economy/investingcom_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,21 @@ def display_economic_calendar(
console.print("[red]No data.[/red]\n")
else:
df.fillna(value="", inplace=True)
df.columns = df.columns.str.title()
if df["Zone"].eq(df["Zone"].iloc[0]).all():
del df["Zone"]
title = f"{countries.title()} economic calendar ({time_zone})"
else:
title = f"Economic Calendar ({time_zone})"
df["Zone"] = df["Zone"].str.title()

df["Importance"] = df["Importance"].str.title()

print_rich_table(
df[:limit],
headers=list(df.columns),
show_index=False,
title=f"Economic Calendar ({time_zone})",
title=title,
)

export_data(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
date time zone currency importance event actual forecast previous
0 08/07/2022 08:30 united states USD high Nonfarm Payrolls (Jun) 372K 268K 384K
1 08/07/2022 08:30 united states USD high Unemployment Rate (Jun) 3.6% 3.6% 3.6%
2 14/07/2022 08:30 united states USD high Initial Jobless Claims 240K 235K
Date Time Currency Importance Event Actual Forecast Previous
0 2022-07-08 08:30 USD High Nonfarm Payrolls (Jun) 372K 268K 384K
1 2022-07-08 08:30 USD High Unemployment Rate (Jun) 3.6% 3.6% 3.6%
2 2022-07-14 08:30 USD High Initial Jobless Claims 240K 235K