Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gateio] Implement getting of open orders #4975

Merged
merged 3 commits into from
Dec 7, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,29 @@ public Order toOrder(GateioOrder gateioOrder) {
throw new IllegalArgumentException("Can't map " + gateioOrder.getType());
}

if (orderType == OrderType.BID) {
builder.cumulativeAmount(gateioOrder.getFilledTotalQuote());
} else if (orderType == OrderType.ASK) {
BigDecimal filledAssetAmount =
gateioOrder
.getFilledTotalQuote()
.divide(gateioOrder.getAvgDealPrice(), MathContext.DECIMAL32);
builder.cumulativeAmount(filledAssetAmount);
} else {
throw new IllegalArgumentException("Can't map " + orderType);
// if filled then calculate amounts
OrderStatus status = toOrderStatus(gateioOrder.getStatus());

if (status == OrderStatus.FILLED) {
if (orderType == OrderType.BID) {
builder.cumulativeAmount(gateioOrder.getFilledTotalQuote());
} else if (orderType == OrderType.ASK) {
BigDecimal filledAssetAmount =
gateioOrder
.getFilledTotalQuote()
.divide(gateioOrder.getAvgDealPrice(), MathContext.DECIMAL32);
builder.cumulativeAmount(filledAssetAmount);
} else {
throw new IllegalArgumentException("Can't map " + orderType);
}
}

return builder
.id(gateioOrder.getId())
.originalAmount(gateioOrder.getAmount())
.userReference(gateioOrder.getClientOrderId())
.timestamp(Date.from(gateioOrder.getCreatedAt()))
.orderStatus(toOrderStatus(gateioOrder.getStatus()))
.orderStatus(status)
.averagePrice(gateioOrder.getAvgDealPrice())
.fee(gateioOrder.getFee())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.knowm.xchange.dto.marketdata.Trades.TradeSortType;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.dto.trade.OpenOrders;
import org.knowm.xchange.dto.trade.UserTrade;
import org.knowm.xchange.dto.trade.UserTrades;
import org.knowm.xchange.gateio.GateioAdapters;
Expand All @@ -23,7 +24,9 @@
import org.knowm.xchange.service.trade.TradeService;
import org.knowm.xchange.service.trade.params.CancelOrderParams;
import org.knowm.xchange.service.trade.params.DefaultCancelOrderByInstrumentAndIdParams;
import org.knowm.xchange.service.trade.params.InstrumentParam;
import org.knowm.xchange.service.trade.params.TradeHistoryParams;
import org.knowm.xchange.service.trade.params.orders.OpenOrdersParams;
import org.knowm.xchange.service.trade.params.orders.OrderQueryParamInstrument;
import org.knowm.xchange.service.trade.params.orders.OrderQueryParams;

Expand All @@ -33,6 +36,17 @@ public GateioTradeService(GateioExchange exchange) {
super(exchange);
}

@Override
public OpenOrders getOpenOrders(OpenOrdersParams params) throws IOException {
Validate.isInstanceOf(InstrumentParam.class, params);
Instrument instrument = ((InstrumentParam) params).getInstrument();
List<LimitOrder> limitOrders = listOrders(instrument, OrderStatus.OPEN).stream()
.map(GateioAdapters::toOrder)
.map(LimitOrder.class::cast)
.collect(Collectors.toList());
return new OpenOrders(limitOrders);
}

@Override
public String placeMarketOrder(MarketOrder marketOrder) throws IOException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order.OrderStatus;
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.gateio.GateioExchangeWiremock;
import org.knowm.xchange.gateio.dto.account.GateioOrder;
Expand Down Expand Up @@ -49,16 +47,6 @@ class GateioTradeServiceRawTest extends GateioExchangeWiremock {
.finishAs("filled")
.build();

@Test
void listOrders() throws IOException {
List<GateioOrder> orders =
gateioTradeServiceRaw.listOrders(CurrencyPair.BTC_USDT, OrderStatus.OPEN);

assertThat(orders).hasSize(1);
assertThat(orders.get(0).getId()).isEqualTo("339440374909");
assertThat(orders.get(0).getStatus()).isEqualTo("open");
}

@Test
void valid_market_buy_order() throws IOException {
GateioOrder gateioOrder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.dto.trade.OpenOrders;
import org.knowm.xchange.dto.trade.UserTrade;
import org.knowm.xchange.dto.trade.UserTrades;
import org.knowm.xchange.exceptions.FundsExceededException;
Expand All @@ -24,6 +25,7 @@
import org.knowm.xchange.gateio.dto.trade.Role;
import org.knowm.xchange.gateio.service.params.GateioTradeHistoryParams;
import org.knowm.xchange.service.trade.params.DefaultCancelOrderByInstrumentAndIdParams;
import org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument;
import org.knowm.xchange.service.trade.params.orders.DefaultQueryOrderParamInstrument;

class GateioTradeServiceTest extends GateioExchangeWiremock {
Expand Down Expand Up @@ -142,6 +144,45 @@ void sell_order_details() throws IOException {
assertThat(orders).first().usingRecursiveComparison().isEqualTo(expected);
}

@Test
void open_limit_order_details() throws IOException {
LimitOrder expected =
new LimitOrder.Builder(OrderType.BID, CurrencyPair.BTC_USDT)
.id("745504484392")
.limitPrice(new BigDecimal("80000"))
.timestamp(Date.from(Instant.parse("2024-12-05T23:46:54.447Z")))
.originalAmount(new BigDecimal("0.00012"))
.orderStatus(OrderStatus.OPEN)
.fee(BigDecimal.ZERO)
.userReference("web")
.build();

Collection<Order> orders =
gateioTradeService.getOrder(
new DefaultQueryOrderParamInstrument(CurrencyPair.BTC_USDT, "745504484392"));
assertThat(orders).hasSize(1);
assertThat(orders).first().usingRecursiveComparison().isEqualTo(expected);
}

@Test
void open_orders() throws IOException {
LimitOrder expected =
new LimitOrder.Builder(OrderType.BID, CurrencyPair.BTC_USDT)
.id("745504484392")
.limitPrice(new BigDecimal("80000"))
.timestamp(Date.from(Instant.parse("2024-12-05T23:46:54.447Z")))
.originalAmount(new BigDecimal("0.00012"))
.orderStatus(OrderStatus.OPEN)
.fee(BigDecimal.ZERO)
.userReference("web")
.build();

OpenOrders openOrders =
gateioTradeService.getOpenOrders(new DefaultOpenOrdersParamInstrument(CurrencyPair.BTC_USDT));
assertThat(openOrders.getOpenOrders()).hasSize(1);
assertThat(openOrders.getOpenOrders()).first().usingRecursiveComparison().isEqualTo(expected);
}

@Test
void trade_history() throws IOException {
UserTrades userTrades =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"id": "745504484392",
"text": "web",
"amend_text": "-",
"create_time": "1733442414",
"update_time": "1733442414",
"create_time_ms": 1733442414447,
"update_time_ms": 1733442414447,
"status": "open",
"currency_pair": "BTC_USDT",
"type": "limit",
"account": "spot",
"side": "buy",
"amount": "0.00012",
"price": "80000",
"time_in_force": "gtc",
"iceberg": "0",
"left": "0.00012",
"filled_amount": "0",
"fill_price": "0",
"filled_total": "0",
"fee": "0",
"fee_currency": "BTC",
"point_fee": "0",
"gt_fee": "0",
"gt_maker_fee": "0",
"gt_taker_fee": "0",
"gt_discount": false,
"rebated_fee": "0",
"rebated_fee_currency": "USDT",
"finish_as": "open"
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
[
{
"id": "339440374909",
"text": "3",
"id": "745504484392",
"text": "web",
"amend_text": "-",
"create_time": "1685391045",
"update_time": "1685391045",
"create_time_ms": 1685391045952,
"update_time_ms": 1685391045952,
"create_time": "1733442414",
"update_time": "1733442414",
"create_time_ms": 1733442414447,
"update_time_ms": 1733442414447,
"status": "open",
"currency_pair": "BTC_USDT",
"type": "limit",
"account": "spot",
"side": "buy",
"amount": "0.0001",
"price": "22053",
"biz_info": "-",
"amount": "0.00012",
"price": "80000",
"time_in_force": "gtc",
"iceberg": "0",
"left": "0.0001",
"left": "0.00012",
"filled_amount": "0",
"fill_price": "0",
"filled_total": "0",
"fee": "0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id" : "f8252706-fe0c-4407-b415-02ae3999fc40",
"name" : "api_v4_spot_orders_745504484392",
"request" : {
"url" : "/api/v4/spot/orders/745504484392?currency_pair=BTC_USDT",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "api_v4_spot_orders-open-limit-order.json"
},
"uuid" : "f8252706-fe0c-4407-b415-02ae3999fc40",
"persistent" : true,
"insertionIndex" : 38
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"id" : "8597ffe9-f28d-40d6-9a47-5f378dd53515",
"id" : "703035f1-28e6-4e58-8af5-7a486fe294bb",
"name" : "api_v4_spot_orders",
"request" : {
"url" : "/api/v4/spot/orders?currency_pair=BTC_USDT&status=open",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "api_v4_spot_orders-btc-open.json"
"bodyFileName" : "api_v4_spot_orders.json"
},
"uuid" : "8597ffe9-f28d-40d6-9a47-5f378dd53515",
"uuid" : "703035f1-28e6-4e58-8af5-7a486fe294bb",
"persistent" : true,
"insertionIndex" : 16
"insertionIndex" : 37
}
13 changes: 13 additions & 0 deletions xchange-gateio-v4/src/test/resources/rest/spot.http
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,16 @@ SIGN: {{sign}}
Timestamp: {{timestamp}}
Content-Type: application/json


### Get a single order
< {%
import {gen_sign} from 'sign.js'
gen_sign("GET", request);
%}

GET {{api_v4}}/spot/orders/745504484392?currency_pair=BTC_USDT
KEY: {{api_key}}
SIGN: {{sign}}
Timestamp: {{timestamp}}
Content-Type: application/json

Loading