Add multiple countries option to events (#3938)

* allow multiple countries in events cmd

* fix test

* fix last test

* black

Co-authored-by: James Maslek <jmaslek11@gmail.com>
This commit is contained in:
montezdesousa
2023-01-14 00:10:20 +00:00
committed by GitHub
parent 19919189ec
commit 86fd7ae426
7 changed files with 71 additions and 46 deletions

View File

@@ -30,7 +30,6 @@ from openbb_terminal.economy import (
fred_model,
yfinance_model,
yfinance_view,
investingcom_model,
plot_view,
commodity_view,
)
@@ -196,6 +195,10 @@ class EconomyController(BaseController):
c: {} for c in nasdaq_model.get_country_codes()["Code"].values
}
choices["bigmac"]["-c"] = "--countries"
choices["events"]["--countries"] = {
c: {} for c in nasdaq_model.get_country_names()
}
choices["events"]["-c"] = "--countries"
self.choices = choices
self.completer = NestedCompleter.from_nested_dict(choices)
@@ -1053,16 +1056,21 @@ class EconomyController(BaseController):
)
parser.add_argument(
"-c",
"--country",
"--countries",
action="store",
dest="country",
choices=[
x.replace(" ", "_") for x in investingcom_model.CALENDAR_COUNTRIES
],
dest="countries",
type=str,
default="",
help="Display calendar for specific country.",
)
parser.add_argument(
"-n",
"--names",
help="Flag to show all available country names",
dest="names",
action="store_true",
default=False,
)
parser.add_argument(
"-s",
"--start",
@@ -1087,22 +1095,6 @@ class EconomyController(BaseController):
help="Get a specific date for events. Overrides start and end dates.",
default=None,
)
parser.add_argument(
"-i",
"--importance",
action="store",
dest="importance",
choices=investingcom_model.IMPORTANCES,
help="Event importance classified as high, medium, low or all.",
)
parser.add_argument(
"--categories",
action="store",
dest="category",
choices=investingcom_model.CATEGORIES,
default=None,
help="[INVESTING source only] Event category.",
)
ns_parser = self.parse_known_args_and_warn(
parser,
other_args,
@@ -1113,6 +1105,11 @@ class EconomyController(BaseController):
if ns_parser:
if ns_parser.names:
for name in nasdaq_model.get_country_names():
console.print(name)
return
if ns_parser.start_date:
start_date = ns_parser.start_date.strftime("%Y-%m-%d")
else:
@@ -1125,20 +1122,17 @@ class EconomyController(BaseController):
# TODO: Add `Investing` to sources again when `investpy` is fixed
countries = (
ns_parser.country.replace("_", " ").title().split(",")
if ns_parser.country
else []
)
if ns_parser.spec_date:
start_date = ns_parser.spec_date.strftime("%Y-%m-%d")
end_date = ns_parser.spec_date.strftime("%Y-%m-%d")
else:
start_date, end_date = sorted([start_date, end_date])
countries = list_from_str(ns_parser.countries)
nasdaq_view.display_economic_calendar(
country=countries,
countries=countries,
start_date=start_date,
end_date=end_date,
limit=ns_parser.limit,

View File

@@ -43,11 +43,11 @@ def get_economic_calendar(
--------
Get todays economic calendar for the United States
>>> from openbb_terminal.sdk import openbb
>>> calendar = openbb.economy.events("United States")
>>> calendar = openbb.economy.events("united_states")
To get multiple countries for a given date, pass the same start and end date as well as
a list of countries
>>> calendars = openbb.economy.events(["United States","Canada"], start_date="2022-11-18", end_date="2022-11-18")
>>> calendars = openbb.economy.events(["united_states", "canada"], start_date="2022-11-18", end_date="2022-11-18")
"""
if start_date is None:
@@ -60,6 +60,9 @@ def get_economic_calendar(
countries = []
if isinstance(countries, str):
countries = [countries]
countries = [country.replace("_", " ").title() for country in countries]
if start_date == end_date:
dates = [start_date]
else:
@@ -88,7 +91,14 @@ def get_economic_calendar(
return pd.DataFrame()
calendar = calendar.rename(
columns={"gmt": "Time (GMT)", "country": "Country", "eventName": "Event"}
columns={
"gmt": "Time (GMT)",
"country": "Country",
"eventName": "Event",
"actual": "Actual",
"consensus": "Consensus",
"previous": "Previous",
}
)
calendar = calendar.drop(columns=["description"])
@@ -97,7 +107,7 @@ def get_economic_calendar(
calendar = calendar[calendar["Country"].isin(countries)].reset_index(drop=True)
if calendar.empty:
console.print(f"[red]No data found for {','.join(countries)}[/red]")
console.print(f"[red]No data found for {', '.join(countries)}[/red]")
return pd.DataFrame()
return calendar
@@ -135,6 +145,22 @@ def get_country_codes() -> List[str]:
return codes
@log_start_end(log=logger)
def get_country_names() -> List[str]:
"""Get available country names in Nasdaq API
Returns
-------
List[str]
List of country names.
"""
file = os.path.join(os.path.dirname(__file__), "NASDAQ_CountryCodes.csv")
df = pd.read_csv(file, index_col=0)
countries = df["Country"]
countries_list = [x.lower().replace(" ", "_") for x in countries]
return countries_list
@log_start_end(log=logger)
@check_api_key(["API_KEY_QUANDL"])
def get_big_mac_index(country_code: str = "USA") -> pd.DataFrame:

View File

@@ -25,14 +25,18 @@ logger = logging.getLogger(__name__)
@log_start_end(log=logger)
def display_economic_calendar(
country: str, start_date: str, end_date: str, limit: int = 10, export: str = ""
countries: List[str],
start_date: str,
end_date: str,
limit: int = 10,
export: str = "",
) -> None:
"""Display economic calendar for specified country between start and end dates
Parameters
----------
country : str
Country to display calendar for
countries : List[str]
List of countries to include in calendar. Empty returns all
start_date : str
Start date for calendar
end_date : str
@@ -42,7 +46,7 @@ def display_economic_calendar(
export : str
Export data to csv or excel file
"""
df = nasdaq_model.get_economic_calendar(country, start_date, end_date)
df = nasdaq_model.get_economic_calendar(countries, start_date, end_date)
if df.empty:
return
print_rich_table(

View File

@@ -24,8 +24,9 @@ ycrv
ycrv --source FRED --date 2022-10-03 --raw
ycrv -c united_states --source Investing
events
events --country united_states -i high --cat employment -s 2022-07-07 -e 2022-07-08
events -c spain --source Investing
events --country united_states -s 2022-07-07 -e 2022-07-08
events -c spain,canada
events -n
edebt --limit 10
spread
spread --group PIIGS --color binary

View File

@@ -1,4 +1,4 @@
,Time (GMT),Country,Event,actual,consensus,previous,Date
,Time (GMT),Country,Event,Actual,Consensus,Previous,Date
0,08:30,United States,Continuing Jobless Claims,"1,385K","1,375K","1,364K",2022-10-20
1,08:30,United States,Initial Jobless Claims,214K,230K,226K,2022-10-20
2,08:30,United States,Jobless Claims 4-Week Avg.,212.25K,-,211.00K,2022-10-20
1 Time (GMT) Country Event actual Actual consensus Consensus previous Previous Date
2 0 08:30 United States Continuing Jobless Claims 1,385K 1,375K 1,364K 2022-10-20
3 1 08:30 United States Initial Jobless Claims 214K 230K 226K 2022-10-20
4 2 08:30 United States Jobless Claims 4-Week Avg. 212.25K - 211.00K 2022-10-20

View File

@@ -576,7 +576,7 @@ def test_call_func_expect_queue(expected_queue, func, queue):
"call_events",
[
"--export=csv",
"--country=united_states",
"--countries=united_states",
"--start=2022-10-20",
"--end=2022-10-21",
"--limit=10",
@@ -584,7 +584,7 @@ def test_call_func_expect_queue(expected_queue, func, queue):
"nasdaq_view.display_economic_calendar",
[],
dict(
country=["United States"],
countries=["united_states"],
start_date="2022-10-20",
end_date="2022-10-21",
export="csv",
@@ -595,18 +595,18 @@ def test_call_func_expect_queue(expected_queue, func, queue):
"call_events",
[
"--export=csv",
"--country=united_states",
"--countries=united_states,canada",
"--date=2023-10-20",
"--limit=10",
],
"nasdaq_view.display_economic_calendar",
[],
dict(
country=["United States"],
countries=["united_states", "canada"],
start_date="2023-10-20",
end_date="2023-10-20",
export="csv",
limit=10,
export="csv",
),
),
(

View File

@@ -1,4 +1,4 @@
Time (GMT) Country Event actual consensus previous Date
Time (GMT) Country Event Actual Consensus Previous Date
0 08:30 United States Continuing Jobless Claims 1,385K 1,375K 1,364K 2022-10-20
1 08:30 United States Initial Jobless Claims 214K 230K 226K 2022-10-20
2 08:30 United States Jobless Claims 4-Week Avg. 212.25K - 211.00K 2022-10-20