public void start() throws ExecutionException, InterruptedException { // Use the default MtGox settings Exchange mtGoxExchange = ExchangeFactory.INSTANCE.createExchange(MtGoxExchange.class.getName()); // Configure BTC/USD ticker stream for MtGox ExchangeStreamingConfiguration btcusdConfiguration = new MtGoxStreamingConfiguration(10, 10000, Currencies.BTC, Currencies.USD, false); // Interested in the public streaming market data feed (no authentication) StreamingExchangeService btcusdStreamingMarketDataService = mtGoxExchange.getStreamingExchangeService(btcusdConfiguration); // Requesting initial order book using the polling service PollingMarketDataService marketDataService = mtGoxExchange.getPollingMarketDataService(); MarketDataRunnable.book = marketDataService.getPartialOrderBook(Currencies.BTC, Currencies.USD); // Open the connections to the exchange btcusdStreamingMarketDataService.connect(); ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<?> mtGoxMarketDataFuture = executorService.submit(new MarketDataRunnable(btcusdStreamingMarketDataService)); // the thread waits here until the Runnable is done. mtGoxMarketDataFuture.get(); executorService.shutdown(); // Disconnect and exit System.out.println(Thread.currentThread().getName() + ": Disconnecting..."); btcusdStreamingMarketDataService.disconnect(); }
@Override public void run() { try { while (true) { ExchangeEvent exchangeEvent = streamingExchangeService.getNextEvent(); if (exchangeEvent.getEventType() == ExchangeEventType.TICKER) { MarketDataRunnable.lastTicker = (Ticker) exchangeEvent.getPayload(); } else if (exchangeEvent.getEventType() == ExchangeEventType.TRADE) { Trade trade = (Trade) exchangeEvent.getPayload(); System.out.println(trade.toString()); } else if (exchangeEvent.getEventType() == ExchangeEventType.DEPTH) { OrderBookUpdate update = (OrderBookUpdate) exchangeEvent.getPayload(); if (update.getLimitOrder().getTransactionCurrency().equals("USD") && MarketDataRunnable.lastTicker != null) { MarketDataRunnable.book.update(update); if (MarketDataRunnable.book .getAsks() .get(0) .getLimitPrice() .compareTo(MarketDataRunnable.lastTicker.getAsk()) != 0) { System.out.println( "ERROR IN ORDERBOOK (ASKS) -> BOOK PRICE:" + MarketDataRunnable.book .getAsks() .get(0) .getLimitPrice() .getAmount() .doubleValue() + " TICKER PRICE:" + MarketDataRunnable.lastTicker.getAsk().getAmount().doubleValue()); } else { System.out.println( "ASKS BOOK OK!! " + MarketDataRunnable.lastTicker.getAsk().getAmount().doubleValue()); } if (MarketDataRunnable.book .getBids() .get(0) .getLimitPrice() .compareTo(MarketDataRunnable.lastTicker.getBid()) != 0) { System.out.println( "ERROR IN ORDERBOOK (BIDS) -> -> BOOK_PRICE:" + MarketDataRunnable.book .getBids() .get(0) .getLimitPrice() .getAmount() .doubleValue() + " TICKER_PRICE:" + MarketDataRunnable.lastTicker.getBid().getAmount().doubleValue()); } else { System.out.println( "BIDS BOOK OK!! " + MarketDataRunnable.lastTicker.getBid().getAmount().doubleValue()); } } } } } catch (InterruptedException e) { System.out.println("ERROR in Runnable!!!"); } }