Skip to content

Commit

Permalink
add trade status
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyyangz committed Jun 11, 2019
1 parent b717830 commit cc65dd8
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 75 deletions.
15 changes: 8 additions & 7 deletions plugins/dex/matcheng/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (me *MatchEng) fillOrders(i int, j int) {
trade,
buys[k].CumQty,
sells[h].CumQty,
buys[k].Id})
buys[k].Id,
Unknown})
h++
case r < 0:
trade := buys[k].nxtTrade
Expand All @@ -95,7 +96,8 @@ func (me *MatchEng) fillOrders(i int, j int) {
trade,
buys[k].CumQty,
sells[h].CumQty,
buys[k].Id})
buys[k].Id,
Unknown})
k++
case r == 0:
trade := sells[h].nxtTrade
Expand All @@ -109,7 +111,8 @@ func (me *MatchEng) fillOrders(i int, j int) {
trade,
buys[k].CumQty,
sells[h].CumQty,
buys[k].Id})
buys[k].Id,
Unknown})
h++
k++
}
Expand Down Expand Up @@ -217,8 +220,6 @@ func (me *MatchEng) MatchDeprecated() bool {
return true
}



//DropFilledOrder() would clear the order to remove
func (me *MatchEng) DropFilledOrder() (droppedIds []string) {
droppedIds = make([]string, 0, len(me.overLappedLevel)<<1)
Expand Down Expand Up @@ -269,13 +270,13 @@ func (me *MatchEng) Dump() {
"LastTradePrice", me.LastTradePrice)
buys, sells := me.Book.GetAllLevels()

if sellsBz, err:= json.MarshalIndent(sells, "", " "); err != nil {
if sellsBz, err := json.MarshalIndent(sells, "", " "); err != nil {
me.logger.Error("marshal sells failed")
} else {
me.logger.Info(string(sellsBz))
}

if buysBz, err:= json.MarshalIndent(buys, "", " "); err != nil {
if buysBz, err := json.MarshalIndent(buys, "", " "); err != nil {
me.logger.Error("marshal buys failed")
} else {
me.logger.Info(string(buysBz))
Expand Down
18 changes: 16 additions & 2 deletions plugins/dex/matcheng/engine_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (me *MatchEng) Match(height int64) bool {
return false
}
me.LastTradePrice = tradePrice
surplus := me.overLappedLevel[index].BuySellSurplus

// 1. drop redundant qty
// 2. rearrange the orders by their trade price and time.
Expand All @@ -39,7 +40,7 @@ func (me *MatchEng) Match(height int64) bool {
me.logger.Error("create MakerTakerOrders failed", "error", err)
return false
}
me.fillOrdersNew(makerTakerOrders)
me.fillOrdersNew(makerTakerOrders, surplus)
return true
}

Expand Down Expand Up @@ -224,7 +225,7 @@ func mergeOnePriceLevel(side int8, height int64, priceLevel *OverLappedLevel,
}
}

func (me *MatchEng) fillOrdersNew(makerTakerOrders *MakerTakerOrders) {
func (me *MatchEng) fillOrdersNew(makerTakerOrders *MakerTakerOrders, surplus int64) {
takers := makerTakerOrders.takerSide.orders
totalTakerQty := makerTakerOrders.takerSide.totalQty
nTakers := len(takers)
Expand Down Expand Up @@ -260,12 +261,25 @@ func (me *MatchEng) fillOrdersNew(makerTakerOrders *MakerTakerOrders) {
LastPx: makerLevel.price,
LastQty: filledQty,
}
if surplus < 0 {
trade.Status = SellSurplus
} else if surplus > 0 {
trade.Status = BuySurplus
} else {
trade.Status = Neutral
}
if makerTakerOrders.isBuySideMaker {
trade.Sid, trade.Bid = taker.Id, maker.Id
trade.SellCumQty, trade.BuyCumQty = taker.CumQty, maker.CumQty
if maker.Time < taker.Time {
trade.Status = SellTaker
}
} else {
trade.Sid, trade.Bid = maker.Id, taker.Id
trade.SellCumQty, trade.BuyCumQty = maker.CumQty, taker.CumQty
if maker.Time < taker.Time {
trade.Status = BuyTaker
}
}
me.Trades = append(me.Trades, trade)
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/dex/matcheng/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func getTradePrice(overlapped *[]OverLappedLevel, maxExec *LevelIndex,
return math.MaxInt64, -1
}

// allocateResidualDeprecated() assumes toAlloc is less than sum of quantity in orders.
// allocateResidual() assumes toAlloc is less than sum of quantity in orders.
// It would try best to evenly allocate toAlloc among orders in proportion of order qty meanwhile by whole lot
// Due to lotsize change, it is possible the order would not be allocated with a full lot.
func allocateResidual(toAlloc *int64, orders []OrderPart, lotSize int64) bool {
Expand Down
94 changes: 47 additions & 47 deletions plugins/dex/matcheng/match_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,20 +1029,20 @@ func TestMatchEng_fillOrdersNew(t *testing.T) {
},
},
}
me.fillOrdersNew(makerTakerOrders)
me.fillOrdersNew(makerTakerOrders, 100)
assert.Equal([]Trade{
{"6", 110, 160, 860, 760, "1"},
{"7", 110, 40, 900, 940, "1"},
{"8", 110, 100, 1000, 800, "1"},
{"8", 110, 20, 920, 820, "2"},
{"9", 110, 80, 1000, 880, "2"},
{"6", 110, 160, 860, 760, "1", SellTaker},
{"7", 110, 40, 900, 940, "1", SellTaker},
{"8", 110, 100, 1000, 800, "1", SellTaker},
{"8", 110, 20, 920, 820, "2", SellTaker},
{"9", 110, 80, 1000, 880, "2", SellTaker},

{"6", 100, 200, 1000, 960, "3"},
{"6", 100, 40, 740, 1000, "4"},
{"7", 100, 60, 800, 1000, "4"},
{"8", 100, 180, 980, 1000, "4"},
{"9", 100, 20, 1000, 900, "4"},
{"9", 100, 100, 300, 1000, "5"},
{"6", 100, 200, 1000, 960, "3", SellTaker},
{"6", 100, 40, 740, 1000, "4", BuySurplus},
{"7", 100, 60, 800, 1000, "4", BuySurplus},
{"8", 100, 180, 980, 1000, "4", BuySurplus},
{"9", 100, 20, 1000, 900, "4", BuySurplus},
{"9", 100, 100, 300, 1000, "5", BuySurplus},
}, me.Trades)
assert.Equal([]*OrderPart{
{"1", 99, 1000, 1000, 0},
Expand Down Expand Up @@ -1094,20 +1094,20 @@ func TestMatchEng_fillOrdersNew(t *testing.T) {
},
},
}
me.fillOrdersNew(makerTakerOrders)
me.fillOrdersNew(makerTakerOrders, -100)
assert.Equal([]Trade{
{"6", 100, 150, 850, 750, "1"},
{"6", 100, 50, 950, 800, "2"},
{"6", 100, 100, 900, 900, "3"},
{"6", 100, 100, 800, 1000, "4"},
{"7", 100, 50, 850, 950, "4"},
{"7", 100, 50, 250, 1000, "5"},
{"6", 100, 150, 850, 750, "1", BuyTaker},
{"6", 100, 50, 950, 800, "2", BuyTaker},
{"6", 100, 100, 900, 900, "3", BuyTaker},
{"6", 100, 100, 800, 1000, "4", BuyTaker},
{"7", 100, 50, 850, 950, "4", BuyTaker},
{"7", 100, 50, 250, 1000, "5", BuyTaker},

{"8", 90, 150, 1000, 850, "1"},
{"8", 90, 50, 1000, 900, "2"},
{"8", 90, 100, 1000, 1000, "3"},
{"9", 90, 150, 1000, 950, "4"},
{"9", 90, 50, 300, 1000, "5"},
{"8", 90, 150, 1000, 850, "1", BuyTaker},
{"8", 90, 50, 1000, 900, "2", BuyTaker},
{"8", 90, 100, 1000, 1000, "3", BuyTaker},
{"9", 90, 150, 1000, 950, "4", SellSurplus},
{"9", 90, 50, 300, 1000, "5", SellSurplus},
}, me.Trades)
assert.Equal([]*OrderPart{
{"6", 99, 1000, 1000, 0},
Expand Down Expand Up @@ -1155,15 +1155,15 @@ func TestMatchEng_fillOrdersNew(t *testing.T) {
},
},
}
me.fillOrdersNew(makerTakerOrders)
me.fillOrdersNew(makerTakerOrders, 0)
assert.Equal([]Trade{
{"1", 100, 300, 900, 1000, "6"},
{"2", 100, 100, 1000, 1000, "6"},
{"3", 100, 100, 1000, 900, "7"},
{"3", 100, 100, 800, 1000, "8"},
{"4", 100, 200, 1000, 900, "8"},
{"4", 100, 100, 900, 1000, "9"},
{"5", 100, 100, 1000, 300, "9"},
{"1", 100, 300, 900, 1000, "6", Neutral},
{"2", 100, 100, 1000, 1000, "6", Neutral},
{"3", 100, 100, 1000, 900, "7", Neutral},
{"3", 100, 100, 800, 1000, "8", Neutral},
{"4", 100, 200, 1000, 900, "8", Neutral},
{"4", 100, 100, 900, 1000, "9", Neutral},
{"5", 100, 100, 1000, 300, "9", Neutral},
}, me.Trades)
assert.Equal([]*OrderPart{
{"1", 100, 1000, 1000, 0},
Expand Down Expand Up @@ -1204,14 +1204,14 @@ func TestMatchEng_Match(t *testing.T) {
assert.Equal(4, len(me.overLappedLevel))
assert.Equal(int64(100), me.LastTradePrice)
assert.Equal([]Trade{
{"7", 100, 25, 25, 25, "12"},
{"3", 100, 10, 35, 10, "12"},
{"1", 100, 5, 40, 5, "12"},
{"15", 100, 40, 80, 40, "12"},
{"17", 100, 20, 100, 20, "12"},
{"13", 100, 10, 110, 10, "12"},
{"11", 100, 20, 20, 20, "16"},
{"11", 100, 10, 10, 30, "14"},
{"7", 100, 25, 25, 25, "12", BuyTaker},
{"3", 100, 10, 35, 10, "12", BuyTaker},
{"1", 100, 5, 40, 5, "12", BuyTaker},
{"15", 100, 40, 80, 40, "12", SellSurplus},
{"17", 100, 20, 100, 20, "12", SellSurplus},
{"13", 100, 10, 110, 10, "12", SellSurplus},
{"11", 100, 20, 20, 20, "16", SellSurplus},
{"11", 100, 10, 10, 30, "14", SellSurplus},
}, me.Trades)
me.DropFilledOrder()
buys, sells := me.Book.GetAllLevels()
Expand Down Expand Up @@ -1258,13 +1258,13 @@ func TestMatchEng_Match(t *testing.T) {
assert.Equal(4, len(me.overLappedLevel))
assert.Equal(int64(104), me.LastTradePrice)
assert.Equal([]Trade{
{"1", 100, 20, 20, 20, "12"},
{"3", 100, 10, 30, 10, "12"},
{"5", 100, 10, 40, 10, "12"},
{"15", 104, 40, 80, 40, "12"},
{"17", 104, 20, 100, 20, "12"},
{"13", 104, 10, 110, 10, "12"},
{"11", 104, 30, 140, 30, "12"},
{"1", 100, 20, 20, 20, "12", BuyTaker},
{"3", 100, 10, 30, 10, "12", BuyTaker},
{"5", 100, 10, 40, 10, "12", BuyTaker},
{"15", 104, 40, 80, 40, "12", SellSurplus},
{"17", 104, 20, 100, 20, "12", SellSurplus},
{"13", 104, 10, 110, 10, "12", SellSurplus},
{"11", 104, 30, 140, 30, "12", SellSurplus},
}, me.Trades)
me.DropFilledOrder()
buys, sells = me.Book.GetAllLevels()
Expand Down
30 changes: 15 additions & 15 deletions plugins/dex/matcheng/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,12 @@ func TestMatchEng_fillOrders(t *testing.T) {
assert.Equal(int64(0), me.overLappedLevel[0].SellTotal)
t.Log(me.Trades)
assert.Equal([]Trade{
Trade{"6", 999, 70, 70, 70, "1"},
Trade{"6", 999, 30, 30, 100, "2"},
Trade{"7", 999, 50, 80, 50, "2"},
Trade{"8", 999, 70, 70, 70, "3"},
Trade{"9", 999, 30, 100, 30, "3"},
Trade{"9", 999, 30, 30, 60, "4"},
Trade{"6", 999, 70, 70, 70, "1", Unknown},
Trade{"6", 999, 30, 30, 100, "2", Unknown},
Trade{"7", 999, 50, 80, 50, "2", Unknown},
Trade{"8", 999, 70, 70, 70, "3", Unknown},
Trade{"9", 999, 30, 100, 30, "3", Unknown},
Trade{"9", 999, 30, 30, 60, "4", Unknown},
}, me.Trades)

me.Trades = me.Trades[:0]
Expand Down Expand Up @@ -530,12 +530,12 @@ func TestMatchEng_fillOrders(t *testing.T) {
assert.Equal(int64(0), me.overLappedLevel[1].SellTotal)
t.Log(me.Trades) //
assert.Equal([]Trade{
Trade{"6", 999, 70, 70, 70, "1"},
Trade{"6", 999, 30, 30, 100, "2"},
Trade{"7", 999, 50, 80, 50, "2"},
Trade{"8", 999, 70, 70, 70, "3"},
Trade{"9", 999, 30, 100, 30, "3"},
Trade{"9", 999, 30, 30, 60, "4"},
Trade{"6", 999, 70, 70, 70, "1", Unknown},
Trade{"6", 999, 30, 30, 100, "2", Unknown},
Trade{"7", 999, 50, 80, 50, "2", Unknown},
Trade{"8", 999, 70, 70, 70, "3", Unknown},
Trade{"9", 999, 30, 100, 30, "3", Unknown},
Trade{"9", 999, 30, 30, 60, "4", Unknown},
}, me.Trades)
}

Expand Down Expand Up @@ -776,7 +776,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
assert.True(me.MatchDeprecated())
assert.Equal(3, len(me.overLappedLevel))
assert.Equal(int64(98), me.LastTradePrice)
assert.Equal("[{92 98 50 50 50 1} {3 98 80 80 80 2} {3 98 20 20 100 4} {5 98 50 50 50 6} {5 98 50 50 100 91} {9 98 50 50 50 8}]", fmt.Sprint(me.Trades))
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))

me.Book = NewOrderBookOnULList(4, 2)
me.Book.InsertOrder("3", SELLSIDE, 100, 101, 100)
Expand All @@ -801,7 +801,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {

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

me.Book = NewOrderBookOnULList(4, 2)
me.Book.InsertOrder("3", SELLSIDE, 100, 98, 100)
Expand All @@ -817,7 +817,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {

assert.True(me.MatchDeprecated())
assert.Equal(3, len(me.overLappedLevel))
assert.Equal("[{92 98 50 50 50 1} {3 98 80 80 80 2} {3 98 20 20 100 4} {5 98 50 50 50 6} {5 98 50 50 100 91}]", fmt.Sprint(me.Trades))
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))

/* 3. the least abs surplus imbalance (Step 2)
--------------------------------------------------------------
Expand Down
16 changes: 13 additions & 3 deletions plugins/dex/matcheng/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ const (
// PRECISION is the last effective decimal digit of the price of currency pair
const PRECISION = 1

// Trade status
const (
Unknown = iota
SellTaker
BuyTaker
BuySurplus
SellSurplus
Neutral
)

//Trade stores an execution between 2 orders on a *currency pair*.
//3 things needs attention:
// - srcId and oid are just different names; actually no concept of source or destination;
Expand All @@ -27,6 +37,7 @@ type Trade struct {
BuyCumQty int64 // cumulative executed quantity for the buy order
SellCumQty int64 // cumulative executed quantity for the sell order
Bid string // buy order Id
Status int8
}

type OrderPart struct {
Expand Down Expand Up @@ -163,8 +174,8 @@ type MergedPriceLevel struct {

func NewMergedPriceLevel(price int64) *MergedPriceLevel {
return &MergedPriceLevel{
price: price,
orders: make([]*OrderPart, 0),
price: price,
orders: make([]*OrderPart, 0),
totalQty: 0,
}
}
Expand All @@ -188,4 +199,3 @@ type MakerSideOrders struct {
type TakerSideOrders struct {
*MergedPriceLevel
}

0 comments on commit cc65dd8

Please sign in to comment.