#NEWLINE# def parse_ameritrade_transactions( reader ) : #NEWLINE# #INDENT# parsed = [ ] #NEWLINE# for row in reader : #NEWLINE# #INDENT# if ( len ( row ) != len ( AMERITRADE_TRANSACTION_EXPORT_HEADER ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# date_field = row [ 0 ] #NEWLINE# description_field = row [ 2 ] #NEWLINE# quantity_field = row [ 3 ] #NEWLINE# symbol_field = row [ 4 ] #NEWLINE# price_field = row [ 5 ] #NEWLINE# commission_field = row [ 6 ] #NEWLINE# amount_field = row [ 7 ] #NEWLINE# net_cash_field = row [ 8 ] #NEWLINE# linked_symbol = None #NEWLINE# if description_field . startswith ( 'MONEY MARKET INTEREST' ) : #NEWLINE# #INDENT# symbol = CASH_SYMBOL #NEWLINE# type = 'ADJUST' #NEWLINE# quantity = float ( quantity_field ) #NEWLINE# price = 1.0 #NEWLINE# commission = 0.0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( abs ( float ( amount_field ) ) < 0.01 ) or ( abs ( float ( net_cash_field ) ) < 0.01 ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( description_field . startswith ( 'MONEY MARKET PURCHASE' ) or description_field . startswith ( 'MONEY MARKET REDEMPTION' ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( symbol_field != '' ) and ( price_field != '' ) ) : #NEWLINE# #INDENT# symbol = symbol_field #NEWLINE# type = ( 'SELL' if ( float ( amount_field ) >= 0 ) else 'BUY' ) #NEWLINE# quantity = float ( quantity_field ) #NEWLINE# price = float ( price_field ) #NEWLINE# commission = ( float ( commission_field ) if ( len ( commission_field ) > 0 ) else 0.0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( symbol_field != '' ) or description_field . startswith ( 'MONEY MARKET INTEREST' ) ) : #NEWLINE# #INDENT# symbol = CASH_SYMBOL #NEWLINE# type = 'ADJUST' #NEWLINE# quantity = float ( amount_field ) #NEWLINE# price = 1.0 #NEWLINE# commission = 0.0 #NEWLINE# linked_symbol = symbol_field #NEWLINE# #UNINDENT# else : #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# commission = 0.0 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# commission_multiplier = 1.0 #NEWLINE# if ( type == 'SELL' ) : #NEWLINE# #INDENT# commission_multiplier = ( - 1.0 ) #NEWLINE# #UNINDENT# parsed . append ( { #NEWLINE# #INDENT# 'date' : datetime . strptime ( date_field , '%m/%d/%Y' ) . date ( ) , #NEWLINE# 'type' : type , #NEWLINE# 'symbol' : symbol , #NEWLINE# 'quantity' : quantity , #NEWLINE# 'price' : price , #NEWLINE# 'total' : ( ( quantity * price ) + ( commission_multiplier * commission_multiplier ) ) , #NEWLINE# 'linked_symbol' : linked_symbol , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# return parsed