1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/** * Demonstrate requesting Ticker at Bitstamp. You can access both the raw data from Bitstamp or the XChange generic DTO data format. */ 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(); generic(marketDataService); raw((BitstampMarketDataServiceRaw) marketDataService); } private static void generic(MarketDataService marketDataService) throws IOException { Ticker ticker = marketDataService.getTicker(CurrencyPair.BTC_USD); System.out.println(ticker.toString()); } private static void raw(BitstampMarketDataServiceRaw marketDataService) throws IOException { BitstampTicker bitstampTicker = marketDataService.getBitstampTicker(CurrencyPair.BTC_USD); System.out.println(bitstampTicker.toString()); } } |
1 2 |
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] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
/** * Demonstrate requesting OrderBook from Bitstamp and plotting it using XChart. */ public class DepthChartDemo { public static void main(String[] args) throws IOException { // Use the factory to get the version 1 Bitstamp exchange API using default settings Exchange bitstampExchange = ExchangeFactory.INSTANCE.createExchange(BitstampExchange.class.getName()); // Interested in the public market data feed (no authentication) MarketDataService marketDataService = bitstampExchange.getMarketDataService(); System.out.println("fetching data..."); // Get the current orderbook OrderBook orderBook = marketDataService.getOrderBook(CurrencyPair.BTC_USD); System.out.println("received data."); System.out.println("plotting..."); // Create Chart XYChart chart = new XYChartBuilder().width(800).height(600).title("Bitstamp Order Book").xAxisTitle("BTC").yAxisTitle("USD").build(); // Customize Chart 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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/** * * Example showing the following: * * <ul> * <li>Connect to Bitstamp exchange with authentication</li> * <li>View account balance</li> * <li>Get the bitcoin deposit address</li> * <li>List unconfirmed deposits (raw interface only)</li> * <li>List recent withdrawals (raw interface only)</li> * <li>Withdraw a small amount of BTC</li> * </ul> */ public class BitstampAccountDemo { public static void main(String[] args) throws IOException { Exchange bitstamp = BitstampDemoUtils.createExchange(); AccountService accountService = bitstamp.getAccountService(); generic(accountService); raw((BitstampAccountServiceRaw) accountService); } private static void generic(AccountService accountService) throws IOException { // Get the account information AccountInfo accountInfo = accountService.getAccountInfo(); System.out.println("AccountInfo as String: " + accountInfo.toString()); String depositAddress = accountService.requestDepositAddress(Currency.BTC); System.out.println("Deposit address: " + depositAddress); String withdrawResult = accountService.withdrawFunds(Currency.BTC, new BigDecimal(1).movePointLeft(4), "XXX"); System.out.println("withdrawResult = " + withdrawResult); } 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
/** * * Example showing the following: * * <ul> * <li>Connect to Bitstamp exchange with authentication</li> * <li>Enter, review and cancel limit orders</li> * </ul> */ public class BitstampTradeDemo { public static void main(String[] args) throws IOException { Exchange bitstamp = BitstampDemoUtils.createExchange(); TradeService tradeService = bitstamp.getTradeService(); generic(tradeService); raw((BitstampTradeServiceRaw) tradeService); } private static void generic(TradeService tradeService) throws IOException { final OpenOrdersParamCurrencyPair openOrdersParamsBtcEur = (OpenOrdersParamCurrencyPair) tradeService.createOpenOrdersParams(); openOrdersParamsBtcEur.setCurrencyPair(CurrencyPair.BTC_EUR); final OpenOrdersParamCurrencyPair openOrdersParamsBtcUsd = (OpenOrdersParamCurrencyPair) tradeService.createOpenOrdersParams(); openOrdersParamsBtcUsd.setCurrencyPair(CurrencyPair.BTC_USD); final OpenOrdersParams openOrdersParamsAll = tradeService.createOpenOrdersParams(); printOpenOrders(tradeService, openOrdersParamsAll); // place a limit buy order LimitOrder limitOrder = new LimitOrder((OrderType.BID), new BigDecimal(".01"), CurrencyPair.BTC_EUR, null, null, new BigDecimal("500.00")); String limitOrderReturnValue = tradeService.placeLimitOrder(limitOrder); System.out.println("Limit Order return value: " + limitOrderReturnValue); printOpenOrders(tradeService, openOrdersParamsAll); printOpenOrders(tradeService, openOrdersParamsBtcEur); printOpenOrders(tradeService, openOrdersParamsBtcUsd); // Cancel the added order boolean cancelResult = tradeService.cancelOrder(limitOrderReturnValue); System.out.println("Canceling returned " + cancelResult); printOpenOrders(tradeService, openOrdersParamsAll); } private static void printOpenOrders(TradeService tradeService, OpenOrdersParams openOrdersParams) throws IOException { OpenOrders openOrders = tradeService.getOpenOrders(openOrdersParams); System.out.printf("Open Orders for %s: %s%n", openOrdersParams, openOrders); } private static void raw(BitstampTradeServiceRaw tradeService) throws IOException { printRawOpenOrders(tradeService); // place a limit buy order BitstampOrder order = tradeService.placeBitstampOrder(CurrencyPair.BTC_USD, Side.sell, new BigDecimal(".001"), new BigDecimal("1000.00")); System.out.println("BitstampOrder return value: " + order); printRawOpenOrders(tradeService); // Cancel the added order boolean cancelResult = tradeService.cancelBitstampOrder(order.getId()); System.out.println("Canceling returned " + cancelResult); printRawOpenOrders(tradeService); } private static void printRawOpenOrders(BitstampTradeServiceRaw tradeService) throws IOException { BitstampOrder[] openOrders = tradeService.getBitstampOpenOrders(CurrencyPair.BTC_USD); System.out.println("Open Orders: " + openOrders.length); for (BitstampOrder order : openOrders) { System.out.println(order.toString()); } } } |
1 2 3 4 5 6 7 8 9 10 11 |
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); } } |