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

[Binance] Support rolling window #4970

Closed
wants to merge 1 commit into from
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
Expand Up @@ -71,6 +71,7 @@ public class BinanceStreamingMarketDataService implements StreamingMarketDataSer
LoggerFactory.getLogger(BinanceStreamingMarketDataService.class);

private static final JavaType TICKER_TYPE = getTickerType();
private static final JavaType WINDOW_TICKER_TYPE = getWindowTickerType();
private static final JavaType BOOK_TICKER_TYPE = getBookTickerType();
private static final JavaType TRADE_TYPE = getTradeType();
private static final JavaType DEPTH_TYPE = getDepthType();
Expand All @@ -84,13 +85,16 @@ public class BinanceStreamingMarketDataService implements StreamingMarketDataSer
private final int oderBookFetchLimitParameter;

private final Map<Instrument, Observable<BinanceTicker24h>> tickerSubscriptions;
private final Map<Instrument, Observable<BinanceTicker24h>> rollingWindowTickerSubscriptions;
private final Map<Instrument, Observable<BinanceBookTicker>> bookTickerSubscriptions;
private final Map<Instrument, Observable<OrderBook>> orderbookSubscriptions;
private final Map<Instrument, Observable<BinanceRawTrade>> tradeSubscriptions;
private final Map<Instrument, Observable<List<OrderBookUpdate>>> orderBookUpdatesSubscriptions;
private final Map<Instrument, Map<KlineInterval, Observable<BinanceKline>>> klineSubscriptions;
private final Map<Instrument, Observable<DepthBinanceWebSocketTransaction>>
orderBookRawUpdatesSubscriptions;
private Observable<List<BinanceTicker24h>> allRollingWindowTickerSubscriptions;


/**
* A scheduler for initialisation of binance order book snapshots, which is delegated to a
Expand Down Expand Up @@ -125,6 +129,7 @@ public BinanceStreamingMarketDataService(
this.marketDataService = marketDataService;
this.onApiCall = onApiCall;
this.tickerSubscriptions = new ConcurrentHashMap<>();
this.rollingWindowTickerSubscriptions = new ConcurrentHashMap<>();
this.bookTickerSubscriptions = new ConcurrentHashMap<>();
this.orderbookSubscriptions = new ConcurrentHashMap<>();
this.tradeSubscriptions = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -211,6 +216,33 @@ public Observable<BinanceTicker24h> getRawTicker(Instrument instrument) {
instrument, s -> triggerObservableBody(rawTickerStream(instrument)).share());
}

public Observable<BinanceTicker24h> rollingWindow(Instrument instrument, KlineInterval windowSize) {
if (!service.isLiveSubscriptionEnabled()
&& !service.getProductSubscription().getTicker().contains(instrument)) {
throw new UpFrontSubscriptionRequiredException();
}
if(windowSize.equals(KlineInterval.h1) || windowSize.equals(KlineInterval.h4) || windowSize.equals(KlineInterval.d1)) {
return rollingWindowTickerSubscriptions.computeIfAbsent(
instrument, s -> triggerObservableBody(rollingWindowStream(instrument, windowSize)).share());
}else {
throw new UnsupportedOperationException("RollingWindow not supported for other window size!");
}
}

public Observable<List<BinanceTicker24h>> allRollingWindow(KlineInterval windowSize) {
if (!service.isLiveSubscriptionEnabled()) {
throw new UpFrontSubscriptionRequiredException();
}
if(windowSize.equals(KlineInterval.h1) || windowSize.equals(KlineInterval.h4) || windowSize.equals(KlineInterval.d1)) {
Observable<List<BinanceTicker24h>> observable = triggerObservableBody(
allRollingWindowStream(windowSize)).share();
allRollingWindowTickerSubscriptions = observable;
return observable;
}else {
throw new UnsupportedOperationException("RollingWindow not supported for other window size!");
}
}

public Observable<BinanceBookTicker> getRawBookTicker(Instrument instrument) {
if (!service.isLiveSubscriptionEnabled()
&& !service.getProductSubscription().getTicker().contains(instrument)) {
Expand Down Expand Up @@ -396,6 +428,13 @@ private void unsubscribe(
case TICKER:
tickerSubscriptions.remove(instrument);
break;
case TICKER_WINDOW:
if(null == instrument){
allRollingWindowTickerSubscriptions = null;
}else {
rollingWindowTickerSubscriptions.remove(instrument);
}
break;
case BOOK_TICKER:
bookTickerSubscriptions.remove(instrument);
break;
Expand Down Expand Up @@ -455,6 +494,27 @@ private Observable<BinanceTicker24h> rawTickerStream(Instrument instrument) {
.map(transaction -> transaction.getData().getTicker());
}

private Observable<BinanceTicker24h> rollingWindowStream(Instrument instrument, KlineInterval windowSize) {
return this.service
.subscribeChannel(this.getChannelPrefix(instrument) + "@" + BinanceSubscriptionType.TICKER_WINDOW.getType() + windowSize.code(), new Object[0])
.map(
(it) -> this.<TickerBinanceWebsocketTransaction>readTransaction(it, TICKER_TYPE, "ticker"))
.filter(
transaction ->
BinanceAdapters.adaptSymbol(
transaction.getData().getSymbol(), instrument instanceof FuturesContract)
.equals(instrument))
.map(transaction -> transaction.getData().getTicker());
}

private Observable<List<BinanceTicker24h>> allRollingWindowStream(KlineInterval windowSize) {
return this.service
.subscribeChannel("!" + BinanceSubscriptionType.TICKER_WINDOW.getType() + windowSize.code() + "@arr", new Object[0])
.map(
(it) -> this.<List<TickerBinanceWebsocketTransaction>>readTransaction(it, WINDOW_TICKER_TYPE , "ticker"))
.map( transaction -> transaction.getData().stream().map(TickerBinanceWebsocketTransaction::getTicker).collect(Collectors.toList()));
}

private Observable<BinanceBookTicker> rawBookTickerStream(Instrument instrument) {
return service
.subscribeChannel(
Expand Down Expand Up @@ -694,6 +754,13 @@ private static JavaType getTickerType() {
new TypeReference<BinanceWebsocketTransaction<TickerBinanceWebsocketTransaction>>() {});
}

private static JavaType getWindowTickerType() {
return getObjectMapper()
.getTypeFactory()
.constructType(
new TypeReference<BinanceWebsocketTransaction<List<TickerBinanceWebsocketTransaction>>>() {});
}

private static JavaType getBookTickerType() {
return getObjectMapper()
.getTypeFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum BinanceSubscriptionType {
FUNDING_RATES("markPrice"),
TRADE("trade"),
TICKER("ticker"),
TICKER_WINDOW("ticker_"),
BOOK_TICKER("bookTicker"),
KLINE("kline");

Expand Down