-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
193 lines (130 loc) · 6.81 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from ofxtools.models import *
from ofxtools.Types import *
from ofxtools.utils import UTC
from decimal import Decimal
from datetime import datetime
from pprint import pprint
from ofxtools.header import make_header
import xml.etree.ElementTree as ET
import csv
import uuid
import os
import re
PATH = "data/History_for_Account_9554419996.csv"
OUT_PATH = "output/transactions.qfx"
HEADER = ['Run Date', 'Action', 'Symbol', 'Security Description', 'Security Type', 'Quantity', 'Price ($)', 'Commission ($)', 'Fees ($)', 'Accrued Interest ($)', 'Amount ($)', 'Settlement Date' ]
filename = os.path.basename( PATH )
filename = re.search( ".*_(.*)\.csv", filename )
acctid = filename.group(1)
def validate_file( lines ):
if lines[3] != ['Brokerage']:
print( "[!] Forth line does not contain Brokerage" )
print( lines[3] )
return False
fileHeader = lines[5]
if len( HEADER ) != len( fileHeader ):
print( "[!] Header Length Mismatch" )
return False
for column in HEADER:
if column not in fileHeader:
print( f"[!] Header Column Not Found: {column}" )
return False
return True
def extract_unique_securities( lines ):
lines = lines[1:]
uniqueSecurities = set()
identifier = 100000000
for line in lines:
if line[3].strip() != 'No Description':
uniqueSecurities.add( ( line[2].strip(), line[3].strip(), identifier ) )
identifier = identifier + 1
uniqueSecurities = list( uniqueSecurities )
securityMap = {}
for security in uniqueSecurities:
securityMap[ security[0] ] = security
# pprint( securityMap )
return securityMap
def make_security_list_message_set_response_messages( securityMap ): # SECLISTMSGSRSV1
messages = []
securityList = []
for security in securityMap.values():
secid = SECID( uniqueid = str( security[2] ), uniqueidtype = 'OTHER' )
secname = security[1]
ticker = security[0]
secinfo = SECINFO( secid = secid, secname = secname, ticker = ticker )
mfinfo = MFINFO( secinfo = secinfo )
securityList.append( mfinfo )
seclist = SECLIST( *securityList )
messages = SECLISTMSGSRSV1( seclist )
return messages
def make_investment_statement_message_set_response_messages( securityMap, transactions ):
transactionList = []
response = None
trnuid = "0"
status = STATUS( code = 0, severity = 'INFO' )
startDate = datetime( 3000, 1, 1, tzinfo = UTC )
endDate = datetime( 1970, 1, 1, tzinfo = UTC )
for transaction in transactions[1:]:
transactionDate = datetime.strptime( transaction[0].strip().rstrip(" ET"), "%m/%d/%Y %I:%M:%S %p" ).replace( tzinfo = UTC )
if startDate > transactionDate:
startDate = transactionDate
if endDate < transactionDate:
endDate = transactionDate
description = transaction[1].strip()
symbol = transaction[2].strip() if len( transaction[2].strip() ) > 0 else None
securityType = transaction[4].strip() if len( transaction[4].strip() ) > 0 else None
quantity = float( transaction[5].strip() ) if len( transaction[5].strip() ) > 0 else None
price = float( transaction[6].strip() ) if len( transaction[6].strip() ) > 0 else None
fee = float( transaction[8].strip() ) if len( transaction[8].strip() ) > 0 else None
amount = float( transaction[10].strip() ) if len( transaction[10].strip() ) > 0 else None
settlementDate = datetime.strptime( transaction[11].strip(), "%m/%d/%Y" ).replace( tzinfo = UTC ) if len( transaction[11].strip() ) > 0 else None
# print( f"{transactionDate} {symbol} {quantity} {price} {fee} {amount} {settlementDate}" )
if description.startswith("You bought"):
if amount < 0:
invtran = INVTRAN( fitid = str( uuid.uuid4() ), dttrade = transactionDate, dtsettle = settlementDate )
secid = SECID( uniqueid = str( securityMap[ symbol ][2] ), uniqueidtype = 'OTHER' )
units = quantity
unitprice = price
fees = fee
total = amount * -1
subacctsec = securityType
subacctfund = 'CASH'
invbuy = INVBUY( invtran = invtran, secid = secid, units = units, unitprice = unitprice, fees = fees, total = total, subacctsec = subacctsec, subacctfund = subacctfund )
buyother = BUYOTHER( invbuy = invbuy )
transactionList.append( buyother )
elif description.startswith("Transfer in from brokerage a/c"):
# deal with transfer in from brokerage
pass
else:
print( f"[?] Not Handled {transaction}" )
invtranlist = INVTRANLIST( dtstart = startDate, dtend = endDate, *transactionList )
currentDate = datetime.now().replace( tzinfo = UTC )
invacctfrom = INVACCTFROM( brokerid = "fidelity.com", acctid = acctid )
invstmtrs = INVSTMTRS( dtasof = currentDate, curdef = 'USD', invacctfrom = invacctfrom, invtranlist = invtranlist )
transactionResponse = INVSTMTTRNRS( trnuid = trnuid, status = status, invstmtrs = invstmtrs )
messages = INVSTMTMSGSRSV1( transactionResponse )
return messages
def process_file( lines ):
transactions = list( filter( lambda line: len( line ) > 1, lines ) )
currentDate = datetime.now().replace( tzinfo = UTC )
securityMap = extract_unique_securities( transactions )
securityMessages = make_security_list_message_set_response_messages( securityMap )
transactionMessages = make_investment_statement_message_set_response_messages( securityMap, transactions )
status = STATUS( code = 0, severity = 'INFO' )
fi = FI( org = 'Fidelity Investments', fid = '07776' )
sonrs = SONRS( status = status, dtserver = currentDate, language='ENG', fi = fi )
signonmsgs = SIGNONMSGSRSV1( sonrs = sonrs )
ofx = OFX( signonmsgsrsv1 = signonmsgs, seclistmsgsrsv1 = securityMessages, invstmtmsgsrsv1 = transactionMessages )
root = ofx.to_etree()
ET.indent( root )
fileData = ET.tostring( root ).decode()
header = str( make_header( version = 102 ) )
# print( header )
# print( fileData )
with open( OUT_PATH, "w" ) as fp:
fp.write( header )
fp.write( fileData )
with open(PATH, newline='') as f:
fileLines = list( csv.reader( f ) )
if validate_file( fileLines ):
process_file( fileLines[5:] )