From 0e4252efee7c6f6536ee5238784fc2696b7958ca Mon Sep 17 00:00:00 2001 From: nectariosouzo Date: Mon, 25 Sep 2023 14:11:11 -0400 Subject: [PATCH] Set up controller and view flow --- openbb_terminal/etf/etf_controller.py | 11 +++-- openbb_terminal/etf/fmp_view.py | 66 +++++++++++++++++++-------- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/openbb_terminal/etf/etf_controller.py b/openbb_terminal/etf/etf_controller.py index a6801d01fd3..130a070aefb 100644 --- a/openbb_terminal/etf/etf_controller.py +++ b/openbb_terminal/etf/etf_controller.py @@ -643,8 +643,8 @@ class ETFController(BaseController): @log_start_end(log=logger) def call_holding_perf(self, other_args: List[str]): - print(self.etf_holdings) """Process holdings performance command""" + parser = argparse.ArgumentParser( add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter, @@ -675,11 +675,13 @@ class ETFController(BaseController): help="Number of holdings to get", default=20, ) - if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-l") - ns_parser = self.parse_known_args_and_warn(parser, other_args, EXPORT_BOTH_RAW_DATA_AND_FIGURES, raw= True) + ns_parser = self.parse_known_args_and_warn(parser, other_args, + export_allowed=EXPORT_BOTH_RAW_DATA_AND_FIGURES, + raw=True + ) if ns_parser: if self.etf_name: fmp_view.view_etf_holdings_performance( @@ -687,6 +689,9 @@ class ETFController(BaseController): start_date=ns_parser.start, end_date=ns_parser.end, limit=ns_parser.limit, + export=ns_parser.export, + sheet_name=ns_parser.sheet_name, + raw=ns_parser.raw, ) else: console.print("Please load a ticker using . \n") diff --git a/openbb_terminal/etf/fmp_view.py b/openbb_terminal/etf/fmp_view.py index 486d21c9d10..7d6e77c4edc 100644 --- a/openbb_terminal/etf/fmp_view.py +++ b/openbb_terminal/etf/fmp_view.py @@ -6,8 +6,6 @@ import os from typing import Optional import pandas as pd -import seaborn as sns -import matplotlib.pyplot as plt from openbb_terminal import OpenBBFigure, theme from openbb_terminal.decorators import check_api_key, log_start_end @@ -127,17 +125,26 @@ def display_etf_weightings( @log_start_end(log=logger) @check_api_key(['API_KEY_FINANCIALMODELINGPREP']) def view_etf_holdings_performance(ticker: str, start_date: str, end_date: str, limit: int = 10, raw: bool = False, export: str = "", sheet_name: Optional[str] = None): - + '''Display ETF's holdings' performance over specified time. [Source: FinancialModelingPrep] + Parameters + ---------- + ticker: str + ETF ticker + start_date: str + Date from which data is fetched in format YYYY-MM-DD + end: str + Date from which data is fetched in format YYYY-MM-DD + limit: int + Limit number of holdings to view. Sorted by descending percent change. + raw: bool + Display holding performance + sheet_name: str + Optionally specify the name of the sheet the data is exported to. + export: str + Type of format to export data + ''' data = fmp_model.get_holdings_pct_change(ticker, start_date, end_date)[0:limit] - colors = ['g' if x > 0 else 'r' for x in data['Percent Change']] - - sns.barplot( - x= 'Percent Change', - y= 'Name', - data=data, - palette=colors, - ) if raw: print_rich_table( data, @@ -148,13 +155,34 @@ def view_etf_holdings_performance(ticker: str, start_date: str, end_date: str, l export=bool(export), ) - if export: - export_data( - export, - os.path.dirname(os.path.abspath(__file__)), - "holdings_perf", - data, - sheet_name, + fig = OpenBBFigure() + + if not raw or fig.is_image_export(export): + + fig.add_bar( + hovertext=[f'{x:.2f}'+"%" for x in data['Percent Change']], + x=data['Percent Change'], + y=data['Name'], + name = 'Stock', + orientation='h', + marker_color=['darkgreen' if x > 0 else 'darkred' for x in data['Percent Change']], ) - return plt.show() + fig.update_layout( + title=f"Percent Change from {start_date} to {end_date}", + xaxis=dict(title="Percent Change"), + yaxis=dict(title="Asset Name"), + ) + + if export: + export_data( + export_type=export, + dir_path=os.path.dirname(os.path.abspath(__file__)), + func_name=f"{ticker}_holdings_perf", + df=data, + sheet_name=sheet_name, + figure=fig, + ) + return + + return fig.show(external= raw or export)