-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinteractive_brokers.py
132 lines (114 loc) · 4.41 KB
/
interactive_brokers.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
# Copyright 2012 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Implements InteractiveBrokers.
Does not handle:
* dividends
"""
from __future__ import annotations
import csv
from datetime import datetime
from decimal import Decimal
from typing import Optional
from broker import Broker
from typing_extensions import override
import utils
FIRST_LINE = 'Title,Worksheet for Form 8949,'
class InteractiveBrokers(Broker):
@classmethod
@override
def name(cls) -> str:
return 'Interactive Brokers'
@classmethod
def DetermineEntryCode(cls, part: int, box: str) -> Optional[int]:
if part == 1:
if box == 'A':
return 321
elif box == 'B':
return 711
elif box == 'C':
return 712
elif part == 2:
if box == 'A':
return 323
elif box == 'B':
return 713
elif box == 'C':
return 714
return None
@classmethod
def TryParseYear(cls, date_str: str) -> Optional[int]:
try:
return datetime.strptime(date_str, '%m/%d/%Y').year
except ValueError:
return None
@classmethod
def ParseDollarValue(cls, value: str) -> Decimal:
return Decimal(value.replace(',', '').replace('"', ''))
@classmethod
@override
def isFileForBroker(cls, filename: str) -> bool:
with open(filename) as f:
first_line = f.readline()
return first_line.find(FIRST_LINE) == 0
@classmethod
@override
def parseFileToTxnList(cls, filename: str, tax_year: Optional[int]) -> list[utils.Transaction]:
with open(filename) as f:
# First 2 lines are headers.
f.readline()
f.readline()
txns = csv.reader(f, delimiter=',', quotechar='"')
txn_list: list[utils.Transaction] = []
part: Optional[int] = None
box: Optional[str] = None
entry_code: Optional[int] = None
for row in txns:
if row[0] == 'Part' and len(row) == 3:
box = None
if row[1] == 'I':
part = 1
elif row[1] == 'II':
part = 2
else:
utils.Warning('unknown part line: "%s"\n' % row)
elif row[0] == 'Box' and len(row) == 3:
if row[1] == 'A' or row[1] == 'B' or row[1] == 'C':
box = row[1]
entry_code = cls.DetermineEntryCode(part, box)
else:
utils.Warning('unknown box line: "%s"\n' % row)
elif row[0] == 'Data' and len(row) == 9:
if not entry_code:
utils.Warning(
'ignoring data: "%s" as the code is not defined\n')
continue
txn = utils.Transaction()
txn.desc = row[1]
txn.buyDateStr = row[3]
txn.sellDateStr = row[4]
year = cls.TryParseYear(txn.sellDateStr)
txn.saleProceeds = cls.ParseDollarValue(row[5])
txn.costBasis = cls.ParseDollarValue(row[6])
if row[7]:
txn.adjustment = cls.ParseDollarValue(row[7])
txn.entryCode = entry_code
if tax_year and year and year != tax_year:
utils.Warning('ignoring txn: "%s" as the sale is not from %d\n' %
(txn.desc, tax_year))
else:
txn_list.append(txn)
txn = None
elif (row[0] != 'Header' and row[0] != 'Footer') or len(row) != 9:
utils.Warning('unknown line: "%s"\n' % row)
return txn_list