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

Commit f0dd378

Browse files
committed
Implemented Binance Futures API
1 parent cb90b21 commit f0dd378

28 files changed

Lines changed: 1706 additions & 150 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.binance.api.client;
2+
3+
import com.binance.api.client.domain.account.*;
4+
import com.binance.api.client.domain.account.request.CancelOrderRequest;
5+
import com.binance.api.client.domain.account.request.CancelOrderResponse;
6+
import com.binance.api.client.domain.account.request.OrderRequest;
7+
import com.binance.api.client.domain.account.request.OrderStatusRequest;
8+
9+
import java.util.List;
10+
11+
/**
12+
* Binance API façade, supporting asynchronous/non-blocking access Binance's Futures REST API.
13+
*
14+
* @author Mahdi Shiekh Hosseini
15+
*/
16+
public interface BinanceApiAsyncFuturesRestClient {
17+
18+
void getAccount(Long recvWindow, Long timestamp, BinanceApiCallback<FuturesAccount> callback);
19+
20+
void getAccount(BinanceApiCallback<FuturesAccount> callback);
21+
22+
void getOpenOrders(OrderRequest orderRequest, BinanceApiCallback<List<Order>> callback);
23+
24+
void newOrder(FuturesNewOrder order, BinanceApiCallback<FuturesNewOrderResponse> callback);
25+
26+
void cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback<CancelOrderResponse> callback);
27+
28+
void getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback<Order> callback);
29+
30+
}

src/main/java/com/binance/api/client/BinanceApiAsyncRestClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public interface BinanceApiAsyncRestClient {
8080
* Get older trades. Weight: 5
8181
*
8282
* @param symbol ticker symbol (e.g. ETHBTC)
83-
* @param limit of last trades (Default 500; max 1000.)
83+
* @param limit of last trades (Default 500; max 1000.)getMyTrades
8484
* @param fromId TradeId to fetch from. Default gets most recent trades.
8585
* @param callback the callback that handles the response
8686
*/

src/main/java/com/binance/api/client/BinanceApiClientFactory.java

Lines changed: 103 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,107 @@
99
*/
1010
public class BinanceApiClientFactory {
1111

12-
/**
13-
* API Key
14-
*/
15-
private String apiKey;
16-
17-
/**
18-
* Secret.
19-
*/
20-
private String secret;
21-
22-
/**
23-
* Instantiates a new binance api client factory.
24-
*
25-
* @param apiKey the API key
26-
* @param secret the Secret
27-
*/
28-
private BinanceApiClientFactory(String apiKey, String secret) {
29-
this.apiKey = apiKey;
30-
this.secret = secret;
31-
}
32-
33-
/**
34-
* New instance.
35-
*
36-
* @param apiKey the API key
37-
* @param secret the Secret
38-
*
39-
* @return the binance api client factory
40-
*/
41-
public static BinanceApiClientFactory newInstance(String apiKey, String secret) {
42-
return new BinanceApiClientFactory(apiKey, secret);
43-
}
44-
45-
/**
46-
* New instance without authentication.
47-
*
48-
* @return the binance api client factory
49-
*/
50-
public static BinanceApiClientFactory newInstance() {
51-
return new BinanceApiClientFactory(null, null);
52-
}
53-
54-
/**
55-
* Creates a new synchronous/blocking REST client.
56-
*/
57-
public BinanceApiRestClient newRestClient() {
58-
return new BinanceApiRestClientImpl(apiKey, secret);
59-
}
60-
61-
/**
62-
* Creates a new asynchronous/non-blocking REST client.
63-
*/
64-
public BinanceApiAsyncRestClient newAsyncRestClient() {
65-
return new BinanceApiAsyncRestClientImpl(apiKey, secret);
66-
}
67-
68-
/**
69-
* Creates a new asynchronous/non-blocking Margin REST client.
70-
*/
71-
public BinanceApiAsyncMarginRestClient newAsyncMarginRestClient() {
72-
return new BinanceApiAsyncMarginRestClientImpl(apiKey, secret);
73-
}
74-
75-
/**
76-
* Creates a new synchronous/blocking Margin REST client.
77-
*/
78-
public BinanceApiMarginRestClient newMarginRestClient() {
79-
return new BinanceApiMarginRestClientImpl(apiKey, secret);
80-
}
81-
82-
/**
83-
* Creates a new web socket client used for handling data streams.
84-
*/
85-
public BinanceApiWebSocketClient newWebSocketClient() {
86-
return new BinanceApiWebSocketClientImpl(getSharedClient());
87-
}
88-
89-
/**
90-
* Creates a new synchronous/blocking Swap REST client.
91-
*/
92-
public BinanceApiSwapRestClient newSwapRestClient() {
93-
return new BinanceApiSwapRestClientImpl(apiKey, secret);
94-
}
12+
/**
13+
* API Key
14+
*/
15+
private String apiKey;
16+
17+
/**
18+
* Secret.
19+
*/
20+
private String secret;
21+
22+
/**
23+
* Instantiates a new binance api client factory.
24+
*
25+
* @param apiKey the API key
26+
* @param secret the Secret
27+
*/
28+
private BinanceApiClientFactory(String apiKey, String secret) {
29+
this.apiKey = apiKey;
30+
this.secret = secret;
31+
}
32+
33+
/**
34+
* New instance.
35+
*
36+
* @param apiKey the API key
37+
* @param secret the Secret
38+
* @return the binance api client factory
39+
*/
40+
public static BinanceApiClientFactory newInstance(String apiKey, String secret) {
41+
return new BinanceApiClientFactory(apiKey, secret);
42+
}
43+
44+
/**
45+
* New instance without authentication.
46+
*
47+
* @return the binance api client factory
48+
*/
49+
public static BinanceApiClientFactory newInstance() {
50+
return new BinanceApiClientFactory(null, null);
51+
}
52+
53+
/**
54+
* Creates a new synchronous/blocking REST client.
55+
*/
56+
public BinanceApiRestClient newRestClient() {
57+
return new BinanceApiRestClientImpl(apiKey, secret);
58+
}
59+
60+
/**
61+
* Creates a new asynchronous/non-blocking REST client.
62+
*/
63+
public BinanceApiAsyncRestClient newAsyncRestClient() {
64+
return new BinanceApiAsyncRestClientImpl(apiKey, secret);
65+
}
66+
67+
/**
68+
* Creates a new asynchronous/non-blocking Margin REST client.
69+
*/
70+
public BinanceApiAsyncMarginRestClient newAsyncMarginRestClient() {
71+
return new BinanceApiAsyncMarginRestClientImpl(apiKey, secret);
72+
}
73+
74+
/**
75+
* Creates a new asynchronous/non-blocking Margin REST client.
76+
*/
77+
public BinanceApiAsyncFuturesRestClient newAsyncFuturesRestClient() {
78+
return new BinanceApiAsyncFuturesRestClientImpl(apiKey, secret);
79+
}
80+
81+
/**
82+
* Creates a new synchronous/blocking Margin REST client.
83+
*/
84+
public BinanceApiMarginRestClient newMarginRestClient() {
85+
return new BinanceApiMarginRestClientImpl(apiKey, secret);
86+
}
87+
88+
/**
89+
* Creates a new synchronous/blocking Futures REST client.
90+
*/
91+
public BinanceApiFuturesRestClient newFuturesRestClient() {
92+
return new BinanceApiFuturesRestClientImpl(apiKey, secret);
93+
}
94+
95+
/**
96+
* Creates a new web socket client used for handling data streams.
97+
*/
98+
public BinanceApiWebSocketClient newWebSocketClient() {
99+
return new BinanceApiWebSocketClientImpl(getSharedClient(), BinanceEngineType.SPOT);
100+
}
101+
102+
/**
103+
* Creates a new web socket client used for handling data streams.
104+
*/
105+
public BinanceApiWebSocketClient newFuturesWebSocketClient() {
106+
return new BinanceApiWebSocketClientImpl(getSharedClient(), BinanceEngineType.FUTURES);
107+
}
108+
109+
/**
110+
* Creates a new synchronous/blocking Swap REST client.
111+
*/
112+
public BinanceApiSwapRestClient newSwapRestClient() {
113+
return new BinanceApiSwapRestClientImpl(apiKey, secret);
114+
}
95115
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.binance.api.client;
2+
3+
import com.binance.api.client.domain.account.FuturesAccount;
4+
import com.binance.api.client.domain.account.FuturesNewOrder;
5+
import com.binance.api.client.domain.account.FuturesNewOrderResponse;
6+
import com.binance.api.client.domain.account.Order;
7+
import com.binance.api.client.domain.account.request.CancelOrderRequest;
8+
import com.binance.api.client.domain.account.request.CancelOrderResponse;
9+
import com.binance.api.client.domain.account.request.OrderRequest;
10+
import com.binance.api.client.domain.account.request.OrderStatusRequest;
11+
12+
import java.util.List;
13+
14+
public interface BinanceApiFuturesRestClient {
15+
/**
16+
* Get current margin account information using default parameters.
17+
*/
18+
FuturesAccount getAccount();
19+
20+
/**
21+
* Get all open orders on margin account for a symbol.
22+
*
23+
* @param orderRequest order request parameters
24+
*/
25+
List<Order> getOpenOrders(OrderRequest orderRequest);
26+
27+
/**
28+
* Send in a new margin order.
29+
*
30+
* @param order the new order to submit.
31+
* @return a response containing details about the newly placed order.
32+
*/
33+
FuturesNewOrderResponse newOrder(FuturesNewOrder order);
34+
35+
/**
36+
* Cancel an active margin order.
37+
*
38+
* @param cancelOrderRequest order status request parameters
39+
*/
40+
CancelOrderResponse cancelOrder(CancelOrderRequest cancelOrderRequest);
41+
42+
/**
43+
* Check margin order's status.
44+
*
45+
* @param orderStatusRequest order status request options/filters
46+
* @return an order
47+
*/
48+
Order getOrderStatus(OrderStatusRequest orderStatusRequest);
49+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.binance.api.client;
2+
3+
/**
4+
* @author Mahdi Sheikh Hosseini
5+
*/
6+
public enum BinanceEngineType {
7+
SPOT,
8+
FUTURES
9+
}

src/main/java/com/binance/api/client/config/BinanceApiConfig.java

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,61 @@
55
*/
66
public class BinanceApiConfig {
77

8-
/**
9-
* Base domain for URLs.
10-
*/
11-
private static String BASE_DOMAIN = "binance.com";
12-
13-
/**
14-
* Set the URL base domain name (e.g., binance.com).
15-
*
16-
* @param baseDomain Base domain name
17-
*/
18-
public static void setBaseDomain(final String baseDomain) {
19-
BASE_DOMAIN = baseDomain;
20-
}
21-
22-
/**
23-
* Get the URL base domain name (e.g., binance.com).
24-
*
25-
* @return The base domain for URLs
26-
*/
27-
public static String getBaseDomain() {
28-
return BASE_DOMAIN;
29-
}
30-
31-
/**
32-
* REST API base URL.
33-
*/
34-
public static String getApiBaseUrl() {
35-
return String.format("https://api.%s", getBaseDomain());
36-
}
37-
38-
/**
39-
* Streaming API base URL.
40-
*/
41-
public static String getStreamApiBaseUrl() {
42-
return String.format("wss://stream.%s:9443/ws", getBaseDomain());
43-
}
44-
45-
/**
46-
* Asset info base URL.
47-
*/
48-
public static String getAssetInfoApiBaseUrl() {
49-
return String.format("https://%s/", getBaseDomain());
50-
}
8+
/**
9+
* Base domain for URLs.
10+
*/
11+
private static String BASE_DOMAIN = "binance.com";
5112

13+
/**
14+
* Set the URL base domain name (e.g., binance.com).
15+
*
16+
* @param baseDomain Base domain name
17+
*/
18+
public static void setBaseDomain(final String baseDomain) {
19+
BASE_DOMAIN = baseDomain;
20+
}
21+
22+
/**
23+
* Get the URL base domain name (e.g., binance.com).
24+
*
25+
* @return The base domain for URLs
26+
*/
27+
public static String getBaseDomain() {
28+
return BASE_DOMAIN;
29+
}
30+
31+
/**
32+
* REST API base URL.
33+
*/
34+
public static String getApiBaseUrl() {
35+
return String.format("https://api.%s", getBaseDomain());
36+
}
37+
38+
/**
39+
* Streaming API base URL.
40+
*/
41+
public static String getStreamApiBaseUrl() {
42+
return String.format("wss://stream.%s:9443/ws", getBaseDomain());
43+
}
44+
45+
/**
46+
* Asset info base URL.
47+
*/
48+
public static String getAssetInfoApiBaseUrl() {
49+
return String.format("https://%s/", getBaseDomain());
50+
}
51+
52+
/**
53+
* Futures REST API base URL.
54+
*/
55+
public static String getFuturesApiBaseUrl() {
56+
return String.format("https://fapi.%s", getBaseDomain());
57+
}
58+
59+
/**
60+
* Futures Streaming API base URL.
61+
*/
62+
public static String getFuturesStreamApiBaseUrl() {
63+
return String.format("wss://fstream.%s/ws", getBaseDomain());
64+
}
5265
}

0 commit comments

Comments
 (0)