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 the initiating side to Binance trades #1782

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions exchanges/binance/binance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ type TradeStream struct {
BuyerOrderID int64 `json:"b"`
SellerOrderID int64 `json:"a"`
TimeStamp time.Time `json:"T"`
Maker bool `json:"m"`
IsBuyerMaker bool `json:"m"`
BestMatchPrice bool `json:"M"`
}

Expand Down Expand Up @@ -288,7 +288,7 @@ type AggregatedTrade struct {
FirstTradeID int64 `json:"f"`
LastTradeID int64 `json:"l"`
TimeStamp time.Time `json:"T"`
Maker bool `json:"m"`
IsBuyerMaker bool `json:"m"`
BestMatchPrice bool `json:"M"`
}

Expand Down
25 changes: 15 additions & 10 deletions exchanges/binance/binance_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,21 @@ func (b *Binance) wsHandleData(respRaw []byte) error {
b.Name,
err)
}
return b.Websocket.Trade.Update(saveTradeData,
trade.Data{
CurrencyPair: pair,
Timestamp: t.TimeStamp,
Price: t.Price.Float64(),
Amount: t.Quantity.Float64(),
Exchange: b.Name,
AssetType: asset.Spot,
TID: strconv.FormatInt(t.TradeID, 10),
})
td := trade.Data{
CurrencyPair: pair,
Timestamp: t.TimeStamp,
Price: t.Price.Float64(),
Amount: t.Quantity.Float64(),
Exchange: b.Name,
AssetType: asset.Spot,
TID: strconv.FormatInt(t.TradeID, 10)}

if t.IsBuyerMaker { // Seller is Taker
td.Side = order.Sell
} else { // Buyer is Taker
td.Side = order.Buy
}
Beadko marked this conversation as resolved.
Show resolved Hide resolved
return b.Websocket.Trade.Update(saveTradeData, td)
case "ticker":
var t TickerStream
err = json.Unmarshal(jsonData, &t)
Expand Down
39 changes: 31 additions & 8 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,15 +783,21 @@ func (b *Binance) GetRecentTrades(ctx context.Context, p currency.Pair, a asset.
}

for i := range tradeData {
resp = append(resp, trade.Data{
td := trade.Data{
TID: strconv.FormatInt(tradeData[i].ID, 10),
Exchange: b.Name,
CurrencyPair: p,
AssetType: a,
Price: tradeData[i].Price,
Amount: tradeData[i].Quantity,
Timestamp: tradeData[i].Time,
})
}
if tradeData[i].IsBuyerMaker { // Seller is Taker
td.Side = order.Sell
} else { // Buyer is Taker
td.Side = order.Buy
}
resp = append(resp, td)
}
case asset.USDTMarginedFutures:
tradeData, err := b.URecentTrades(ctx, pFmt, "", limit)
Expand All @@ -800,15 +806,21 @@ func (b *Binance) GetRecentTrades(ctx context.Context, p currency.Pair, a asset.
}

for i := range tradeData {
resp = append(resp, trade.Data{
td := trade.Data{
TID: strconv.FormatInt(tradeData[i].ID, 10),
Exchange: b.Name,
CurrencyPair: p,
AssetType: a,
Price: tradeData[i].Price,
Amount: tradeData[i].Qty,
Timestamp: tradeData[i].Time.Time(),
})
}
if tradeData[i].IsBuyerMaker { // Seller is Taker
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me wish for c# ? operator

td.Side = order.Sell
} else { // Buyer is Taker
td.Side = order.Buy
}
resp = append(resp, td)
}
case asset.CoinMarginedFutures:
tradeData, err := b.GetFuturesPublicTrades(ctx, pFmt, limit)
Expand All @@ -817,15 +829,21 @@ func (b *Binance) GetRecentTrades(ctx context.Context, p currency.Pair, a asset.
}

for i := range tradeData {
resp = append(resp, trade.Data{
td := trade.Data{
TID: strconv.FormatInt(tradeData[i].ID, 10),
Exchange: b.Name,
CurrencyPair: p,
AssetType: a,
Price: tradeData[i].Price,
Amount: tradeData[i].Qty,
Timestamp: tradeData[i].Time.Time(),
})
}
if tradeData[i].IsBuyerMaker { // Seller is Taker
td.Side = order.Sell
} else { // Buyer is Taker
td.Side = order.Buy
}
resp = append(resp, td)
}
}

Expand Down Expand Up @@ -864,16 +882,21 @@ func (b *Binance) GetHistoricTrades(ctx context.Context, p currency.Pair, a asse
}
result := make([]trade.Data, len(trades))
for i := range trades {
result[i] = trade.Data{
td := trade.Data{
CurrencyPair: p,
TID: strconv.FormatInt(trades[i].ATradeID, 10),
Amount: trades[i].Quantity,
Exchange: b.Name,
Price: trades[i].Price,
Timestamp: trades[i].TimeStamp,
AssetType: a,
Side: order.AnySide,
}
if trades[i].IsBuyerMaker { // Seller is Taker
td.Side = order.Sell
} else { // Buyer is Taker
td.Side = order.Buy
}
result[i] = td
}
return result, nil
}
Expand Down
Loading