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

Refactor table rows #14

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pypmanager/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Constants."""

NUMBER_FORMATTER = ",.0f"
43 changes: 36 additions & 7 deletions pypmanager/holding.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Handle securities."""
from dataclasses import dataclass
from datetime import date
from typing import cast

import numpy as np
import pandas as pd

from pypmanager.const import NUMBER_FORMATTER
from pypmanager.data_loader import TransactionTypeValues
from pypmanager.security import MutualFund

Expand All @@ -18,15 +20,15 @@ def _calculate_aggregates(data: pd.DataFrame, security_name: str) -> pd.DataFram
df["realized_pnl"] = 0.0
df["cumulative_invested_amount"] = 0.0

cumulative_buy_amount = 0.0
cumulative_buy_volume = 0.0
cumulative_invested_amount = 0.0
average_price = 0.0
cumulative_buy_amount: float = 0.0
cumulative_buy_volume: float | None = 0.0
cumulative_invested_amount: float = 0.0
average_price: float | None = 0.0

for index, row in df.iterrows():
amount = abs(row["amount"])
no_traded = abs(row["no_traded"])
commission = abs(row["commission"])
amount = cast(float, abs(row["amount"]))
no_traded = cast(float, abs(row["no_traded"]))
commission = cast(float, abs(row["commission"]))

if row["transaction_type"] == TransactionTypeValues.BUY.value:
cumulative_buy_volume += no_traded
Expand Down Expand Up @@ -177,3 +179,30 @@ def invested_amount(self) -> float | None:
return None

return self.average_price * self.current_holdings

@property
def cli_table_row(self) -> list[str]:
"""Represent the holding for CLI reports."""
invested_amount = (
f"{self.invested_amount:{NUMBER_FORMATTER}}"
if self.invested_amount
else None
)

current_holdings = (
f"{self.current_holdings:{NUMBER_FORMATTER}}"
if self.current_holdings
else None
)

return [
self.name,
invested_amount,
current_holdings,
f"{self.current_price}",
f"{self.date_market_value}",
f"{self.total_pnl:{NUMBER_FORMATTER}}",
f"{self.realized_pnl:{NUMBER_FORMATTER}}",
f"{self.unrealized_pnl:{NUMBER_FORMATTER}}",
f"{self.total_transactions}",
]
16 changes: 16 additions & 0 deletions pypmanager/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from dataclasses import dataclass

from pypmanager.const import NUMBER_FORMATTER
from pypmanager.holding import Holding


Expand Down Expand Up @@ -40,3 +41,18 @@ def unrealized_pnl(self) -> float:
return sum(
s.unrealized_pnl for s in self.holdings if s.unrealized_pnl is not None
)

@property
def cli_table_row_total(self) -> list[str]:
"""Represent totals for CLI reports."""
return [
"Total",
f"{self.invested_amount:{NUMBER_FORMATTER}}",
"",
"",
"",
f"{self.total_pnl:{NUMBER_FORMATTER}}",
f"{self.unrealized_pnl:{NUMBER_FORMATTER}}",
f"{self.realized_pnl:{NUMBER_FORMATTER}}",
"",
]
42 changes: 2 additions & 40 deletions pypmanager/reports/cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from pypmanager.holding import Holding
from pypmanager.portfolio import Portfolio

NUMBER_FORMATTER = ",.0f"

TABLE_HEADER = [
"Name",
"Invested",
Expand Down Expand Up @@ -34,46 +32,10 @@ def print_pretty_table(holdings: list[Holding]) -> None:
table.align["Name"] = "l"

for security_data in sorted_holdings:
invested_amount = (
f"{security_data.invested_amount:{NUMBER_FORMATTER}}"
if security_data.invested_amount
else None
)

current_holdings = (
f"{security_data.current_holdings:{NUMBER_FORMATTER}}"
if security_data.current_holdings
else None
)

table.add_row(
[
security_data.name,
invested_amount,
current_holdings,
f"{security_data.current_price}",
f"{security_data.date_market_value}",
f"{security_data.total_pnl:{NUMBER_FORMATTER}}",
f"{security_data.realized_pnl:{NUMBER_FORMATTER}}",
f"{security_data.unrealized_pnl:{NUMBER_FORMATTER}}",
f"{security_data.total_transactions}",
],
)
table.add_row(security_data.cli_table_row)

portfolio = Portfolio(holdings=holdings)

table.add_row(
[
"Total",
f"{portfolio.invested_amount:{NUMBER_FORMATTER}}",
"",
"",
"",
f"{portfolio.total_pnl:{NUMBER_FORMATTER}}",
f"{portfolio.unrealized_pnl:{NUMBER_FORMATTER}}",
f"{portfolio.realized_pnl:{NUMBER_FORMATTER}}",
"",
],
)
table.add_row(portfolio.cli_table_row_total)

print(table)