Skip to content

Commit

Permalink
Add the initiating side to Binance trades
Browse files Browse the repository at this point in the history
  • Loading branch information
Beadko committed Jan 20, 2025
1 parent f0c785d commit 902f580
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions exchanges/binance/binance_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ func (b *Binance) wsHandleData(respRaw []byte) error {
b.Name,
err)
}
// maker placed buy order, so taker market sell
side := order.Buy
if t.Maker {
side = order.Sell
}
return b.Websocket.Trade.Update(saveTradeData,
trade.Data{
CurrencyPair: pair,
Expand All @@ -345,6 +350,7 @@ func (b *Binance) wsHandleData(respRaw []byte) error {
Amount: t.Quantity.Float64(),
Exchange: b.Name,
AssetType: asset.Spot,
Side: side,
TID: strconv.FormatInt(t.TradeID, 10),
})
case "ticker":
Expand Down
25 changes: 24 additions & 1 deletion exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,13 +783,19 @@ func (b *Binance) GetRecentTrades(ctx context.Context, p currency.Pair, a asset.
}

for i := range tradeData {
// maker placed buy order, so taker market sell
side := order.Buy
if tradeData[i].IsBuyerMaker {
side = order.Sell
}
resp = append(resp, trade.Data{
TID: strconv.FormatInt(tradeData[i].ID, 10),
Exchange: b.Name,
CurrencyPair: p,
AssetType: a,
Price: tradeData[i].Price,
Amount: tradeData[i].Quantity,
Side: side,
Timestamp: tradeData[i].Time,
})
}
Expand All @@ -800,13 +806,19 @@ func (b *Binance) GetRecentTrades(ctx context.Context, p currency.Pair, a asset.
}

for i := range tradeData {
// maker placed buy order, so taker market sell
side := order.Buy
if tradeData[i].IsBuyerMaker {
side = order.Sell
}
resp = append(resp, trade.Data{
TID: strconv.FormatInt(tradeData[i].ID, 10),
Exchange: b.Name,
CurrencyPair: p,
AssetType: a,
Price: tradeData[i].Price,
Amount: tradeData[i].Qty,
Side: side,
Timestamp: tradeData[i].Time.Time(),
})
}
Expand All @@ -817,13 +829,19 @@ func (b *Binance) GetRecentTrades(ctx context.Context, p currency.Pair, a asset.
}

for i := range tradeData {
// maker placed buy order, so taker market sell
side := order.Buy
if tradeData[i].IsBuyerMaker {
side = order.Sell
}
resp = append(resp, trade.Data{
TID: strconv.FormatInt(tradeData[i].ID, 10),
Exchange: b.Name,
CurrencyPair: p,
AssetType: a,
Price: tradeData[i].Price,
Amount: tradeData[i].Qty,
Side: side,
Timestamp: tradeData[i].Time.Time(),
})
}
Expand Down Expand Up @@ -864,6 +882,11 @@ func (b *Binance) GetHistoricTrades(ctx context.Context, p currency.Pair, a asse
}
result := make([]trade.Data, len(trades))
for i := range trades {
// maker placed buy order, so taker market sell
side := order.Buy
if trades[i].Maker {
side = order.Sell
}
result[i] = trade.Data{
CurrencyPair: p,
TID: strconv.FormatInt(trades[i].ATradeID, 10),
Expand All @@ -872,7 +895,7 @@ func (b *Binance) GetHistoricTrades(ctx context.Context, p currency.Pair, a asse
Price: trades[i].Price,
Timestamp: trades[i].TimeStamp,
AssetType: a,
Side: order.AnySide,
Side: side,
}
}
return result, nil
Expand Down

0 comments on commit 902f580

Please sign in to comment.