Writing a Bitcoin Liquidity Aggregator with CoralMD

In this article we explore how we used CoralMD to write a liquidity aggregator for the Bitcoin market. Most importantly, we show how you can distribute market data to internal applications and to external clients using straightforward and efficient APIs over standard protocols. CoralMD supports three market data APIs on top of HTTP, TCP and FIX as you will see below.

Aggregated Book

When you write and run multiple market data feeds, CoralMD automatically and transparently aggregates the quotes from all feeds inside the security market data book. See the example below:

Security sec = Security.get("BTC-USD");

NioReactor nio = NioReactor.create();

Feed coinbase = CoinbaseFeed.create(nio, 1);
Feed okcoin = OkcoinFeed.create(nio, 2);
Feed lakebtc = LakeBtcFeed.create(nio, 3);
Feed coinsetter = CoinsetterFeed.create(nio, 4);

coinbase.subscribe(sec);
okcoin.subscribe(sec);
lakebtc.subscribe(sec);
coinsetter.subscribe(sec);

coinbase.open();
okcoin.open();
lakebtc.open();
coinsetter.open();

nio.start();


Now you have access to the following market data books:

Security sec = Security.get("BTC-USD");

Book aggregated = Book.get(sec); // aggregated book with quotes from all feeds

Book coinbase = Book.get(sec, CoinbaseFeed.EXCHANGE_ID); // book with quotes coming only from Coinbase exchange

Book okcoin = Book.get(sec, OkcoinFeed.EXCHANGE_ID); // book with quotes coming only from Okcoin exchange


You can also display the market data book using the com.coralblocks.coralmd.BookViewer class, as the example below shows:

Security sec = Security.get("BTC-USD");
Book aggregated = Book.get(sec);

BookViewer bookViewer = new BookViewer(nio, aggregated);
bookViewer.start();



In your terminal you see:
aggregated

Market Data over HTTP

CoralMD comes with a MarketDataHttpServer that you can use to serve your market data to http clients. Below how you launch the server:

MapConfiguration config = new MapConfiguration();
config.add("priceDecimals", 2);

Server httpServer = new MarketDataHttpServer(nio, 45453, config);
httpServer.open();


Market Data over TCP

For a fast, clean, efficient and simple binary protocol, you can use MarketDataTcpServer. Click here to check the message specification and to learn how to write your own TCP client that can get access to live Bitcoin market data from our servers. Below how you would launch a TCP server with CoralMD:

Server tcpServer = new MarketDataTcpServer(nio, 45454);
tcpServer.open();


Market Data over FIX

To use the standard FIX protocol, you can launch CoralMD’s MarketDataFixServer. Click here to check the FIX message specification and to learn how to write your own FIX client that can get access to live Bitcoin market data from our FIX servers. Below how you would launch the FIX server with CoralMD:

Server fixServer = new MarketDataFixServer(nio, 45455);
fixServer.open();


Conclusion

CoralMD makes it very easy to aggregate market data from multiple sources (i.e. feeds) and to serve them internally or externally through simple and efficient HTTP, TCP and FIX APIs.