From 5ddb322ff2a771287fc9014e301c3f261616f013 Mon Sep 17 00:00:00 2001 From: didier Date: Sun, 2 May 2021 19:32:43 +0100 Subject: [PATCH] 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 " )