mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 21:12:16 +00:00
Set up controller and view flow
This commit is contained in:
@@ -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 <load name>. \n")
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user