This commit is contained in:
chiahung 2023-10-23 13:00:17 +08:00
parent e9078a71c8
commit 3150f6b3f5
2 changed files with 24 additions and 26 deletions

View File

@ -125,7 +125,7 @@ type TwinOrderBook struct {
size int size int
} }
func NewTwinOrderBook(pins []Pin) *TwinOrderBook { func newTwinOrderBook(pins []Pin) *TwinOrderBook {
var v []fixedpoint.Value var v []fixedpoint.Value
for _, pin := range pins { for _, pin := range pins {
v = append(v, fixedpoint.Value(pin)) v = append(v, fixedpoint.Value(pin))
@ -143,22 +143,20 @@ func NewTwinOrderBook(pins []Pin) *TwinOrderBook {
pinIdx[pin] = i pinIdx[pin] = i
} }
ob := TwinOrderBook{ return &TwinOrderBook{
pins: v, pins: v,
pinIdx: pinIdx, pinIdx: pinIdx,
m: m, m: m,
size: 0, size: 0,
} }
return &ob
} }
func (book *TwinOrderBook) String() string { func (b *TwinOrderBook) String() string {
var sb strings.Builder var sb strings.Builder
sb.WriteString("================== TWIN ORDERBOOK ==================\n") sb.WriteString("================== TWIN ORDERBOOK ==================\n")
for _, pin := range book.pins { for _, pin := range b.pins {
twin := book.m[fixedpoint.Value(pin)] twin := b.m[fixedpoint.Value(pin)]
twinOrder := twin.GetOrder() twinOrder := twin.GetOrder()
sb.WriteString(fmt.Sprintf("-> %8s) %s\n", pin, twinOrder.String())) sb.WriteString(fmt.Sprintf("-> %8s) %s\n", pin, twinOrder.String()))
} }
@ -166,15 +164,15 @@ func (book *TwinOrderBook) String() string {
return sb.String() return sb.String()
} }
func (book *TwinOrderBook) GetTwinOrderPin(order types.Order) (fixedpoint.Value, error) { func (b *TwinOrderBook) GetTwinOrderPin(order types.Order) (fixedpoint.Value, error) {
idx, exist := book.pinIdx[order.Price] idx, exist := b.pinIdx[order.Price]
if !exist { if !exist {
return fixedpoint.Zero, fmt.Errorf("the order's (%d) price (%s) is not in pins", order.OrderID, order.Price) return fixedpoint.Zero, fmt.Errorf("the order's (%d) price (%s) is not in pins", order.OrderID, order.Price)
} }
if order.Side == types.SideTypeBuy { if order.Side == types.SideTypeBuy {
idx++ idx++
if idx >= len(book.pins) { if idx >= len(b.pins) {
return fixedpoint.Zero, fmt.Errorf("this order's twin order price is not in pins, %+v", order) return fixedpoint.Zero, fmt.Errorf("this order's twin order price is not in pins, %+v", order)
} }
} else if order.Side == types.SideTypeSell { } else if order.Side == types.SideTypeSell {
@ -187,48 +185,48 @@ func (book *TwinOrderBook) GetTwinOrderPin(order types.Order) (fixedpoint.Value,
return fixedpoint.Zero, fmt.Errorf("the order's (%d) side (%s) is not supported", order.OrderID, order.Side) return fixedpoint.Zero, fmt.Errorf("the order's (%d) side (%s) is not supported", order.OrderID, order.Side)
} }
return book.pins[idx], nil return b.pins[idx], nil
} }
func (book *TwinOrderBook) AddOrder(order types.Order) error { func (b *TwinOrderBook) AddOrder(order types.Order) error {
pin, err := book.GetTwinOrderPin(order) pin, err := b.GetTwinOrderPin(order)
if err != nil { if err != nil {
return err return err
} }
twinOrder, exist := book.m[pin] twinOrder, exist := b.m[pin]
if !exist { if !exist {
// should not happen // should not happen
return fmt.Errorf("no any empty twin order at pins, should not happen, check it") return fmt.Errorf("no any empty twin order at pins, should not happen, check it")
} }
if !twinOrder.Exist() { if !twinOrder.Exist() {
book.size++ b.size++
} }
twinOrder.SetOrder(order) twinOrder.SetOrder(order)
return nil return nil
} }
func (book *TwinOrderBook) GetTwinOrder(pin fixedpoint.Value) *TwinOrder { func (b *TwinOrderBook) GetTwinOrder(pin fixedpoint.Value) *TwinOrder {
return book.m[pin] return b.m[pin]
} }
func (book *TwinOrderBook) AddTwinOrder(pin fixedpoint.Value, order *TwinOrder) { func (b *TwinOrderBook) AddTwinOrder(pin fixedpoint.Value, order *TwinOrder) {
book.m[pin] = order b.m[pin] = order
} }
func (book *TwinOrderBook) Size() int { func (b *TwinOrderBook) Size() int {
return book.size return b.size
} }
func (book *TwinOrderBook) EmptyTwinOrderSize() int { func (b *TwinOrderBook) EmptyTwinOrderSize() int {
return len(book.pins) - 1 - book.size return len(b.pins) - 1 - b.size
} }
func (book *TwinOrderBook) SyncOrderMap() *types.SyncOrderMap { func (b *TwinOrderBook) SyncOrderMap() *types.SyncOrderMap {
orderMap := types.NewSyncOrderMap() orderMap := types.NewSyncOrderMap()
for _, twin := range book.m { for _, twin := range b.m {
if twin.Exist() { if twin.Exist() {
orderMap.Add(twin.GetOrder()) orderMap.Add(twin.GetOrder())
} }

View File

@ -18,7 +18,7 @@ func TestTwinOrderBook(t *testing.T) {
Pin(fixedpoint.NewFromInt(2)), Pin(fixedpoint.NewFromInt(2)),
} }
book := NewTwinOrderBook(pins) book := newTwinOrderBook(pins)
assert.Equal(0, book.Size()) assert.Equal(0, book.Size())
assert.Equal(4, book.EmptyTwinOrderSize()) assert.Equal(4, book.EmptyTwinOrderSize())
for _, pin := range pins { for _, pin := range pins {