Search in sources :

Example 1 with FuturesContract

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);
    });
}
Also used : OkCoinDepth(org.knowm.xchange.okcoin.dto.marketdata.OkCoinDepth) FuturesContract(org.knowm.xchange.okcoin.FuturesContract) OkCoinOrderbook(info.bitrich.xchangestream.okcoin.dto.OkCoinOrderbook)

Example 2 with FuturesContract

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);
}
Also used : StreamingMarketDataService(info.bitrich.xchangestream.core.StreamingMarketDataService) Ticker(org.knowm.xchange.dto.marketdata.Ticker) OkCoinTickerResponse(org.knowm.xchange.okcoin.dto.marketdata.OkCoinTickerResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashMap(java.util.HashMap) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) Trades(org.knowm.xchange.dto.marketdata.Trades) StreamingObjectMapperHelper(info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper) OkCoinWebSocketTrade(info.bitrich.xchangestream.okcoin.dto.OkCoinWebSocketTrade) BigDecimal(java.math.BigDecimal) Order(org.knowm.xchange.dto.Order) FuturesContract(org.knowm.xchange.okcoin.FuturesContract) OkCoinAdapters(org.knowm.xchange.okcoin.OkCoinAdapters) Map(java.util.Map) OkCoinOrderbook(info.bitrich.xchangestream.okcoin.dto.OkCoinOrderbook) Observable(io.reactivex.Observable) OkCoinDepth(org.knowm.xchange.okcoin.dto.marketdata.OkCoinDepth) Trade(org.knowm.xchange.dto.marketdata.Trade) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) FutureTicker(info.bitrich.xchangestream.okcoin.dto.marketdata.FutureTicker) OkCoinTicker(org.knowm.xchange.okcoin.dto.marketdata.OkCoinTicker) OkCoinWebSocketTrade(info.bitrich.xchangestream.okcoin.dto.OkCoinWebSocketTrade) Trades(org.knowm.xchange.dto.marketdata.Trades) FuturesContract(org.knowm.xchange.okcoin.FuturesContract)

Example 3 with FuturesContract

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;
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Order(org.knowm.xchange.dto.Order) OkCoinFuturesOrder(org.knowm.xchange.okcoin.dto.trade.OkCoinFuturesOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) FuturesContract(org.knowm.xchange.okcoin.FuturesContract) ArrayList(java.util.ArrayList) OkCoinFuturesOrder(org.knowm.xchange.okcoin.dto.trade.OkCoinFuturesOrder) OkCoinFuturesOrderResult(org.knowm.xchange.okcoin.dto.trade.OkCoinFuturesOrderResult) HashMap(java.util.HashMap) Map(java.util.Map) OrderQueryParams(org.knowm.xchange.service.trade.params.orders.OrderQueryParams) CancelOrderByCurrencyPair(org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair) OrderQueryParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OrderQueryParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) HashSet(java.util.HashSet)

Example 4 with FuturesContract

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);
}
Also used : OkCoinFuturesTradeHistoryResult(org.knowm.xchange.okcoin.dto.trade.OkCoinFuturesTradeHistoryResult) FuturesContract(org.knowm.xchange.okcoin.FuturesContract) CancelOrderByCurrencyPair(org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair) OrderQueryParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OrderQueryParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 5 with FuturesContract

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;
}
Also used : OkCoinTradeResult(org.knowm.xchange.okcoin.dto.trade.OkCoinTradeResult) FuturesContract(org.knowm.xchange.okcoin.FuturesContract) NotYetImplementedForExchangeException(org.knowm.xchange.exceptions.NotYetImplementedForExchangeException) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) CancelOrderByCurrencyPair(org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair) OrderQueryParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OrderQueryParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Aggregations

FuturesContract (org.knowm.xchange.okcoin.FuturesContract)5 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)4 CancelOrderByCurrencyPair (org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair)3 TradeHistoryParamCurrencyPair (org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)3 OrderQueryParamCurrencyPair (org.knowm.xchange.service.trade.params.orders.OrderQueryParamCurrencyPair)3 OkCoinOrderbook (info.bitrich.xchangestream.okcoin.dto.OkCoinOrderbook)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Order (org.knowm.xchange.dto.Order)2 OkCoinDepth (org.knowm.xchange.okcoin.dto.marketdata.OkCoinDepth)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 StreamingMarketDataService (info.bitrich.xchangestream.core.StreamingMarketDataService)1 OkCoinWebSocketTrade (info.bitrich.xchangestream.okcoin.dto.OkCoinWebSocketTrade)1 FutureTicker (info.bitrich.xchangestream.okcoin.dto.marketdata.FutureTicker)1 StreamingObjectMapperHelper (info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper)1 Observable (io.reactivex.Observable)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1