From 5ddb322ff2a771287fc9014e301c3f261616f013 Mon Sep 17 00:00:00 2001 From: didier Date: Sun, 2 May 2021 19:32:43 +0100 Subject: [PATCH 1/7] Economic calendar event impact --- gamestonk_terminal/config_terminal.py | 3 + gamestonk_terminal/econ/econ_controller.py | 7 ++ gamestonk_terminal/econ/finnhub_view.py | 136 +++++++++++++++++++++ gamestonk_terminal/main_helper.py | 2 +- 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 gamestonk_terminal/econ/finnhub_view.py diff --git a/gamestonk_terminal/config_terminal.py b/gamestonk_terminal/config_terminal.py index dd4092eb45d..f92788f94d5 100644 --- a/gamestonk_terminal/config_terminal.py +++ b/gamestonk_terminal/config_terminal.py @@ -59,3 +59,6 @@ PATH_TO_SELENIUM_DRIVER = None # Replace with "PATH" # https://coinmarketcap.com/api/ COINMARKETCAP_KEY = os.getenv("GT_CMC_API_KEY") or "REPLACE_ME" + +# https://finnhub.io +API_FINNHUB_KEY = os.getenv("GT_API_FINNHUB_KEY") or "REPLACE_ME" \ No newline at end of file diff --git a/gamestonk_terminal/econ/econ_controller.py b/gamestonk_terminal/econ/econ_controller.py index 2bdaf1e8727..9ec37eec7c7 100644 --- a/gamestonk_terminal/econ/econ_controller.py +++ b/gamestonk_terminal/econ/econ_controller.py @@ -9,6 +9,7 @@ from gamestonk_terminal import feature_flags as gtff from gamestonk_terminal.helper_funcs import get_flair from gamestonk_terminal.menu import session from gamestonk_terminal.econ import fred_view +from gamestonk_terminal.econ import finnhub_view class EconController: @@ -19,6 +20,7 @@ class EconController: "help", "q", "quit", + "events", "fred", "vixcls", "gdp", @@ -50,6 +52,7 @@ class EconController: print(" q quit this menu, and shows back to main menu") print(" quit quit to abandon program") print(" ") + print(" events economic events [Finnhub]") print( " fred display customized FRED data from https://fred.stlouisfed.org" ) @@ -95,6 +98,10 @@ class EconController: """Process Quit command - quit the program""" return True + def call_events(self, other_args: List[str]): + """Process events command""" + finnhub_view.economy_calendar_events(other_args) + def call_fred(self, other_args: List[str]): """Process fred command""" fred_view.display_fred(other_args, "") diff --git a/gamestonk_terminal/econ/finnhub_view.py b/gamestonk_terminal/econ/finnhub_view.py new file mode 100644 index 00000000000..8ec8ccdcef7 --- /dev/null +++ b/gamestonk_terminal/econ/finnhub_view.py @@ -0,0 +1,136 @@ +import argparse +from typing import List +import math +from datetime import datetime +import requests +import yfinance as yf +import mplfinance as mpf +import pandas as pd +from gamestonk_terminal import config_terminal as cfg +from gamestonk_terminal.helper_funcs import ( + parse_known_args_and_warn, + plot_autoscale, + check_positive, +) + + +def get_economy_calendar_events() -> pd.DataFrame: + """Get economic calendar events + + Returns + ------- + pd.DataFrame + Get dataframe with economic calendar events + """ + response = requests.get( + f"https://finnhub.io/api/v1/calendar/economic?token={cfg.API_FINNHUB_KEY}" + ) + if response.status_code == 200: + d_data = response.json() + if "economicCalendar" in d_data: + return pd.DataFrame(d_data["economicCalendar"]) + + return pd.DataFrame() + + +def economy_calendar_events(other_args: List[str]): + """Output economy calendar events + + Parameters + ---------- + other_args : List[str] + Command line arguments to be processed with argparse + """ + parser = argparse.ArgumentParser( + add_help=False, + prog="events", + description=""" + Output economy calendar events. [Source: https://finnhub.io] + """, + ) + parser.add_argument( + "-c", + "--country", + action="store", + dest="country", + type=str, + default="US", + choices=["NZ", "AU", "ERL", "CA", "EU", "US", "JP", "CN", "GB", "CH"], + help="Country from where to get economic calendar events", + ) + parser.add_argument( + "-n", + "--num", + action="store", + dest="num", + type=check_positive, + default=10, + help="Number economic calendar events to display", + ) + parser.add_argument( + "-i", + "--impact", + action="store", + dest="impact", + type=str, + default="all", + choices=["low", "medium", "high", "all"], + help="Country from where to get economic calendar events", + ) + + try: + if other_args: + if "-" not in other_args[0]: + other_args.insert(0, "-c") + + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + df_events = get_economy_calendar_events() + + if df_events.empty: + print("No latest economy calendar events found\n") + return + + df_econ_calendar = df_events[ + df_events["country"] == ns_parser.country + ].sort_values("time", ascending=False) + + if df_econ_calendar.empty: + print("No latet economy calendar events found in the specified country\n") + return + + if ns_parser.impact != "all": + df_econ_calendar = df_econ_calendar[ + df_econ_calendar["impact"] == ns_parser.impact + ] + + if df_econ_calendar.empty: + print( + "No latet economy calendar events found in the specified country with this impact\n" + ) + return + + df_econ_calendar = df_econ_calendar.fillna("---").head(n=ns_parser.num) + + d_econ_calendar_map = { + "actual": "Actual release", + "prev": "Previous release", + "country": "Country", + "unit": "Unit", + "estimate": "Estimate", + "event": "Event", + "impact": "Impact Level", + "time": "Release time", + } + + df_econ_calendar = df_econ_calendar[ + ["time", "event", "impact", "prev", "estimate", "actual", "unit"] + ].rename(columns=d_econ_calendar_map) + + print(df_econ_calendar.to_string(index=False)) + print("") + + except Exception as e: + print(e, "\n") diff --git a/gamestonk_terminal/main_helper.py b/gamestonk_terminal/main_helper.py index 824e14918b2..5723f7b3460 100644 --- a/gamestonk_terminal/main_helper.py +++ b/gamestonk_terminal/main_helper.py @@ -55,7 +55,7 @@ def print_help(s_ticker, s_start, s_interval, b_is_market_open): " > scr screener stocks, \t\t e.g. overview/performance, using preset filters" ) print(" > mill papermill menu, \t\t menu to generate notebook reports") - print(" > econ economic data, \t\t from: FRED, VIX") + print(" > econ economic data, \t\t e.g.: FRED, events") print( " > pa portfolio analysis, \t\t supports: robinhood, alpaca, ally " ) From 4132a1e4681b683d03b8fadf077d0cd9a38fe155 Mon Sep 17 00:00:00 2001 From: didier Date: Sun, 2 May 2021 19:34:28 +0100 Subject: [PATCH 2/7] improve description --- gamestonk_terminal/econ/econ_controller.py | 2 +- gamestonk_terminal/econ/finnhub_view.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gamestonk_terminal/econ/econ_controller.py b/gamestonk_terminal/econ/econ_controller.py index 9ec37eec7c7..1b47f16d6b1 100644 --- a/gamestonk_terminal/econ/econ_controller.py +++ b/gamestonk_terminal/econ/econ_controller.py @@ -52,7 +52,7 @@ class EconController: print(" q quit this menu, and shows back to main menu") print(" quit quit to abandon program") print(" ") - print(" events economic events [Finnhub]") + print(" events economic impact events [Finnhub]") print( " fred display customized FRED data from https://fred.stlouisfed.org" ) diff --git a/gamestonk_terminal/econ/finnhub_view.py b/gamestonk_terminal/econ/finnhub_view.py index 8ec8ccdcef7..ca86eb448e9 100644 --- a/gamestonk_terminal/econ/finnhub_view.py +++ b/gamestonk_terminal/econ/finnhub_view.py @@ -34,7 +34,7 @@ def get_economy_calendar_events() -> pd.DataFrame: def economy_calendar_events(other_args: List[str]): - """Output economy calendar events + """Output economy calendar impact events Parameters ---------- @@ -45,7 +45,7 @@ def economy_calendar_events(other_args: List[str]): add_help=False, prog="events", description=""" - Output economy calendar events. [Source: https://finnhub.io] + Output economy impact calendar impact events. [Source: https://finnhub.io] """, ) parser.add_argument( @@ -56,7 +56,7 @@ def economy_calendar_events(other_args: List[str]): type=str, default="US", choices=["NZ", "AU", "ERL", "CA", "EU", "US", "JP", "CN", "GB", "CH"], - help="Country from where to get economic calendar events", + help="Country from where to get economy calendar impact events", ) parser.add_argument( "-n", @@ -65,7 +65,7 @@ def economy_calendar_events(other_args: List[str]): dest="num", type=check_positive, default=10, - help="Number economic calendar events to display", + help="Number economy calendar impact events to display", ) parser.add_argument( "-i", @@ -75,7 +75,7 @@ def economy_calendar_events(other_args: List[str]): type=str, default="all", choices=["low", "medium", "high", "all"], - help="Country from where to get economic calendar events", + help="Impact of the economy event", ) try: From 8ccc9c1a9c326504eb6921423cf60bcace68ca69 Mon Sep 17 00:00:00 2001 From: didier Date: Sun, 2 May 2021 19:40:20 +0100 Subject: [PATCH 3/7] fix black --- gamestonk_terminal/config_terminal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gamestonk_terminal/config_terminal.py b/gamestonk_terminal/config_terminal.py index f92788f94d5..e305115bf48 100644 --- a/gamestonk_terminal/config_terminal.py +++ b/gamestonk_terminal/config_terminal.py @@ -61,4 +61,4 @@ PATH_TO_SELENIUM_DRIVER = None # Replace with "PATH" COINMARKETCAP_KEY = os.getenv("GT_CMC_API_KEY") or "REPLACE_ME" # https://finnhub.io -API_FINNHUB_KEY = os.getenv("GT_API_FINNHUB_KEY") or "REPLACE_ME" \ No newline at end of file +API_FINNHUB_KEY = os.getenv("GT_API_FINNHUB_KEY") or "REPLACE_ME" From 999d6818db9deab6f8b54f8ae4d52c7b50aa1dbe Mon Sep 17 00:00:00 2001 From: didier Date: Sun, 2 May 2021 19:46:50 +0100 Subject: [PATCH 4/7] flake8 remove unused imports --- gamestonk_terminal/econ/finnhub_view.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/gamestonk_terminal/econ/finnhub_view.py b/gamestonk_terminal/econ/finnhub_view.py index ca86eb448e9..2f4d45d6c32 100644 --- a/gamestonk_terminal/econ/finnhub_view.py +++ b/gamestonk_terminal/econ/finnhub_view.py @@ -1,15 +1,10 @@ import argparse from typing import List -import math -from datetime import datetime import requests -import yfinance as yf -import mplfinance as mpf import pandas as pd from gamestonk_terminal import config_terminal as cfg from gamestonk_terminal.helper_funcs import ( parse_known_args_and_warn, - plot_autoscale, check_positive, ) From 32f13f8e80099c1bad233c4b6d678c76a40682ec Mon Sep 17 00:00:00 2001 From: DidierRLopes Date: Sun, 2 May 2021 20:01:21 +0100 Subject: [PATCH 5/7] Update README.md --- gamestonk_terminal/econ/README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/gamestonk_terminal/econ/README.md b/gamestonk_terminal/econ/README.md index 1e6bb26f4b9..8bd5054abed 100644 --- a/gamestonk_terminal/econ/README.md +++ b/gamestonk_terminal/econ/README.md @@ -1,7 +1,9 @@ -# ECONONMY +# ECONOMY This menu aims to assess economy data, and the usage of the following commands along with an example will be exploited below. +* [events](#events) + * economic impact events [Finnhub] * [fred](#fred) * display customized FRED from https://fred.stlouisfed.org * [vixcls](#vixcls) @@ -28,6 +30,20 @@ This menu aims to assess economy data, and the usage of the following commands a * Canada / U.S. Foreign Exchange Rate (CAD per 1 USD) +## events +```text +usage: events [-c {NZ,AU,ERL,CA,EU,US,JP,CN,GB,CH}] [-i {low,medium,high,all}] [-n NUM] +``` + +Output economy impact calendar impact events. [Source: https://finnhub.io] + +* -c : Country from where to get economy calendar impact events, between NZ,AU,ERL,CA,EU,US,JP,CN,GB,CH. Default: US. +* -i : Impact of the economy event, between low,medium,high,all. Default: all. +* -n : Number economy calendar impact events to display. Default: 10. + +Captura de ecrã 2021-05-02, às 19 51 47 + + ## fred ```text usage: fred [-i SERIES_ID] [-s START_DATE] [-t] From d4fc5bb87211fd4195a3e739ed117945aec0e9bd Mon Sep 17 00:00:00 2001 From: DidierRLopes Date: Sun, 2 May 2021 20:05:15 +0100 Subject: [PATCH 6/7] Update README.md --- gamestonk_terminal/README.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/gamestonk_terminal/README.md b/gamestonk_terminal/README.md index 5034728b90c..df483d6ec66 100644 --- a/gamestonk_terminal/README.md +++ b/gamestonk_terminal/README.md @@ -328,19 +328,21 @@ Command|Description|Source   ## Economy [»](econ/README.md) -Command|Description ------- | -------- -`vixcls` | Volatility Index -`gdp` | Gross Domestic Product -`unrate` | Unemployment Rate -`dgs1` | 1-Year Treasury Constant Maturity Rate -`dgs5` | 5-Year Treasury Constant Maturity Rate -`dgs10` | 10-Year Treasury Constant Maturity Rate -`dgs30` | 30-Year Treasury Constant Maturity Rate -`mortgage30us` | 30-Year Fixed Rate Mortgage Average -`fedfunds` | Effective Federal Funds Rate -`aaa` | Moody's Seasoned AAA Corporate Bond Yield -`dexcaus` | Canada / U.S. Foreign Exchange Rate (CAD per 1 USD) +Command|Description|Source +------ | -------- | -------- +`events` | economic impact events | https://finnhub.io +`fred` | display customized FRED data | https://fred.stlouisfed.org +`vixcls` | Volatility Index | https://fred.stlouisfed.org +`gdp` | Gross Domestic Product | https://fred.stlouisfed.org +`unrate` | Unemployment Rate | https://fred.stlouisfed.org +`dgs1` | 1-Year Treasury Constant Maturity Rate | https://fred.stlouisfed.org +`dgs5` | 5-Year Treasury Constant Maturity Rate | https://fred.stlouisfed.org +`dgs10` | 10-Year Treasury Constant Maturity Rate | https://fred.stlouisfed.org +`dgs30` | 30-Year Treasury Constant Maturity Rate | https://fred.stlouisfed.org +`mortgage30us` | 30-Year Fixed Rate Mortgage Average | https://fred.stlouisfed.org +`fedfunds` | Effective Federal Funds Rate | https://fred.stlouisfed.org +`aaa` | Moody's Seasoned AAA Corporate Bond Yield | https://fred.stlouisfed.org +`dexcaus` | Canada / U.S. Foreign Exchange Rate (CAD per 1 USD) | https://fred.stlouisfed.org   From abd43a9d439014bb7940f10253261d52bb9a9b15 Mon Sep 17 00:00:00 2001 From: DidierRLopes Date: Sun, 2 May 2021 20:06:35 +0100 Subject: [PATCH 7/7] Update ROADMAP.md --- ROADMAP.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ROADMAP.md b/ROADMAP.md index f872efde669..0b59abfba5b 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -171,6 +171,7 @@ ___ ## Economy * [x] Implement Economic data (gpd, unemployment rate, ...) (@jmaslek) - [PR #167](https://github.com/DidierRLopes/GamestonkTerminal/pull/167) * [x] Refactor FRED to ECON menu and add VIX view (@jmaslek) - [PR #405](https://github.com/DidierRLopes/GamestonkTerminal/pull/405) +* [x] Output economic impact events calendar (@didier) - [PR #430](https://github.com/DidierRLopes/GamestonkTerminal/pull/430) **NEXT**