Posted By : Shubham
XChange is a library that provides a simple and consistent API for interacting with a diverse set of cryptocurrency exchanges. Its basic usage is simple: create an Exchange instance, get the appropriate service, and request data. This library can be used to get ticker data of the coins, as well as plot the values in the graph. Most of the popular exchanges like Binance, Kraken, Poloneix have their API implementation, which can be used to fetch data from them. But, if there is a requirement to integrate a large number of crypto exchanges then it would not be feasible or optimal to have separate implementation for each exchange platform. This library acts like a wrapper and interacts with most of the popular exchanges.
A few of the APIs are public APIs for getting ticker data like OHLC (open, high, low, close ) of any altcoin that can be directly consumed. But to place an order on an exchange or get account related info, an API key and secret key must be set in exchange object.
How to set the API key and Secret Key
public class BitstampDemoUtils { public static Exchange createExchange() { ExchangeSpecification exSpec = new BitstampExchange().getDefaultExchangeSpecification(); exSpec.setUserName("34387"); exSpec.setApiKey("a4SDmpl9s6xWJS5fkKRT6yn41vXuY0AM"); exSpec.setSecretKey("sisJixU6Xd0d1yr6w02EHCb9UwYzTNuj"); return ExchangeFactory.INSTANCE.createExchange(exSpec); } }
Operations allowed with authenticated Exchange Object
After setting the API key and Secret key in exchange object, you need to perform the following operations.
public class BitstampAccountDemo { public static void main(String[] args) throws IOException { Exchange bitstamp = BitstampDemoUtils.createExchange(); AccountService accountService = bitstamp.getAccountService(); raw((BitstampAccountServiceRaw) accountService); } private static void raw(BitstampAccountServiceRaw accountService) throws IOException { // Get the account information BitstampBalance bitstampBalance = accountService.getBitstampBalance(); System.out.println("BitstampBalance: " + bitstampBalance); BitstampDepositAddress depositAddress = accountService.getBitstampBitcoinDepositAddress(); System.out.println("BitstampDepositAddress address: " + depositAddress); final List<DepositTransaction> unconfirmedDeposits = accountService.getUnconfirmedDeposits(); System.out.println("Unconfirmed deposits:"); for (DepositTransaction unconfirmedDeposit : unconfirmedDeposits) { System.out.println(unconfirmedDeposit); } final List<WithdrawalRequest> withdrawalRequests = accountService.getWithdrawalRequests(); System.out.println("Withdrawal requests:"); for (WithdrawalRequest unconfirmedDeposit : withdrawalRequests) { System.out.println(unconfirmedDeposit); } BitstampWithdrawal withdrawResult = accountService.withdrawBitstampFunds(Currency.BTC, new BigDecimal(1).movePointLeft(4), "XXX", null); System.out.println("BitstampBooleanResponse = " + withdrawResult); } }
Get Bitcoin Ticker Data from Bitstamp
The ticker data represents the OHLC levels i.e open, high, low, and close of a particular cryptocurrency. Some exchanges like Binance also support 24-hour ticker data. So, before fetching 24 hr ticker data one must check whether it is supported by that particular exchange or not.
public class BitstampTickerDemo { public static void main(String[] args) throws IOException { // Use the factory to get Bitstamp exchange API using default settings Exchange bitstamp = ExchangeFactory.INSTANCE.createExchange(BitstampExchange.class.getName()); // Interested in the public market data feed (no authentication) MarketDataService marketDataService = bitstamp.getMarketDataService(); raw((BitstampMarketDataServiceRaw) marketDataService); } private static void raw(BitstampMarketDataServiceRaw marketDataService) throws IOException { BitstampTicker bitstampTicker = marketDataService.getBitstampTicker(CurrencyPair.BTC_USD); System.out.println(bitstampTicker.toString()); } }
Result
Ticker [currencyPair=BTC/USD, last=227.14, bid=226.29, ask=226.68, high=232.75, low=225.04,avg=228.22, volume=17223.97619435, timestamp=1440963564000] BitstampTicker [last=227.14, high=232.75, low=225.04, vwap=228.22, volume=17223.97619435, bid=226.29, ask=226.68, timestamp=1440963564]
The price levels of altcoins on any exchange can be plotted on the graph easily using the Xchange library. Below is an example to demonstrate requesting OrderBook from Bitstamp and plotting it using a chart.
// Create Chart XYChart chart = new XYChartBuilder().width(800).height(600).title("Bitstamp Order Book").xAxisTitle("BTC").yAxisTitle("USD").build(); chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area); // BIDS List<Number> xData = new ArrayList<>(); List<Number> yData = new ArrayList<>(); BigDecimal accumulatedBidUnits = new BigDecimal("0"); for (LimitOrder limitOrder : orderBook.getBids()) { if (limitOrder.getLimitPrice().doubleValue() > 10) { xData.add(limitOrder.getLimitPrice()); accumulatedBidUnits = accumulatedBidUnits.add(limitOrder.getTradableAmount()); yData.add(accumulatedBidUnits); } } Collections.reverse(xData); Collections.reverse(yData); // Bids Series XYSeries series = chart.addSeries("bids", xData, yData); series.setMarker(SeriesMarkers.NONE); // ASKS xData = new ArrayList<>(); yData = new ArrayList<>(); BigDecimal accumulatedAskUnits = new BigDecimal("0"); for (LimitOrder limitOrder : orderBook.getAsks()) { if (limitOrder.getLimitPrice().doubleValue() < 1000) { xData.add(limitOrder.getLimitPrice()); accumulatedAskUnits = accumulatedAskUnits.add(limitOrder.getTradableAmount()); yData.add(accumulatedAskUnits); } } // Asks Series series = chart.addSeries("asks", xData, yData); series.setMarker(SeriesMarkers.NONE); new SwingWrapper(chart).displayChart();
September 8, 2024 at 02:25 am
Your comment is awaiting moderation.