Binance websocket: Order Detail Execution Report (#750)

* * Binance websocket: set commission of order Detail

* * Binance websocket: set commissionAsset of order.Detail

* * Binance websocket: change CostAsset to type currency.Code

* * Binance websocket: call NewCode after checking CommissionAsset is not empty. This guarantees that NULL CommissionAsset is mapped to an empty order.Detail.CostAsset. Otherwise, reflect.DeepEqual will fail on test. Update TestWsOrderExecutionReport json message to BTC commission.

* * Binance websocket: binance websocket "ExecutionReport" only provides "trade" status (partially filled), so check that executed amount == total amount before change status to filled
This commit is contained in:
Rocky Yang
2021-08-17 12:33:54 +08:00
committed by GitHub
parent 96669e29cf
commit c38757a689
3 changed files with 14 additions and 1 deletions

View File

@@ -2488,7 +2488,8 @@ func TestSetExchangeOrderExecutionLimits(t *testing.T) {
func TestWsOrderExecutionReport(t *testing.T) {
// cannot run in parallel due to inspecting the DataHandler result
payload := []byte(`{"stream":"jTfvpakT2yT0hVIo5gYWVihZhdM2PrBgJUZ5PyfZ4EVpCkx4Uoxk5timcrQc","data":{"e":"executionReport","E":1616627567900,"s":"BTCUSDT","c":"c4wyKsIhoAaittTYlIVLqk","S":"BUY","o":"LIMIT","f":"GTC","q":"0.00028400","p":"52789.10000000","P":"0.00000000","F":"0.00000000","g":-1,"C":"","x":"NEW","X":"NEW","r":"NONE","i":5340845958,"l":"0.00000000","z":"0.00000000","L":"0.00000000","n":"0","N":null,"T":1616627567900,"t":-1,"I":11388173160,"w":true,"m":false,"M":false,"O":1616627567900,"Z":"0.00000000","Y":"0.00000000","Q":"0.00000000"}}`)
payload := []byte(`{"stream":"jTfvpakT2yT0hVIo5gYWVihZhdM2PrBgJUZ5PyfZ4EVpCkx4Uoxk5timcrQc","data":{"e":"executionReport","E":1616627567900,"s":"BTCUSDT","c":"c4wyKsIhoAaittTYlIVLqk","S":"BUY","o":"LIMIT","f":"GTC","q":"0.00028400","p":"52789.10000000","P":"0.00000000","F":"0.00000000","g":-1,"C":"","x":"NEW","X":"NEW","r":"NONE","i":5340845958,"l":"0.00000000","z":"0.00000000","L":"0.00000000","n":"0","N":"BTC","T":1616627567900,"t":-1,"I":11388173160,"w":true,"m":false,"M":false,"O":1616627567900,"Z":"0.00000000","Y":"0.00000000","Q":"0.00000000"}}`)
// this is a buy BTC order, normally commission is charged in BTC, vice versa.
expRes := order.Detail{
Price: 52789.1,
Amount: 0.00028400,
@@ -2502,6 +2503,8 @@ func TestWsOrderExecutionReport(t *testing.T) {
Pair: currency.NewPair(currency.BTC, currency.USDT),
RemainingAmount: 0.000284,
Date: time.Unix(0, 1616627567900*int64(time.Millisecond)),
Cost: 0,
CostAsset: currency.BTC,
}
// empty the channel. otherwise mock_test will fail
for len(b.Websocket.DataHandler) > 0 {

View File

@@ -234,6 +234,9 @@ func (b *Binance) wsHandleData(respRaw []byte) error {
Err: err,
}
}
if oStatus == order.PartiallyFilled && data.Data.CumulativeFilledQuantity == data.Data.Quantity {
oStatus = order.Filled
}
var p currency.Pair
var a asset.Item
p, a, err = b.GetRequestFormattedPairAndAssetType(data.Data.Symbol)
@@ -244,6 +247,10 @@ func (b *Binance) wsHandleData(respRaw []byte) error {
if oStatus == order.Cancelled {
clientOrderID = data.Data.CancelledClientOrderID
}
var costAsset currency.Code
if data.Data.CommissionAsset != "" {
costAsset = currency.NewCode(data.Data.CommissionAsset)
}
b.Websocket.DataHandler <- &order.Detail{
Price: data.Data.Price,
Amount: data.Data.Quantity,
@@ -258,6 +265,8 @@ func (b *Binance) wsHandleData(respRaw []byte) error {
Date: data.Data.OrderCreationTime,
Pair: p,
ClientOrderID: clientOrderID,
Cost: data.Data.Commission,
CostAsset: costAsset,
}
return nil
case "listStatus":

View File

@@ -133,6 +133,7 @@ type Detail struct {
ExecutedAmount float64
RemainingAmount float64
Cost float64
CostAsset currency.Code
Fee float64
Exchange string
InternalOrderID string