use of org.knowm.xchange.okcoin.FuturesContract in project XChange by knowm.
the class OkCoinStreamingMarketDataService method getOrderBook.
/**
* #### spot #### 2. ok_sub_spot_X_depth 订阅币币市场深度(200增量数据返回) 3. ok_sub_spot_X_depth_Y 订阅市场深度 ####
* future #### 3. ok_sub_futureusd_X_depth_Y 订阅合约市场深度(200增量数据返回) 3. ok_sub_futureusd_X_depth_Y
* Subscribe Contract Market Depth(Incremental) 4. ok_sub_futureusd_X_depth_Y_Z 订阅合约市场深度(全量返回) 4.
* ok_sub_futureusd_X_depth_Y_Z Subscribe Contract Market Depth(Full)
*
* @param currencyPair Currency pair of the order book
* @param args if the first arg is {@link FuturesContract} means future, the next arg is amount
* @return
*/
@Override
public Observable<OrderBook> getOrderBook(CurrencyPair currencyPair, Object... args) {
String channel = String.format("ok_sub_spot_%s_%s_depth", currencyPair.base.toString().toLowerCase(), currencyPair.counter.toString().toLowerCase());
if (args.length > 0) {
if (args[0] instanceof FuturesContract) {
FuturesContract contract = (FuturesContract) args[0];
channel = String.format("ok_sub_future%s_%s_depth_%s", currencyPair.counter.toString().toLowerCase(), currencyPair.base.toString().toLowerCase(), contract.getName());
if (args.length > 1) {
channel = channel + "_" + args[1];
}
} else {
channel = channel + "_" + args[1];
}
}
final String key = channel;
return service.subscribeChannel(channel).map(s -> {
OkCoinOrderbook okCoinOrderbook;
if (!orderbooks.containsKey(key)) {
OkCoinDepth okCoinDepth = mapper.treeToValue(s.get("data"), OkCoinDepth.class);
okCoinOrderbook = new OkCoinOrderbook(okCoinDepth);
orderbooks.put(key, okCoinOrderbook);
} else {
okCoinOrderbook = orderbooks.get(key);
if (s.get("data").has("asks")) {
if (s.get("data").get("asks").size() > 0) {
BigDecimal[][] askLevels = mapper.treeToValue(s.get("data").get("asks"), BigDecimal[][].class);
okCoinOrderbook.updateLevels(askLevels, Order.OrderType.ASK);
}
}
if (s.get("data").has("bids")) {
if (s.get("data").get("bids").size() > 0) {
BigDecimal[][] bidLevels = mapper.treeToValue(s.get("data").get("bids"), BigDecimal[][].class);
okCoinOrderbook.updateLevels(bidLevels, Order.OrderType.BID);
}
}
}
return OkCoinAdapters.adaptOrderBook(okCoinOrderbook.toOkCoinDepth(s.get("data").get("timestamp").asLong()), currencyPair);
});
}
use of org.knowm.xchange.okcoin.FuturesContract in project XChange by knowm.
the class OkCoinStreamingMarketDataService method getTrades.
/**
* #### spot #### 4. ok_sub_spot_X_deals 订阅成交记录
*
* <p>#### future #### 5. ok_sub_futureusd_X_trade_Y 订阅合约交易信息 5. ok_sub_futureusd_X_trade_Y
* Subscribe Contract Trade Record
*
* @param currencyPair Currency pair of the trades
* @param args the first arg {@link FuturesContract}
* @return
*/
@Override
public Observable<Trade> getTrades(CurrencyPair currencyPair, Object... args) {
String channel = String.format("ok_sub_spot_%s_%s_deals", currencyPair.base.toString().toLowerCase(), currencyPair.counter.toString().toLowerCase());
if (args.length > 0) {
FuturesContract contract = (FuturesContract) args[0];
channel = String.format("ok_sub_future%s_%s_trade_%s", currencyPair.counter.toString().toLowerCase(), currencyPair.base.toString().toLowerCase(), contract.getName());
}
return service.subscribeChannel(channel).map(s -> {
String[][] trades = mapper.treeToValue(s.get("data"), String[][].class);
// I don't know how to parse this array of arrays in Jacson.
OkCoinWebSocketTrade[] okCoinTrades = new OkCoinWebSocketTrade[trades.length];
for (int i = 0; i < trades.length; ++i) {
OkCoinWebSocketTrade okCoinWebSocketTrade = new OkCoinWebSocketTrade(trades[i]);
okCoinTrades[i] = okCoinWebSocketTrade;
}
return OkCoinAdapters.adaptTrades(okCoinTrades, currencyPair);
}).flatMapIterable(Trades::getTrades);
}
use of org.knowm.xchange.okcoin.FuturesContract in project XChange by knowm.
the class OkCoinFuturesTradeService method getOrder.
@Override
public Collection<Order> getOrder(OrderQueryParams... orderQueryParams) throws IOException {
Map<CurrencyPair, Map<FuturesContract, Set<String>>> ordersToQuery = new HashMap<CurrencyPair, Map<FuturesContract, Set<String>>>();
List<String> orderIdsRequest = new ArrayList<>();
List<OkCoinFuturesOrder> orderResults = new ArrayList<>();
List<Order> openOrders = new ArrayList<>();
for (OrderQueryParams orderQueryParam : orderQueryParams) {
OkCoinFuturesOrderQueryParams myParams = (OkCoinFuturesOrderQueryParams) orderQueryParam;
CurrencyPair currencyPair = myParams.getCurrencyPair();
FuturesContract reqFuturesContract = myParams.futuresContract;
long orderId = myParams.getOrderId() != null ? Long.valueOf(myParams.getOrderId()) : -1;
if (ordersToQuery.get(currencyPair) == null) {
Set<String> orderSet = new HashSet<>();
orderSet.add(String.valueOf(orderId));
HashMap<FuturesContract, Set<String>> futuresContractMap = new HashMap<FuturesContract, Set<String>>();
futuresContractMap.put(reqFuturesContract, orderSet);
ordersToQuery.put(currencyPair, futuresContractMap);
} else if (ordersToQuery.get(currencyPair).get(reqFuturesContract) == null) {
Set<String> orderSet = new HashSet<>();
orderSet.add(String.valueOf(orderId));
ordersToQuery.get(currencyPair).put(reqFuturesContract, orderSet);
} else {
ordersToQuery.get(currencyPair).get(reqFuturesContract).add(String.valueOf(orderId));
}
}
for (CurrencyPair pair : ordersToQuery.keySet()) {
for (FuturesContract contract : ordersToQuery.get(pair).keySet()) {
int count = 0;
orderIdsRequest.clear();
for (String order : ordersToQuery.get(pair).get(contract)) {
orderIdsRequest.add(order);
count++;
if (count % batchSize == 0) {
OkCoinFuturesOrderResult orderResult = getFuturesOrders(createDelimitedString(orderIdsRequest.toArray(new String[orderIdsRequest.size()])), OkCoinAdapters.adaptSymbol(pair), contract);
orderIdsRequest.clear();
if (orderResult.getOrders().length > 0) {
orderResults.addAll(new ArrayList<>(Arrays.asList(orderResult.getOrders())));
}
}
}
OkCoinFuturesOrderResult orderResult;
if (!orderIdsRequest.isEmpty()) {
orderResult = getFuturesOrders(createDelimitedString(orderIdsRequest.toArray(new String[orderIdsRequest.size()])), OkCoinAdapters.adaptSymbol(pair), contract);
} else {
orderResult = getFuturesFilledOrder(-1, OkCoinAdapters.adaptSymbol(pair), "0", "50", contract);
}
if (orderResult.getOrders().length > 0) {
for (int o = 0; o < orderResult.getOrders().length; o++) {
OkCoinFuturesOrder singleOrder = orderResult.getOrders()[o];
openOrders.add(OkCoinAdapters.adaptOpenOrderFutures(singleOrder));
}
}
}
}
return openOrders;
}
use of org.knowm.xchange.okcoin.FuturesContract in project XChange by knowm.
the class OkCoinFuturesTradeService method getTradeHistory.
/**
* Parameters: see {@link OkCoinFuturesTradeService.OkCoinFuturesTradeHistoryParams}
*/
@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {
OkCoinFuturesTradeHistoryParams myParams = (OkCoinFuturesTradeHistoryParams) params;
long orderId = myParams.getOrderId() != null ? Long.valueOf(myParams.getOrderId()) : -1;
CurrencyPair currencyPair = myParams.getCurrencyPair();
String date = myParams.getDate();
String page = myParams.getPageNumber().toString();
String pageLength = myParams.getPageLength().toString();
FuturesContract reqFuturesContract = myParams.futuresContract;
OkCoinFuturesTradeHistoryResult[] orderHistory = getFuturesTradesHistory(OkCoinAdapters.adaptSymbol(currencyPair), orderId, date);
return OkCoinAdapters.adaptTradeHistory(orderHistory);
}
use of org.knowm.xchange.okcoin.FuturesContract in project XChange by knowm.
the class OkCoinFuturesTradeService method cancelOrder.
@Override
public boolean cancelOrder(CancelOrderParams orderParams) throws IOException {
OkCoinFuturesCancelOrderParams myParams = (OkCoinFuturesCancelOrderParams) orderParams;
CurrencyPair currencyPair = myParams.getCurrencyPair();
FuturesContract reqFuturesContract = myParams.futuresContract;
long orderId = myParams.getOrderId() != null ? Long.valueOf(myParams.getOrderId()) : -1;
boolean ret = false;
try {
OkCoinTradeResult cancelResult = futuresCancelOrder(orderId, OkCoinAdapters.adaptSymbol(currencyPair), reqFuturesContract);
if (orderId == cancelResult.getOrderId()) {
ret = true;
}
} catch (ExchangeException e) {
if (e.getMessage().equals(OkCoinUtils.getErrorMessage(1009)) || e.getMessage().equals(OkCoinUtils.getErrorMessage(20015))) {
// order not found.
} else
throw e;
}
return ret;
}
Aggregations