Skip to main content
Version: 1.x

Spot

 Maven Central Sonatype Nexus  License: MIT

Description

Binance4j-spot is a Java connector for the SPOT endpoints of the Binance REST API.

Installation

pom.xml
<dependency>
<groupId>{{groupId}}</groupId>
<artifactId>{{artifactId}}</artifactId>
<version>{{version}}</version>
</dependency>

Dependencies

Send an order

     

Sends a trade order for execution.

SpotClient client = new SpotClient(key, secret);

NewOrder request = new NewOrder("BNBBTC", OrderType.MARKET, OrderSide.BUY, new BigDecimal("100"));

try{
NewOrderResponse response = client.newOrder(request).execute();
}catch(ApiException e){
//...
}

Here we executed a MARKET order to buy 100 BNB with BTC

NewOrder static methods

The NewOrder class offers static methods to generate NewOrder instances.

NewOrder newOrder = NewOrder.buyMarket("BNBBTC", new BigDecimal(100));

Test new order

    

Tests the trade order without executing it. Works like the newOrder method.

The request returns nothing but will throw an ApiException if it fails.

SpotClient client = new SpotClient(key, secret);

NewOrder request = new NewOrder("BNBBTC", OrderType.MARKET, OrderSide.BUY, new BigDecimal("100"));

try{
client.newOrderTest(request).execute();
}catch(ApiException e){
//...
}

Cancel order

     

Cancels an active order.

SpotClient client = new SpotClient(key, secret);

CancelOrderRequest request = new CancelOrderRequest("BNBBTC", 56935218L);

try{
CancelOrderResponse response = client.cancelOrder(request).execute();
}catch(ApiException e){
//...
}

Here we canceled the order on BNB/BTC with the 56935218L id

Cancel all open orders

     

Cancels all active orders on a symbol. This includes OCO orders.

SpotClient client = new SpotClient(key, secret);

CancelOpenOrdersRequest request = new CancelOpenOrdersRequest("BNBBTC");

try{
List<CancelOrderResponse> response = client.cancelOpenOrders(request).execute();
}catch(ApiException e){
//...
}

Here we canceled all open orders on BNB/BTC

Get order status

     

Check an order's status.

SpotClient client = new SpotClient(key, secret);

OrderStatusRequest request = new OrderStatusRequest("BNBBTC", 65293729L);

try{
OrderInfo response = client.getOrderStatus(request).execute();
}catch(ApiException e){
//...
}

he we got the status of order on BNB/BTC with id 65293729

Get open orders

     

Get all open orders on a symbol. Careful when accessing this with no symbol.

SpotClient client = new SpotClient(key, secret);

try{
List<OrderInfo> response = client.getOpenOrders().execute();
}catch(ApiException e){
//...
}

Get all orders

     

Get all orders on a symbol.

SpotClient client = new SpotClient(key, secret);

AllOrdersRequest request = new AllOrdersRequest("BNBBTC");

try{
List<OrderInfo> response = client.getAllOrders(request).execute();
}catch(ApiException e){
//...
}

Send an OCO order

     

An OCO order is made of two LIMIT orders. The first to execute cancels the other.

SpotClient client = new SpotClient(key, secret);

OCOOrder request = new OCOOrder("BTCBUSD", OrderSide.BUY, new BigDecimal(1), new BigDecimal(50000), new BigDecimal(51000));

try{
OCOResponse response = client.newOCO(request).execute();
}catch(ApiException e){
//...
}

Here we send an order to buy 1 BTC for 50000 BUSD or 51000 BUSD.

Cancel an OCO/order list

     
SpotClient client = new SpotClient(key, secret);

CancelOCORequest request = new CancelOCORequest("BNBBTC");

try{
List<OCOResponse> response = client.cancelOCO(request).execute();
}catch(ApiException e){
//...
}
note

Canceling an individual leg will cancel the entire OCO.

If both orderListId and listClientOrderID are provided, orderId takes precedence.

Retrieve an OCO order

     
SpotClient client = new SpotClient(key, secret);

OCOInfoRequest request = new OCOInfoRequest("C3wyj4WVEktd7u9aVBRXcN");

try{
OCOInfo response = client.queryOCO(request).execute();
}catch(ApiException e){
//...
}

Retrieve all OCO orders

     
SpotClient client = new SpotClient(key, secret);

try{
List<OCOInfo> response = client.getAllOCO().execute();
}catch(ApiException e){
//...
}

Retrieve open OCO orders

     
SpotClient client = new SpotClient(key, secret);

try{
List<OCOInfo> response = client.getOpenOCO().execute();
}catch(ApiException e){
//...
}

Get SPOT account infos

     
SpotClient client = new SpotClient(key, secret);

try{
Account response = client.getAccount().execute();
}catch(ApiException e){
//...
}

Get your trades for a symbol

     
SpotClient client = new SpotClient(key, secret);

MyTradesRequest request = new MyTradesRequest("BNBBTC");

try{
List<Trade> response = client.getMyTrades(request).execute();
}catch(ApiException e){
//...
}

Get order count

     

Displays the user's current order count usage for all intervals. May be useful when rate limiting.

SpotClient client = new SpotClient(key, secret);

try{
List<OrderCount> response = client.getOrderCount().execute();
}catch(ApiException e){
//...
}