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

Script for automatically converting a CSV download from Fidelity #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions contrib/convert-fidelity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
import argparse
import csv
import decimal
import yaml
import logging
from decimal import Decimal, InvalidOperation

logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser(
prog="convert-fidelity.py",
description="Converts a CSV file provided by Fidelity to the format used by https://github.com/achannarasappa/ticker",
)

parser.add_argument("filename", help="The name of the CSV file provided by fidelity")
args = parser.parse_args()

with open(args.filename, "r") as csvfile:
c = csv.DictReader(csvfile)
ticker_config = {
"show-summary": True,
"show-fundamentals": True,
"show-tags": True,
"show-separator": True,
"show-holdings": True,
"currency-summary-only": True,
"interval": 10,
"currency": "USD",
}

watchlist = []
lots = []

for itm in c:
watchlist.append(itm["Symbol"])
try:
itmdict = {
"symbol": itm["Symbol"],
"quantity": float(itm["Quantity"]),
"unit_cost": float(itm["Average Cost Basis"][1:]),
}
lots.append(itmdict)
except ValueError:
logger.warn(
"Unable to convert average cost basis '%s' or quantity '%s' for %s"
% (itm["Average Cost Basis"][1:], itm["Quantity"], itm["Symbol"])
)

ticker_config.update({"watchlist": watchlist, "lots": lots})
print(yaml.dump(ticker_config))