Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add LastMatchHeight in match engine #611

Merged
merged 1 commit into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions plugins/dex/matcheng/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

type MatchEng struct {
Book OrderBookInterface
LastMatchHeight int64
Book OrderBookInterface
// LotSize may be based on price level, which can be set
// before any match() call
LotSize int64
Expand All @@ -30,6 +31,7 @@ type MatchEng struct {
// NewMatchEng constructs a new MatchEng.
func NewMatchEng(pairSymbol string, basePrice, lotSize int64, priceLimit float64) *MatchEng {
return &MatchEng{
LastMatchHeight: 0,
Book: NewOrderBookOnULList(10000, 16),
LotSize: lotSize,
PriceLimitPct: priceLimit,
Expand Down Expand Up @@ -164,7 +166,7 @@ func (me *MatchEng) reserveQty(residual int64, orders []OrderPart) bool {
// in such case, there should be alerts and all the new orders in this round should be rejected and dropped from order books
// cancel order should be handled 1st before calling Match().
// IOC orders should be handled after Match()
func (me *MatchEng) MatchBeforeGalileo() bool {
func (me *MatchEng) MatchBeforeGalileo(height int64) bool {
me.Trades = me.Trades[:0]
r := me.Book.GetOverlappedRange(&me.overLappedLevel, &me.buyBuf, &me.sellBuf)
if r <= 0 {
Expand All @@ -178,6 +180,7 @@ func (me *MatchEng) MatchBeforeGalileo() bool {
totalExec := me.overLappedLevel[index].AccumulatedExecutions
me.Trades = me.Trades[:0]
me.LastTradePrice = lastPx
me.LastMatchHeight = height
i, j := 0, len(me.overLappedLevel)-1
//sell below the price at index or buy above the price would not get filled
for i <= index && j >= index && compareBuy(totalExec, 0) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion plugins/dex/matcheng/engine_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func (me *MatchEng) Match(height int64) bool {
if !sdk.IsUpgrade(upgrade.BEP19) {
return me.MatchBeforeGalileo()
return me.MatchBeforeGalileo(height)
}
me.logger.Debug("match starts...", "height", height)
me.Trades = me.Trades[:0]
Expand All @@ -41,6 +41,7 @@ func (me *MatchEng) Match(height int64) bool {
surplus := me.overLappedLevel[index].BuySellSurplus
me.fillOrdersNew(takerSide, takerSideOrders, index, tradePrice, surplus)
me.LastTradePrice = tradePrice
me.LastMatchHeight = height
return true
}

Expand Down
12 changes: 6 additions & 6 deletions plugins/dex/matcheng/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("91", BUYSIDE, 107, 100, 50)
me.Book.InsertOrder("92", SELLSIDE, 108, 90, 50)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
assert.Equal(3, len(me.overLappedLevel))
assert.Equal(int64(98), me.LastTradePrice)
assert.Equal("[{92 98 50 50 50 1 0} {3 98 80 80 80 2 0} {3 98 20 20 100 4 0} {5 98 50 50 50 6 0} {5 98 50 50 100 91 0} {9 98 50 50 50 8 0}]", fmt.Sprint(me.Trades))
Expand All @@ -789,7 +789,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("9", SELLSIDE, 106, 101, 50)
me.Book.InsertOrder("91", BUYSIDE, 107, 100, 50)
me.Book.InsertOrder("92", SELLSIDE, 108, 102, 50)
assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
assert.Equal(0, len(me.overLappedLevel))
assert.Equal(0, len(me.Trades))

Expand All @@ -799,7 +799,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("1", BUYSIDE, 102, 100, 100)
me.Book.InsertOrder("8", BUYSIDE, 103, 99, 100)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
assert.Equal(3, len(me.overLappedLevel))
assert.Equal("[{3 99 100 100 100 1 0} {5 99 100 100 100 8 0}]", fmt.Sprint(me.Trades))

Expand All @@ -815,7 +815,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("91", BUYSIDE, 107, 100, 50)
me.Book.InsertOrder("92", SELLSIDE, 108, 97, 50)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
assert.Equal(3, len(me.overLappedLevel))
assert.Equal("[{92 98 50 50 50 1 0} {3 98 80 80 80 2 0} {3 98 20 20 100 4 0} {5 98 50 50 50 6 0} {5 98 50 50 100 91 0}]", fmt.Sprint(me.Trades))

Expand Down Expand Up @@ -843,7 +843,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("92", SELLSIDE, 105, 100, 100)
me.Book.InsertOrder("93", BUYSIDE, 105, 100, 300)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
t.Log(me.overLappedLevel)
assert.Equal(6, len(me.overLappedLevel))
assert.Equal(int64(100), me.LastTradePrice)
Expand Down Expand Up @@ -879,7 +879,7 @@ func TestMatchEng_DropFilledOrder(t *testing.T) {
me.Book.InsertOrder("92", SELLSIDE, 105, 100, 100)
me.Book.InsertOrder("93", BUYSIDE, 105, 100, 300)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
t.Log(me.overLappedLevel)
assert.Equal(6, len(me.overLappedLevel))
assert.Equal(int64(100), me.LastTradePrice)
Expand Down
10 changes: 10 additions & 0 deletions plugins/dex/order/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ func (kp *Keeper) GetPriceLevel(pair string, side int8, price int64) *me.PriceLe
}
}

func (kp *Keeper) GetLastTrades(height int64, pair string) ([]me.Trade, int64) {
if eng, ok := kp.engines[pair]; ok {
if eng.LastMatchHeight == height {
return eng.Trades, eng.LastTradePrice
}
}
return nil, 0
}

// !!! FOR TEST USE ONLY
func (kp *Keeper) GetLastTradesForPair(pair string) ([]me.Trade, int64) {
if eng, ok := kp.engines[pair]; ok {
return eng.Trades, eng.LastTradePrice
Expand Down