use of info.bitrich.xchangestream.core.ProductSubscription in project XChange by knowm.
the class BinanceLiveSubscriptionExample method main.
public static void main(String[] args) throws InterruptedException {
ExchangeSpecification spec = StreamingExchangeFactory.INSTANCE.createExchange(BinanceStreamingExchange.class).getDefaultExchangeSpecification();
BinanceStreamingExchange exchange = (BinanceStreamingExchange) StreamingExchangeFactory.INSTANCE.createExchange(spec);
// First, we subscribe only for one currency pair at connection time (minimum requirement)
ProductSubscription subscription = ProductSubscription.create().addTrades(CurrencyPair.BTC_USDT).addOrderbook(CurrencyPair.BTC_USDT).build();
// Note: at connection time, the live subscription is disabled
exchange.connect(subscription).blockingAwait();
// Note: See the doOnDispose below. It's here that we will send an unsubscribe request to
// Binance through the websocket instance.
Disposable tradesBtc = exchange.getStreamingMarketDataService().getTrades(CurrencyPair.BTC_USDT).doOnDispose(() -> exchange.getStreamingMarketDataService().unsubscribe(CurrencyPair.BTC_USDT, BinanceSubscriptionType.TRADE)).subscribe(trade -> {
LOG.info("Trade: {}", trade);
});
Disposable orderBooksBtc = exchange.getStreamingMarketDataService().getOrderBook(CurrencyPair.BTC_USDT).doOnDispose(() -> exchange.getStreamingMarketDataService().unsubscribe(CurrencyPair.BTC_USDT, BinanceSubscriptionType.DEPTH)).subscribe(orderBook -> {
LOG.info("Order book: {}", orderBook);
});
Thread.sleep(5000);
// Now we enable the live subscription/unsubscription to add new currencies to the streams
LOG.info("Enable live subscription/unsubscription");
exchange.enableLiveSubscription();
// We subscribe to 3 new currency pairs for trade (live subscription)
// IMPORTANT!! Binance has a websocket limit of 5 incoming messages per second. If you bypass
// this limit, the websocket will be disconnected.
// (See
// https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#websocket-limits for more details)
// If you plan to subscribe/unsubscribe more than 5 currency pairs at a time, use a rate limiter
// or keep the live subscription
// feature disabled and connect your pairs at connection time only (default value).
final List<CurrencyPair> currencyPairs = Arrays.asList(CurrencyPair.ETH_USDT, CurrencyPair.LTC_USDT, CurrencyPair.XRP_USDT);
final List<Disposable> disposableTrades = new ArrayList<>();
for (final CurrencyPair currencyPair : currencyPairs) {
// Note: See the doOnDispose below. It's here that we will send an unsubscribe request to
// Binance through the websocket instance.
Disposable tradeDisposable = exchange.getStreamingMarketDataService().getTrades(currencyPair).doOnDispose(() -> exchange.getStreamingMarketDataService().unsubscribe(currencyPair, BinanceSubscriptionType.TRADE)).subscribe(trade -> {
LOG.info("Trade: {}", trade);
});
disposableTrades.add(tradeDisposable);
}
Thread.sleep(5000);
// Now we unsubscribe BTC/USDT from the stream (TRADE and DEPTH) and also the another currency
// pairs (TRADE 3x)
// Note: we are ok with live unsubscription because we not bypass the limit of 5 messages per
// second.
tradesBtc.dispose();
orderBooksBtc.dispose();
disposableTrades.forEach(Disposable::dispose);
LOG.info("Now all symbols are live unsubscribed (BTC, ETH, LTC & XRP). We will live subscribe to XML/USDT and EOS/BTC...");
Thread.sleep(5000);
Disposable xlmDisposable = exchange.getStreamingMarketDataService().getTrades(CurrencyPair.XLM_USDT).doOnDispose(() -> exchange.getStreamingMarketDataService().unsubscribe(CurrencyPair.XLM_USDT, BinanceSubscriptionType.TRADE)).subscribe(trade -> {
});
Disposable eosDisposable = exchange.getStreamingMarketDataService().getTrades(CurrencyPair.EOS_BTC).doOnDispose(() -> exchange.getStreamingMarketDataService().unsubscribe(CurrencyPair.EOS_BTC, BinanceSubscriptionType.TRADE)).subscribe(trade -> {
});
Thread.sleep(5000);
LOG.info("Test finished, we unsubscribe XML/USDT and EOS/BTC from the streams.");
xlmDisposable.dispose();
eosDisposable.dispose();
exchange.disconnect().blockingAwait();
}
use of info.bitrich.xchangestream.core.ProductSubscription in project XChange by knowm.
the class BinanceOrderbookHighVolumeExample method main.
public static void main(String[] args) throws InterruptedException {
final ExchangeSpecification exchangeSpecification = new ExchangeSpecification(BinanceStreamingExchange.class);
exchangeSpecification.setShouldLoadRemoteMetaData(true);
StreamingExchange exchange = StreamingExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
ProductSubscription subscription = exchange.getExchangeSymbols().stream().limit(50).reduce(ProductSubscription.create(), ProductSubscription.ProductSubscriptionBuilder::addOrderbook, (productSubscriptionBuilder, productSubscriptionBuilder2) -> {
throw new UnsupportedOperationException();
}).build();
exchange.connect(subscription).blockingAwait();
Thread.sleep(Long.MAX_VALUE);
}
use of info.bitrich.xchangestream.core.ProductSubscription in project XChange by knowm.
the class GateioManualExample method main.
public static void main(String[] args) throws Exception {
ProductSubscription productSubscription = ProductSubscription.create().addOrderbook(CurrencyPair.ETH_USDT).addOrderbook(CurrencyPair.BTC_USDT).addTrades(CurrencyPair.ETH_USDT).build();
ExchangeSpecification spec = StreamingExchangeFactory.INSTANCE.createExchangeWithoutSpecification(GateioStreamingExchange.class).getDefaultExchangeSpecification();
spec.setShouldLoadRemoteMetaData(false);
GateioStreamingExchange exchange = (GateioStreamingExchange) StreamingExchangeFactory.INSTANCE.createExchange(spec);
exchange.connect(productSubscription).blockingAwait();
Disposable sub1 = exchange.getStreamingMarketDataService().getOrderBook(CurrencyPair.ETH_USDT).subscribe(orderBook -> {
LOG.info("First ask: {}", orderBook.getAsks().get(0));
LOG.info("First bid: {}", orderBook.getBids().get(0));
}, throwable -> LOG.error("ERROR in getting order book: ", throwable));
Disposable sub2 = exchange.getStreamingMarketDataService().getOrderBook(CurrencyPair.BTC_USDT).subscribe(orderBook -> {
LOG.info("First ask: {}", orderBook.getAsks().get(0));
LOG.info("First bid: {}", orderBook.getBids().get(0));
}, throwable -> LOG.error("ERROR in getting order book: ", throwable));
Disposable sub3 = exchange.getStreamingMarketDataService().getTrades(CurrencyPair.BTC_USDT).subscribe(trade -> {
LOG.info("Trade Price: {}", trade.getPrice());
LOG.info("Trade Amount: {}", trade.getOriginalAmount());
}, throwable -> LOG.error("ERROR in getting trade: ", throwable));
Thread.sleep(1000);
sub1.dispose();
sub2.dispose();
sub3.dispose();
}
use of info.bitrich.xchangestream.core.ProductSubscription in project XChange by knowm.
the class GeminiWebSocketSubscriptionMessage method constructSubscriptions.
private Subscription[] constructSubscriptions(ProductSubscription productSubscription) {
if (productSubscription == null)
return null;
List<Subscription> subscriptions = new ArrayList<>();
List<String> L2Symbols = new ArrayList<>();
for (CurrencyPair currencyPair : productSubscription.getOrderBook()) {
L2Symbols.add(currencyPair.base.toString() + currencyPair.counter.toString());
}
subscriptions.add(new Subscription(L2, L2Symbols.stream().toArray(String[]::new)));
return subscriptions.stream().toArray(Subscription[]::new);
}
use of info.bitrich.xchangestream.core.ProductSubscription in project XChange by knowm.
the class BinanceStreamingExchange method connect.
/**
* Binance streaming API expects connections to multiple channels to be defined at connection
* time. To define the channels for this connection pass a `ProductSubscription` in at connection
* time.
*
* @param args A single `ProductSubscription` to define the subscriptions required to be available
* during this connection.
* @return A completable which fulfils once connection is complete.
*/
@Override
public Completable connect(ProductSubscription... args) {
if (args == null || args.length == 0) {
throw new IllegalArgumentException("Subscriptions must be made at connection time");
}
if (streamingService != null) {
throw new UnsupportedOperationException("Exchange only handles a single connection - disconnect the current connection.");
}
ProductSubscription subscriptions = args[0];
streamingService = createStreamingService(subscriptions);
List<Completable> completables = new ArrayList<>();
if (subscriptions.hasUnauthenticated()) {
completables.add(streamingService.connect());
}
if (subscriptions.hasAuthenticated()) {
if (exchangeSpecification.getApiKey() == null) {
throw new IllegalArgumentException("API key required for authenticated streams");
}
LOG.info("Connecting to authenticated web socket");
BinanceAuthenticated binance = ExchangeRestProxyBuilder.forInterface(BinanceAuthenticated.class, getExchangeSpecification()).build();
userDataChannel = new BinanceUserDataChannel(binance, exchangeSpecification.getApiKey(), onApiCall);
try {
completables.add(createAndConnectUserDataService(userDataChannel.getListenKey()));
} catch (NoActiveChannelException e) {
throw new IllegalStateException("Failed to establish user data channel", e);
}
}
streamingMarketDataService = new BinanceStreamingMarketDataService(streamingService, (BinanceMarketDataService) marketDataService, onApiCall, orderBookUpdateFrequencyParameter, realtimeOrderBookTicker, oderBookFetchLimitParameter);
streamingAccountService = new BinanceStreamingAccountService(userDataStreamingService);
streamingTradeService = new BinanceStreamingTradeService(userDataStreamingService);
return Completable.concat(completables).doOnComplete(() -> streamingMarketDataService.openSubscriptions(subscriptions)).doOnComplete(() -> streamingAccountService.openSubscriptions()).doOnComplete(() -> streamingTradeService.openSubscriptions());
}
Aggregations