Websocket connector for Blockchain.com Exchange (https://exchange.blockchain.com) written in Java.
Features
- Create market and limit orders
- Cancel orders
- Cancel all orders
- Get order status updates
- Subscribe to balances
- Subscribe to market data
- Subscribe to symbol reference data
- Subscribe to candlestick updates
- Subscribe to trades (trades across the exchange)
- Subscribe to ticker (24h volume, 24h price, last traded price)
- Subscribe to heartbeat (heartbeat messages)
To build the project
./gradlew clean build
This will create a jar under build/libs
that can be used in your project (I'm working on adding a dependency to Maven Central, so it's easier to start using it)
To run the example (com.blockchain.bxc.Example
)
./gradlew runExample
To fully run the example you'll have to generate your API key and have USD balance (more than $5) on the exchange. You can modify the example before running.
First create a client instance
final BcxClient bcxClient = new BcxClient();
Connect to the exchange with your API key (you can generate one on https://exchange.blockchain.com/settings/api)
bcxClient.connect(key);
This section describes all the available websocket channels.
Full description of each channel - https://exchange.blockchain.com/api/#websocket-api
You can connect to all channels that do not require extra parameters (heartbeat, trading, balances, symbol) by using the following command
bcxClient.subscribeAll();
If you'd like to receive all the updates from all the channels you can do it in the following ways
- By passing a handler when subscribing to all channels
bcxClient.subscribeAll(event -> System.out.println("event - " + event.toString()));
- By passing a handler to the
onEvent
methodbcxClient.subscribeAll(); bcxClient.onEvent(event -> System.out.println("event - " + event.toString()));
This will receive all the events of type Event
. If you'd like to receive more specific events you can use channel specific handlers described below.
Heartbeat channel is used for receiving Heartbeat
events from the exchange that help with identifying if the exchange is reachable. Heartbeat messages are sent every 5 seconds.
You can connect to the heartbeat channel using the following command
bcxClient.subscribeHeartbeat();
To receive Heartbeat
events you can
- Pass a handler when subscribing to the heartbeat channel
bcxClient.subscribeHeartbeat(heartbeat -> System.out.println("heartbeat - " + heartbeat.toString()));
- Use a separate
onHeartbeat
method after subscriptionbcxClient.subscribeHeartbeat() bcxClient.onHeartbeat(heartbeat -> System.out.println("heartbeat - " + heartbeat.toString()));
L2 channel is used for receiving L2
order book events from the exchange.
You can connect to the L2 channel using the following command
bcxClient.subscribeL2("BTC-USD");
It requires passing a symbol
parameter that represents a trading pair available on the exchange
To receive L2
events you can
- Pass a handler when subscribing to the L2 channel
bcxClient.subscribeL2("BTC-USD", l2 -> System.out.println("l2 - " + l2.toString()));
- Use a separate
onL2
method after subscriptionbcxClient.subscribeL2("BTC-USD"); bcxClient.onL2(l2 -> System.out.println("l2 - " + l2.toString()));
L3 channel is used for receiving L3
order book events from the exchange.
You can connect to the L3 channel using the following command
bcxClient.subscribeL3("BTC-USD");
To receive L3
events you can
- Pass a handler when subscribing to the L3 channel
bcxClient.subscribeL3("BTC-USD", l3 -> System.out.println("l3 - " + l3.toString()));
- Use a separate
onL3
method after subscriptionbcxClient.subscribeL3("BTC-USD"); bcxClient.onL3(l3 -> System.out.println("l3 - " + l3.toString()));
Prices channel is used for receiving candlestick market data events (PricesUpdate
) from the exchange.
You can connect to the prices channel using the following command
bcxClient.subscribePrices("BTC-USD", 60);
To receive PricesUpdate
events you can
- Pass a handler when subscribing to the prices channel
bcxClient.subscribePrices("BTC-USD", 60, pricesUpdate -> System.out.println("prices - " + pricesUpdate.toString()));
- Use a separate
onPrices
method after subscriptionbcxClient.subscribePrices("BTC-USD", 60); bcxClient.onPrices(pricesUpdate -> System.out.println("prices - " + pricesUpdate.toString()));
Symbols channel is used for receiving symbol updates (SymbolsSnapshot
) from the exchange.
You can connect to the symbols channel using the following command
bcxClient.subscribeSymbols();
To receive SymbolsSnapshot
you can pass a handler when subscribing to the symbols channel
bcxClient.subscribeSymbols(symbolsSnapshot -> System.out.println("symbol snapshot - " + symbolsSnapshot.getSymbols()));
Ticker channel is used for receiving ticker updates (TickerSnapshot
) from the exchange.
You can connect to the ticker channel using the following command
bcxClient.subscribeTicker("BTC-USD");
To receive TickerSnapshot
updates you can pass a handler when subscribing to the ticker channel
bcxClient.subscribeTicker("BTC-USD", tickerSnapshot -> System.out.println("ticker - " + tickerSnapshot.toString()));
Trades channel is used for receiving trade update events (TradesUpdate
) from the exchange.
You can connect to the trades channel using the following command
bcxClient.subscribeTrades("BTC-USD");
To receive TradesUpdate
events you can
- Pass a handler when subscribing to the trades channel
bcxClient.onTrades(tradesUpdate -> System.out.println("tradesUpdate - " + tradesUpdate.toString()));
- Use a separate
onTrades
method after subscriptionbcxClient.subscribeTrades("BTC-USD"); bcxClient.onTrades(tradesUpdate -> System.out.println("tradesUpdate - " + tradesUpdate.toString()));
Trading channel is used for sending new orders, cancelling existing orders and receiving trading updates from the exchange.
You can subscribe to the trading channel using the following command
bcxClient.subscribeTrading();
To receive all trading events you can
- Pass a handler when subscribing to the trading channel
bcxClient.subscribeTrading(tradingEvent -> System.out.println("tradingEvent - " + tradingEvent.toString()));
- Use a separate
onTrading
method after subscriptionbcxClient.subscribeTrading(); bcxClient.onTrading(tradingEvent -> System.out.println("tradingEvent - " + tradingEvent.toString()));
To receive only specific trading events you can
- Pass separate handlers when subscribing to the trading channel
bcxClient.subscribeTrading(tradingSnapshot -> { System.out.println("trading snapshot - " + tradingSnapshot.toString()); }, tradingUpdate -> { System.out.println("trading snapshot - " + tradingUpdate.toString()); }, tradingRejected -> { System.out.println("trading snapshot - " + tradingRejected.toString()); });
- Use separate methods after subscription
bcxClient.subscribeTrading(); bcxClient.onTradingSnapshot(tradingSnapshot -> System.out.println("tradingSnapshot - " + tradingSnapshot.toString())); bcxClient.onTradingUpdate(tradingUpdate -> System.out.println("tradingUpdate - " + tradingUpdate.toString())); bcxClient.onTradingReject(tradingRejected -> System.out.println("tradingRejected - " + tradingRejected.toString()));
You can create a new order and send it to the exchange in the following way
bcxClient.createOrder(
"bcx-api-1",
"BTC-GBP",
OrderType.LIMIT,
TimeInForce.GTC,
Side.BUY,
0.001,
5000,
true
);
You can cancel an existing order in the following way
bcxClient.cancelOrder("123");
Passed parameter is an order id that is received after placing a new order.
You can cancel all of your open orders in the following way
bcxClient.cancelAllOrders()
Balances channel is used for receiving balance snapshot events (BalanceSnapshot
) from the exchange.
You can connect to the balances channel using the following command
bcxClient.subscribeBalances();
To receive BalanceSnapshot
events you can
- Pass a handler when subscribing to the balances channel
bcxClient.subscribeBalances(balancesSnapshot -> System.out.println("balanceSnapshot - " + balancesSnapshot.toString()));
- Use a separate
onBalances
methodbcxClient.subscribeBalances(); bcxClient.onBalances(balancesSnapshot -> System.out.println("balanceSnapshot - " + balancesSnapshot.toString()));
- Optimise the library for speed (speed up Json parsing)