Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.
Closed
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.binance.api.client;

import com.binance.api.client.domain.event.AggTradeEvent;
import com.binance.api.client.domain.event.AllMarketTickersEvent;
import com.binance.api.client.domain.event.CandlestickEvent;
import com.binance.api.client.domain.event.DepthEvent;
import com.binance.api.client.domain.event.UserDataUpdateEvent;
import com.binance.api.client.domain.event.*;
import com.binance.api.client.domain.market.CandlestickInterval;

import java.io.Closeable;
Expand All @@ -15,6 +11,15 @@
*/
public interface BinanceApiWebSocketClient extends Closeable {

/**
* Open a new web socket to receive {@link BookTickerEvent bookTickerEvents} on a callback.
*
* @param symbols market (one or coma-separated) symbol(s) to subscribe to
* @param callback the callback to call on new events
* @return a {@link Closeable} that allows the underlying web socket to be closed.
*/
Closeable onBookTicker(String symbols, BinanceApiCallback<BookTickerEvent> callback);

/**
* Open a new web socket to receive {@link DepthEvent depthEvents} on a callback.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.binance.api.client.domain.event;

import com.binance.api.client.constant.BinanceApiConstants;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.math.BigDecimal;

@JsonIgnoreProperties(ignoreUnknown = true)
public class BookTickerEvent {

@JsonProperty("u")
private long updateId;

@JsonProperty("s")
private String symbol;

@JsonProperty("b")
private String bestBidPrice;

@JsonProperty("B")
private String bestBidQuantity;

@JsonProperty("a")
private String bestAskPrice;

@JsonProperty("A")
private String bestAskQuantity;

public long getUpdateId() {
return updateId;
}

public void setUpdateId(long updateId) {
this.updateId = updateId;
}

public String getSymbol() {
return symbol;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}

public String getBestBidPrice() {
return bestBidPrice;
}

public void setBestBidPrice(String bestBidPrice) {
this.bestBidPrice = bestBidPrice;
}

public String getBestBidQuantity() {
return bestBidQuantity;
}

public void setBestBidQuantity(String bestBidQuantity) {
this.bestBidQuantity = bestBidQuantity;
}

public String getBestAskPrice() {
return bestAskPrice;
}

public void setBestAskPrice(String bestAskPrice) {
this.bestAskPrice = bestAskPrice;
}

public String getBestAskQuantity() {
return bestAskQuantity;
}

public void setBestAskQuantity(String bestAskQuantity) {
this.bestAskQuantity = bestAskQuantity;
}

@Override
public String toString() {
return new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE)
.append("updateId", updateId)
.append("symbol", symbol)
.append("bestBidPrice", bestBidPrice)
.append("bestBidQuantity", bestBidQuantity)
.append("bestAskPrice", bestAskPrice)
.append("bestAskQuantity", bestAskQuantity)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import com.binance.api.client.BinanceApiWebSocketClient;
import com.binance.api.client.config.BinanceApiConfig;
import com.binance.api.client.constant.BinanceApiConstants;
import com.binance.api.client.domain.event.AggTradeEvent;
import com.binance.api.client.domain.event.AllMarketTickersEvent;
import com.binance.api.client.domain.event.CandlestickEvent;
import com.binance.api.client.domain.event.DepthEvent;
import com.binance.api.client.domain.event.UserDataUpdateEvent;
import com.binance.api.client.domain.event.*;
import com.binance.api.client.domain.market.CandlestickInterval;
import com.fasterxml.jackson.core.type.TypeReference;

Expand All @@ -32,6 +28,15 @@ public BinanceApiWebSocketClientImpl(OkHttpClient client) {
this.client = client;
}

@Override
public Closeable onBookTicker(String symbols, BinanceApiCallback<BookTickerEvent> callback) {
final String channel = Arrays.stream(symbols.split(","))
.map(String::trim)
.map(s -> String.format("%s@bookTicker", s))
.collect(Collectors.joining("/"));
return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, BookTickerEvent.class));
}

@Override
public Closeable onDepthEvent(String symbols, BinanceApiCallback<DepthEvent> callback) {
final String channel = Arrays.stream(symbols.split(","))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public static void main(String[] args) throws InterruptedException, IOException
// Listen for aggregated trade events for ETH/BTC
client.onAggTradeEvent("ethbtc", response -> System.out.println(response));

// Listen for book ticker events for ETH/BTC
client.onBookTicker("ethbtc", response -> System.out.println(response));

// Listen for changes in the order book in ETH/BTC
client.onDepthEvent("ethbtc", response -> System.out.println(response));

Expand Down