Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #264 from jmresendiz/feat/onBookTickerEvent
Browse files Browse the repository at this point in the history
Add WebSocket BookTickerEvent
  • Loading branch information
joaopsilva authored Oct 4, 2020
2 parents c916c2c + 2b6eaf1 commit 8d38e8e
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 10 deletions.
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 Down Expand Up @@ -60,6 +56,15 @@ public interface BinanceApiWebSocketClient extends Closeable {
*/
Closeable onAllMarketTickersEvent(BinanceApiCallback<List<AllMarketTickersEvent>> callback);

/**
* 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 onBookTickerEvent(String symbols, BinanceApiCallback<BookTickerEvent> callback);

/**
* @deprecated This method is no longer functional. Please use the returned {@link Closeable} from any of the other methods to close the web socket.
*/
Expand Down
113 changes: 113 additions & 0 deletions src/main/java/com/binance/api/client/domain/event/BookTickerEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.binance.api.client.domain.event;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.binance.api.client.constant.BinanceApiConstants;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* BookTickerEvent event for a symbol. Pushes any update to the best bid or
* ask's price or quantity in real-time for a specified symbol.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class BookTickerEvent {

@JsonProperty("u")
private long updateId;

@JsonProperty("s")
private String symbol;

@JsonProperty("b")
private String bidPrice;

@JsonProperty("B")
private String bidQuantity;

@JsonProperty("a")
private String askPrice;

@JsonProperty("A")
private String askQuantity;

public BookTickerEvent() {
super();
}

public BookTickerEvent(long updateId, String symbol, String bidPrice, String bidQuantity, String askPrice,
String askQuantity) {
super();
this.updateId = updateId;
this.symbol = symbol;
this.bidPrice = bidPrice;
this.bidQuantity = bidQuantity;
this.askPrice = askPrice;
this.askQuantity = askQuantity;
}

public BookTickerEvent(String symbol, String bidPrice, String bidQuantity, String askPrice, String askQuantity) {
super();
this.symbol = symbol;
this.bidPrice = bidPrice;
this.bidQuantity = bidQuantity;
this.askPrice = askPrice;
this.askQuantity = askQuantity;
}

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 getBidPrice() {
return bidPrice;
}

public void setBidPrice(String bidPrice) {
this.bidPrice = bidPrice;
}

public String getBidQuantity() {
return bidQuantity;
}

public void setBidQuantity(String bidQuantity) {
this.bidQuantity = bidQuantity;
}

public String getAskPrice() {
return askPrice;
}

public void setAskPrice(String askPrice) {
this.askPrice = askPrice;
}

public String getAskQuantity() {
return askQuantity;
}

public void setAskQuantity(String askQuantity) {
this.askQuantity = askQuantity;
}

@Override
public String toString() {
return new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE).append("eventType", "BookTicker")
.append("updateId", updateId).append("symbol", symbol).append("bidPrice", bidPrice)
.append("bidQuantity", bidQuantity).append("askPrice", askPrice).append("askQuantity", askQuantity)
.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 Down Expand Up @@ -67,6 +63,15 @@ public Closeable onAllMarketTickersEvent(BinanceApiCallback<List<AllMarketTicker
return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, new TypeReference<List<AllMarketTickersEvent>>() {}));
}

@Override
public Closeable onBookTickerEvent(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));
}

/**
* @deprecated This method is no longer functional. Please use the returned {@link Closeable} from any of the other methods to close the web socket.
*/
Expand Down

0 comments on commit 8d38e8e

Please sign in to comment.