-
Notifications
You must be signed in to change notification settings - Fork 1
/
lot.go
216 lines (178 loc) · 6.19 KB
/
lot.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (C) 2019 David N. Cohen
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"log"
"math/big"
"sort"
"time"
"src.d10.dev/command"
)
type Lot struct {
name string
date time.Time
weight uint // order tie-break when dates are equal
inventory Amount
startInventory Amount
startCost Amount
price *big.Rat
}
var weight uint // counter for each lot created
func NewLot(name string, date time.Time, inventory, basis Amount) *Lot {
if inventory.Sign() < 1 {
log.Panicf("lot must have positive inventory (%s)", inventory.String()) // sanity
}
if basis.Sign() < 0 {
log.Panicf("lot must have non-negative basis (%s)", basis.String()) // sanity
}
price := new(big.Rat).Quo(basis.Rat, inventory.Rat) // price = (total cost) / (how many)
weight++
this := &Lot{
name: name,
date: date,
weight: weight,
inventory: inventory,
startInventory: inventory,
startCost: basis,
price: price,
}
// sanity
if this.price.Sign() < 0 {
log.Panicf("Calculated new lot (%q) price %s = %s / %s", name, this.price, this.startCost, this.startInventory)
}
return this
}
func (this *Lot) Sell(delta Amount) (actual, basis Amount) {
// sanity
if delta.Sign() > -1 {
log.Panicf("lot.Sell() expects negative amount, got %s", delta)
}
if !delta.Compatible(this.inventory) {
log.Panic("lot.Sell() account/asset mismatch")
}
tmp := new(big.Rat)
tmp.Add(this.inventory.Rat, delta.Rat) // adding negative delta
// tmp is now (inventory - amount to sell)
switch tmp.Sign() {
case -1:
// inventory does not cover delta, actual is limited to inventory amount
actual = this.inventory
this.inventory = this.inventory.ZeroClone() // nothing remains in inventory
case 1:
// inventory has more than enough, put remainder back
this.inventory.Set(tmp)
actual = delta.NegClone()
case 0:
// exact amount, actual is full delta, set inventory to zero
actual = delta.NegClone()
this.inventory = this.inventory.ZeroClone() // nothing remains
}
// calculate basis that corresponds to inventory consumed
basis = this.startCost.ZeroClone()
basis.Mul(this.price, actual.Rat)
basis.Neg(basis.Rat) // convention: amount sold is positive, basis is negative
// sanity
if actual.Sign() < 1 {
log.Panic("lot.Sell() calculated:", actual)
}
if basis.Sign() > 0 { // Note that 0 basis is allowed (i.e. BCH from hard fork)
log.Panic("lot.Sell() basis: ", basis, " from price ", this.price)
}
return actual, basis
}
type order string
const (
FIFO order = "fifo" // first in, first out
LIFO order = "lifo" // last in, first out
)
type LotQueue struct {
lot []Lot
order order
}
func (this LotQueue) Len() int { return len(this.lot) }
func (this LotQueue) Swap(i, j int) { this.lot[i], this.lot[j] = this.lot[j], this.lot[i] }
func (this LotQueue) Less(i, j int) bool {
// we sell from the tail of slice
switch this.order {
case FIFO:
// earliest lot comes last in slice
// treat equal as later, respecting order of transactions in source
return this.lot[i].date.After(this.lot[j].date) || (this.lot[i].date.Equal(this.lot[j].date) && this.lot[i].weight > this.lot[j].weight)
case LIFO:
return this.lot[i].date.Before(this.lot[j].date) || (this.lot[i].date.Equal(this.lot[j].date) && this.lot[i].weight < this.lot[j].weight)
}
log.Panicf("unexpected lot order (%q)", this.order)
return false
}
func (this *LotQueue) Buy(lot Lot) {
this.sanity(lot.inventory)
// TODO(dnc): perhaps we can be more efficient than calling sort
// each time, given we are already ordered.
this.lot = append(this.lot, lot)
sort.Sort(this)
}
// Sell consumes inventory and basis from lots.
func (this *LotQueue) Sell(delta Amount) (lot []Lot, inventory, basis []Amount, err error) {
this.sanity(delta)
command.V(1).Infof("LotQueue.Sell() %s from queue of %d lots", delta.String(), this.Len()) // troubleshoot
remaining := delta.Clone()
var l Lot
for remaining.Sign() != 0 {
if this.Len() == 0 {
// We haven't consumed original delta, but the queue is empty.
err = fmt.Errorf("failed to sell %s (of %s), no remaining inventory", remaining.String(), delta.String())
return
}
// pop from end of slice
l, this.lot = this.lot[len(this.lot)-1], this.lot[:len(this.lot)-1]
sold, soldBasis := l.Sell(remaining)
// sanity
if sold.Sign() == -1 || soldBasis.Sign() == 1 { // basis may be zero
log.Panicf("insane sale: sold %s, basis %s", sold, soldBasis)
}
command.V(1).Infof("Sold %s (%s basis) from lot %s", sold, soldBasis, l.name)
lot = append(lot, l)
inventory = append(inventory, sold)
basis = append(basis, soldBasis)
// note that remaining is negative, sold is positive
remaining.Add(remaining.Rat, sold.Rat)
if remaining.Sign() > -1 {
// entire amount has been consumed from inventory
if remaining.Sign() != 0 { // sanity
log.Panic("lotFIFO.Sell() remaining:", remaining) // should never be reached
}
if l.inventory.Sign() > 0 {
// append unsold inventory back to queue
this.lot = append(this.lot, l)
}
}
}
command.V(1).Infof("LotQueue.Sell() sold %s, %d lots remain", delta.String(), this.Len()) // troubleshoot
return lot, inventory, basis, err
}
func (this LotQueue) sanity(delta Amount) {
if delta.Sign() == 0 {
log.Panic("attempt to buy/sell zero amount")
}
if this.Len() == 0 {
if delta.Sign() < 0 {
log.Panicf("attempt to sell (%#v %s) from empty inventory (%#v)", delta, delta.Asset, this)
} else {
return
}
}
// sanity
if delta.Asset != this.lot[0].inventory.Asset {
log.Panicf("currency mismatch: want %q, got %q", delta.Asset, this.lot[0].inventory.Asset)
}
}