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

Commit

Permalink
Merge pull request #122 from nilswxa/master
Browse files Browse the repository at this point in the history
Support newOrderRespType and fix onFailure routing in BinanceApiCallbackAdapter.
  • Loading branch information
joaopsilva authored May 4, 2018
2 parents bf55921 + 16e94f3 commit eb1ff5c
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ System.out.println(order.getExecutedQty());

#### Placing a MARKET order
```java
NewOrderResponse newOrderResponse = client.newOrder(marketBuy("LINKETH", "1000"));
NewOrderResponse newOrderResponse = client.newOrder(marketBuy("LINKETH", "1000").orderRespType(OrderResponseType.FULL));
List<Trade> fills = newOrderResponse.getFills();
System.out.println(newOrderResponse.getClientOrderId());
```
<details>
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/binance/api/client/domain/account/NewOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class NewOrder {
*/
private String icebergQty;

/**
* Set the response JSON. ACK, RESULT, or FULL; default: RESULT.
*/
private NewOrderResponseType newOrderRespType;

/**
* Receiving window.
*/
Expand All @@ -75,6 +80,7 @@ public NewOrder(String symbol, OrderSide side, OrderType type, TimeInForce timeI
this.type = type;
this.timeInForce = timeInForce;
this.quantity = quantity;
this.newOrderRespType = NewOrderResponseType.RESULT;
this.timestamp = System.currentTimeMillis();
this.recvWindow = BinanceApiConstants.DEFAULT_RECEIVING_WINDOW;
}
Expand Down Expand Up @@ -168,6 +174,15 @@ public NewOrder icebergQty(String icebergQty) {
return this;
}

public NewOrderResponseType getNewOrderRespType() {
return newOrderRespType;
}

public NewOrder newOrderRespType(NewOrderResponseType newOrderRespType) {
this.newOrderRespType = newOrderRespType;
return this;
}

public Long getRecvWindow() {
return recvWindow;
}
Expand Down Expand Up @@ -234,6 +249,7 @@ public String toString() {
.append("newClientOrderId", newClientOrderId)
.append("stopPrice", stopPrice)
.append("icebergQty", icebergQty)
.append("newOrderRespType", newOrderRespType)
.append("recvWindow", recvWindow)
.append("timestamp", timestamp)
.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.binance.api.client.domain.account;

import com.binance.api.client.constant.BinanceApiConstants;
import com.binance.api.client.domain.OrderSide;
import com.binance.api.client.domain.OrderStatus;
import com.binance.api.client.domain.OrderType;
import com.binance.api.client.domain.TimeInForce;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.List;
import java.util.stream.Collectors;

/**
* Response returned when placing a new order on the system.
*
Expand All @@ -28,6 +35,22 @@ public class NewOrderResponse {
*/
private String clientOrderId;

private String price;

private String origQty;

private String executedQty;

private OrderStatus status;

private TimeInForce timeInForce;

private OrderType type;

private OrderSide side;

private List<Trade> fills;

/**
* Transact time for this order.
*/
Expand Down Expand Up @@ -65,13 +88,85 @@ public void setTransactTime(Long transactTime) {
this.transactTime = transactTime;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public String getOrigQty() {
return origQty;
}

public void setOrigQty(String origQty) {
this.origQty = origQty;
}

public String getExecutedQty() {
return executedQty;
}

public void setExecutedQty(String executedQty) {
this.executedQty = executedQty;
}

public OrderStatus getStatus() {
return status;
}

public void setStatus(OrderStatus status) {
this.status = status;
}

public TimeInForce getTimeInForce() {
return timeInForce;
}

public void setTimeInForce(TimeInForce timeInForce) {
this.timeInForce = timeInForce;
}

public OrderType getType() {
return type;
}

public void setType(OrderType type) {
this.type = type;
}

public OrderSide getSide() {
return side;
}

public void setSide(OrderSide side) {
this.side = side;
}

public List<Trade> getFills() {
return fills;
}

public void setFills(List<Trade> fills) {
this.fills = fills;
}

@Override
public String toString() {
return new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE)
.append("symbol", symbol)
.append("orderId", orderId)
.append("clientOrderId", clientOrderId)
.append("transactTime", transactTime)
.append("price", price)
.append("origQty", origQty)
.append("executedQty", executedQty)
.append("status", status)
.append("timeInForce", timeInForce)
.append("type", type)
.append("side", side)
.append("fills", fills.stream().map(Object::toString).collect(Collectors.joining(", ")))
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.binance.api.client.domain.account;

/**
* Desired response type of NewOrder requests.
* @see NewOrderResponse
*/
public enum NewOrderResponseType {
ACK,
RESULT,
FULL
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.binance.api.client.constant.BinanceApiConstants;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
Expand Down Expand Up @@ -54,10 +55,18 @@ public Long getId() {
return id;
}

@JsonSetter("id")
public void setId(Long id) {
this.id = id;
}

@JsonSetter("tradeId")
public void setTradeId(Long id) {
if (this.id == null) {
setId(id);
}
}

public String getPrice() {
return price;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ public void getBookTickers(BinanceApiCallback<List<BookTicker>> callback) {
public void newOrder(NewOrder order, BinanceApiCallback<NewOrderResponse> callback) {
binanceApiService.newOrder(order.getSymbol(), order.getSide(), order.getType(),
order.getTimeInForce(), order.getQuantity(), order.getPrice(), order.getNewClientOrderId(), order.getStopPrice(),
order.getIcebergQty(), order.getRecvWindow(), order.getTimestamp()).enqueue(new BinanceApiCallbackAdapter<>(callback));
order.getIcebergQty(), order.getNewOrderRespType(), order.getRecvWindow(), order.getTimestamp()).enqueue(new BinanceApiCallbackAdapter<>(callback));
}

@Override
public void newOrderTest(NewOrder order, BinanceApiCallback<Void> callback) {
binanceApiService.newOrderTest(order.getSymbol(), order.getSide(), order.getType(),
order.getTimeInForce(), order.getQuantity(), order.getPrice(), order.getNewClientOrderId(), order.getStopPrice(),
order.getIcebergQty(), order.getRecvWindow(), order.getTimestamp()).enqueue(new BinanceApiCallbackAdapter<>(callback));
order.getIcebergQty(), order.getNewOrderRespType(), order.getRecvWindow(), order.getTimestamp()).enqueue(new BinanceApiCallbackAdapter<>(callback));
}

// Account endpoints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ public void onResponse(Call<T> call, Response<T> response) {
}
try {
BinanceApiError apiError = getBinanceApiError(response);
throw new BinanceApiException(apiError);
onFailure(call, new BinanceApiException(apiError));
} catch (IOException e) {
throw new BinanceApiException(e);
onFailure(call, new BinanceApiException(e));
}
}
}

@Override
public void onFailure(Call<T> call, Throwable throwable) {
throw new BinanceApiException(throwable);
if (throwable instanceof BinanceApiException) {
callback.onFailure(throwable);
} else {
callback.onFailure(new BinanceApiException(throwable));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ public List<BookTicker> getBookTickers() {
public NewOrderResponse newOrder(NewOrder order) {
return executeSync(binanceApiService.newOrder(order.getSymbol(), order.getSide(), order.getType(),
order.getTimeInForce(), order.getQuantity(), order.getPrice(), order.getNewClientOrderId(), order.getStopPrice(),
order.getIcebergQty(), order.getRecvWindow(), order.getTimestamp()));
order.getIcebergQty(), order.getNewOrderRespType(), order.getRecvWindow(), order.getTimestamp()));
}

@Override
public void newOrderTest(NewOrder order) {
executeSync(binanceApiService.newOrderTest(order.getSymbol(), order.getSide(), order.getType(),
order.getTimeInForce(), order.getQuantity(), order.getPrice(), order.getNewClientOrderId(), order.getStopPrice(),
order.getIcebergQty(), order.getRecvWindow(), order.getTimestamp()));
order.getIcebergQty(), order.getNewOrderRespType(), order.getRecvWindow(), order.getTimestamp()));
}

// Account endpoints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.binance.api.client.domain.account.DepositAddress;
import com.binance.api.client.domain.account.DepositHistory;
import com.binance.api.client.domain.account.NewOrderResponse;
import com.binance.api.client.domain.account.NewOrderResponseType;
import com.binance.api.client.domain.account.Order;
import com.binance.api.client.domain.account.Trade;
import com.binance.api.client.domain.account.TradeHistoryItem;
Expand Down Expand Up @@ -95,14 +96,16 @@ Call<List<Candlestick>> getCandlestickBars(@Query("symbol") String symbol, @Quer
Call<NewOrderResponse> newOrder(@Query("symbol") String symbol, @Query("side") OrderSide side, @Query("type") OrderType type,
@Query("timeInForce") TimeInForce timeInForce, @Query("quantity") String quantity, @Query("price") String price,
@Query("newClientOrderId") String newClientOrderId, @Query("stopPrice") String stopPrice,
@Query("icebergQty") String icebergQty, @Query("recvWindow") Long recvWindow, @Query("timestamp") Long timestamp);
@Query("icebergQty") String icebergQty, @Query("newOrderRespType") NewOrderResponseType newOrderRespType,
@Query("recvWindow") Long recvWindow, @Query("timestamp") Long timestamp);

@Headers(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_SIGNED_HEADER)
@POST("/api/v3/order/test")
Call<Void> newOrderTest(@Query("symbol") String symbol, @Query("side") OrderSide side, @Query("type") OrderType type,
@Query("timeInForce") TimeInForce timeInForce, @Query("quantity") String quantity, @Query("price") String price,
@Query("newClientOrderId") String newClientOrderId, @Query("stopPrice") String stopPrice,
@Query("icebergQty") String icebergQty, @Query("recvWindow") Long recvWindow, @Query("timestamp") Long timestamp);
@Query("icebergQty") String icebergQty, @Query("newOrderRespType") NewOrderResponseType newOrderRespType,
@Query("recvWindow") Long recvWindow, @Query("timestamp") Long timestamp);

@Headers(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_SIGNED_HEADER)
@GET("/api/v3/order")
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/binance/api/examples/OrdersExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.binance.api.client.BinanceApiRestClient;
import com.binance.api.client.domain.TimeInForce;
import com.binance.api.client.domain.account.NewOrderResponse;
import com.binance.api.client.domain.account.NewOrderResponseType;
import com.binance.api.client.domain.account.Order;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
Expand Down Expand Up @@ -51,7 +52,7 @@ public static void main(String[] args) {
client.newOrderTest(marketBuy("LINKETH", "1000"));

// Placing a real LIMIT order
NewOrderResponse newOrderResponse = client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001"));
NewOrderResponse newOrderResponse = client.newOrder(limitBuy("LINKETH", TimeInForce.GTC, "1000", "0.0001").newOrderRespType(NewOrderResponseType.FULL));
System.out.println(newOrderResponse);
}

Expand Down

0 comments on commit eb1ff5c

Please sign in to comment.