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

issue #409 API should allow integration with ExecutorService to allow… #410

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.binance.api</groupId>
<artifactId>binance-api-client</artifactId>
<version>1.0.1</version>
<version>1.0.1-el</version>
<licenses>
<license>
<name>The MIT License</name>
Expand Down Expand Up @@ -53,8 +53,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
package com.binance.api.client;

import com.binance.api.client.domain.account.*;
import com.binance.api.client.domain.account.request.*;
import com.binance.api.client.domain.event.ListenKey;
import com.binance.api.client.domain.general.Asset;
import com.binance.api.client.domain.general.ExchangeInfo;
import com.binance.api.client.domain.general.ServerTime;
import com.binance.api.client.domain.market.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
* Binance API facade, supporting asynchronous/non-blocking access Binance's REST API using ExecutorServices to
* handling the request and response threading.
*/
public interface BinanceApiAsyncExecutorRestClient {

// General endpoints

/**
* Test connectivity to the Rest API.
* @return
*/
CompletableFuture<Void> ping(BinanceApiCallback<Void> callback);

/**
* Check server time.
* @return
*/
CompletableFuture<ServerTime> getServerTime(BinanceApiCallback<ServerTime> callback);

/**
* Current exchange trading rules and symbol information
*
* @return
*/
CompletableFuture<ExchangeInfo> getExchangeInfo(BinanceApiCallback<ExchangeInfo> callback);

/**
* ALL supported assets and whether or not they can be withdrawn.
*
* @return
*/
CompletableFuture<List<Asset>> getAllAssets(BinanceApiCallback<List<Asset>> callback);

// Market Data endpoints

/**
* Get order book of a symbol (asynchronous)
*
* @param symbol ticker symbol (e.g. ETHBTC)
* @param limit depth of the order book (max 100)
* @param callback the callback that handles the response
*
* @return
*/
CompletableFuture<OrderBook> getOrderBook(String symbol, Integer limit, BinanceApiCallback<OrderBook> callback);

/**
* Get recent trades (up to last 500). Weight: 1
*
* @param symbol ticker symbol (e.g. ETHBTC)
* @param limit of last trades (Default 500; max 1000.)
* @param callback the callback that handles the response
*
* @return
*/
CompletableFuture<List<TradeHistoryItem>> getTrades(String symbol, Integer limit, BinanceApiCallback<List<TradeHistoryItem>> callback);

/**
* Get older trades. Weight: 5
*
* @param symbol ticker symbol (e.g. ETHBTC)
* @param limit of last trades (Default 500; max 1000.)
* @param fromId TradeId to fetch from. Default gets most recent trades.
* @param callback the callback that handles the response
*
* @return
*/
CompletableFuture<List<TradeHistoryItem>> getHistoricalTrades(String symbol, Integer limit, Long fromId, BinanceApiCallback<List<TradeHistoryItem>> callback);

/**
* Get compressed, aggregate trades. Trades that fill at the time, from the same order, with
* the same price will have the quantity aggregated.
*
* If both <code>startTime</code> and <code>endTime</code> are sent, <code>limit</code>should not
* be sent AND the distance between <code>startTime</code> and <code>endTime</code> must be less than 24 hours.
*
* @param symbol symbol to aggregate (mandatory)
* @param fromId ID to get aggregate trades from INCLUSIVE (optional)
* @param limit Default 500; max 1000 (optional)
* @param startTime Timestamp in ms to get aggregate trades from INCLUSIVE (optional).
* @param endTime Timestamp in ms to get aggregate trades until INCLUSIVE (optional).
* @param callback the callback that handles the response
* @return a list of aggregate trades for the given symbol
*/
CompletableFuture<List<AggTrade>> getAggTrades(String symbol, String fromId, Integer limit, Long startTime, Long endTime, BinanceApiCallback<List<AggTrade>> callback);

/**
* Return the most recent aggregate trades for <code>symbol</code>
*
* @see #getAggTrades(String, String, Integer, Long, Long, BinanceApiCallback)
*/
CompletableFuture<List<AggTrade>> getAggTrades(String symbol, BinanceApiCallback<List<AggTrade>> callback);

/**
* Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.
*
* @param symbol symbol to aggregate (mandatory)
* @param interval candlestick interval (mandatory)
* @param limit Default 500; max 1000 (optional)
* @param startTime Timestamp in ms to get candlestick bars from INCLUSIVE (optional).
* @param endTime Timestamp in ms to get candlestick bars until INCLUSIVE (optional).
* @param callback the callback that handles the response containing a candlestick bar for the given symbol and interval
*/
CompletableFuture<List<Candlestick>> getCandlestickBars(String symbol, CandlestickInterval interval, Integer limit, Long startTime, Long endTime, BinanceApiCallback<List<Candlestick>> callback);

/**
* Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.
*
* @see #getCandlestickBars(String, CandlestickInterval, BinanceApiCallback)
*/
CompletableFuture<List<Candlestick>> getCandlestickBars(String symbol, CandlestickInterval interval, BinanceApiCallback<List<Candlestick>> callback);

/**
* Get 24 hour price change statistics (asynchronous).
*
* @param symbol ticker symbol (e.g. ETHBTC)
* @param callback the callback that handles the response
*/
CompletableFuture<TickerStatistics> get24HrPriceStatistics(String symbol, BinanceApiCallback<TickerStatistics> callback);

/**
* Get 24 hour price change statistics for all symbols (asynchronous).
*
* @param callback the callback that handles the response
*/
CompletableFuture<List<TickerStatistics>> getAll24HrPriceStatistics(BinanceApiCallback<List<TickerStatistics>> callback);

/**
* Get Latest price for all symbols (asynchronous).
*
* @param callback the callback that handles the response
*/
CompletableFuture<List<TickerPrice>> getAllPrices(BinanceApiCallback<List<TickerPrice>> callback);

/**
* Get latest price for <code>symbol</code> (asynchronous).
*
* @param symbol ticker symbol (e.g. ETHBTC)
* @param callback the callback that handles the response
*/
CompletableFuture<TickerPrice> getPrice(String symbol , BinanceApiCallback<TickerPrice> callback);

/**
* Get best price/qty on the order book for all symbols (asynchronous).
*
* @param callback the callback that handles the response
*/
CompletableFuture<List<BookTicker>> getBookTickers(BinanceApiCallback<List<BookTicker>> callback);

// Account endpoints

/**
* Send in a new order (asynchronous)
*
* @param order the new order to submit.
* @param callback the callback that handles the response
*/
CompletableFuture<NewOrderResponse> newOrder(NewOrder order, BinanceApiCallback<NewOrderResponse> callback);

/**
* Test new order creation and signature/recvWindow long. Creates and validates a new order but does not send it into the matching engine.
*
* @param order the new TEST order to submit.
* @param callback the callback that handles the response
*/
CompletableFuture<Void> newOrderTest(NewOrder order, BinanceApiCallback<Void> callback);

/**
* Check an order's status (asynchronous).
*
* @param orderStatusRequest order status request parameters
* @param callback the callback that handles the response
*/
CompletableFuture<Order> getOrderStatus(OrderStatusRequest orderStatusRequest, BinanceApiCallback<Order> callback);

/**
* Cancel an active order (asynchronous).
*
* @param cancelOrderRequest order status request parameters
* @param callback the callback that handles the response
*/
CompletableFuture<CancelOrderResponse> cancelOrder(CancelOrderRequest cancelOrderRequest, BinanceApiCallback<CancelOrderResponse> callback);

/**
* Get all open orders on a symbol (asynchronous).
*
* @param orderRequest order request parameters
* @param callback the callback that handles the response
*/
CompletableFuture<List<Order>> getOpenOrders(OrderRequest orderRequest, BinanceApiCallback<List<Order>> callback);

/**
* Get all account orders; active, canceled, or filled.
*
* @param orderRequest order request parameters
* @param callback the callback that handles the response
*/
CompletableFuture<List<Order>> getAllOrders(AllOrdersRequest orderRequest, BinanceApiCallback<List<Order>> callback);

/**
* Get current account information (async).
*/
CompletableFuture<Account> getAccount(Long recvWindow, Long timestamp, BinanceApiCallback<Account> callback);

/**
* Get current account information using default parameters (async).
*/
CompletableFuture<Account> getAccount(BinanceApiCallback<Account> callback);

/**
* Get trades for a specific account and symbol.
*
* @param symbol symbol to get trades from
* @param limit default 500; max 1000
* @param fromId TradeId to fetch from. Default gets most recent trades.
* @param callback the callback that handles the response with a list of trades
*/
CompletableFuture<List<Trade>> getMyTrades(String symbol, Integer limit, Long fromId, Long recvWindow, Long timestamp, BinanceApiCallback<List<Trade>> callback);

/**
* Get trades for a specific account and symbol.
*
* @param symbol symbol to get trades from
* @param limit default 500; max 1000
* @param callback the callback that handles the response with a list of trades
*/
CompletableFuture<List<Trade>> getMyTrades(String symbol, Integer limit, BinanceApiCallback<List<Trade>> callback);

/**
* Get trades for a specific account and symbol.
*
* @param symbol symbol to get trades from
* @param callback the callback that handles the response with a list of trades
*/
CompletableFuture<List<Trade>> getMyTrades(String symbol, BinanceApiCallback<List<Trade>> callback);

/**
* Submit a withdraw request.
*
* Enable Withdrawals option has to be active in the API settings.
*
* @param asset asset symbol to withdraw
* @param address address to withdraw to
* @param amount amount to withdraw
* @param name description/alias of the address
* @param addressTag Secondary address identifier for coins like XRP,XMR etc.
*/
CompletableFuture<WithdrawResult> withdraw(String asset, String address, String amount, String name, String addressTag, BinanceApiCallback<WithdrawResult> callback);

/**
* Fetch account deposit history.
*
* @param callback the callback that handles the response and returns the deposit history
*/
CompletableFuture<DepositHistory> getDepositHistory(String asset, BinanceApiCallback<DepositHistory> callback);

/**
* Fetch account withdraw history.
*
* @param callback the callback that handles the response and returns the withdraw history
*/
CompletableFuture<WithdrawHistory> getWithdrawHistory(String asset, BinanceApiCallback<WithdrawHistory> callback);

/**
* Fetch deposit address.
*
* @param callback the callback that handles the response and returns the deposit address
*/
CompletableFuture<DepositAddress> getDepositAddress(String asset, BinanceApiCallback<DepositAddress> callback);

// User stream endpoints

/**
* Start a new user data stream.
*
* @param callback the callback that handles the response which contains a listenKey
*/
CompletableFuture<ListenKey> startUserDataStream(BinanceApiCallback<ListenKey> callback);

/**
* PING a user data stream to prevent a time out.
*
* @param listenKey listen key that identifies a data stream
* @param callback the callback that handles the response which contains a listenKey
*/
CompletableFuture<Void> keepAliveUserDataStream(String listenKey, BinanceApiCallback<Void> callback);

/**
* Close out a new user data stream.
*
* @param listenKey listen key that identifies a data stream
* @param callback the callback that handles the response which contains a listenKey
*/
CompletableFuture<Void> closeUserDataStream(String listenKey, BinanceApiCallback<Void> callback);
}
16 changes: 16 additions & 0 deletions src/main/java/com/binance/api/client/BinanceApiClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.binance.api.client.impl.*;
import com.binance.api.client.config.BinanceApiConfig;

import java.util.concurrent.ExecutorService;

import static com.binance.api.client.impl.BinanceApiServiceGenerator.getSharedClient;

/**
Expand Down Expand Up @@ -101,6 +104,19 @@ public BinanceApiRestClient newRestClient() {
return new BinanceApiRestClientImpl(apiKey, secret);
}

/**
* Creates a new asynchronous/non-blocking REST client that uses Executor Services for handling
* the request and response threading.
*
* @param requestService
* @param responseService
* @return
*/
public BinanceApiAsyncExecutorRestClient newAsyncExecutorRestClient(ExecutorService requestService,
ExecutorService responseService) {
return new BinanceApiAsyncExecutorRestClientImpl(newRestClient(), requestService, responseService);
}

/**
* Creates a new asynchronous/non-blocking REST client.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ public static UserDataUpdateEventType fromEventTypeId(String eventTypeId) {
throw new UnsupportedEventException("Unrecognized user data update event type id: " + eventTypeId);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public enum FilterType {
MARKET_LOT_SIZE,
MAX_NUM_ICEBERG_ORDERS,
MAX_POSITION,
TRAILING_DELTA,
PERCENT_PRICE_BY_SIDE,

// Exchange
EXCHANGE_MAX_NUM_ORDERS,
EXCHANGE_MAX_ALGO_ORDERS
EXCHANGE_MAX_ALGO_ORDERS,

NOTIONAL
}
Loading