Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() account_info = {'institution': 'IB'} pos_data = {} tree = etree.parse(pos_fname) flexquery_response = tree.getroot() flexquery_statements = flexquery_response.getchildren()[0] for statement in flexquery_statements: account_info['account'] = statement.attrib['accountId'] pos_data['id'] = get_id(account_info, session) date = statement.attrib['toDate'] date, time = add_timezone(date, '235959', fmt='%Y%m%d %H%M%S') pos_data['timestamp'] = '%s %s' % (date, time) for report in statement: for position in report: if 'cash' in report.tag.lower(): cash = report.getchildren()[0].attrib['endingCash'] cash = Decimal('%16.3f' % float(cash)) pos_data['symbol'] = 'USD' pos_data['description'] = 'Brokerage' pos_data['price'] = 1 pos_data['qty'] = cash pos_data['total_value'] = cash else: pos_data['symbol'] = position.attrib['symbol'] pos_data['description'] = position.attrib['description']
except ProgrammingError: pass Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() account_info = {'institution': 'Schwab'} pos_data = {} # Get the timestamp pos_file = open(pos_fname, 'r') lines = [x.strip() for x in pos_file.read().split('\n')] pos_line = [x for x in lines if 'positions' in x.lower()][0] try: dateinfo = pos_line.split('as of ')[-1].split()[0:2] date, time = add_timezone(*dateinfo) except ValueError as err: dateinfo = pos_line.split('as of ')[-1].split(',')[0:2] date, time = add_timezone(*dateinfo, fmt='%I:%M %p ET %m/%d/%Y"') pos_data['timestamp'] = '%s %s' % (date, time) # Split up the data by account accounts = [x for x in lines if 'xxxx-' in x.lower()] totals = [x for x in lines if 'account total' in x.lower()] starts = [lines.index(x) for x in accounts] ends = [lines[x:].index(y) + x for x,y in zip(starts, totals)] ranges = zip(starts, ends) data = ['\n'.join(lines[x[0]+1:x[1]]) for x in ranges]
tree = etree.parse(x) trades = tree.xpath('%s/Trades/Trade' % PATH) trades_dict = dict([(t.get('tradeID'), t.attrib) for t in trades]) sofls = tree.xpath('%s/StmtFunds/StatementOfFundsLine' % PATH) rows = [] for sofl in sofls: tid = sofl.get('tradeID') if tid in trades_dict: row = dict(sofl.attrib.items() + trades_dict[tid].items()) try: row['commission'] = row.pop('ibCommission') except KeyError: pass try: row['commissionCurrency'] = row.pop('ibCommissionCurrency') except KeyError: pass row.pop('date') row['timestamp'] = ' '.join(add_timezone(row.pop('tradeDate'), row.pop('tradeTime'), fmt='%Y%m%d %H%M%S')) else: row = dict(sofl.attrib) row['tradeID'] = -1 row['timestamp'] = ' '.join(add_timezone(row.pop('date'), '160000', fmt='%Y-%m-%d %H%M%S')) row['id'] = get_id({'account': row.pop('accountId'), 'institution': 'IB'}, session) row = dict([(k,row[k]) for k in row if row[k] not in NULL_SYMBOLS]) t = Transaction(**row) tpk = dict([(k, row[k]) for k in t.__table__.primary_key.columns.keys()]) session.add(t) try: session.commit()