Skip to content

Commit 5ba2583

Browse files
authored
Merge pull request #5168 from ivans083/kucoin-stoporders
[Kucoin] Fixed stop orders and use new HF endpoint for other order types
2 parents 8b12d35 + e46c1c2 commit 5ba2583

File tree

7 files changed

+81
-48
lines changed

7 files changed

+81
-48
lines changed

xchange-examples/src/main/java/org/knowm/xchange/examples/kucoin/trade/KucoinTradeRawDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ private static void raw(KucoinTradeServiceRaw tradeService) throws Exception {
209209

210210
OrderCreateApiRequest limitOrder =
211211
OrderCreateApiRequest.builder()
212-
.size(AMOUNT)
213-
.price(PRICE)
212+
.size(AMOUNT.toPlainString())
213+
.price(PRICE.toPlainString())
214214
.side("sell")
215215
.symbol(SYMBOL)
216216
.type("limit")

xchange-kucoin/src/main/java/org/knowm/xchange/kucoin/KucoinAdapters.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public static UserTrade adaptHistOrder(HistOrdersResponse histOrder) {
292292
public static OrderCreateApiRequest adaptLimitOrder(LimitOrder limitOrder) {
293293
return ((OrderCreateApiRequest.OrderCreateApiRequestBuilder) adaptOrder(limitOrder))
294294
.type("limit")
295-
.price(limitOrder.getLimitPrice())
295+
.price(limitOrder.getLimitPrice().toPlainString())
296296
.postOnly(limitOrder.hasFlag(POST_ONLY))
297297
.hidden(limitOrder.hasFlag(HIDDEN))
298298
.iceberg(limitOrder.hasFlag(ICEBERG))
@@ -302,9 +302,9 @@ public static OrderCreateApiRequest adaptLimitOrder(LimitOrder limitOrder) {
302302
public static OrderCreateApiRequest adaptStopOrder(StopOrder stopOrder) {
303303
return ((OrderCreateApiRequest.OrderCreateApiRequestBuilder) adaptOrder(stopOrder))
304304
.type(stopOrder.getLimitPrice() == null ? "market" : "limit")
305-
.price(stopOrder.getLimitPrice())
305+
.price(stopOrder.getLimitPrice().toPlainString())
306306
.stop(stopOrder.getType().equals(ASK) ? "loss" : "entry")
307-
.stopPrice(stopOrder.getStopPrice())
307+
.stopPrice(stopOrder.getStopPrice().toPlainString())
308308
.build();
309309
}
310310

@@ -316,11 +316,11 @@ public static OrderCreateApiRequest adaptMarketOrder(MarketOrder marketOrder) {
316316
// on buy order amount corresponds to counter currency
317317
if (marketOrder.getType() == BID) {
318318
builder.size(null);
319-
builder.funds(marketOrder.getOriginalAmount());
319+
builder.funds(marketOrder.getOriginalAmount().toPlainString());
320320
}
321321
// on sell order amount corresponds to base currency
322322
else if (marketOrder.getType() == ASK) {
323-
builder.size(marketOrder.getOriginalAmount());
323+
builder.size(marketOrder.getOriginalAmount().toPlainString());
324324
builder.funds(null);
325325
}
326326

@@ -353,7 +353,7 @@ private static Object adaptOrder(Order order) {
353353
}
354354
return request
355355
.symbol(adaptCurrencyPair((CurrencyPair) order.getInstrument()))
356-
.size(order.getOriginalAmount())
356+
.size(order.getOriginalAmount().toPlainString())
357357
.side(adaptSide(order.getType()));
358358
}
359359

xchange-kucoin/src/main/java/org/knowm/xchange/kucoin/KucoinTradeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public String placeMarketOrder(MarketOrder marketOrder) throws IOException {
222222

223223
@Override
224224
public String placeStopOrder(StopOrder stopOrder) throws IOException {
225-
return kucoinCreateOrder(KucoinAdapters.adaptStopOrder(stopOrder)).getOrderId();
225+
return kucoinCreateStopOrder(KucoinAdapters.adaptStopOrder(stopOrder)).getOrderId();
226226
}
227227

228228
private OpenOrders convertOpenOrders(Collection<OrderResponse> orders, OpenOrdersParams params) {

xchange-kucoin/src/main/java/org/knowm/xchange/kucoin/KucoinTradeServiceRaw.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ public OrderCreateResponse kucoinCreateOrder(OrderCreateApiRequest opsRequest)
114114
() -> orderApi.createOrder(apiKey, digest, nonceFactory, passphrase, opsRequest));
115115
}
116116

117+
public OrderCreateResponse kucoinCreateStopOrder(OrderCreateApiRequest opsRequest)
118+
throws IOException {
119+
checkAuthenticated();
120+
return classifyingExceptions(
121+
() -> orderApi.createStopOrder(apiKey, digest, nonceFactory, passphrase, opsRequest));
122+
}
123+
117124
public List<OrderResponse> getKucoinRecentOrders() throws IOException {
118125
this.checkAuthenticated();
119126
return classifyingExceptions(
Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,74 @@
1-
/** Copyright 2019 Mek Global Limited. */
21
package org.knowm.xchange.kucoin.dto.request;
32

43
import java.math.BigDecimal;
54
import lombok.Builder;
65
import lombok.Getter;
76

87
/**
9-
* 订单创建对象
10-
*
11-
* @author 屈亮
12-
* @since 2018-09-17
8+
* Represents a request to create a new order (limit, market, or stop) on KuCoin.
139
*/
1410
@Getter
1511
@Builder
1612
public class OrderCreateApiRequest {
1713

18-
/** a valid trading symbol code. e.g. ETH-BTC */
14+
/** Unique order id created by users to identify their orders, e.g. UUID. */
15+
private final String clientOid;
16+
17+
/** A valid trading symbol code, e.g. ETH-BTC. */
1918
private final String symbol;
2019

21-
/** [optional] limit or market (default is limit) */
22-
@Builder.Default private final String type = "limit";
20+
/** The type of trading, e.g. TRADE (Spot Trade). Default is TRADE. */
21+
@Builder.Default private final String tradeType = "TRADE";
2322

24-
/** buy or sell */
23+
/** buy or sell. */
2524
private final String side;
2625

27-
/** price per base currency */
28-
private final BigDecimal price;
26+
/** limit or market (default is limit). */
27+
@Builder.Default private final String type = "limit";
28+
29+
/** Remark for the order, length cannot exceed 100 utf8 characters. */
30+
private final String remark;
2931

30-
/** amount of base currency to buy or sell */
31-
private final BigDecimal size;
32+
/** Self-trade prevention strategy: CN, CO, CB or DC. */
33+
private final String stp;
3234

33-
/** [optional] Desired amount of quote currency to use */
34-
private final BigDecimal funds;
35+
// Stop-related fields (optional)
3536

36-
/** [optional] self trade protect , CN, CO, CB or DC */
37-
@Builder.Default private final String stp = "";
37+
/** "entry" or "loss" (KuCoin’s stop types). */
38+
private final String stop;
3839

39-
/** [optional] Either loss or entry. Requires stopPrice to be defined */
40-
@Builder.Default private final String stop = "";
40+
/** Trigger price for the stop order. Null for regular orders. */
41+
private final String stopPrice;
4142

42-
/** [optional] Only if stop is defined. Sets trigger price for stop order */
43-
private final BigDecimal stopPrice;
43+
// Common fields
4444

45-
/** [optional] GTC, GTT, IOC, or FOK (default is GTC) */
45+
/** [limit order] Price per base currency. */
46+
private final String price;
47+
48+
/** [limit order] Amount of base currency to buy or sell. */
49+
private final String size;
50+
51+
/** [limit order] Time in force strategy: GTC, GTT, IOC, or FOK (default is GTC). */
4652
@Builder.Default private final String timeInForce = "GTC";
4753

48-
/** [optional] * cancel after n seconds */
49-
private final long cancelAfter;
54+
/** [limit order] Cancel after n seconds, requires timeInForce = GTT. */
55+
private final Long cancelAfter;
5056

51-
/** [optional] ** Post only flag */
57+
/** [limit order] Post only flag. */
5258
private final boolean postOnly;
5359

54-
/** [optional] Orders not displayed in order book */
60+
/** [limit order] Order will not be displayed in the order book. */
5561
private final boolean hidden;
5662

57-
/** [optional] Only visible portion of the order is displayed in the order book */
63+
/** [limit order] Only a portion of the order is displayed in the order book. */
5864
private final boolean iceberg;
5965

60-
/** [optional] The maximum visible size of an iceberg order */
61-
private final BigDecimal visibleSize;
66+
/** [limit order] The maximum visible size of an iceberg order. */
67+
private final String visibleSize;
6268

63-
/** Unique order id selected by you to identify your order e.g. UUID */
64-
private final String clientOid;
65-
66-
/** [optional] remark for the order, length cannot exceed 100 utf8 characters */
67-
private final String remark;
69+
/**
70+
* [market order] The amount of quote currency to spend.
71+
* size and funds are mutually exclusive.
72+
*/
73+
private final String funds;
6874
}

xchange-kucoin/src/main/java/org/knowm/xchange/kucoin/service/OrderAPI.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import si.mazi.rescu.SynchronizedValueFactory;
2323

2424
/** Based on code by chenshiwei on 2019/1/10. */
25-
@Path("/api/v1/orders")
25+
@Path("/api/v1")
2626
@Produces(MediaType.APPLICATION_JSON)
2727
public interface OrderAPI {
2828

@@ -41,6 +41,7 @@ public interface OrderAPI {
4141
* @return A response containing the order id.
4242
*/
4343
@POST
44+
@Path("/hf/orders")
4445
@Consumes(MediaType.APPLICATION_JSON)
4546
KucoinResponse<OrderCreateResponse> createOrder(
4647
@HeaderParam(APIConstants.API_HEADER_KEY) String apiKey,
@@ -50,6 +51,23 @@ KucoinResponse<OrderCreateResponse> createOrder(
5051
OrderCreateApiRequest opsRequest)
5152
throws IOException;
5253

54+
/**
55+
* Place a new stop-order.
56+
*
57+
* @param opsRequest Stop-Order creation request.
58+
* @return A response containing the order id.
59+
*/
60+
@POST
61+
@Path("/stop-order")
62+
@Consumes(MediaType.APPLICATION_JSON)
63+
KucoinResponse<OrderCreateResponse> createStopOrder(
64+
@HeaderParam(APIConstants.API_HEADER_KEY) String apiKey,
65+
@HeaderParam(APIConstants.API_HEADER_SIGN) ParamsDigest signature,
66+
@HeaderParam(APIConstants.API_HEADER_TIMESTAMP) SynchronizedValueFactory<Long> nonce,
67+
@HeaderParam(APIConstants.API_HEADER_PASSPHRASE) String apiPassphrase,
68+
OrderCreateApiRequest opsRequest)
69+
throws IOException;
70+
5371
/**
5472
* Cancel an order
5573
*
@@ -59,7 +77,7 @@ KucoinResponse<OrderCreateResponse> createOrder(
5977
* @return A response containing the id of the cancelled order.
6078
*/
6179
@DELETE
62-
@Path("/{orderId}")
80+
@Path("/orders/{orderId}")
6381
KucoinResponse<OrderCancelResponse> cancelOrder(
6482
@HeaderParam(APIConstants.API_HEADER_KEY) String apiKey,
6583
@HeaderParam(APIConstants.API_HEADER_SIGN) ParamsDigest signature,
@@ -75,6 +93,7 @@ KucoinResponse<OrderCancelResponse> cancelOrder(
7593
* @return A response containing the ids of all open orders.
7694
*/
7795
@DELETE
96+
@Path("/orders")
7897
KucoinResponse<OrderCancelResponse> cancelOrders(
7998
@HeaderParam(APIConstants.API_HEADER_KEY) String apiKey,
8099
@HeaderParam(APIConstants.API_HEADER_SIGN) ParamsDigest signature,
@@ -90,7 +109,7 @@ KucoinResponse<OrderCancelResponse> cancelOrders(
90109
* @return The requested order.
91110
*/
92111
@GET
93-
@Path("/{orderId}")
112+
@Path("/orders/{orderId}")
94113
KucoinResponse<OrderResponse> getOrder(
95114
@HeaderParam(APIConstants.API_HEADER_KEY) String apiKey,
96115
@HeaderParam(APIConstants.API_HEADER_SIGN) ParamsDigest signature,
@@ -116,6 +135,7 @@ KucoinResponse<OrderResponse> getOrder(
116135
* @return A page of orders.
117136
*/
118137
@GET
138+
@Path("/orders")
119139
KucoinResponse<Pagination<OrderResponse>> queryOrders(
120140
@HeaderParam(APIConstants.API_HEADER_KEY) String apiKey,
121141
@HeaderParam(APIConstants.API_HEADER_SIGN) ParamsDigest signature,

xchange-kucoin/src/test/java/org/knowm/xchange/kucoin/KucoinAdaptersTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class KucoinAdaptersTest {
1515
void adapt_market_buy_order() {
1616
OrderCreateApiRequest expected =
1717
OrderCreateApiRequest.builder()
18-
.funds(new BigDecimal("15"))
18+
.funds(new BigDecimal("15").toPlainString())
1919
.clientOid("abc")
2020
.side("buy")
2121
.symbol("BTC-USDT")
@@ -37,7 +37,7 @@ void adapt_market_buy_order() {
3737
void adapt_market_sell_order() {
3838
OrderCreateApiRequest expected =
3939
OrderCreateApiRequest.builder()
40-
.size(new BigDecimal("0.002"))
40+
.size(new BigDecimal("0.002").toPlainString())
4141
.clientOid("abc")
4242
.side("sell")
4343
.symbol("BTC-USDT")

0 commit comments

Comments
 (0)