From 1413456bd967eeb4ad6cdd0ff87498ec50cd105a Mon Sep 17 00:00:00 2001 From: montezdesousa <79287829+montezdesousa@users.noreply.github.com> Date: Thu, 4 Aug 2022 09:29:47 +0100 Subject: [PATCH] Ecocal improve (#2225) * fix available countries and title * fix title * capitalize headers * display just one day by default * default all countries & high imporatnce * format output * fix display to standard date yyyy/mm/dd * rewrite expected ecocal test * change date format slash to dash * mypy * complete description Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Co-authored-by: minhhoang1023 <40023817+minhhoang1023@users.noreply.github.com> --- openbb_terminal/economy/economy_controller.py | 25 +++++++++++------- openbb_terminal/economy/investingcom_model.py | 26 +++++++++++++------ openbb_terminal/economy/investingcom_view.py | 11 +++++++- .../test_display_economic_calendar.txt | 8 +++--- 4 files changed, 47 insertions(+), 23 deletions(-) diff --git a/openbb_terminal/economy/economy_controller.py b/openbb_terminal/economy/economy_controller.py index ad408a78375..37a33281200 100644 --- a/openbb_terminal/economy/economy_controller.py +++ b/openbb_terminal/economy/economy_controller.py @@ -182,16 +182,18 @@ class EconomyController(BaseController): 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"] = { @@ -1021,7 +1023,9 @@ class EconomyController(BaseController): 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, @@ -1036,7 +1040,7 @@ class EconomyController(BaseController): 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", @@ -1044,7 +1048,7 @@ class EconomyController(BaseController): action="store", dest="country", nargs="+", - default="united states", + default="all", help="Display calendar for specific country.", ) parser.add_argument( @@ -1053,7 +1057,6 @@ class EconomyController(BaseController): action="store", dest="importances", choices=investingcom_model.IMPORTANCES, - default="all", help="Event importance classified as high, medium, low or all.", ) parser.add_argument( @@ -1089,7 +1092,7 @@ class EconomyController(BaseController): other_args, export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED, raw=True, - limit=10, + limit=100, ) if ns_parser: @@ -1100,7 +1103,9 @@ class EconomyController(BaseController): 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, diff --git a/openbb_terminal/economy/investingcom_model.py b/openbb_terminal/economy/investingcom_model.py index e928466f1a7..e228a8e7f69 100644 --- a/openbb_terminal/economy/investingcom_model.py +++ b/openbb_terminal/economy/investingcom_model.py @@ -17,7 +17,8 @@ from openbb_terminal import helper_funcs 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", @@ -31,13 +32,13 @@ CATEGORIES = [ 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 @@ -114,6 +115,13 @@ def get_economic_calendar( 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) @@ -124,9 +132,8 @@ def get_economic_calendar( 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()) @@ -163,8 +170,11 @@ def get_economic_calendar( 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"]: diff --git a/openbb_terminal/economy/investingcom_view.py b/openbb_terminal/economy/investingcom_view.py index 763b60f2975..a420427502e 100644 --- a/openbb_terminal/economy/investingcom_view.py +++ b/openbb_terminal/economy/investingcom_view.py @@ -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( diff --git a/tests/openbb_terminal/economy/txt/test_investingcom_view/test_display_economic_calendar.txt b/tests/openbb_terminal/economy/txt/test_investingcom_view/test_display_economic_calendar.txt index c0b203e686c..8379bd5d5fa 100644 --- a/tests/openbb_terminal/economy/txt/test_investingcom_view/test_display_economic_calendar.txt +++ b/tests/openbb_terminal/economy/txt/test_investingcom_view/test_display_economic_calendar.txt @@ -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