handle stock split
This commit is contained in:
@@ -114,7 +114,7 @@ func TestFillerQueueNilReceiver(t *testing.T) {
|
||||
t.Fatalf(`want panic message %q but got "%v"`, expMsg, r)
|
||||
}
|
||||
}()
|
||||
rq.Push(NewFiller(nil))
|
||||
rq.Push(NewFiller(&testRecord{}))
|
||||
}
|
||||
|
||||
type testRecord struct {
|
||||
@@ -122,11 +122,11 @@ type testRecord struct {
|
||||
|
||||
id int
|
||||
quantity decimal.Decimal
|
||||
price decimal.Decimal
|
||||
}
|
||||
|
||||
func (tr testRecord) Quantity() decimal.Decimal {
|
||||
return tr.quantity
|
||||
}
|
||||
func (tr testRecord) Quantity() decimal.Decimal { return tr.quantity }
|
||||
func (tr testRecord) Price() decimal.Decimal { return tr.price }
|
||||
|
||||
func TestFiller_Fill(t *testing.T) {
|
||||
tests := []struct {
|
||||
@@ -185,3 +185,89 @@ func TestFiller_Fill(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFiller_ApplySplit(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
qty float64
|
||||
price float64
|
||||
prefilled float64
|
||||
ratio float64
|
||||
wantQty float64
|
||||
wantPrice float64
|
||||
wantFilled float64
|
||||
wantCostBasis float64
|
||||
}{
|
||||
{
|
||||
name: "5:1 split on unfilled lot preserves cost basis",
|
||||
qty: 10, price: 100, prefilled: 0, ratio: 5,
|
||||
wantQty: 50, wantPrice: 20, wantFilled: 0, wantCostBasis: 1000,
|
||||
},
|
||||
{
|
||||
name: "5:1 split on partially filled lot",
|
||||
qty: 10, price: 100, prefilled: 4, ratio: 5,
|
||||
wantQty: 50, wantPrice: 20, wantFilled: 20, wantCostBasis: 1000,
|
||||
},
|
||||
{
|
||||
name: "1:2 reverse split on unfilled lot preserves cost basis",
|
||||
qty: 10, price: 100, prefilled: 0, ratio: 0.5,
|
||||
wantQty: 5, wantPrice: 200, wantFilled: 0, wantCostBasis: 1000,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f := NewFiller(&testRecord{
|
||||
quantity: decimal.NewFromFloat(tt.qty),
|
||||
price: decimal.NewFromFloat(tt.price),
|
||||
})
|
||||
if tt.prefilled > 0 {
|
||||
f.Fill(decimal.NewFromFloat(tt.prefilled))
|
||||
}
|
||||
|
||||
f.ApplySplit(decimal.NewFromFloat(tt.ratio))
|
||||
|
||||
if !f.Quantity().Equal(decimal.NewFromFloat(tt.wantQty)) {
|
||||
t.Errorf("want quantity %v but got %v", tt.wantQty, f.Quantity())
|
||||
}
|
||||
if !f.Price().Equal(decimal.NewFromFloat(tt.wantPrice)) {
|
||||
t.Errorf("want price %v but got %v", tt.wantPrice, f.Price())
|
||||
}
|
||||
if !f.filled.Equal(decimal.NewFromFloat(tt.wantFilled)) {
|
||||
t.Errorf("want filled %v but got %v", tt.wantFilled, f.filled)
|
||||
}
|
||||
costBasis := f.Quantity().Mul(f.Price())
|
||||
if !costBasis.Equal(decimal.NewFromFloat(tt.wantCostBasis)) {
|
||||
t.Errorf("want cost basis %v but got %v", tt.wantCostBasis, costBasis)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFillerQueue_AdjustForSplit(t *testing.T) {
|
||||
var fq FillerQueue
|
||||
fq.Push(NewFiller(&testRecord{quantity: decimal.NewFromFloat(10), price: decimal.NewFromFloat(100)}))
|
||||
fq.Push(NewFiller(&testRecord{quantity: decimal.NewFromFloat(5), price: decimal.NewFromFloat(200)}))
|
||||
|
||||
fq.AdjustForSplit(decimal.NewFromFloat(5))
|
||||
|
||||
lot1, _ := fq.Pop()
|
||||
if !lot1.Quantity().Equal(decimal.NewFromFloat(50)) {
|
||||
t.Errorf("lot1: want quantity 50 but got %v", lot1.Quantity())
|
||||
}
|
||||
if !lot1.Price().Equal(decimal.NewFromFloat(20)) {
|
||||
t.Errorf("lot1: want price 20 but got %v", lot1.Price())
|
||||
}
|
||||
|
||||
lot2, _ := fq.Pop()
|
||||
if !lot2.Quantity().Equal(decimal.NewFromFloat(25)) {
|
||||
t.Errorf("lot2: want quantity 25 but got %v", lot2.Quantity())
|
||||
}
|
||||
if !lot2.Price().Equal(decimal.NewFromFloat(40)) {
|
||||
t.Errorf("lot2: want price 40 but got %v", lot2.Price())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFillerQueue_AdjustForSplit_NilReceiver(t *testing.T) {
|
||||
var fq *FillerQueue
|
||||
fq.AdjustForSplit(decimal.NewFromFloat(5)) // must not panic
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user