Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow direct orderbook export from degiro paexport to portfolio load command #2091

Merged
merged 14 commits into from
Jul 20, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ def call_update(self, other_args: List[str]):

@log_start_end(log=logger)
def call_paexport(self, other_args: List[str]):
"""Export transactions for Portfolio Analysis Menu into CSV format."""
"""Export transactions for Portfolio menu into csv format. The transactions
file is exported to the portfolio/holdings folder and can be loaded directly
in the Portfolio menu."""

# PARSING ARGS
parser = argparse.ArgumentParser(
Expand Down
24 changes: 24 additions & 0 deletions openbb_terminal/portfolio/brokers/degiro/degiro_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import math
from typing import List, Union
from pathlib import Path

# IMPORTATION THIRDPARTY
import pandas as pd
Expand All @@ -22,7 +23,9 @@

# IMPORTATION INTERNAL
import openbb_terminal.config_terminal as config
from openbb_terminal.rich_config import console
from openbb_terminal.decorators import log_start_end
from openbb_terminal.portfolio import portfolio_helper

# pylint: disable=no-member
# pylint: disable=no-else-return
Expand Down Expand Up @@ -438,3 +441,24 @@ def get_transactions_export(
portfolio_df = portfolio_df.set_index("Date")

return portfolio_df

@staticmethod
@log_start_end(log=logger)
def export_data(portfolio_df: pd.DataFrame, export: str):
# In this scenario the path was provided, e.g. --export pt.csv, pt.jpg

if "." in export:
if export.endswith("csv"):
filename = export
# In this scenario we use the default filename
else:
console.print("Wrong export file specified.\n")
else:
now = datetime.datetime.now()
filename = f"{now.strftime('%Y%m%d_%H%M%S')}_paexport_degiro.csv"

file_path = Path(str(portfolio_helper.DEFAULT_HOLDINGS_PATH), filename)

portfolio_df.to_csv(file_path)

console.print(f"Saved file: {file_path}\n")
12 changes: 4 additions & 8 deletions openbb_terminal/portfolio/brokers/degiro/degiro_view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# IMPORTATION THIRDPARTY
import logging
from argparse import Namespace
from pathlib import Path

# IMPORTATION THIRDPARTY
import pandas as pd
Expand All @@ -21,7 +20,6 @@

# IMPORTATION INTERNAL
from openbb_terminal.helper_funcs import (
export_data,
print_rich_table,
)
from openbb_terminal.portfolio.brokers.degiro.degiro_model import DegiroModel
Expand Down Expand Up @@ -507,17 +505,15 @@ def transactions_export(self, ns_parser: Namespace):
)

if portfolio_df is not None:

print_rich_table(
df=portfolio_df,
headers=list(portfolio_df.columns),
show_index=True,
title="Degiro Transactions",
)
export_data(
export_type=ns_parser.export,
dir_path=str(Path(__file__).parent.parent.parent.absolute()),
func_name="paexport",
df=portfolio_df,
)

degiro_model.export_data(portfolio_df, ns_parser.export)

else:
console.print("Error while fetching or processing Transactions.")
38 changes: 24 additions & 14 deletions openbb_terminal/portfolio/portfolio_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ def __init__(self, queue: List[str] = None):
super().__init__(queue)
self.file_types = ["xlsx", "csv"]

self.DEFAULT_HOLDINGS_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "..", "portfolio", "holdings")
)
self.DEFAULT_HOLDINGS_PATH = portfolio_helper.DEFAULT_HOLDINGS_PATH

self.DATA_HOLDINGS_FILES = {
filepath.name: filepath
Expand Down Expand Up @@ -135,17 +133,30 @@ def __init__(self, queue: List[str] = None):
)

if session and obbff.USE_PROMPT_TOOLKIT:
choices: dict = {c: {} for c in self.controller_choices}
choices["load"] = {c: None for c in self.DATA_HOLDINGS_FILES}
choices["bench"] = {c: None for c in portfolio_helper.BENCHMARK_LIST}
choices["alloc"] = {c: None for c in self.AGGREGATION_METRICS}
choices["metric"] = {c: None for c in self.VALID_METRICS}
self.choices = choices
self.update_choices()

def update_choices(self):

self.DEFAULT_HOLDINGS_PATH = portfolio_helper.DEFAULT_HOLDINGS_PATH

self.DATA_HOLDINGS_FILES = {
filepath.name: filepath
for file_type in self.file_types
for filepath in Path(self.DEFAULT_HOLDINGS_PATH).rglob(f"*.{file_type}")
if filepath.is_file()
}

choices: dict = {c: {} for c in self.controller_choices}
choices["load"] = {c: None for c in self.DATA_HOLDINGS_FILES}
choices["bench"] = {c: None for c in portfolio_helper.BENCHMARK_LIST}
choices["alloc"] = {c: None for c in self.AGGREGATION_METRICS}
choices["metric"] = {c: None for c in self.VALID_METRICS}
self.choices = choices

choices["support"] = self.SUPPORT_CHOICES
choices["about"] = self.ABOUT_CHOICES
choices["support"] = self.SUPPORT_CHOICES
choices["about"] = self.ABOUT_CHOICES

self.completer = NestedCompleter.from_nested_dict(choices)
self.completer = NestedCompleter.from_nested_dict(choices)

def print_help(self):
"""Print help"""
Expand Down Expand Up @@ -237,6 +248,7 @@ def print_help(self):
# [info]Reports:[/info]
# ar annual report for performance of a given portfolio
console.print(text=help_text, menu="Portfolio")
self.update_choices()

def custom_reset(self):
"""Class specific component of reset command"""
Expand Down Expand Up @@ -284,7 +296,6 @@ def call_load(self, other_args: List[str]):
"-f",
"--file",
type=str,
choices=self.DATA_HOLDINGS_FILES,
dest="file",
required="-h" not in other_args,
help="The file to be loaded",
Expand Down Expand Up @@ -336,7 +347,6 @@ def call_load(self, other_args: List[str]):
console.print(
f"[bold]Risk Free Rate:[/bold] {self.portfolio.risk_free_rate}"
)

console.print()

@log_start_end(log=logger)
Expand Down
11 changes: 4 additions & 7 deletions openbb_terminal/portfolio/portfolio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@
"10y": 10 * 12 * 21,
}

DEFAULT_HOLDINGS_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "..", "portfolio", "holdings")
)


def is_ticker(ticker: str) -> bool:
"""Determine whether a string is a valid ticker
Expand Down Expand Up @@ -426,13 +430,6 @@ def get_info_from_ticker(ticker: str) -> list:

filename = "tickers_info.csv"

# Direct file to openbb_terminal\portfolio
DEFAULT_HOLDINGS_PATH = os.path.abspath(
os.path.join(
os.path.dirname(__file__), "..", "..", "openbb_terminal", "portfolio"
)
)

file_path = Path(str(DEFAULT_HOLDINGS_PATH), filename)

if file_path.is_file() and os.stat(file_path).st_size > 0:
Expand Down
Empty file.