Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Binance Futures API #340

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Implemented Binance Futures API
mah454 committed Mar 31, 2021
commit f0dd37875026b8329e486b1731e8c72b33c265e4
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.binance.api.client;

import com.binance.api.client.domain.account.*;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.CancelOrderResponse;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.account.request.OrderStatusRequest;

import java.util.List;

/**
* Binance API façade, supporting asynchronous/non-blocking access Binance's Futures REST API.
*
* @author Mahdi Shiekh Hosseini
*/
public interface BinanceApiAsyncFuturesRestClient {

void getAccount(Long recvWindow, Long timestamp, BinanceApiCallback<FuturesAccount> callback);

void getAccount(BinanceApiCallback<FuturesAccount> callback);

void getOpenOrders(OrderRequest orderRequest, BinanceApiCallback<List<Order>> callback);

void newOrder(FuturesNewOrder order, BinanceApiCallback<FuturesNewOrderResponse> callback);

void cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback<CancelOrderResponse> callback);

void getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback<Order> callback);

}
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ public interface BinanceApiAsyncRestClient {
* Get older trades. Weight: 5
*
* @param symbol ticker symbol (e.g. ETHBTC)
* @param limit of last trades (Default 500; max 1000.)
* @param limit of last trades (Default 500; max 1000.)getMyTrades
* @param fromId TradeId to fetch from. Default gets most recent trades.
* @param callback the callback that handles the response
*/
186 changes: 103 additions & 83 deletions src/main/java/com/binance/api/client/BinanceApiClientFactory.java
Original file line number Diff line number Diff line change
@@ -9,87 +9,107 @@
*/
public class BinanceApiClientFactory {

/**
* API Key
*/
private String apiKey;

/**
* Secret.
*/
private String secret;

/**
* Instantiates a new binance api client factory.
*
* @param apiKey the API key
* @param secret the Secret
*/
private BinanceApiClientFactory(String apiKey, String secret) {
this.apiKey = apiKey;
this.secret = secret;
}

/**
* New instance.
*
* @param apiKey the API key
* @param secret the Secret
*
* @return the binance api client factory
*/
public static BinanceApiClientFactory newInstance(String apiKey, String secret) {
return new BinanceApiClientFactory(apiKey, secret);
}

/**
* New instance without authentication.
*
* @return the binance api client factory
*/
public static BinanceApiClientFactory newInstance() {
return new BinanceApiClientFactory(null, null);
}

/**
* Creates a new synchronous/blocking REST client.
*/
public BinanceApiRestClient newRestClient() {
return new BinanceApiRestClientImpl(apiKey, secret);
}

/**
* Creates a new asynchronous/non-blocking REST client.
*/
public BinanceApiAsyncRestClient newAsyncRestClient() {
return new BinanceApiAsyncRestClientImpl(apiKey, secret);
}

/**
* Creates a new asynchronous/non-blocking Margin REST client.
*/
public BinanceApiAsyncMarginRestClient newAsyncMarginRestClient() {
return new BinanceApiAsyncMarginRestClientImpl(apiKey, secret);
}

/**
* Creates a new synchronous/blocking Margin REST client.
*/
public BinanceApiMarginRestClient newMarginRestClient() {
return new BinanceApiMarginRestClientImpl(apiKey, secret);
}

/**
* Creates a new web socket client used for handling data streams.
*/
public BinanceApiWebSocketClient newWebSocketClient() {
return new BinanceApiWebSocketClientImpl(getSharedClient());
}

/**
* Creates a new synchronous/blocking Swap REST client.
*/
public BinanceApiSwapRestClient newSwapRestClient() {
return new BinanceApiSwapRestClientImpl(apiKey, secret);
}
/**
* API Key
*/
private String apiKey;

/**
* Secret.
*/
private String secret;

/**
* Instantiates a new binance api client factory.
*
* @param apiKey the API key
* @param secret the Secret
*/
private BinanceApiClientFactory(String apiKey, String secret) {
this.apiKey = apiKey;
this.secret = secret;
}

/**
* New instance.
*
* @param apiKey the API key
* @param secret the Secret
* @return the binance api client factory
*/
public static BinanceApiClientFactory newInstance(String apiKey, String secret) {
return new BinanceApiClientFactory(apiKey, secret);
}

/**
* New instance without authentication.
*
* @return the binance api client factory
*/
public static BinanceApiClientFactory newInstance() {
return new BinanceApiClientFactory(null, null);
}

/**
* Creates a new synchronous/blocking REST client.
*/
public BinanceApiRestClient newRestClient() {
return new BinanceApiRestClientImpl(apiKey, secret);
}

/**
* Creates a new asynchronous/non-blocking REST client.
*/
public BinanceApiAsyncRestClient newAsyncRestClient() {
return new BinanceApiAsyncRestClientImpl(apiKey, secret);
}

/**
* Creates a new asynchronous/non-blocking Margin REST client.
*/
public BinanceApiAsyncMarginRestClient newAsyncMarginRestClient() {
return new BinanceApiAsyncMarginRestClientImpl(apiKey, secret);
}

/**
* Creates a new asynchronous/non-blocking Margin REST client.
*/
public BinanceApiAsyncFuturesRestClient newAsyncFuturesRestClient() {
return new BinanceApiAsyncFuturesRestClientImpl(apiKey, secret);
}

/**
* Creates a new synchronous/blocking Margin REST client.
*/
public BinanceApiMarginRestClient newMarginRestClient() {
return new BinanceApiMarginRestClientImpl(apiKey, secret);
}

/**
* Creates a new synchronous/blocking Futures REST client.
*/
public BinanceApiFuturesRestClient newFuturesRestClient() {
return new BinanceApiFuturesRestClientImpl(apiKey, secret);
}

/**
* Creates a new web socket client used for handling data streams.
*/
public BinanceApiWebSocketClient newWebSocketClient() {
return new BinanceApiWebSocketClientImpl(getSharedClient(), BinanceEngineType.SPOT);
}

/**
* Creates a new web socket client used for handling data streams.
*/
public BinanceApiWebSocketClient newFuturesWebSocketClient() {
return new BinanceApiWebSocketClientImpl(getSharedClient(), BinanceEngineType.FUTURES);
}

/**
* Creates a new synchronous/blocking Swap REST client.
*/
public BinanceApiSwapRestClient newSwapRestClient() {
return new BinanceApiSwapRestClientImpl(apiKey, secret);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.binance.api.client;

import com.binance.api.client.domain.account.FuturesAccount;
import com.binance.api.client.domain.account.FuturesNewOrder;
import com.binance.api.client.domain.account.FuturesNewOrderResponse;
import com.binance.api.client.domain.account.Order;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.CancelOrderResponse;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.account.request.OrderStatusRequest;

import java.util.List;

public interface BinanceApiFuturesRestClient {
/**
* Get current margin account information using default parameters.
*/
FuturesAccount getAccount();

/**
* Get all open orders on margin account for a symbol.
*
* @param orderRequest order request parameters
*/
List<Order> getOpenOrders(OrderRequest orderRequest);

/**
* Send in a new margin order.
*
* @param order the new order to submit.
* @return a response containing details about the newly placed order.
*/
FuturesNewOrderResponse newOrder(FuturesNewOrder order);

/**
* Cancel an active margin order.
*
* @param cancelOrderRequest order status request parameters
*/
CancelOrderResponse cancelOrder(CancelOrderRequest cancelOrderRequest);

/**
* Check margin order's status.
*
* @param orderStatusRequest order status request options/filters
* @return an order
*/
Order getOrderStatus(OrderStatusRequest orderStatusRequest);
}
9 changes: 9 additions & 0 deletions src/main/java/com/binance/api/client/BinanceEngineType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.binance.api.client;

/**
* @author Mahdi Sheikh Hosseini
*/
public enum BinanceEngineType {
SPOT,
FUTURES
}
99 changes: 56 additions & 43 deletions src/main/java/com/binance/api/client/config/BinanceApiConfig.java
Original file line number Diff line number Diff line change
@@ -5,48 +5,61 @@
*/
public class BinanceApiConfig {

/**
* Base domain for URLs.
*/
private static String BASE_DOMAIN = "binance.com";

/**
* Set the URL base domain name (e.g., binance.com).
*
* @param baseDomain Base domain name
*/
public static void setBaseDomain(final String baseDomain) {
BASE_DOMAIN = baseDomain;
}

/**
* Get the URL base domain name (e.g., binance.com).
*
* @return The base domain for URLs
*/
public static String getBaseDomain() {
return BASE_DOMAIN;
}

/**
* REST API base URL.
*/
public static String getApiBaseUrl() {
return String.format("https://api.%s", getBaseDomain());
}

/**
* Streaming API base URL.
*/
public static String getStreamApiBaseUrl() {
return String.format("wss://stream.%s:9443/ws", getBaseDomain());
}

/**
* Asset info base URL.
*/
public static String getAssetInfoApiBaseUrl() {
return String.format("https://%s/", getBaseDomain());
}
/**
* Base domain for URLs.
*/
private static String BASE_DOMAIN = "binance.com";

/**
* Set the URL base domain name (e.g., binance.com).
*
* @param baseDomain Base domain name
*/
public static void setBaseDomain(final String baseDomain) {
BASE_DOMAIN = baseDomain;
}

/**
* Get the URL base domain name (e.g., binance.com).
*
* @return The base domain for URLs
*/
public static String getBaseDomain() {
return BASE_DOMAIN;
}

/**
* REST API base URL.
*/
public static String getApiBaseUrl() {
return String.format("https://api.%s", getBaseDomain());
}

/**
* Streaming API base URL.
*/
public static String getStreamApiBaseUrl() {
return String.format("wss://stream.%s:9443/ws", getBaseDomain());
}

/**
* Asset info base URL.
*/
public static String getAssetInfoApiBaseUrl() {
return String.format("https://%s/", getBaseDomain());
}

/**
* Futures REST API base URL.
*/
public static String getFuturesApiBaseUrl() {
return String.format("https://fapi.%s", getBaseDomain());
}

/**
* Futures Streaming API base URL.
*/
public static String getFuturesStreamApiBaseUrl() {
return String.format("wss://fstream.%s/ws", getBaseDomain());
}
}
Loading