Merge pull request #235 from jessy1092/binance_parser

Fix: Correct the binance executionReport parser
This commit is contained in:
Yo-An Lin 2021-05-12 19:01:14 +08:00 committed by GitHub
commit dcd66d3449
2 changed files with 57 additions and 16 deletions

View File

@ -50,7 +50,7 @@ executionReport
"M": false, // Ignore "M": false, // Ignore
"O": 1499405658657, // Order creation time "O": 1499405658657, // Order creation time
"Z": "0.00000000", // Cumulative quote asset transacted quantity "Z": "0.00000000", // Cumulative quote asset transacted quantity
"Y": "0.00000000", // Last quote asset transacted quantity (i.e. lastPrice * lastQty) "Y": "0.00000000", // Last quote asset transacted quantity (i.e. lastPrice * lastQty)
"Q": "0.00000000" // Quote Order Qty "Q": "0.00000000" // Quote Order Qty
} }
*/ */
@ -58,17 +58,27 @@ type ExecutionReportEvent struct {
EventBase EventBase
Symbol string `json:"s"` Symbol string `json:"s"`
ClientOrderID string `json:"c"`
Side string `json:"S"` Side string `json:"S"`
OrderType string `json:"o"`
TimeInForce string `json:"f"`
OrderQuantity string `json:"q"` ClientOrderID string `json:"c"`
OriginalClientOrderID string `json:"C"`
OrderType string `json:"o"`
OrderCreationTime int64 `json:"O"`
TimeInForce string `json:"f"`
IcebergQuantity string `json:"F"`
OrderQuantity string `json:"q"`
QuoteOrderQuantity string `json:"Q"`
OrderPrice string `json:"p"` OrderPrice string `json:"p"`
StopPrice string `json:"P"` StopPrice string `json:"P"`
IsOnBook bool `json:"w"` IsOnBook bool `json:"w"`
IsMaker bool `json:"m"` IsMaker bool `json:"m"`
Ignore bool `json:"M"`
CommissionAmount string `json:"n"` CommissionAmount string `json:"n"`
CommissionAsset string `json:"N"` CommissionAsset string `json:"N"`
@ -82,12 +92,13 @@ type ExecutionReportEvent struct {
TradeID int64 `json:"t"` TradeID int64 `json:"t"`
TransactionTime int64 `json:"T"` TransactionTime int64 `json:"T"`
LastExecutedQuantity string `json:"l"` LastExecutedQuantity string `json:"l"`
CumulativeFilledQuantity string `json:"z"` LastExecutedPrice string `json:"L"`
LastExecutedPrice string `json:"L"`
LastQuoteAssetTransactedQuantity string `json:"Y"`
OrderCreationTime int64 `json:"O"` CumulativeFilledQuantity string `json:"z"`
CumulativeQuoteAssetTransactedQuantity string `json:"Z"`
LastQuoteAssetTransactedQuantity string `json:"Y"`
} }
func (e *ExecutionReportEvent) Order() (*types.Order, error) { func (e *ExecutionReportEvent) Order() (*types.Order, error) {
@ -246,6 +257,7 @@ type ResultEvent struct {
func ParseEvent(message string) (interface{}, error) { func ParseEvent(message string) (interface{}, error) {
val, err := fastjson.Parse(message) val, err := fastjson.Parse(message)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -144,7 +144,7 @@ func TestParseOrderUpdate(t *testing.T) {
"f": "GTC", // Time in force "f": "GTC", // Time in force
"q": "1.00000000", // Order quantity "q": "1.00000000", // Order quantity
"p": "0.10264410", // Order price "p": "0.10264410", // Order price
"P": "0.00000000", // Stop price "P": "0.222", // Stop price
"F": "0.00000000", // Iceberg quantity "F": "0.00000000", // Iceberg quantity
"g": -1, // OrderListId "g": -1, // OrderListId
"C": null, // Original client order ID; This is the ID of the order being canceled "C": null, // Original client order ID; This is the ID of the order being canceled
@ -154,7 +154,7 @@ func TestParseOrderUpdate(t *testing.T) {
"i": 4293153, // Order ID "i": 4293153, // Order ID
"l": "0.00000000", // Last executed quantity "l": "0.00000000", // Last executed quantity
"z": "0.00000000", // Cumulative filled quantity "z": "0.00000000", // Cumulative filled quantity
"L": "0.00000000", // Last executed price "L": "0.00000001", // Last executed price
"n": "0", // Commission amount "n": "0", // Commission amount
"N": null, // Commission asset "N": null, // Commission asset
"T": 1499405658657, // Transaction time "T": 1499405658657, // Transaction time
@ -162,11 +162,11 @@ func TestParseOrderUpdate(t *testing.T) {
"I": 8641984, // Ignore "I": 8641984, // Ignore
"w": true, // Is the order on the book? "w": true, // Is the order on the book?
"m": false, // Is this trade the maker side? "m": false, // Is this trade the maker side?
"M": false, // Ignore "M": true, // Ignore
"O": 1499405658657, // Order creation time "O": 1499405658657, // Order creation time
"Z": "0.00000000", // Cumulative quote asset transacted quantity "Z": "0.1", // Cumulative quote asset transacted quantity
"Y": "0.00000000", // Last quote asset transacted quantity (i.e. lastPrice * lastQty) "Y": "0.00000000", // Last quote asset transacted quantity (i.e. lastPrice * lastQty)
"Q": "0.00000000" // Quote Order Qty "Q": "2.0" // Quote Order Qty
}` }`
payload = jsCommentTrimmer.ReplaceAllLiteralString(payload, "") payload = jsCommentTrimmer.ReplaceAllLiteralString(payload, "")
@ -179,6 +179,35 @@ func TestParseOrderUpdate(t *testing.T) {
assert.True(t, ok) assert.True(t, ok)
assert.NotNil(t, executionReport) assert.NotNil(t, executionReport)
assert.Equal(t, executionReport.Symbol, "ETHBTC")
assert.Equal(t, executionReport.Side, "BUY")
assert.Equal(t, executionReport.ClientOrderID, "mUvoqJxFIILMdfAW5iGSOW")
assert.Equal(t, executionReport.OriginalClientOrderID, "")
assert.Equal(t, executionReport.OrderType, "LIMIT")
assert.Equal(t, executionReport.OrderCreationTime, int64(1499405658657))
assert.Equal(t, executionReport.TimeInForce, "GTC")
assert.Equal(t, executionReport.IcebergQuantity, "0.00000000")
assert.Equal(t, executionReport.OrderQuantity, "1.00000000")
assert.Equal(t, executionReport.QuoteOrderQuantity, "2.0")
assert.Equal(t, executionReport.OrderPrice, "0.10264410")
assert.Equal(t, executionReport.StopPrice, "0.222")
assert.Equal(t, executionReport.IsOnBook, true)
assert.Equal(t, executionReport.IsMaker, false)
assert.Equal(t, executionReport.Ignore, true)
assert.Equal(t, executionReport.CommissionAmount, "0")
assert.Equal(t, executionReport.CommissionAsset, "")
assert.Equal(t, executionReport.CurrentExecutionType, "NEW")
assert.Equal(t, executionReport.CurrentOrderStatus, "NEW")
assert.Equal(t, executionReport.OrderID, int64(4293153))
assert.Equal(t, executionReport.Ignored, int64(8641984))
assert.Equal(t, executionReport.TradeID, int64(-1))
assert.Equal(t, executionReport.TransactionTime, int64(1499405658657))
assert.Equal(t, executionReport.LastExecutedQuantity, "0.00000000")
assert.Equal(t, executionReport.LastExecutedPrice, "0.00000001")
assert.Equal(t, executionReport.CumulativeFilledQuantity, "0.00000000")
assert.Equal(t, executionReport.CumulativeQuoteAssetTransactedQuantity, "0.1")
assert.Equal(t, executionReport.LastQuoteAssetTransactedQuantity, "0.00000000")
orderUpdate, err := executionReport.Order() orderUpdate, err := executionReport.Order()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, orderUpdate) assert.NotNil(t, orderUpdate)