Skip to content

Instantly share code, notes, and snippets.

@rchamarthi
Created November 26, 2017 03:56
Show Gist options
  • Save rchamarthi/7c8ce95879a4ad6aff62e5cbfc8b3ef6 to your computer and use it in GitHub Desktop.
Save rchamarthi/7c8ce95879a4ad6aff62e5cbfc8b3ef6 to your computer and use it in GitHub Desktop.
Script to convert stock portfolio data from ally/tradeking export format to the format expected by Morningstar
#expected format by morningstar
# Symbol,Quantity,Price,Action,TradeDate,Amount,Commission
# ABT,200,49.1475,Buy,8/31/2010,9836.5,7
#current output format by Ally/Tradeking
# "Symbol","Description","Qty","Underl.Stock","CostBasis","Avg Price","Price<sup>*</sup>","Change","Change<br />%","TotalG/L","MarketValue","","",
# "AGN","Allergan Plc","10",,"$2,448.25","$244.83","$173.75","-$1.03","-0.59","-$710.75","$1,737.50","",
import csv
output_columns = "Symbol,Quantity,Price,Amount"
with open('ally.csv', 'r') as ally, open('morningstar.csv','w') as mstar:
portfolio = csv.reader(ally, delimiter=',', quotechar='"')
mstar_csv = csv.writer(mstar, delimiter=",")
header = next(portfolio)
mstar_csv.writerow(output_columns.replace('"','').split(','))
for holding in portfolio:
holding_dict = dict(zip(header, holding))
if holding_dict['Avg Price'] != "": #ignore summary and other rows that don't have a transaction price on them
mstar_csv.writerow([
holding_dict['Symbol'],
holding_dict['Qty'],
holding_dict['Avg Price'].replace("$","").replace(",",""),
holding_dict['CostBasis'].replace("$","").replace(",","")
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment