-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
CompareExchanges
github-actions[bot] edited this page Jun 11, 2026
·
1 revision
package examples;
import io.github.ccxt.Exchange;
import java.util.Map;
/**
* Compare the price of a symbol across multiple exchanges.
* Uses Exchange.dynamicallyCreateInstance for the generic/dynamic pattern.
* The result is untyped (Object/Map) since the exchange type is unknown at compile time.
*
* Usage:
* cd java && ./gradlew :examples:run -PmainClass=examples.CompareExchanges
*/
public class CompareExchanges {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String symbol = args.length > 0 ? args[0] : "BTC/USDT";
String[] exchangeIds = {"binance", "bybit", "okx", "kraken", "bitget"};
System.out.println("Comparing " + symbol + " across exchanges\n");
System.out.printf("%-12s %12s %12s %12s %10s%n",
"Exchange", "Last", "Bid", "Ask", "Spread");
System.out.println("-".repeat(60));
for (String id : exchangeIds) {
try {
Exchange exchange = Exchange.dynamicallyCreateInstance(id, null);
exchange.loadMarkets(false).join();
// Untyped: fetchTicker returns CompletableFuture<Object>
Map<String, Object> ticker = (Map<String, Object>) exchange.fetchTicker(symbol).join();
Double last = toDouble(ticker.get("last"));
Double bid = toDouble(ticker.get("bid"));
Double ask = toDouble(ticker.get("ask"));
double spread = 0;
if (ask != null && bid != null) {
spread = ask - bid;
}
System.out.printf("%-12s %12.2f %12.2f %12.2f %10.2f%n",
id,
safe(last),
safe(bid),
safe(ask),
spread);
} catch (Exception e) {
System.out.printf("%-12s %s%n", id, "ERROR: " + rootMessage(e));
}
}
}
static Double toDouble(Object v) {
if (v instanceof Number n) return n.doubleValue();
return null;
}
static double safe(Double v) { return v != null ? v : 0.0; }
static String rootMessage(Exception e) {
Throwable c = e;
while (c.getCause() != null) c = c.getCause();
String msg = c.getMessage();
return msg != null && msg.length() > 60 ? msg.substring(0, 60) + "..." : msg;
}
}(If the page is not being rendered for you, you can refer to the mirror at https://docs.ccxt.com/)
- Install
- Examples
- Manual
- CCXT Pro
- Contributing
- Supported Exchanges
- Exchanges By Country
- API Spec By Method
- FAQ
- Changelog
- Awesome
- API Spec by Exchange
- fetchCurrencies
- alpaca
- apex
- ascendex
- aster
- backpack
- bigone
- binance
- bingx
- bit2c
- bitbank
- bitbns
- bitfinex
- bitflyer
- bitget
- bithumb
- bitmart
- bitmex
- bitopro
- bitrue
- bitso
- bitstamp
- bitteam
- bittrade
- bitvavo
- blockchaincom
- blofin
- btcbox
- btcmarkets
- btcturk
- bullish
- bybit
- bydfi
- cex
- coinbase
- coinbaseexchange
- coinbaseinternational
- coincheck
- coinex
- coinmate
- coinmetro
- coinone
- coinsph
- coinspot
- cryptocom
- cryptomus
- deepcoin
- delta
- deribit
- derive
- digifinex
- dydx
- exmo
- extended
- foxbit
- gate
- gemini
- grvt
- hashkey
- hibachi
- hitbtc
- hollaex
- htx
- hyperliquid
- independentreserve
- indodax
- kraken
- krakenfutures
- kucoin
- fetchBidsAsks
- latoken
- lbank
- lighter
- luno
- mercado
- mexc
- modetrade
- ndax
- novadax
- okx
- onetrading
- p2b
- pacifica
- paradex
- paymium
- phemex
- poloniex
- tokocrypto
- toobit
- upbit
- weex
- whitebit
- woo
- woofipro
- xt
- zaif
- fetchStatus