diff --git a/gamestonk_terminal/economy/README.md b/gamestonk_terminal/economy/README.md index d4c3aaaee97..4fc75f05f10 100644 --- a/gamestonk_terminal/economy/README.md +++ b/gamestonk_terminal/economy/README.md @@ -10,6 +10,11 @@ Command|Description|Source `usbonds` | US bonds overview | [Wall St. Journal](https://www.wsj.com) `glbonds` | global bonds overview | [Wall St. Journal](https://www.wsj.com) `currencies` | currencies overview | [Wall St. Journal](https://www.wsj.com) +`energy` | energy futures overview | [Finviz](https://finviz.com/futures.ashx) +`metals` | metals futures overview | [Finviz](https://finviz.com/futures.ashx) +`meats` | meats futures overview | [Finviz](https://finviz.com/futures.ashx) +`grains` | grains futures overview | [Finviz](https://finviz.com/futures.ashx) +`softs` | softs futures overview | [Finviz](https://finviz.com/futures.ashx) `map` | S&P500 index stocks map | [Finviz](https://finviz.com/map.ashx) `valuation` | valuation of sectors, industry, country | [Finviz](https://finviz.com/groups.ashx) `performance` | performance of sectors, industry, country | [Finviz](https://finviz.com/groups.ashx) diff --git a/gamestonk_terminal/economy/economy_controller.py b/gamestonk_terminal/economy/economy_controller.py index 88c8fc357dd..ac9ae8120f2 100644 --- a/gamestonk_terminal/economy/economy_controller.py +++ b/gamestonk_terminal/economy/economy_controller.py @@ -72,6 +72,11 @@ class EconomyController: "glbonds", "futures", "currencies", + "energy", + "metals", + "meats", + "grains", + "softs", "search", "series", "valuation", @@ -115,6 +120,11 @@ Wall St. Journal: glbonds global bonds overview currencies currencies overview Finviz: + energy energy futures overview + metals metals futures overview + meats meats futures overview + grains grains futures overview + softs softs futures overview map S&P500 index stocks map valuation valuation of sectors, industry, country performance performance of sectors, industry, country @@ -432,6 +442,136 @@ FRED: export=ns_parser.export, ) + @try_except + def call_energy(self, other_args: List[str]): + """Process energy command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="energy", + description="Energy future overview. [Source: Finviz]", + ) + parser.add_argument( + "--export", + choices=["csv", "json", "xlsx"], + default="", + type=str, + dest="export", + help="Export dataframe data to csv,json,xlsx file", + ) + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + finviz_view.display_future( + future_type="Energy", + export=ns_parser.export, + ) + + @try_except + def call_metals(self, other_args: List[str]): + """Process metals command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="metals", + description="Metals future overview. [Source: Finviz]", + ) + parser.add_argument( + "--export", + choices=["csv", "json", "xlsx"], + default="", + type=str, + dest="export", + help="Export dataframe data to csv,json,xlsx file", + ) + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + finviz_view.display_future( + future_type="Metals", + export=ns_parser.export, + ) + + @try_except + def call_meats(self, other_args: List[str]): + """Process meats command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="meats", + description="Meats future overview. [Source: Finviz]", + ) + parser.add_argument( + "--export", + choices=["csv", "json", "xlsx"], + default="", + type=str, + dest="export", + help="Export dataframe data to csv,json,xlsx file", + ) + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + finviz_view.display_future( + future_type="Meats", + export=ns_parser.export, + ) + + @try_except + def call_grains(self, other_args: List[str]): + """Process grains command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="grains", + description="Grains future overview. [Source: Finviz]", + ) + parser.add_argument( + "--export", + choices=["csv", "json", "xlsx"], + default="", + type=str, + dest="export", + help="Export dataframe data to csv,json,xlsx file", + ) + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + finviz_view.display_future( + future_type="Grains", + export=ns_parser.export, + ) + + @try_except + def call_softs(self, other_args: List[str]): + """Process softs command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="softs", + description="Softs future overview. [Source: Finviz]", + ) + parser.add_argument( + "--export", + choices=["csv", "json", "xlsx"], + default="", + type=str, + dest="export", + help="Export dataframe data to csv,json,xlsx file", + ) + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + finviz_view.display_future( + future_type="Softs", + export=ns_parser.export, + ) + @try_except def call_map(self, other_args: List[str]): """Process map command""" diff --git a/gamestonk_terminal/economy/finviz_model.py b/gamestonk_terminal/economy/finviz_model.py index c9ded457892..c86d2c0293e 100644 --- a/gamestonk_terminal/economy/finviz_model.py +++ b/gamestonk_terminal/economy/finviz_model.py @@ -1,12 +1,16 @@ """ Finviz Model """ __docformat__ = "numpy" +from ast import literal_eval +import requests import pandas as pd from finvizfinance.group import performance, spectrum, valuation +from gamestonk_terminal.helper_funcs import get_user_agent + def get_valuation_performance_data(group: str, data_type: str) -> pd.DataFrame: - """Get group (sectors, industry or country) valuation/performance data + """Get group (sectors, industry or country) valuation/performance data. [Source: Finviz] Parameters ---------- @@ -26,7 +30,7 @@ def get_valuation_performance_data(group: str, data_type: str) -> pd.DataFrame: def get_spectrum_data(group: str): - """Get group (sectors, industry or country) valuation/performance data + """Get group (sectors, industry or country) valuation/performance data. [Source: Finviz] Parameters ---------- @@ -34,3 +38,40 @@ def get_spectrum_data(group: str): sectors, industry or country """ spectrum.Spectrum().ScreenerView(group=group) + + +def get_futures() -> dict: + """Get futures data. [Source: Finviz] + + Parameters + ---------- + futures : dict + Indices, Energy, Metals, Meats, Grains, Softs, Bonds, Currencies + """ + source = requests.get( + "https://finviz.com/futures.ashx", headers={"User-Agent": get_user_agent()} + ).text + + slice_source = source[ + source.find("var groups = ") : source.find( + "\r\n\r\n groups.forEach(function(group) " + ) + ] + groups = literal_eval( + slice_source[ + : slice_source.find("\r\n var tiles = ") - 1 + ].strip("var groups = ") + ) + titles = literal_eval( + slice_source[ + slice_source.find("\r\n var tiles = ") : -1 + ].strip("\r\n var tiles = ") + ) + + d_futures: dict = {} + for future in groups: + d_futures[future["label"]] = [] + for ticker in future["contracts"]: + d_futures[future["label"]].append(titles[ticker["ticker"]]) + + return d_futures diff --git a/gamestonk_terminal/economy/finviz_view.py b/gamestonk_terminal/economy/finviz_view.py index 9a0ec054493..0bf64c11cb6 100644 --- a/gamestonk_terminal/economy/finviz_view.py +++ b/gamestonk_terminal/economy/finviz_view.py @@ -3,6 +3,7 @@ __docformat__ = "numpy" import os import webbrowser +import pandas as pd from PIL import Image from tabulate import tabulate @@ -34,7 +35,7 @@ def map_sp500_view(period: str, map_type: str): def view_group_data(s_group: str, data_type: str, export: str): - """View group (sectors, industry or country) valuation/performance/spectrum data + """View group (sectors, industry or country) valuation/performance/spectrum data. [Source: Finviz] Parameters ---------- @@ -87,3 +88,40 @@ def view_group_data(s_group: str, data_type: str, export: str): print( "Invalid data type. Choose between valuation, performance and spectrum.\n" ) + + +def display_future(future_type: str = "Indices", export: str = ""): + """Display table of a particular future type. [Source: Finviz] + + Parameters + ---------- + future_type : str + From the following: Indices, Energy, Metals, Meats, Grains, Softs, Bonds, Currencies + export : str + Export data to csv,json,xlsx or png,jpg,pdf,svg file + """ + d_futures = finviz_model.get_futures() + + df = pd.DataFrame(d_futures[future_type]) + df = df.set_index("label") + + if gtff.USE_TABULATE_DF: + print( + tabulate( + df[["prevClose", "last", "change"]].fillna(""), + showindex=True, + floatfmt=".2f", + headers=df.columns, + tablefmt="fancy_grid", + ) + ) + else: + print(df[["prevClose", "last", "change"]].fillna("").to_string(index=True)) + + export_data( + export, + os.path.dirname(os.path.abspath(__file__)), + future_type.lower(), + df, + ) + print("") diff --git a/website/content/economy/energy/_index.md b/website/content/economy/energy/_index.md new file mode 100644 index 00000000000..8ee24d863bf --- /dev/null +++ b/website/content/economy/energy/_index.md @@ -0,0 +1,12 @@ +``` +usage: energy [--export {csv,json,xlsx}] [-h] +``` + +Energy future overview. [Source: Finviz] + +``` +optional arguments: + --export {csv,json,xlsx} + Export dataframe data to csv,json,xlsx file (default: ) + -h, --help show this help message (default: False) +``` diff --git a/website/content/economy/grains/_index.md b/website/content/economy/grains/_index.md new file mode 100644 index 00000000000..127dede43c0 --- /dev/null +++ b/website/content/economy/grains/_index.md @@ -0,0 +1,12 @@ +``` +usage: grains [--export {csv,json,xlsx}] [-h] +``` + +Grains future overview. [Source: Finviz] + +``` +optional arguments: + --export {csv,json,xlsx} + Export dataframe data to csv,json,xlsx file (default: ) + -h, --help show this help message (default: False) +``` diff --git a/website/content/economy/meats/_index.md b/website/content/economy/meats/_index.md new file mode 100644 index 00000000000..aea362c5daf --- /dev/null +++ b/website/content/economy/meats/_index.md @@ -0,0 +1,12 @@ +``` +usage: meats [--export {csv,json,xlsx}] [-h] +``` + +Meats future overview. [Source: Finviz] + +``` +optional arguments: + --export {csv,json,xlsx} + Export dataframe data to csv,json,xlsx file (default: ) + -h, --help show this help message (default: False) +``` diff --git a/website/content/economy/metals/_index.md b/website/content/economy/metals/_index.md new file mode 100644 index 00000000000..6f14a84d3d7 --- /dev/null +++ b/website/content/economy/metals/_index.md @@ -0,0 +1,12 @@ +``` +usage: metals [--export {csv,json,xlsx}] [-h] +``` + +Metals future overview. [Source: Finviz] + +``` +optional arguments: + --export {csv,json,xlsx} + Export dataframe data to csv,json,xlsx file (default: ) + -h, --help show this help message (default: False) +``` diff --git a/website/content/economy/softs/_index.md b/website/content/economy/softs/_index.md new file mode 100644 index 00000000000..306edb9bd6f --- /dev/null +++ b/website/content/economy/softs/_index.md @@ -0,0 +1,12 @@ +``` +usage: softs [--export {csv,json,xlsx}] [-h] +``` + +Softs future overview. [Source: Finviz] + +``` +optional arguments: + --export {csv,json,xlsx} + Export dataframe data to csv,json,xlsx file (default: ) + -h, --help show this help message (default: False) +``` diff --git a/website/data/menu/main.yml b/website/data/menu/main.yml index 9655d53047b..abf5558f5e4 100755 --- a/website/data/menu/main.yml +++ b/website/data/menu/main.yml @@ -779,6 +779,16 @@ main: ref: "/economy/glbonds" - name: currencies ref: "/economy/currencies" + - name: energy + ref: "/economy/energy" + - name: metals + ref: "/economy/metals" + - name: meats + ref: "/economy/meats" + - name: grains + ref: "/economy/grains" + - name: softs + ref: "/economy/softs" - name: map ref: "/economy/map" - name: valuation