2023-10-20 08:23:18 +00:00
|
|
|
package grid2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTwinOrderBook(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
pins := []Pin{
|
|
|
|
Pin(fixedpoint.NewFromInt(3)),
|
|
|
|
Pin(fixedpoint.NewFromInt(4)),
|
|
|
|
Pin(fixedpoint.NewFromInt(1)),
|
|
|
|
Pin(fixedpoint.NewFromInt(5)),
|
|
|
|
Pin(fixedpoint.NewFromInt(2)),
|
|
|
|
}
|
|
|
|
|
2023-10-23 05:00:17 +00:00
|
|
|
book := newTwinOrderBook(pins)
|
2023-10-20 08:23:18 +00:00
|
|
|
assert.Equal(0, book.Size())
|
|
|
|
assert.Equal(4, book.EmptyTwinOrderSize())
|
|
|
|
for _, pin := range pins {
|
|
|
|
twinOrder := book.GetTwinOrder(fixedpoint.Value(pin))
|
|
|
|
if !assert.NotNil(twinOrder) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.False(twinOrder.Exist())
|
|
|
|
}
|
|
|
|
|
|
|
|
orders := []types.Order{
|
|
|
|
{
|
|
|
|
OrderID: 1,
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
|
|
|
Price: fixedpoint.NewFromInt(2),
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrderID: 2,
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
|
|
|
Price: fixedpoint.NewFromInt(4),
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, order := range orders {
|
|
|
|
assert.NoError(book.AddOrder(order))
|
|
|
|
}
|
|
|
|
assert.Equal(2, book.Size())
|
|
|
|
assert.Equal(2, book.EmptyTwinOrderSize())
|
|
|
|
|
|
|
|
for _, order := range orders {
|
|
|
|
pin, err := book.GetTwinOrderPin(order)
|
|
|
|
if !assert.NoError(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
twinOrder := book.GetTwinOrder(pin)
|
|
|
|
if !assert.True(twinOrder.Exist()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(order.OrderID, twinOrder.GetOrder().OrderID)
|
|
|
|
}
|
|
|
|
}
|