XChange Java Library

repository·develop·Indexed 26 days ago

https://github.com/knowm/xchange

A Java library providing a consistent API for interacting with over 60 cryptocurrency exchanges. It supports trading operations and access to public and private market data via REST and WebSocket APIs, including Reactive Streams for real-time data. The library includes specific implementations for exchanges such as Bitstamp, Binance, Bitmex, Coins.ph, Deribit, and Ripple, as well as Bitcoin Core wallet RPC connectivity.

Tokens
6.7K
Snippets
14
Records
25
Agent score
87%

What's inside XChange

  1. Understand Ripple fee types

    develop

    When working with Ripple via XChange, be aware of two distinct fee types:

    1. Transaction fee: A network charge levied in XRP, taken from the org.knowm.xchange.ripple.dto.account balance for each order or org.knowm.xchange.ripple.dto.trade transaction. These are populated in the UserTrade fee fields.
    2. Transfer fee: A fee charged by the issuer in the currency of the traded instrument. The sender pays this fee, while the receiver does not. These are stored in the RippleUserTrade object transfer fee fields.
  2. Use IntelliJ IDEA HTTP Client for API requests

    develop

    The xchange-bitmex module includes *.http files located in src/test/resources/rest for testing API requests via the IntelliJ IDEA HTTP Client.

    For requests requiring authorization, you must store your API credentials in a file named http-client.private.env.json located in the module's root directory. You can use example.http-client.private.env.json as a template for the required structure.

    Warning: Never commit your API credentials to the repository.

  3. Configure environment variables for integration tests

    develop

    Integration tests that require API keys fetch their credentials from environment variables defined in integration-test.env.properties.

    To enable these tests, create an integration-test.env.properties file in the module root using the format found in example.integration-test.env.properties. If no keys are provided in this file, the integration tests requiring them will be skipped automatically.

    Warning: Never commit your API credentials to the repository.

  4. Connect to a Bitcoin Core wallet via RPC

    develop

    To use the Bitcoin Core exchange implementation, you must connect to a Bitcoin Core wallet running an RPC server. By default, it attempts to connect to localhost:8332.

    Configuration Requirements

    1. RPC Password: You must set up an RPC password on your Bitcoin Core wallet.
    2. Custom Host/Port: You can specify a different host and port by using a custom plain text URI in the exchange specification.
    3. Security Warning: The connection is not encrypted. Direct remote access to the RPC port is strongly discouraged. It is recommended to connect locally or follow Bitcoin Core's guidelines for enabling SSL on the client daemon.
  5. Configure API keys for integration tests

    develop

    Integration tests that require API keys read their configuration from environment variables defined in a file named integration-test.env.properties.

    To set up your keys, create this file in the module root and use the format provided in example.integration-test.env.properties. If no keys are provided in this file, the integration tests requiring them will be automatically skipped.

    Warning: Never commit your API credentials to the repository.

  6. Use the Coins.ph Exchange WebSocket API

    develop

    For real-time data streaming, use StreamingExchangeFactory to create a CoinsphStreamingExchange. You must connect to the stream using .connect().blockingAwait() before subscribing to updates.

    Supported streaming features:

    • Market Data: Order book, ticker, and trade updates.
    • User Data: Order changes, trade updates, and balance updates.
    // Create Streaming Exchange
    ExchangeSpecification spec = new ExchangeSpecification(CoinsphStreamingExchange.class);
    spec.setApiKey("your-api-key");
    spec.setSecretKey("your-secret-key");
    spec.setExchangeSpecificParametersItem(StreamingExchange.USE_SANDBOX, false); // Set to true for sandbox
    
    StreamingExchange streamingExchange = StreamingExchangeFactory.INSTANCE.createExchange(spec);
    streamingExchange.connect().blockingAwait();
    
    // Subscribe to order book updates
    CurrencyPair pair = CurrencyPair.BTC_PHP;
    streamingExchange.getStreamingMarketDataService()
        .getOrderBook(pair)
        .subscribe(orderBook -> {
            System.out.println("Received order book: " + orderBook);
        });
    
    // Subscribe to user order updates
    streamingExchange.getStreamingTradeService()
        .getOrderChanges(pair)
        .subscribe(order -> {
            System.out.println("Order update: " + order);
        });
  7. Enable use of the public api.ripple.com REST API

    develop

    If you are using a test account or explicitly choose to trust the public Ripple Labs API (https://api.ripple.com/), you must enable it in the ExchangeSpecification to prevent an exception. This is done by setting the trust.api.ripple.com parameter to true using the RippleExchange.TRUST_API_RIPPLE_COM key.

    ExchangeSpecification specification = new ExchangeSpecification(RippleExchange.class);
    specification.setSslUri(RippleExchange.REST_API_RIPPLE_LABS);
    specification.setSecretKey("s****************************");
    specification.setExchangeSpecificParametersItem(RippleExchange.TRUST_API_RIPPLE_COM, true);
    
    Exchange exchange = ExchangeFactory.INSTANCE.createExchange(specification);
  8. Install XChange Snapshot versions via Maven

    develop

    To use the latest development snapshots, add the Central Portal Snapshots repository to your pom.xml and use the 6.0.0-SNAPSHOT version.

    <repository>
        <name>Central Portal Snapshots</name>
        <id>central-portal-snapshots</id>
        <url>https://central.sonatype.com/repository/maven-snapshots/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
  9. Use the Coins.ph Exchange REST API

    develop

    To interact with the Coins.ph Exchange via REST, use the ExchangeFactory to create a CoinsphExchange instance. You must configure your API credentials using ExchangeSpecification.

    Supported features include:

    • Market Data: Ticker, order book, and trades.
    • Trading: Account info, open orders, market/limit/stop orders, order cancellation, order status, and trade history.
    // Create Exchange
    Exchange exchange = ExchangeFactory.INSTANCE.createExchange(CoinsphExchange.class);
    
    // Configure API keys
    ExchangeSpecification spec = exchange.getDefaultExchangeSpecification();
    spec.setApiKey("your-api-key");
    spec.setSecretKey("your-secret-key");
    spec.setExchangeSpecificParametersItem(Exchange.USE_SANDBOX, false); // Set to true for sandbox
    exchange.applySpecification(spec);
    
    // Get market data
    MarketDataService marketDataService = exchange.getMarketDataService();
    Ticker ticker = marketDataService.getTicker(CurrencyPair.BTC_PHP);
    
    // Get account info
    AccountService accountService = exchange.getAccountService();
    AccountInfo accountInfo = accountService.getAccountInfo();
    
    // Place order
    TradeService tradeService = exchange.getTradeService();
    MarketOrder marketOrder = new MarketOrder.Builder(Order.OrderType.BID, CurrencyPair.BTC_PHP)
        .originalAmount(new BigDecimal("0.001"))
        .build();
    String orderId = tradeService.placeMarketOrder(marketOrder);