From 3150f6b3f57504486560632b58a864584b3658b0 Mon Sep 17 00:00:00 2001 From: chiahung Date: Mon, 23 Oct 2023 13:00:17 +0800 Subject: [PATCH] fix --- pkg/strategy/grid2/twin_order.go | 48 +++++++++++++-------------- pkg/strategy/grid2/twin_order_test.go | 2 +- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/pkg/strategy/grid2/twin_order.go b/pkg/strategy/grid2/twin_order.go index 9b9cf7e16..bfdf57721 100644 --- a/pkg/strategy/grid2/twin_order.go +++ b/pkg/strategy/grid2/twin_order.go @@ -125,7 +125,7 @@ type TwinOrderBook struct { size int } -func NewTwinOrderBook(pins []Pin) *TwinOrderBook { +func newTwinOrderBook(pins []Pin) *TwinOrderBook { var v []fixedpoint.Value for _, pin := range pins { v = append(v, fixedpoint.Value(pin)) @@ -143,22 +143,20 @@ func NewTwinOrderBook(pins []Pin) *TwinOrderBook { pinIdx[pin] = i } - ob := TwinOrderBook{ + return &TwinOrderBook{ pins: v, pinIdx: pinIdx, m: m, size: 0, } - - return &ob } -func (book *TwinOrderBook) String() string { +func (b *TwinOrderBook) String() string { var sb strings.Builder sb.WriteString("================== TWIN ORDERBOOK ==================\n") - for _, pin := range book.pins { - twin := book.m[fixedpoint.Value(pin)] + for _, pin := range b.pins { + twin := b.m[fixedpoint.Value(pin)] twinOrder := twin.GetOrder() sb.WriteString(fmt.Sprintf("-> %8s) %s\n", pin, twinOrder.String())) } @@ -166,15 +164,15 @@ func (book *TwinOrderBook) String() string { return sb.String() } -func (book *TwinOrderBook) GetTwinOrderPin(order types.Order) (fixedpoint.Value, error) { - idx, exist := book.pinIdx[order.Price] +func (b *TwinOrderBook) GetTwinOrderPin(order types.Order) (fixedpoint.Value, error) { + idx, exist := b.pinIdx[order.Price] if !exist { return fixedpoint.Zero, fmt.Errorf("the order's (%d) price (%s) is not in pins", order.OrderID, order.Price) } if order.Side == types.SideTypeBuy { 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) } } 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 book.pins[idx], nil + return b.pins[idx], nil } -func (book *TwinOrderBook) AddOrder(order types.Order) error { - pin, err := book.GetTwinOrderPin(order) +func (b *TwinOrderBook) AddOrder(order types.Order) error { + pin, err := b.GetTwinOrderPin(order) if err != nil { return err } - twinOrder, exist := book.m[pin] + twinOrder, exist := b.m[pin] if !exist { // should not happen return fmt.Errorf("no any empty twin order at pins, should not happen, check it") } if !twinOrder.Exist() { - book.size++ + b.size++ } twinOrder.SetOrder(order) return nil } -func (book *TwinOrderBook) GetTwinOrder(pin fixedpoint.Value) *TwinOrder { - return book.m[pin] +func (b *TwinOrderBook) GetTwinOrder(pin fixedpoint.Value) *TwinOrder { + return b.m[pin] } -func (book *TwinOrderBook) AddTwinOrder(pin fixedpoint.Value, order *TwinOrder) { - book.m[pin] = order +func (b *TwinOrderBook) AddTwinOrder(pin fixedpoint.Value, order *TwinOrder) { + b.m[pin] = order } -func (book *TwinOrderBook) Size() int { - return book.size +func (b *TwinOrderBook) Size() int { + return b.size } -func (book *TwinOrderBook) EmptyTwinOrderSize() int { - return len(book.pins) - 1 - book.size +func (b *TwinOrderBook) EmptyTwinOrderSize() int { + return len(b.pins) - 1 - b.size } -func (book *TwinOrderBook) SyncOrderMap() *types.SyncOrderMap { +func (b *TwinOrderBook) SyncOrderMap() *types.SyncOrderMap { orderMap := types.NewSyncOrderMap() - for _, twin := range book.m { + for _, twin := range b.m { if twin.Exist() { orderMap.Add(twin.GetOrder()) } diff --git a/pkg/strategy/grid2/twin_order_test.go b/pkg/strategy/grid2/twin_order_test.go index eb75e93a1..d6204ee94 100644 --- a/pkg/strategy/grid2/twin_order_test.go +++ b/pkg/strategy/grid2/twin_order_test.go @@ -18,7 +18,7 @@ func TestTwinOrderBook(t *testing.T) { Pin(fixedpoint.NewFromInt(2)), } - book := NewTwinOrderBook(pins) + book := newTwinOrderBook(pins) assert.Equal(0, book.Size()) assert.Equal(4, book.EmptyTwinOrderSize()) for _, pin := range pins {