Files
OpenBB/gamestonk_terminal/etf/yfinance_view.py
Didier Rodrigues Lopes 78f110b389 ETF context new and improved (#1103)
* Add lookup commands

* main ETF commands

* Add technical analysis to ETF

* add prediction techniques to ETF

* add comparison analysis to etf

* allow to select nr of holdings on load

* add shold and summary commands to ETF

* minor improvs

* screener on ETF menu

* add etf/scr wsj commands

* readd compare command

* add sbc to etf/screener

* rename chold and shold

* fix rogue etf and reset

* minor corrections

* minor menu print improv

* legend of weights and title of plot

* refactor screener to screener and disc on ETF

* add ETF scripts
2021-12-30 03:21:21 +00:00

102 lines
2.7 KiB
Python

"""Yahoo Finance view"""
__docformat__ = "numpy"
import os
import pandas as pd
from matplotlib import pyplot as plt
from tabulate import tabulate
from gamestonk_terminal.etf import yfinance_model
from gamestonk_terminal import feature_flags as gtff
from gamestonk_terminal.config_plot import PLOT_DPI
from gamestonk_terminal.helper_funcs import plot_autoscale, export_data
def display_etf_weightings(
name: str,
raw: bool = False,
min_pct_to_display: float = 5,
export: str = "",
):
"""Display sector weightings allocation of ETF. [Source: Yahoo Finance]
Parameters
----------
name: str
ETF name
raw: bool
Display sector weighting allocation
min_pct_to_display: float
Minimum percentage to display sector
export: str
Type of format to export data
"""
sectors = yfinance_model.get_etf_sector_weightings(name)
if not sectors:
print("No data was found for that ETF\n")
return
holdings = pd.DataFrame(sectors, index=[0]).T
title = f"Sector holdings of {name}"
if raw:
print(f"\n{title}")
holdings.columns = ["% of holdings in the sector"]
if gtff.USE_TABULATE_DF:
print(
tabulate(
holdings,
headers=holdings.columns,
showindex=True,
tablefmt="fancy_grid",
),
)
else:
print(holdings.to_string, "\n")
else:
main_holdings = holdings[holdings.values > min_pct_to_display].to_dict()[
holdings.columns[0]
]
if len(main_holdings) < len(holdings):
main_holdings["Others"] = 100 - sum(main_holdings.values())
legend, values = zip(*main_holdings.items())
leg = [f"{le}\n{round(v,2)}%" for le, v in zip(legend, values)]
plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
plt.pie(
values,
labels=leg,
wedgeprops={"linewidth": 0.5, "edgecolor": "white"},
labeldistance=1.05,
startangle=90,
)
plt.title(title)
plt.tight_layout()
if gtff.USE_ION:
plt.ion()
plt.show()
print("")
export_data(export, os.path.dirname(os.path.abspath(__file__)), "weights", holdings)
def display_etf_description(
name: str,
):
"""Display ETF description summary. [Source: Yahoo Finance]
Parameters
----------
name: str
ETF name
"""
description = yfinance_model.get_etf_summary_description(name)
if not description:
print("No data was found for that ETF\n")
return
print(description, "\n")