#NEWLINE# def parse_fidelity_transactions( reader ) : #NEWLINE# #INDENT# parsed = [ ] #NEWLINE# for row in reader : #NEWLINE# #INDENT# if ( len ( row ) < 11 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# date_field = row [ 0 ] . strip ( ) #NEWLINE# action_field = row [ 1 ] . strip ( ) #NEWLINE# symbol_field = row [ 2 ] . strip ( ) #NEWLINE# symbol_description_field = row [ 3 ] . strip ( ) #NEWLINE# quantity_field = row [ 5 ] . strip ( ) #NEWLINE# price_field = row [ 6 ] . strip ( ) #NEWLINE# amount_field = row [ 10 ] . strip ( ) #NEWLINE# linked_symbol = None #NEWLINE# if ( ( symbol_field == '' ) and ( price_field == '' ) ) : #NEWLINE# #INDENT# symbol = CASH_SYMBOL #NEWLINE# type = ( 'DEPOSIT' if ( float ( amount_field ) >= 0 ) else 'WITHDRAW' ) #NEWLINE# quantity = abs ( float ( amount_field ) ) #NEWLINE# price = 1.0 #NEWLINE# total = quantity #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action_field . startswith ( 'YOU BOUGHT' ) or action_field . startswith ( 'YOU SOLD' ) or ( ( symbol_description_field != 'CASH' ) and ( action_field in [ 'PURCHASE INTO CORE ACCOUNT' , 'REDEMPTION FROM CORE ACCOUNT' , 'REINVESTMENT' ] ) ) ) : #NEWLINE# #INDENT# symbol = symbol_field #NEWLINE# type = ( 'SELL' if ( action_field . startswith ( 'YOU SOLD' ) or ( action_field == 'REDEMPTION FROM CORE ACCOUNT' ) ) else 'BUY' ) #NEWLINE# quantity = abs ( float ( quantity_field ) ) #NEWLINE# price = float ( price_field ) #NEWLINE# total = abs ( float ( amount_field ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action_field in [ 'SHORT-TERM CAP GAIN' , 'LONG-TERM CAP GAIN' , 'DIVIDEND RECEIVED' , 'INTEREST EARNED' ] ) : #NEWLINE# #INDENT# symbol = CASH_SYMBOL #NEWLINE# type = 'ADJUST' #NEWLINE# quantity = float ( amount_field ) #NEWLINE# price = 1.0 #NEWLINE# total = abs ( float ( amount_field ) ) #NEWLINE# linked_symbol = ( symbol_field if ( symbol_description_field != 'CASH' ) else None ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# parsed . append ( { #NEWLINE# #INDENT# 'date' : datetime . strptime ( date_field , '%m/%d/%Y' ) . date ( ) , #NEWLINE# 'type' : symbol , #NEWLINE# 'symbol' : symbol , #NEWLINE# 'quantity' : quantity , #NEWLINE# 'price' : price , #NEWLINE# 'total' : total , #NEWLINE# 'linked_symbol' : linked_symbol , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# return parsed