Files
OpenBB/website/controller_doc_classes.py
teh_coderer 602cfa2c8c [POC] Plotly Backend (#3941)
* Update portfolio_view.py

* mypy 1.0 test

* Update helpers.py

* black

* ruff

* Update thought_of_the_day.py

* isort

* Update portfolio_view.py

* Update business_insider_view.py

* funds,bt,ca,fa view updates

* economy,etf,futures view updates

* econometrics regression

* mypy

* reports notebooks, PlotlyTA class adjustments, crypto finbrain view

* crypto views updates

* crypto views done?, ba, econometrics, funds, reports

* update docstrings

* Update sec_view.py

* portfolio reports

* Update equity.ipynb

* ruff

* mypy + equity reports

* Update yahoo_finance_view.py

* Update economy_controller.py

* clean up returns

* mypy

* finished dashboards/reports templates

* update some tests

* Update common.py

* Update account_controller.py

* Update banner.txt

* update tests

* tests

* Update helpers_denomination.py

* tests

* tests

* finished tests, refactor add_histplot/add_corr_plot

* ruff

* Update backend.py

* mypy

* Update backend.py

* pywry linux libs for tests

* pywry version bump

* OpenBBFIgure import changes

* Update business_insider_view.py

* Update backend.py

* tests

* bump pywry version

* forecast plot_explainability converted to plotly

* Update helpers.py

* tests

* Update helpers.py

* tests

* Update helpers.py

* Update helpers.py

* default plotly annotation `showarrow` to false

* Update backend.py

* fixes

* Update poetry.lock

* docker

* Update openbb.dockerfile

* up

* Update style.css

* bump pywry version

* Update unit-test.yml

* Update unit-test.yml

* figure to image/pdf export

* requirements txt files, pywry version bump

* mypy, pylint

* cleanup

* Update covid_view.py

* Update helper_funcs.py

* Update backend.py

* updates

* Update backend.py

* Update backend.py

* bump pywry version

* Update poetry.lock

* Update trailmap.py

* update reports

* Update custom_indicators_plugin.py

* Update momentum_plugin.py

* equity report

* reqs files

* Update fred_view.py

* ruff

* Update po_view.py

* bump pywry version

* reqs txt files

* fix installer icons for mac

* test updated torch/darts/pyinstaller on mac

* Update dashboards_controller.py

* Update terminal.py

* Update terminal.spec

* Update terminal.spec

* test

* Update poetry.lock

* Report adjustments, improved dashboard subprocesses termination

* pylint

* Update plotly_helper.py

* Update plotly_helper.py

* pylint

* revert darts

* test

* test

* fix

* improve shap scatter plot

* Update helpers.py

* Update helpers.py

* cleanup

* Update runa_view.py

* more cleanupp

* fixes

* update tests

* Update intrinio_view.py

* Update intrinio_view.py

* Update intrinio_view.py

* reqs files, poetry lock

* Update backend.py

* test for new icon

* Update fmp_view.py

* updates

* Update plotly_helper.py

* Update businessinsider_view.py

* ruff

* pylint

* fix previous merge

* Update plotly_helper.py

* update reqs files

* Update directories.py

* fix alt covid

* fix display_candle data is None load being unreachable, get covid views ready for sdk server

* Update covid_view.py

* refactor cleanup

* plotly_ta adjustments

* terminal wide ignore print_table if export

* Update plotly_helper.py

* forgot to save a few

* added edge case fallback if figures are in queue before backend fail

* Update ta_class.py

* Rename env variable name required to return plotly json

* Resolve pydocstyle warnings

* Resolve linting errors

* revert forecasting/pyinstaller versions

* Update terminal.spec

* finished incoming fixedincome menu, improve get_ecb_yield_curve speed with multiprocessing

* update deps

* Update backend.py

* fix fred model/view from switch to DataFrame, improve ecb_model

* Update config_terminal.py

* Update backend.py

* black

* clean up

* Update fred_view.py

* Update ta_class.py

* Update ta_class.py

* Update plotly_helper.py

* sync fixedincome

* Update ecb_model.py

* convert excel to csv for faster processing

* cleanup

* remove excel files

* Update terminal_controller.py

* update tests

* adjustments/missed figs in export_data

* Update cryptocurrency_helpers.py

* fixes

* account for volume column with no volume when plotting indicators , images autogen POC

* Update plotly_helper.py

* Update etf_controller.py

* Update etf_controller.py

* margin arg for export_data func figures, some plot adjustments

* logic for numpy array for plotly helper `get_dateindex` function

* cleanup

* update

* req file updates

* views

* Update backend.py

* update tests

* Update test_load_none[btc-usd].yaml

* convert merged cpi chart

* linting

* updates

* clean

* tests

* Update openbb.dockerfile

* remove test docker in root

* Update CONTRIBUTING.md

---------

Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
Co-authored-by: Chavithra <chavithra@gmail.com>
2023-03-01 22:48:45 -05:00

526 lines
17 KiB
Python

# pylint: disable=unused-argument
import argparse
import inspect
import sys
from datetime import datetime, timedelta
from importlib.util import module_from_spec, spec_from_file_location
from inspect import FullArgSpec, getmembers, isclass
from pathlib import Path
from types import FunctionType, ModuleType
from typing import Any, Dict, List, Optional
from unittest.mock import patch
import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar
import openbb_terminal
from openbb_terminal.base_helpers import load_dotenv_and_reload_configs
from openbb_terminal.core.plots.backend import plots_backend
from openbb_terminal.decorators import disable_check_api
from openbb_terminal.helper_funcs import (
EXPORT_BOTH_RAW_DATA_AND_FIGURES,
EXPORT_ONLY_FIGURES_ALLOWED,
NO_EXPORT,
set_command_location,
)
from openbb_terminal.parent_classes import BaseController
from openbb_terminal.rich_config import console
from openbb_terminal.sdk import openbb
from openbb_terminal.stocks.comparison_analysis import finviz_compare_model
load_dotenv_and_reload_configs()
disable_check_api()
plots_backend().start()
plots_backend().isatty = True
CRYPTO_DATA = openbb.crypto.load("BTC", to_symbol="usd", source="YahooFinance")
ETF_DATA = openbb.etf.load("SPY")
ETF_DATA.index.name = "date"
FOREX_DATA = openbb.forex.load(to_symbol="USD", from_symbol="EUR")
FOREX_DATA.index.name = "date"
STOCK_DATA = openbb.stocks.load("AAPL", start_date="2022-01-01")
STOCK_DATA.index.name = "date"
data_dict = {
"stocks": {
"data": STOCK_DATA,
"symbol": "AAPL",
},
"crypto": {
"data": CRYPTO_DATA,
"symbol": "BTC",
},
"forex": {
"data": FOREX_DATA,
"symbol": "EURUSD",
},
"futures": {
"data": STOCK_DATA,
"symbol": "ES",
},
"etf": {
"data": ETF_DATA,
"symbol": "SPY",
},
}
sub_folders_abbr = {
"discovery": "disc",
"due_diligence": "dd",
"overview": "ov",
"alternative": "alt",
"cryptocurrency": "crypto",
"behavioural_analysis": "ba",
"comparison_analysis": "ca",
"dark_pool_shorts": "dps",
"portfolio_optimization": "po",
"quantitative_analysis": "qa",
"sector_industry_analysis": "sia",
"technical_analysis": "ta",
"tradinghours": "th",
"fundamental_analysis": "fa",
"mutual_funds": "funds",
"government": "gov",
"insider": "ins",
}
sub_names_full = {
"alt": "Alternative",
"ba": "Behavioural Analysis",
"ca": "Comparison Analysis",
"crypto": "Cryptocurrency",
"dd": "Due Diligence",
"defi": "DeFi",
"disc": "Discovery",
"dps": "Darkpool Shorts",
"etf": "ETFs",
"fa": "Fundamental Analysis",
"forecast": "Forecasting",
"funds": "Mutual Funds",
"gov": "Government",
"ins": "Insiders",
"keys": "Keys",
"nft": "NFTs",
"onchain": "OnChain",
"ov": "Overview",
"po": "Portfolio Optimization",
"qa": "Quantitative Analysis",
"screener": "Screener",
"sia": "Sector Industry Analysis",
"ta": "Technical Analysis",
"th": "Trading Hours",
}
required_flags = {
"crypto": {
"funot": ["-p", "ethereum"],
},
"econometrics": {
"coint": ["-t"], # TODO: add choice
"norm": ["-v"], # TODO: add choice
},
"sia": {
"vis": ["-m", "tcl"],
"metric": ["-m", "roa"],
},
}
def get_expiration_date():
"""Gets the next expiration date for controller init"""
dt = datetime.now()
bdays_indx = pd.bdate_range(
dt.strftime("%Y-%m-%d"),
(dt + timedelta(days=20)).strftime("%Y-%m-%d"),
freq=pd.offsets.CustomBusinessDay(calendar=USFederalHolidayCalendar()),
).tolist()
expiration = [x.strftime("%Y-%m-%d") for x in bdays_indx if x.weekday() == 4][0]
return expiration
start_date = (datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
end_date = datetime.now().strftime("%Y-%m-%d")
param_name_to_value = {
"expiration": get_expiration_date(),
"start_date": start_date,
"start": start_date,
"end_date": end_date,
"end": end_date,
"from_symbol": "USD",
"to_symbol": "EUR",
"vs": "USDT",
}
param_type_to_value = {
(dict, Dict): {},
list: [],
str: "",
int: 0,
float: 0.0,
bool: False,
}
def get_parameters(
controller_cls: BaseController, name: str, df_loaded: pd.DataFrame, symbol: str
) -> Dict[str, Any]:
"""Gets the parameters of a controller's `__init__` signature. If required parameters are missing,
we get the type and use a default value for it.
Parameters
----------
controller_class: Type[BaseController]
The controller class
name: str
The name of the controller
df_loaded: pd.DataFrame
The dataframe loaded from the `load` function
symbol: str
The symbol to use on controller init
Returns
-------
dict[str, Any]
The parameters with their values for controller init
"""
signature = inspect.signature(controller_cls) # type: ignore
kwargs: Dict[str, Any] = {}
for param in signature.parameters.values():
if param.name in ("ticker", "symbol", "coin"):
kwargs[param.name] = symbol
elif param.name == "data" and name in ("forecast", "qa"):
kwargs["data"] = df_loaded
elif (
param.default is inspect.Parameter.empty
and param.kind is not inspect.Parameter.VAR_KEYWORD
):
for param_name, value in param_name_to_value.items():
if param.name == param_name:
kwargs[param.name] = value
break
if param.name not in kwargs:
for param_type, value in param_type_to_value.items():
if isinstance(param_type, tuple):
if param.annotation in param_type:
kwargs[param.name] = {symbol: df_loaded}
break
elif param.annotation is pd.DataFrame:
kwargs[param.name] = df_loaded
break
elif param.annotation is param_type:
kwargs[param.name] = value
break
return kwargs
class ControllerDoc:
"""Class that retrieves the ArgumentParser for each command of the Controller and stores it in a dictionary
for use in auto-generating the documentation.
Parameters
----------
controller: BaseController
The controller to get the commands from
Attributes
----------
controller: BaseController
The controller to get the commands from
cmd_parsers: Dict[str, argparse.ArgumentParser]
A dictionary of the command name and the ArgumentParser for that command
cmd_funcs: Dict[str, FunctionType]
A dictionary of the command name and the function for that command
cmd_fullspec: Dict[str, FullArgSpec]
A dictionary of the command name and the full argument spec for that command
ignore: List[str]
A list of commands to ignore
commands: List[str]
A list of commands to document
Methods
-------
get_commands()
Get commands
get_command_parser(command: str)
Get the parser for a command
get_all_command_parsers()
Get all command parsers
has_commands()
Checks if controller has commands to document
"""
def __init__(self, controller: BaseController, trailmap: str):
self.trailmap = trailmap
self.name = trailmap.split(".")[-1]
params = data_dict.get(trailmap.split(".")[0], data_dict["stocks"])
self.symbol = params.get("symbol", "AAPL")
self.current_df = params.get("data", STOCK_DATA)
self.controller: BaseController = controller( # type: ignore
**get_parameters(controller, self.name, self.current_df, self.symbol)
)
root_menu = self.controller.path[0]
sub_menu = self.controller.path[1] if len(self.controller.path) > 1 else None
if sub_menu and sub_menu:
console.print(
f"[bold green]Loading {root_menu}[/] [bold yellow]{sub_menu}[/]"
)
else:
console.print(f"[bold green]Loading {root_menu}[/]")
self.cmd_parsers: Dict[str, argparse.ArgumentParser] = {}
self.cmd_funcs: Dict[str, FunctionType] = {}
self.cmd_fullspec: Dict[str, FullArgSpec] = {}
self.image_exportable: Dict[str, bool] = {}
self.ignore = [
"call_help",
"call_exit",
"call_clear",
"call_cls",
"call_quit",
"call_about",
"call_reset",
"call_support",
"call_wiki",
"call_record",
"call_stop",
"call_screenshot",
]
self.commands: List[str] = self.get_commands()
if self.name == "options" and hasattr(self.controller, "selected_date"):
self.controller.selected_date = get_expiration_date()
elif self.name == "ca" and hasattr(self.controller, "similar"):
self.controller.similar = finviz_compare_model.get_similar_companies(
self.symbol, ["Sector", "Industry"]
)
if hasattr(self.controller, "current_currency"):
self.controller.current_currency = "usdt"
if hasattr(self.controller, "source") and trailmap.split(".")[0] == "crypto":
self.controller.source = "YahooFinance"
for attr in ["ticker", "symbol", "coin", "etf_name"]:
if hasattr(self.controller, attr):
setattr(self.controller, attr, self.symbol)
for attr in ["current_df", "etf_data"]:
if hasattr(self.controller, attr):
setattr(self.controller, attr, self.current_df)
self.get_all_command_parsers()
def get_commands(self) -> List[str]:
"""Get commands"""
commands = []
for name, _ in getmembers(self.controller, predicate=inspect.ismethod):
if name.startswith("call_") and name not in self.ignore:
func = getattr(self.controller, name)
if hasattr(func, "__wrapped__"):
while hasattr(func, "__wrapped__"):
func = func.__wrapped__
self.cmd_funcs[name] = func
self.cmd_fullspec[name] = inspect.getfullargspec(func)
if "_" not in self.cmd_fullspec[
name
].args and "from openbb_terminal." not in inspect.getsource(func):
commands.append(name)
return commands
def get_command_parser(self, command: str) -> Optional[argparse.ArgumentParser]:
"""Get command parser"""
if command not in self.cmd_parsers:
self._get_parser(command)
if command in self.cmd_parsers:
return self.cmd_parsers[command]
return None
def _get_parser(self, command: str) -> None:
"""Get parser information from source"""
self.image_exportable[command] = False
def mock_func(fparser: argparse.ArgumentParser, *args, **kwargs):
"""Mock function to get the parser"""
allowed = [EXPORT_BOTH_RAW_DATA_AND_FIGURES, EXPORT_ONLY_FIGURES_ALLOWED]
export = kwargs.get("export_allowed", NO_EXPORT)
if export in allowed:
self.image_exportable[command] = True
else:
for arg in args:
if arg in allowed:
self.image_exportable[command] = True
break
self.cmd_parsers[command] = fparser
return
try:
with patch.object(
self.controller, "parse_known_args_and_warn", new=mock_func
) as _:
args = {}
fullspec = self.cmd_fullspec[command]
if "_" in fullspec.args:
return
if len(fullspec.args) > 2:
args.update({arg: ["1234"] for arg in fullspec.args[2:]})
with patch("openbb_terminal.rich_config.console.print"):
try:
_ = getattr(self.controller, command)(["--help"], **args)
except (SystemExit, AttributeError):
pass
except Exception as e:
print(e)
def run_image_exports(self) -> None:
"""Run image exports"""
if not self.image_exportable:
return
for command in self.commands:
self.get_image_export(command)
def get_image_export(self, command: str) -> None:
"""Get image export"""
if not self.image_exportable[command]:
return
console.print(
f"[yellow]Exporting {command} image for {self.name} controller[/]"
)
try:
cmd_name = command.replace("call_", "")
other_args = [
"--export",
f"{'_'.join(self.controller.path)}_{cmd_name}.png",
]
for action in self.cmd_parsers[command]._actions: # pylint: disable=W0212
if action.dest == "strike":
other_args.extend(["--strike", "100"])
if action.dest == "target_dataset":
other_args.extend(["--dataset", "AAPL"])
if action.dest == "ticker":
other_args.extend(["--ticker", self.symbol])
if self.name == "forecast" and action.dest == "values":
other_args.extend(["--values", "AAPL.close"])
set_command_location(f"{'/'.join(self.controller.path)}/{cmd_name}")
_ = getattr(self.controller, command)(other_args)
except (SystemExit, KeyError):
pass
def get_all_command_parsers(self) -> None:
"""Get all command parsers"""
for command in self.commands:
self.get_command_parser(command)
def has_commands(self) -> bool:
"""Checks if controller has commands"""
return len(self.commands) > 0
class LoadControllersDoc:
"""Class that loads all controllers and creates a ControllerDoc class instance for each one
Attributes
----------
controller_docs: Dict[str, ControllerDoc]
A dictionary of the controller name and the ControllerDoc class instance for that controller
Methods
-------
get_controllers()
Gets all controllers to create a ControllerDoc class instance for
get_controller_doc(controller: str)
Gets the ControllerDoc class instance for a controller
available_controllers()
Gets a list of available controllers names
"""
def __init__(self) -> None:
self.controller_docs: Dict[str, ControllerDoc] = {}
self.get_controllers()
def get_controllers(self) -> None:
"""Gets all controllers"""
for trailmap, module in self._get_modules().items():
for name, obj in getmembers(module):
if ( # noqa: SIM102
name != "TerminalController" and "BaseController" not in name
): # noqa: SIM102
if isclass(obj) and issubclass(obj, BaseController):
if trailmap not in self.controller_docs:
ctrl = ControllerDoc(obj, trailmap) # type: ignore
if ctrl.has_commands():
self.controller_docs[trailmap] = ctrl
def _get_modules(self) -> Dict[str, ModuleType]:
"""Gets all controllers modules"""
modules = {}
terminal_path = Path(openbb_terminal.__file__).parent
for file in terminal_path.glob("**/*controller.py"):
spec = spec_from_file_location(file.stem, file)
if spec is not None and spec.loader is not None:
module = module_from_spec(spec)
spec.loader.exec_module(module)
ctrl_path = (
str(file)
.replace(str(terminal_path), "")
.replace("\\", "/")
.split("/")[1:]
)
for sub_name, abbr in sub_folders_abbr.items():
ctrl_path = [
path.lower().replace(sub_name, abbr) for path in ctrl_path
]
trailmap = ".".join(ctrl_path[:-1])
if trailmap not in modules:
modules[trailmap] = module
return modules
def get_controller_doc(self, controller_name: str) -> ControllerDoc:
"""Get the ControllerDoc instance for a controller"""
if controller_name not in self.controller_docs:
raise KeyError(f"Controller {controller_name} not found")
return self.controller_docs[controller_name]
def available_controllers(self) -> List[str]:
"""Get available controllers"""
return [ctrl for ctrl in self.controller_docs if ctrl != ""]
if __name__ == "__main__":
try:
run_image_exports = "--images" in sys.argv
load_controllers = LoadControllersDoc()
for loaded in load_controllers.available_controllers():
controller_doc = load_controllers.get_controller_doc(loaded)
controller_doc.get_all_command_parsers()
if run_image_exports:
controller_doc.run_image_exports()
except KeyboardInterrupt:
sys.exit(0)