(String[] args)
| 15 | public class CompareExchanges { |
| 16 | |
| 17 | @SuppressWarnings("unchecked") |
| 18 | public static void main(String[] args) { |
| 19 | String symbol = args.length > 0 ? args[0] : "BTC/USDT"; |
| 20 | String[] exchangeIds = {"binance", "bybit", "okx", "kraken", "bitget"}; |
| 21 | |
| 22 | System.out.println("Comparing " + symbol + " across exchanges\n"); |
| 23 | |
| 24 | System.out.printf("%-12s %12s %12s %12s %10s%n", |
| 25 | "Exchange", "Last", "Bid", "Ask", "Spread"); |
| 26 | System.out.println("-".repeat(60)); |
| 27 | |
| 28 | for (String id : exchangeIds) { |
| 29 | try { |
| 30 | Exchange exchange = Exchange.dynamicallyCreateInstance(id, null); |
| 31 | exchange.loadMarkets(false).join(); |
| 32 | |
| 33 | // Untyped: fetchTicker returns CompletableFuture<Object> |
| 34 | Map<String, Object> ticker = (Map<String, Object>) exchange.fetchTicker(symbol).join(); |
| 35 | |
| 36 | Double last = toDouble(ticker.get("last")); |
| 37 | Double bid = toDouble(ticker.get("bid")); |
| 38 | Double ask = toDouble(ticker.get("ask")); |
| 39 | |
| 40 | double spread = 0; |
| 41 | if (ask != null && bid != null) { |
| 42 | spread = ask - bid; |
| 43 | } |
| 44 | |
| 45 | System.out.printf("%-12s %12.2f %12.2f %12.2f %10.2f%n", |
| 46 | id, |
| 47 | safe(last), |
| 48 | safe(bid), |
| 49 | safe(ask), |
| 50 | spread); |
| 51 | } catch (Exception e) { |
| 52 | System.out.printf("%-12s %s%n", id, "ERROR: " + rootMessage(e)); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static Double toDouble(Object v) { |
| 58 | if (v instanceof Number n) return n.doubleValue(); |
nothing calls this directly
no test coverage detected