bbgo_origin/pkg/bbgo/activeorderbook_test.go
2023-11-24 19:00:51 +08:00

91 lines
2.0 KiB
Go

package bbgo
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func TestActiveOrderBook_pendingOrders(t *testing.T) {
now := time.Now()
ob := NewActiveOrderBook("")
filled := false
ob.OnFilled(func(o types.Order) {
filled = true
})
// if we received filled order first
// should be added to pending orders
ob.Update(types.Order{
OrderID: 99,
SubmitOrder: types.SubmitOrder{
Symbol: "BTCUSDT",
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Quantity: number(0.01),
Price: number(19000.0),
AveragePrice: fixedpoint.Zero,
StopPrice: fixedpoint.Zero,
},
Status: types.OrderStatusFilled,
CreationTime: types.Time(now),
UpdateTime: types.Time(now),
})
assert.Len(t, ob.pendingOrderUpdates.Orders(), 1)
o99, ok := ob.pendingOrderUpdates.Get(99)
if assert.True(t, ok) {
assert.Equal(t, types.OrderStatusFilled, o99.Status)
}
// should be added to pending orders
ob.Add(types.Order{
OrderID: 99,
SubmitOrder: types.SubmitOrder{
Symbol: "BTCUSDT",
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Quantity: number(0.01),
Price: number(19000.0),
AveragePrice: fixedpoint.Zero,
StopPrice: fixedpoint.Zero,
},
Status: types.OrderStatusNew,
CreationTime: types.Time(now),
UpdateTime: types.Time(now),
})
assert.True(t, filled, "filled event should be fired")
}
func Test_isNewerUpdate(t *testing.T) {
a := types.Order{
Status: types.OrderStatusPartiallyFilled,
ExecutedQuantity: number(0.2),
}
b := types.Order{
Status: types.OrderStatusPartiallyFilled,
ExecutedQuantity: number(0.1),
}
ret := isNewerOrderUpdate(a, b)
assert.True(t, ret)
}
func Test_isNewerUpdateTime(t *testing.T) {
a := types.Order{
UpdateTime: types.NewTimeFromUnix(200, 0),
}
b := types.Order{
UpdateTime: types.NewTimeFromUnix(100, 0),
}
ret := isNewerOrderUpdateTime(a, b)
assert.True(t, ret)
}