XChange is a Java library providing a simple and consistent API for interacting with 60+ Bitcoin and other crypto currency exchanges, providing a consistent interface for trading and accessing market data.
The world of Bitcoin changes quickly and XChange is no exception. For the latest bugfixes and features you should use thesnapshot jarsor build yourself from thedevelop
branch. See below for more details about building with Maven. To report bugs and see what issues people are currently working on see theissues page.
XChange is a Java based library providing a simple and consistent API for interacting with a diverse set of crypto currency exchanges.
Basic usage is very simple: Create anExchange
instance, get the appropriate service, and request data. More complex usages are progressively detailed below.
To use public APIs which do not require authentication:
Exchangebitstamp=ExchangeFactory.INSTANCE.createExchange(BitstampExchange.class);
MarketDataServicemarketDataService=bitstamp.getMarketDataService();
Tickerticker=marketDataService.getTicker(CurrencyPair.BTC_USD);
System.out.println(ticker.toString());
To use APIs which require authentication, create anExchangeSpecification
with your API credentials and pass this tocreateExchange()
:
ExchangeSpecificationexSpec=newBitstampExchange().getDefaultExchangeSpecification();
exSpec.setUserName("34387");
exSpec.setApiKey("a4SDmpl9s6xWJS5fkKRT6yn41vXuY0AM");
exSpec.setSecretKey("sisJixU6Xd0d1yr6w02EHCb9UwYzTNuj");
Exchangebitstamp=ExchangeFactory.INSTANCE.createExchange(exSpec);
Please Note:while most exchanges use just an API key and secret key, others (such asusername
on Bitstamp orpassphrase
on Coinbase Pro) are exchange-specific. For more examples of adding the keys to theExchangeSpecification
,including storing them in a configuration file, seeFrequently Asked Questions.
Once you have an authenticatedExchange
,the private API services,AccountService
andTradeService
,can be used to access private data:
// Get the account information
AccountServiceaccountService=bitstamp.getAccountService();
AccountInfoaccountInfo=accountService.getAccountInfo();
System.out.println(accountInfo.toString());
All exchange implementations expose the same API, but you can also directly access the underlying "raw" data from the individual exchanges if you need to.
The above API is usually fully supported on all exchanges and is best used for occasional requests and polling on relatively long intervals. Many exchanges, however, heavily limit the frequency that these requests can be made, and advise instead that you use their websocket API if you need streaming or real-time data.
For a smaller number of exchanges, the websocket-basedStreamingExchange
API is also available. This usesReactive streamsto allow you to efficiently subscribe to changes relating to thousands of coin pairs without requiring large numbers of threads.
You will need to import an additional dependency for the exchange you are using (see below), then example usage is as follows:
// Use StreamingExchangeFactory instead of ExchangeFactory
StreamingExchangeexchange=StreamingExchangeFactory.INSTANCE.createExchange(BitstampStreamingExchange.class);
// Connect to the Exchange WebSocket API. Here we use a blocking wait.
exchange.connect().blockingAwait();
// Subscribe to live trades update.
Disposablesubscription1=exchange.getStreamingMarketDataService()
.getTrades(CurrencyPair.BTC_USD)
.subscribe(
trade->LOG.info("Trade: {}",trade),
throwable->LOG.error("Error in trade subscription",throwable));
// Subscribe order book data with the reference to the subscription.
Disposablesubscription2=exchange.getStreamingMarketDataService()
.getOrderBook(CurrencyPair.BTC_USD)
.subscribe(orderBook->LOG.info("Order book: {}",orderBook));
// Wait for a while to see some results arrive
Thread.sleep(20000);
// Unsubscribe
subscription1.dispose();
subscription2.dispose();
// Disconnect from exchange (blocking again)
exchange.disconnect().blockingAwait();
Authentication, if supported for the exchange, works the same way as for the main API, via anExchangeSpecification
.For more information on what is supported, see the Wiki.
Now go ahead andstudy some more examples,download the thingandprovide feedback.
More information about reactive streams can be found at theRxJava wiki.
- MIT license
- consistent API across all implemented exchanges
- active development
- very minimal 3rd party dependencies
- modular components
Project Site:http://knowm.org/open-source/xchange
Example Code:http://knowm.org/open-source/xchange/xchange-example-code
Change Log:http://knowm.org/open-source/xchange/xchange-change-log/
Java Docs:http://knowm.org/javadocs/xchange/index.html
- Home
- FAQ
- Design Notes
- Milestones
- Exchange Support
- New Implementation Best Practices
- Installing SSL Certificates into TrustStore
- Getting Started with XChange for Noobies
- Code Style
The XChange release artifacts are hosted on Maven Central:org.knowm.xchange
Add the following dependencies in your pom.xml file. You will need at least xchange-core. Add the additional dependencies for the exchange modules you are interested in (XYZ shown only for a placeholder). There is example code for all the modules in xchange-examples.
<dependency>
<groupId>org.knowm.xchange</groupId>
<artifactId>xchange-core</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.knowm.xchange</groupId>
<artifactId>xchange-XYZ</artifactId>
<version>5.2.0</version>
</dependency>
If it is available for your exchange, you may also want to use the streaming API:
<dependency>
<groupId>org.knowm.xchange</groupId>
<artifactId>xchange-stream-XYZ</artifactId>
<version>5.2.0</version>
</dependency>
For snapshots, add the following repository to your pom.xml file.
<repository>
<id>sonatype-oss-snapshot</id>
<snapshots/>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
The current snapshot version is:
5.2.1-SNAPSHOT
Instruction | Command |
---|---|
run unit tests | mvn clean test |
run unit and integration tests | mvn clean verify -DskipIntegrationTests=false |
install in local Maven repo | mvn clean install |
create project javadocs | mvn javadoc:aggregate |
generate dependency tree | mvn dependency:tree |
check for dependency updates | mvn versions:display-dependency-updates |
check for plugin updates | mvn versions:display-plugin-updates |
code format | mvn com.spotify.fmt:fmt-maven-plugin:format |
pom format/organize | mvn com.github.ekryd.sortpom:sortpom-maven-plugin:sort |
Please report any bugs or submit feature requests toXChange's Github issue tracker.
If you'd like to submit a new implementation for another exchange, please take a look atNew Implementation Best Practicesfirst, as there are lots of time-saving tips!
For more information such as a contributor list and a list of known projects depending on XChange, visit theMain Project Wiki.