Examples of StreamingExchangeService


Examples of com.xeiam.xchange.service.streaming.StreamingExchangeService

    // //Using default vars (commented out to prevent conflicts for this demo)
    // StreamingExchangeService streamingExchangeServiceDefault = ((CoinfloorExchange)coinfloorExchange).getStreamingExchangeService();

    // //Or with new vars:
    ExchangeStreamingConfiguration exchangeStreamingConfiguration = new CoinfloorStreamingConfiguration(10, 10000, 30000, false, true, true);
    StreamingExchangeService streamingExchangeService = coinfloorExchange.getStreamingExchangeService(exchangeStreamingConfiguration);

    // connect, and authenicate using username/cookie/password provided in exSpec
    streamingExchangeService.connect();

    // start handler for events
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<?> eventCatcherThread = executorService.submit(new MarketDataRunnable(streamingExchangeService, secondaryQueue));

    // request AccountInfo data (balances)
    ((CoinfloorStreamingExchangeService) streamingExchangeService).getBalances();
    TimeUnit.MILLISECONDS.sleep(1000);

    // request OpenOrders data
    ((CoinfloorStreamingExchangeService) streamingExchangeService).getOrders();
    TimeUnit.MILLISECONDS.sleep(1000);

    // request 30-day trading volume for this user
    ((CoinfloorStreamingExchangeService) streamingExchangeService).getTradeVolume("BTC");
    TimeUnit.MILLISECONDS.sleep(1000);

    // subscribe to ticker feed
    ((CoinfloorStreamingExchangeService) streamingExchangeService).watchTicker("BTC", "GBP");
    TimeUnit.MILLISECONDS.sleep(1000);

    // subscribe to orderbook
    ((CoinfloorStreamingExchangeService) streamingExchangeService).watchOrders("BTC", "GBP");
    TimeUnit.MILLISECONDS.sleep(1000);

    // send two orders, that will (partially) fulfill each other, to generate a trade.
    LimitOrder buyLimitOrder = new LimitOrder(OrderType.BID, new BigDecimal(1), new CurrencyPair("BTC", "GBP"), null, null, new BigDecimal(320));
    ((CoinfloorStreamingExchangeService) streamingExchangeService).placeOrder(buyLimitOrder);
    TimeUnit.MILLISECONDS.sleep(1000);

    LimitOrder sellLimitOrder = new LimitOrder(OrderType.ASK, new BigDecimal(1.52321512784), new CurrencyPair("BTC", "GBP"), null, null, new BigDecimal(319));
    ((CoinfloorStreamingExchangeService) streamingExchangeService).placeOrder(sellLimitOrder);
    TimeUnit.MILLISECONDS.sleep(1000);

    // then send another order, that will never be fulfilled
    LimitOrder bigLimitOrder = new LimitOrder(OrderType.ASK, new BigDecimal(1.152), new CurrencyPair("BTC", "GBP"), null, null, new BigDecimal(500));
    ((CoinfloorStreamingExchangeService) streamingExchangeService).placeOrder(bigLimitOrder);
    TimeUnit.MILLISECONDS.sleep(1000);

    // request outcome of theoretical marketOrder
    MarketOrder estMarketOrder = new MarketOrder(OrderType.ASK, new BigDecimal(1), new CurrencyPair("BTC", "GBP"));
    ((CoinfloorStreamingExchangeService) streamingExchangeService).estimateMarketOrder(estMarketOrder);
    TimeUnit.MILLISECONDS.sleep(1000);

    // get user's current open orders, cancel all of them.
    CoinfloorOpenOrders openOrders = (CoinfloorOpenOrders) ((CoinfloorStreamingExchangeService) streamingExchangeService).getOrders().getPayloadItem("raw");
    for (CoinfloorOrder order : openOrders.getOrders()) {
      ((CoinfloorStreamingExchangeService) streamingExchangeService).cancelOrder(order.getId());
      TimeUnit.MILLISECONDS.sleep(1000);
    }

    // unsubscribe to ticker feed
    ((CoinfloorStreamingExchangeService) streamingExchangeService).unwatchTicker("BTC", "GBP");
    TimeUnit.MILLISECONDS.sleep(1000);

    // unsubscribe to orderbook
    ((CoinfloorStreamingExchangeService) streamingExchangeService).unwatchOrders("BTC", "GBP");
    TimeUnit.MILLISECONDS.sleep(1000);

    TimeUnit.MINUTES.sleep(1);

    // These next three methods cache all the relevant information, and store them in memory. As new data comes in, it gets updated.
    // This way, the user is not required to update the accountInfo, orderbook, or trades history.
    // These methods are experimental, but still provided for convenience.

    System.out.println("\n\n\n\n\nCached Account Info: ");
    System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedAccountInfo());

    System.out.println("\n\n\n\n\nCached OrderBook: ");
    System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedOrderBook());

    System.out.println("\n\n\n\n\nCached Trades: ");
    System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedTrades());

    // the thread waits here until the Runnable is done.
    eventCatcherThread.get();

    executorService.shutdown();

    // Disconnect and exit
    System.out.println(Thread.currentThread().getName() + ": Disconnecting...");
    streamingExchangeService.disconnect();
    System.exit(0);
  }
View Full Code Here

Examples of com.xeiam.xchange.service.streaming.StreamingExchangeService

  public static void main(String[] args) throws InterruptedException {

    final BTCChinaStreamingConfiguration configuration = new BTCChinaStreamingConfiguration(true, true, true, true, CurrencyPair.BTC_CNY, CurrencyPair.LTC_CNY, CurrencyPair.LTC_BTC);
    final Exchange exchange = BTCChinaExamplesUtils.getExchange();
    final StreamingExchangeService service = exchange.getStreamingExchangeService(configuration);

    Thread consumer = new Thread("consumer") {

      @Override
      public void run() {

        while (!isInterrupted()) {
          try {
            ExchangeEvent event = service.getNextEvent();
            log.info("status: {}, type: {}, data: {}, payload: {}", service.getWebSocketStatus(), event.getEventType(), event.getData(), event.getPayload());
          } catch (InterruptedException e) {
            this.interrupt();
          }
        }
      }

    };

    // Start consumer.
    consumer.start();
    log.info("Consumer started.");

    // Start streaming service.
    service.connect();
    log.info("Streaming service started.");

    // Demonstrate for 30 seconds.
    TimeUnit.SECONDS.sleep(30);

    // Disconnect streaming service.
    service.disconnect();
    log.info("Streaming service disconnected.");

    // Interrupt consumer to exit.
    consumer.interrupt();
    log.info("Consumer interrupted.");
View Full Code Here

Examples of com.xeiam.xchange.service.streaming.StreamingExchangeService

    // //Using default vars (commented out to prevent conflicts for this demo)
    // StreamingExchangeService streamingExchangeServiceDefault = ((CoinfloorExchange)coinfloorExchange).getStreamingExchangeService();

    // //Or with new vars:
    ExchangeStreamingConfiguration exchangeStreamingConfiguration = new CoinfloorStreamingConfiguration(10, 10000, 30000, false, true, false);
    final StreamingExchangeService streamingExchangeService = coinfloorExchange.getStreamingExchangeService(exchangeStreamingConfiguration);

    // connect, and authenicate using username/cookie/password provided in exSpec
    streamingExchangeService.connect();
    Map<String, Object> resultMap;

    // subscribe to ticker feed
    resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).watchTicker("BTC", "GBP").getPayload();
    System.out.println("\n\n\n\n\nSubscribed to Ticker feed: ");
    System.out.println("Raw Object: " + resultMap.get("raw"));
    System.out.println("Generic Object: " + resultMap.get("generic"));
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    }

    // authenticate for rest of the demo
    ((CoinfloorStreamingExchangeService) streamingExchangeService).authenticate();

    // request AccountInfo data (balances)
    resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).getBalances().getPayload();
    System.out.println("\n\n\n\n\nUser balances returned: ");
    System.out.println("Raw Object: " + resultMap.get("raw"));
    System.out.println("Generic Object: " + resultMap.get("generic"));
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    }

    // request OpenOrders data
    resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).getOrders().getPayload();
    System.out.println("\n\n\n\n\nUser Open Orders: ");
    System.out.println("Raw Object: " + resultMap.get("raw"));
    System.out.println("Generic Object: " + resultMap.get("generic"));
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    }

    // request 30-day trading volume for this user
    resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).getTradeVolume("BTC").getPayload();
    System.out.println("\n\n\n\n\nUser 30-Day Trade Volume: ");
    System.out.println("Raw Object: " + resultMap.get("raw"));
    System.out.println("Generic Object: " + resultMap.get("generic"));
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    }

    // subscribe to orderbook
    resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).watchOrders("BTC", "GBP").getPayload();
    System.out.println("\n\n\n\n\nSubscribed to OrderBook feed: ");
    System.out.println("Raw Object: " + resultMap.get("raw"));
    System.out.println("Generic Object: " + resultMap.get("generic"));
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    }

    final long startTime = System.currentTimeMillis();
    ArrayList<Thread> threads = new ArrayList<Thread>();

    for (int i = 0; i < 5; i++) {
      threads.add(new Thread(new Runnable() {

        @Override
        public void run() {

          Map<String, Object> resultMap;

          try {
            TimeUnit.MILLISECONDS.sleep((startTime + 500) - System.currentTimeMillis());
          } catch (InterruptedException e) {
          }

          // send two orders, that will (partially) fulfill each other, to generate a trade.
          LimitOrder buyLimitOrder = new LimitOrder(OrderType.BID, new BigDecimal(1), new CurrencyPair("BTC", "GBP"), null, null, new BigDecimal(3.20));
          resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).placeOrder(buyLimitOrder).getPayload();
          System.out.println("\n\n\n\n\nBuy Limit Order Placed: ");
          System.out.println("Raw Object: " + resultMap.get("raw"));
          System.out.println("Generic Object: " + resultMap.get("generic"));
          try {
            TimeUnit.MILLISECONDS.sleep(1000);
          } catch (InterruptedException e) {
          }

          // check for new balance before submitting second order
          System.out.println("\n\n\n\n\nCached Account Info: ");
          System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedAccountInfo());

          LimitOrder sellLimitOrder = new LimitOrder(OrderType.ASK, new BigDecimal(1.52321512784), new CurrencyPair("BTC", "GBP"), null, null, new BigDecimal(3.19));
          resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).placeOrder(sellLimitOrder).getPayload();
          System.out.println("\n\n\n\n\nSell Limit Order Placed: ");
          System.out.println("Raw Object: " + resultMap.get("raw"));
          System.out.println("Generic Object: " + resultMap.get("generic"));
          try {
            TimeUnit.MILLISECONDS.sleep(1000);
          } catch (InterruptedException e) {
          }

          // check for new balance again
          System.out.println("\n\n\n\n\nCached Account Info: ");
          System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedAccountInfo());

          // then send another order, that will never be fulfilled
          LimitOrder bigLimitOrder = new LimitOrder(OrderType.ASK, new BigDecimal(1.152), new CurrencyPair("BTC", "GBP"), null, null, new BigDecimal(500));
          resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).placeOrder(bigLimitOrder).getPayload();
          System.out.println("\n\n\n\n\nBig Limit Order Placed: ");
          System.out.println("Raw Object: " + resultMap.get("raw"));
          System.out.println("Generic Object: " + resultMap.get("generic"));
          try {
            TimeUnit.MILLISECONDS.sleep(1000);
          } catch (InterruptedException e) {
          }

          // check for new balance before submitting second order
          System.out.println("\n\n\n\n\nCached Account Info: ");
          System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedAccountInfo());

          // request outcome of theoretical marketOrder
          MarketOrder estMarketOrder = new MarketOrder(OrderType.ASK, new BigDecimal(1), new CurrencyPair("BTC", "GBP"));
          ((CoinfloorStreamingExchangeService) streamingExchangeService).estimateMarketOrder(estMarketOrder).getPayload();
          System.out.println("\n\n\n\n\nEstimated Market Order: ");
          System.out.println("Raw Object: " + resultMap.get("raw"));
          System.out.println("Generic Object: " + resultMap.get("generic"));
          try {
            TimeUnit.MILLISECONDS.sleep(1000);
          } catch (InterruptedException e) {
          }

        }
      }));
      threads.get(threads.size() - 1).start();
    }

    for (Thread t : threads) {
      t.join();
    }

    // get user's current open orders, cancel all of them.
    CoinfloorOpenOrders openOrders = (CoinfloorOpenOrders) ((CoinfloorStreamingExchangeService) streamingExchangeService).getOrders().getPayloadItem("raw");
    for (CoinfloorOrder order : openOrders.getOrders()) {
      resultMap = ((CoinfloorStreamingExchangeService) streamingExchangeService).cancelOrder(order.getId()).getPayload();
      System.out.println("\n\n\n\n\nCancelled order: ");
      System.out.println("Raw Object: " + resultMap.get("raw"));
      System.out.println("Generic Object: " + resultMap.get("generic"));

    }
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    }

    // check for new balance after orders cancelled
    System.out.println("\n\n\n\n\nCached Account Info: ");
    System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedAccountInfo());

    // unsubscribe to ticker feed
    ((CoinfloorStreamingExchangeService) streamingExchangeService).unwatchTicker("BTC", "GBP").getPayload();
    System.out.println("\n\n\n\n\nUnwatched Ticker: ");
    System.out.println("Raw Object: " + resultMap.get("raw"));
    System.out.println("Generic Object: " + resultMap.get("generic"));
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    }

    // unsubscribe to orderbook
    ((CoinfloorStreamingExchangeService) streamingExchangeService).unwatchOrders("BTC", "GBP").getPayload();
    System.out.println("\n\n\n\n\nUnwatched Orders: ");
    System.out.println("Raw Object: " + resultMap.get("raw"));
    System.out.println("Generic Object: " + resultMap.get("generic"));
    try {
      TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    }

    TimeUnit.MINUTES.sleep(1);

    // These next three methods cache all the relevant information, and store them in memory. As new data comes in, it gets updated.
    // This way, the user is not required to update the accountInfo, orderbook, or trades history.
    // These methods are experimental, but still provided for convenience.

    System.out.println("\n\n\n\n\nCached Account Info: ");
    System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedAccountInfo());

    System.out.println("\n\n\n\n\nCached OrderBook: ");
    System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedOrderBook());

    System.out.println("\n\n\n\n\nCached Trades: ");
    System.out.println(((CoinfloorStreamingExchangeService) streamingExchangeService).getCachedTrades());

    // Disconnect and exit
    System.out.println(Thread.currentThread().getName() + ": Disconnecting...");
    streamingExchangeService.disconnect();
    System.exit(0);
  }
View Full Code Here

Examples of com.xeiam.xchange.service.streaming.StreamingExchangeService

    if (customerUuid != null) {
      configuration.addEvent("orders room", customerUuid);
    }

    final Exchange coinsetter = ExchangeFactory.INSTANCE.createExchange(CoinsetterExchange.class.getName());
    final StreamingExchangeService service = coinsetter.getStreamingExchangeService(configuration);

    final Thread consumer = new Thread("consumer") {

      @Override
      public void run() {

        while (!isInterrupted()) {
          try {
            ExchangeEvent event = service.getNextEvent();
            log.info("status: {}, type: {}, data: {}, payload: {}", service.getWebSocketStatus(), event.getEventType(), event.getData(), event.getPayload());
          } catch (InterruptedException e) {
            this.interrupt();
          }
        }
      }

    };

    // Start consumer.
    consumer.start();
    log.info("Consumer started.");

    // Start streaming service.
    service.connect();
    log.info("Streaming service started.");

    // Demonstrate for 30 seconds.
    TimeUnit.SECONDS.sleep(30);

    // Disconnect streaming service.
    service.disconnect();
    log.info("Streaming service disconnected.");

    // Interrupt consumer to exit.
    consumer.interrupt();
    log.info("Consumer interrupted.");
View Full Code Here

Examples of com.xeiam.xchange.service.streaming.StreamingExchangeService

    // Use the factory to get Bitstamp exchange API using default settings
    Exchange bitstamp = ExchangeFactory.INSTANCE.createExchange(BitstampExchange.class.getName());
    BitstampStreamingConfiguration streamCfg = new BitstampStreamingConfiguration();
    // Interested in the public streaming market data feed (no authentication)
    StreamingExchangeService streamService = bitstamp.getStreamingExchangeService(streamCfg);
    streamService.connect();
    generic(streamService);
    streamService.disconnect();
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.