Skip to content
Open
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
14 changes: 11 additions & 3 deletions pytr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,13 @@ def formatter(prog):
help=info,
description=info,
)
parser_portfolio.add_argument("-o", "--output", help="Output path of CSV file", type=Path)

parser_portfolio.add_argument("-o", "--output", help="Output path of the file", type=Path)
parser_portfolio.add_argument(
"--export-format",
choices=("json", "csv"),
default="csv",
help="The output file format.",
)
# details
info = "Get details for an ISIN"
parser_details = parser_cmd.add_parser(
Expand Down Expand Up @@ -367,7 +372,10 @@ def main():
)
p.get()
if args.output is not None:
p.portfolio_to_csv(args.output)
if args.export_format == "csv":
p.portfolio_to_csv(args.output)
elif args.export_format == "json":
p.portfolio_to_json(args.output)
elif args.command == "export_transactions":
events = [Event.from_dict(item) for item in json.load(args.input)]
TransactionExporter(
Expand Down
14 changes: 14 additions & 0 deletions pytr/portfolio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import json

from pytr.utils import preview

Expand Down Expand Up @@ -84,6 +85,19 @@ def portfolio_to_csv(self, output_path):

print(f"Wrote {len(csv_lines) + 1} lines to {output_path}")

def portfolio_to_json(self, output_path):
# Combine the portfolio positions with the cash portfolio.
data = {
"positions": sorted(self.portfolio["positions"], key=lambda x: x["netSize"], reverse=True),
"cash": self.cash,
}
if output_path is None or str(output_path) == "-":
print(json.dumps(data, indent=2))
else:
with open(output_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
print(f"Wrote portfolio and cash data to {output_path}")

def overview(self):
print(
"Name ISIN avgCost * quantity = buyCost -> netValue diff %-diff"
Expand Down
Loading