// //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);
}