forked from dmjones/qif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
investment_transaction.go
192 lines (167 loc) · 4.13 KB
/
investment_transaction.go
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
// SPDX-License-Identifier: Apache-2.0
package qif
import "github.com/pkg/errors"
import "github.com/shopspring/decimal"
type InvestmentAction uint
// Constant value for each Investment Action
const (
ActionUndefined InvestmentAction = iota
ActionBuy
ActionBuyX
ActionSell
ActionSellX
ActionCGLong
ActionCGLongX
ActionCGMid
ActionCGMidX
ActionCGShort
ActionCGShortX
ActionDiv
ActionDivX
ActionIntInc
ActionIntIncX
ActionReInvDiv
ActionReInvInt
ActionReInvLg
ActionReInvMd
ActionReInvSh
ActionReprice
ActionXIn
ActionXOut
ActionMiscExp
ActionMiscExpX
ActionMiscInc
ActionMiscIncX
ActionMarginInt
ActionMarginIntX
ActionReturnCap
ActionReturnCapX
ActionStockSplit
ActionSharesOut
ActionSharesIn
)
var (
actionMap = map[string]InvestmentAction {
"Buy": ActionBuy,
"BuyX": ActionBuyX,
"Sell": ActionSell,
"SellX": ActionSellX,
"CGLong": ActionCGLong,
"CGLongX": ActionCGLongX,
"CGMid": ActionCGMid,
"CGMidX": ActionCGMidX,
"CGShort": ActionCGShort,
"CGShortX": ActionCGShortX,
"Div": ActionDiv,
"DivX": ActionDivX,
"IntInc": ActionIntInc,
"IntIncX": ActionIntIncX,
"ReInvDiv": ActionReInvDiv,
"ReInvInt": ActionReInvInt,
"ReInvLg": ActionReInvLg,
"ReInvMd": ActionReInvMd,
"ReInvSh": ActionReInvSh,
"Reprice": ActionReprice,
"XIn": ActionXIn,
"XOut": ActionXOut,
"MiscExp": ActionMiscExp,
"MiscExpX": ActionMiscExpX,
"MiscInc": ActionMiscInc,
"MiscIncX": ActionMiscIncX,
"MargInt": ActionMarginInt,
"MargIntX": ActionMarginIntX,
"RtrnCap": ActionReturnCap,
"RtrnCapX": ActionReturnCapX,
"StkSplit": ActionStockSplit,
"ShrsOut": ActionSharesOut,
"ShrsIn": ActionSharesIn,
}
)
// An InvestmentTransaction contains the information associated with
// transactions for Investment accounts.
type InvestmentTransaction interface {
Transaction
// Investment Action (Buy, Sell, etc.) as type InvestmentAction.
Action() InvestmentAction
// Investment Action (Buy, Sell, etc.) as type string.
ActionString() string
// Name of Security that was bought or sold (name of Stock, Fund, etc).
SecurityName() string
// Quantity of Shares (or split ratio, if Action is StkSplit).
Shares() decimal.Decimal
// Price which Investment Action was executed at.
Price() decimal.Decimal
// Commission cost (generally trades are commission-free these days).
Commission() decimal.Decimal
}
type investmentTransaction struct {
transaction
action InvestmentAction
actionString string
securityName string
shares decimal.Decimal
price decimal.Decimal
commission decimal.Decimal
}
func (t *investmentTransaction) Action() InvestmentAction {
return t.action
}
func (t *investmentTransaction) ActionString() string {
return t.actionString
}
func (t *investmentTransaction) SecurityName() string {
return t.securityName
}
func (t *investmentTransaction) Shares() decimal.Decimal {
return t.shares
}
func (t *investmentTransaction) Price() decimal.Decimal {
return t.price
}
func (t *investmentTransaction) Commission() decimal.Decimal {
return t.commission
}
func (t *investmentTransaction) parseInvestmentTransactionField(line string,
config Config) error {
if line == "" {
return errors.New("line is empty")
}
err := t.parseTransactionField(line, config)
if err == nil {
// Must have been a field from our embedded struct
return nil
}
if _, ok := err.(UnsupportedFieldError); !ok {
// An actual error happened
return err
}
// Otherwise, try and parse it here
switch line[0] {
case 'N':
t.actionString = line[1:]
t.action = actionMap[t.actionString]
return nil
case 'Y':
t.securityName = line[1:]
return nil
case 'Q':
shares := line[1:]
t.shares, err = decimal.NewFromString(shares)
return err
case 'I':
price := line[1:]
t.price, err = decimal.NewFromString(price)
return err
case 'O':
commission := line[1:]
t.commission, err = decimal.NewFromString(commission)
return err
default:
return UnsupportedFieldError(
errors.Errorf("cannot process line '%s'", line))
}
}
func (t *investmentTransaction) parseTransactionTypeField(line string,
config Config) error {
return t.parseInvestmentTransactionField(line, config)
}