Spot
Description
Binance4j-spot is a Java connector for the SPOT endpoints of the Binance REST API.
Installation
- Maven
- Gradle
<dependency>
<groupId>{{groupId}}</groupId>
<artifactId>{{artifactId}}</artifactId>
<version>{{version}}</version>
</dependency>
implementation '{{groupId}}:{{artifactId}}:{{version}}'
Dependencies
- binance4j-core : The core of every binance4j artifact
- Lombok : Prevent boilerplate code.
- OkHttp : HTTP && Websocket clients
- Retrofit : Map API endpoints with annotations.
- Jackson : Payload deserialization
- Apache Common Codecs : Encode/decode urls
Send an order
Sends a trade order for execution.
- Sync
- Async (lambda)
- Async (ApiCallback)
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){
//...
}
SpotClient client = new SpotClient(key, secret);
NewOrder request = new NewOrder("BNBBTC", OrderType.MARKET, OrderSide.BUY, new BigDecimal("100"));
client.newOrder(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
NewOrder request = new NewOrder("BNBBTC", OrderType.MARKET, OrderSide.BUY, new BigDecimal("100"));
client.newOrder().then(new ApiCallback<NewOrderResponse>() {
@Override
public void onResponse(NewOrderResponse response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
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.
- MARKET order
- LIMIT order
- QUOTE order
- Buy order
- Sell order
NewOrder newOrder = NewOrder.buyMarket("BNBBTC", new BigDecimal(100));
NewOrder newOrder = NewOrder.sellMarket("BNBBTC", new BigDecimal(100));
- Buy order
- Sell order
NewOrder newOrder = NewOrder.buyLimit("BNBBTC", new BigDecimal(100), new BigDecimal(0.01));
NewOrder newOrder = NewOrder.sellLimit("BNBBTC", new BigDecimal(100), new BigDecimal(0.01));
- Buy order
- Sell order
NewOrder newOrder = NewOrder.buyQuote("BNBBTC", new BigDecimal(100));
NewOrder newOrder = NewOrder.sellQuote("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.
- Sync
- Async (lambda)
- Async (ApiCallback)
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){
//...
}
SpotClient client = new SpotClient(key, secret);
NewOrder request = new NewOrder("BNBBTC", OrderType.MARKET, OrderSide.BUY, new BigDecimal("100"));
client.newOrderTest(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
NewOrder request = new NewOrder("BNBBTC", OrderType.MARKET, OrderSide.BUY, new BigDecimal("100"));
client.newOrderTest().then(new ApiCallback<Void>() {
@Override
public void onResponse() {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Cancel order
Cancels an active order.
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
CancelOrderRequest request = new CancelOrderRequest("BNBBTC", 56935218L);
try{
CancelOrderResponse response = client.cancelOrder(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
CancelOrderRequest request = new CancelOrderRequest("BNBBTC", 56935218L);
client.cancelOrder(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
CancelOrderRequest request = new CancelOrderRequest("BNBBTC", 56935218L);
client.cancelOrder().then(new ApiCallback<CancelOrderResponse>() {
@Override
public void onResponse(CancelOrderResponse response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
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.
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
CancelOpenOrdersRequest request = new CancelOpenOrdersRequest("BNBBTC");
try{
List<CancelOrderResponse> response = client.cancelOpenOrders(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
CancelOpenOrdersRequest request = new CancelOpenOrdersRequest("BNBBTC");
client.cancelOpenOrders(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
CancelOpenOrdersRequest request = new CancelOpenOrdersRequest("BNBBTC");
client.cancelOpenOrders().then(new ApiCallback<List<CancelOrderResponse>>() {
@Override
public void onResponse(List<CancelOrderResponse> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Here we canceled all open orders on BNB/BTC
Get order status
Check an order's status.
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
OrderStatusRequest request = new OrderStatusRequest("BNBBTC", 65293729L);
try{
OrderInfo response = client.getOrderStatus(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
OrderStatusRequest request = new OrderStatusRequest("BNBBTC", 65293729L);
client.getOrderStatus(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
OrderStatusRequest request = new OrderStatusRequest("BNBBTC", 65293729L);
client.getOrderStatus().then(new ApiCallback<OrderInfo>() {
@Override
public void onResponse(OrderInfo response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
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.
- All open orders
- Specific pair
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
try{
List<OrderInfo> response = client.getOpenOrders().execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
client.getOpenOrders().then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
client.getOpenOrders().then(new ApiCallback<List<OrderInfo>>() {
@Override
public void onResponse(List<OrderInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
OpenOrdersStatusRequest request = new OpenOrdersStatusRequest("BNBBTC");
try{
List<OrderInfo> response = client.getOpenOrders(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
OpenOrdersStatusRequest request = new OpenOrdersStatusRequest("BNBBTC");
client.getOpenOrders(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
OpenOrdersStatusRequest request = new OpenOrdersStatusRequest("BNBBTC");
client.getOpenOrders().then(new ApiCallback<List<OrderInfo>>() {
@Override
public void onResponse(List<OrderInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Get all orders
Get all orders on a symbol.
- Get last 500 (default)
- Get last 1000 orders (max)
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
AllOrdersRequest request = new AllOrdersRequest("BNBBTC");
try{
List<OrderInfo> response = client.getAllOrders(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
AllOrdersRequest request = new AllOrdersRequest("BNBBTC");
client.getAllOrders(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
AllOrdersRequest request = new AllOrdersRequest("BNBBTC");
client.getAllOrders().then(new ApiCallback<List<OrderInfo>>() {
@Override
public void onResponse(List<OrderInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
AllOrdersRequest request = new AllOrdersRequest("BNBBTC", 1000);
try{
List<OrderInfo> response = client.getAllOrders(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
AllOrdersRequest request = new AllOrdersRequest("BNBBTC", 1000);
client.getAllOrders(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
AllOrdersRequest request = new AllOrdersRequest("BNBBTC", 1000);
client.getAllOrders().then(new ApiCallback<List<OrderInfo>>() {
@Override
public void onResponse(List<OrderInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Send an OCO order
An OCO order is made of two LIMIT
orders. The first to execute cancels the other.
- Sync
- Async (lambda)
- Async (ApiCallback)
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){
//...
}
SpotClient client = new SpotClient(key, secret);
OCOOrder request = new OCOOrder("BTCBUSD", OrderSide.BUY, new BigDecimal(1), new BigDecimal(50000), new BigDecimal(51000));
client.newOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
OCOOrder request = new OCOOrder("BTCBUSD", OrderSide.BUY, new BigDecimal(1), new BigDecimal(50000), new BigDecimal(51000));
client.newOCO().then(new ApiCallback<OCOResponse>() {
@Override
public void onResponse(OCOResponse response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Here we send an order to buy 1 BTC for 50000 BUSD or 51000 BUSD.
Cancel an OCO/order list
- All OCO
- with order list id
- With client order id
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
CancelOCORequest request = new CancelOCORequest("BNBBTC");
try{
List<OCOResponse> response = client.cancelOCO(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
CancelOCORequest request = new CancelOCORequest("BNBBTC");
client.cancelOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
CancelOCORequest request = new CancelOCORequest("BNBBTC");
client.cancelOCO().then(new ApiCallback<List<OCOResponse>>() {
@Override
public void onResponse(List<OCOResponse> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
OCOOrder request = new OCOOrder("BNBBTC", 13789789L);
try{
List<OCOResponse> response = client.cancelOCO(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
OCOOrder request = new OCOOrder("BNBBTC", 13789789L);
client.cancelOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
OCOOrder request = new OCOOrder("BNBBTC", 13789789L);
client.cancelOCO().then(new ApiCallback<List<OCOResponse>>() {
@Override
public void onResponse(List<OCOResponse> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
OCOOrder request = new OCOOrder("BNBBTC", "C3wyj4WVEktd7u9aVBRXcN");
try{
List<OCOResponse> response = client.cancelOCO(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
OCOOrder request = new OCOOrder("BNBBTC", "C3wyj4WVEktd7u9aVBRXcN");
client.cancelOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
OCOOrder request = new OCOOrder("BNBBTC", "C3wyj4WVEktd7u9aVBRXcN");
client.cancelOCO().then(new ApiCallback<List<OCOResponse>>() {
@Override
public void onResponse(List<OCOResponse> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Canceling an individual leg will cancel the entire OCO.
If both orderListId
and listClientOrderID
are provided, orderId
takes precedence.
Retrieve an OCO order
- With client order id
- With order list id
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
OCOInfoRequest request = new OCOInfoRequest("C3wyj4WVEktd7u9aVBRXcN");
try{
OCOInfo response = client.queryOCO(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
OCOInfoRequest request = new OCOInfoRequest("C3wyj4WVEktd7u9aVBRXcN");
client.queryOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
OCOInfoRequest request = new OCOInfoRequest("C3wyj4WVEktd7u9aVBRXcN");
client.queryOCO().then(new ApiCallback<OCOInfo>() {
@Override
public void onResponse(OCOInfo response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
OCOInfoRequest request = new OCOInfoRequest(13789789L);
try{
OCOInfo response = client.queryOCO(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
OCOInfoRequest request = new OCOInfoRequest(13789789L);
client.queryOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
OCOInfoRequest request = new OCOInfoRequest(13789789L);
client.queryOCO().then(new ApiCallback<OCOInfo>() {
@Override
public void onResponse(OCOInfo response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Retrieve all OCO orders
- last 500 OCO orders (default)
- last 1000 (max)
- With order id
- With order id and limit
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
try{
List<OCOInfo> response = client.getAllOCO().execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
client.getAllOCO().then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
client.getAllOCO().then(new ApiCallback<List<OCOInfo>>() {
@Override
public void onResponse(List<OCOInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest(1000);
try{
List<OCOInfo> response = client.getAllOCO(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest(1000);
client.getAllOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest(1000);
client.getAllOCO().then(new ApiCallback<List<OCOInfo>>() {
@Override
public void onResponse(List<OCOInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest("13789789L");
try{
List<OCOInfo> response = client.getAllOCO(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest("13789789L");
client.getAllOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest("13789789L");
client.getAllOCO().then(new ApiCallback<List<OCOInfo>>() {
@Override
public void onResponse(List<OCOInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest("13789789L", 666);
try{
List<OCOInfo> response = client.getAllOCO(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest("13789789L", 666);
client.getAllOCO(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
AllOCOInfoRequest request = new AllOCOInfoRequest("13789789L", 666);
client.getAllOCO().then(new ApiCallback<List<OCOInfo>>() {
@Override
public void onResponse(List<OCOInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Retrieve open OCO orders
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
try{
List<OCOInfo> response = client.getOpenOCO().execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
client.getOpenOCO().then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
client.getOpenOCO().then(new ApiCallback<List<OCOInfo>>() {
@Override
public void onResponse(List<OCOInfo> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Get SPOT account infos
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
try{
Account response = client.getAccount().execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
client.getAccount().then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
client.getAccount().then(new ApiCallback<Account>() {
@Override
public void onResponse(Account response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Get your trades for a symbol
- Get last 500 (default)
- Get last 1000 orders (max)
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
MyTradesRequest request = new MyTradesRequest("BNBBTC");
try{
List<Trade> response = client.getMyTrades(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
MyTradesRequest request = new MyTradesRequest("BNBBTC");
client.getMyTrades(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
MyTradesRequest request = new MyTradesRequest("BNBBTC");
client.getMyTrades().then(new ApiCallback<List<Trade>>() {
@Override
public void onResponse(List<Trade> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
MyTradesRequest request = new MyTradesRequest("BNBBTC", 1000);
try{
List<Trade> response = client.getMyTrades(request).execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
MyTradesRequest request = new MyTradesRequest("BNBBTC", 1000);
client.getMyTrades(request).then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
MyTradesRequest request = new MyTradesRequest("BNBBTC", 1000);
client.getMyTrades().then(new ApiCallback<List<Trade>>() {
@Override
public void onResponse(List<Trade> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});
Get order count
Displays the user's current order count usage for all intervals. May be useful when rate limiting.
- Sync
- Async (lambda)
- Async (ApiCallback)
SpotClient client = new SpotClient(key, secret);
try{
List<OrderCount> response = client.getOrderCount().execute();
}catch(ApiException e){
//...
}
SpotClient client = new SpotClient(key, secret);
client.getOrderCount().then(response->{
//...
});
SpotClient client = new SpotClient(key, secret);
client.getOrderCount().then(new ApiCallback<List<OrderCount>>() {
@Override
public void onResponse(List<OrderCount> response) {
//...
}
@Override
public void onFailure(ApiException exception) {
//...
}
});