-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvanguard.py
154 lines (126 loc) · 5.01 KB
/
vanguard.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
# 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 Vanguard.
Assumes reconciled transactions, i.e., sell follows buy.
Does not handle:
* dividends
* short sales
* partial lot sales
"""
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 = ','.join(['"Trade Date"', '"Transaction Type"',
'"Investment Name"', '"Symbol"', '"Shares"',
'"Principal Amount"', '"Net Amount"\n'])
class Vanguard(Broker):
@classmethod
@override
def name(cls) -> str:
return 'Vanguard'
@classmethod
def isBuy(cls, txn: dict[str, str]) -> bool:
return txn['Transaction Type'] == 'Buy'
@classmethod
def isSell(cls, txn: dict[str, str]) -> bool:
return txn['Transaction Type'] == 'Sell'
@classmethod
def date(cls, txn: dict[str, str]) -> datetime:
"""Returns date of transaction as datetime object."""
# Our input date format is YYYY/MM/DD.
return datetime.strptime(txn['Trade Date'], '%Y-%m-%d')
@classmethod
def symbol(cls, txn: dict[str, str]) -> str:
return txn['Symbol']
@classmethod
def investmentName(cls, txn: dict[str, str]) -> str:
return txn['Investment Name']
@classmethod
def numShares(cls, txn: dict[str, str]) -> int:
shares = int(txn['Shares'])
if cls.isSell(txn):
return shares * -1
else:
return shares
@classmethod
def netAmount(cls, txn: dict[str, str]) -> Decimal:
amount = Decimal(txn['Net Amount'])
if cls.isBuy(txn):
return amount * -1
else:
return amount
@classmethod
@override
def isFileForBroker(cls, filename: str) -> bool:
with open(filename) as f:
first_line = f.readline()
return first_line == FIRST_LINE
@classmethod
@override
def parseFileToTxnList(cls, filename: str, tax_year: Optional[int]) -> list[utils.Transaction]:
txns = csv.reader(open(filename), delimiter=',', quotechar='"')
row_num: int = 0
txn_list: list[utils.Transaction] = []
names: list[str] = []
curr_txn: Optional[utils.Transaction] = None
buy: dict[str, str] = {}
sell: dict[str, str] = {}
for row in txns:
row_num = row_num + 1
if row_num == 1:
names = row
continue
txn_dict = {}
for i in range(0, len(names)):
txn_dict[names[i]] = row[i]
if cls.isBuy(txn_dict):
buy = txn_dict
curr_txn = utils.Transaction()
curr_txn.desc = '%d shares %s' % (
cls.numShares(buy), cls.symbol(buy))
curr_txn.buyDate = cls.date(txn_dict)
curr_txn.buyDateStr = utils.txfDate(curr_txn.buyDate)
curr_txn.costBasis = cls.netAmount(txn_dict)
elif cls.isSell(txn_dict):
sell = txn_dict
# Assume that sells follow the buys, so we can attach this sale to the
# current buy txn we are processing.
assert curr_txn is not None
assert cls.numShares(buy) == cls.numShares(sell)
assert cls.symbol(buy) == cls.symbol(sell)
assert cls.investmentName(buy) == cls.investmentName(sell)
buyDate: datetime = curr_txn.buyDate
sellDate: datetime = cls.date(sell)
curr_txn.sellDateStr = utils.txfDate(sellDate)
curr_txn.saleProceeds = cls.netAmount(sell)
if utils.isLongTerm(buyDate, sellDate):
curr_txn.entryCode = 323 # "LT gain/loss - security"
else:
curr_txn.entryCode = 321 # "ST gain/loss - security"
assert sellDate >= buyDate, f'Sell date ({sellDate}) must be on or after buy date ({buyDate})'
if tax_year and sellDate.year != tax_year:
utils.Warning('ignoring txn: "%s" as the sale is not from %d\n' %
(curr_txn.desc, tax_year))
continue
txn_list.append(curr_txn)
# Clear both the buy and the sell as we have matched them up.
buy = {}
sell = {}
curr_txn = None
return txn_list