Search in sources :

Example 16 with CurrencyPair

use of org.knowm.xchange.currency.CurrencyPair in project XChange by knowm.

the class dydxStreamingService method getRawWebsocketTransactions.

/**
 * Creates an observable of a channel using the baseChannelName and currencyPair. For example,
 * subscribing to the trades channel for WETH/USDC will create a new channel "trades-WETH-USDC".
 *
 * @param currencyPair any currency pair supported by dydx
 * @param baseChannelName e.g. "orderbook", "v3_orderbook", etc.
 * @return
 */
public Observable<dydxWebSocketTransaction> getRawWebsocketTransactions(CurrencyPair currencyPair, String baseChannelName) {
    final ObjectMapper mapper = StreamingObjectMapperHelper.getObjectMapper();
    String currencyPairChannelName = String.format("%s-%s", baseChannelName, currencyPair.toString().replace('/', '-'));
    return subscribeChannel(currencyPairChannelName, currencyPair).map(msg -> {
        switch(baseChannelName) {
            case V1_ORDERBOOK:
            case V3_ORDERBOOK:
                return handleOrderbookMessage(currencyPairChannelName, objectMapper, msg);
        }
        return mapper.readValue(msg.toString(), dydxWebSocketTransaction.class);
    }).filter(t -> currencyPair.equals(new CurrencyPair(t.getId()))).filter(t -> baseChannelName.equals(t.getChannel()));
}
Also used : Logger(org.slf4j.Logger) info.bitrich.xchangestream.dydx.dto.dydxWebSocketTransaction(info.bitrich.xchangestream.dydx.dto.dydxWebSocketTransaction) info.bitrich.xchangestream.dydx.dto.v3.dydxUpdateOrderBookMessage(info.bitrich.xchangestream.dydx.dto.v3.dydxUpdateOrderBookMessage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) ProductSubscription(info.bitrich.xchangestream.core.ProductSubscription) StreamingObjectMapperHelper(info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper) info.bitrich.xchangestream.dydx.dto.dydxWebSocketSubscriptionMessage(info.bitrich.xchangestream.dydx.dto.dydxWebSocketSubscriptionMessage) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonNettyStreamingService(info.bitrich.xchangestream.service.netty.JsonNettyStreamingService) Observable(io.reactivex.Observable) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) info.bitrich.xchangestream.dydx.dto.v3.dydxInitialOrderBookMessage(info.bitrich.xchangestream.dydx.dto.v3.dydxInitialOrderBookMessage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 17 with CurrencyPair

use of org.knowm.xchange.currency.CurrencyPair in project XChange by knowm.

the class FtxStreamingAdapters method adaptOrders.

public static Order adaptOrders(JsonNode jsonNode) {
    JsonNode data = jsonNode.get("data");
    LimitOrder.Builder order = new LimitOrder.Builder("buy".equals(data.get("side").asText()) ? Order.OrderType.BID : Order.OrderType.ASK, new CurrencyPair(data.get("market").asText())).id(data.get("id").asText()).timestamp(Date.from(Instant.now())).limitPrice(data.get("price").decimalValue()).originalAmount(data.get("size").decimalValue()).userReference(data.get("clientId").asText()).remainingAmount(data.get("remainingSize").decimalValue()).averagePrice(data.get("avgFillPrice").decimalValue()).orderStatus(Order.OrderStatus.valueOf(data.get("status").asText().toUpperCase()));
    if (data.get("ioc").asBoolean())
        order.flag(FtxOrderFlags.IOC);
    if (data.get("postOnly").asBoolean())
        order.flag(FtxOrderFlags.POST_ONLY);
    if (data.get("reduceOnly").asBoolean())
        order.flag(FtxOrderFlags.REDUCE_ONLY);
    return order.build();
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 18 with CurrencyPair

use of org.knowm.xchange.currency.CurrencyPair in project XChange by knowm.

the class FtxStreamingMarketDataService method getOrderBook.

@Override
public Observable<OrderBook> getOrderBook(CurrencyPair currencyPair, Object... args) {
    OrderBook orderBook = new OrderBook(null, Lists.newArrayList(), Lists.newArrayList());
    String channelName = "orderbook:" + FtxAdapters.adaptCurrencyPairToFtxMarket(currencyPair);
    return service.subscribeChannel(channelName).map(res -> {
        try {
            return FtxStreamingAdapters.adaptOrderbookMessage(orderBook, currencyPair, res);
        } catch (IllegalStateException e) {
            LOG.warn("Resubscribing {} channel after adapter error {}", currencyPair, e.getMessage());
            orderBook.getBids().clear();
            orderBook.getAsks().clear();
            // Resubscribe to the channel
            this.service.sendMessage(service.getUnsubscribeMessage(channelName, args));
            this.service.sendMessage(service.getSubscribeMessage(channelName, args));
            return new OrderBook(null, Lists.newArrayList(), Lists.newArrayList(), false);
        }
    }).filter(ob -> ob.getBids().size() > 0 && ob.getAsks().size() > 0);
}
Also used : StreamingMarketDataService(info.bitrich.xchangestream.core.StreamingMarketDataService) Lists(com.google.common.collect.Lists) Ticker(org.knowm.xchange.dto.marketdata.Ticker) Logger(org.slf4j.Logger) FtxAdapters(org.knowm.xchange.ftx.FtxAdapters) LoggerFactory(org.slf4j.LoggerFactory) Observable(io.reactivex.Observable) Trade(org.knowm.xchange.dto.marketdata.Trade) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook)

Example 19 with CurrencyPair

use of org.knowm.xchange.currency.CurrencyPair in project XChange by knowm.

the class CoinmateStreamingTradeService method getOrderChanges.

@Override
public Observable<Order> getOrderChanges(CurrencyPair currencyPair, Object... args) {
    String channelName = "private-open_orders-" + coinmateStreamingService.getUserId() + "-" + CoinmateStreamingAdapter.getChannelPostfix(currencyPair);
    ObjectReader reader = StreamingObjectMapperHelper.getObjectMapper().readerFor(CoinmateWebsocketOpenOrder.class);
    return coinmateStreamingService.subscribeChannel(channelName, true).map((message) -> {
        JsonNode payload = message.get("payload");
        List<CoinmateWebsocketOpenOrder> websocketOpenOrders;
        if (payload.isArray()) {
            websocketOpenOrders = Arrays.asList(reader.readValue(payload, CoinmateWebsocketOpenOrder[].class));
        } else {
            websocketOpenOrders = Collections.singletonList(reader.readValue(payload, CoinmateWebsocketOpenOrder.class));
        }
        return CoinmateStreamingAdapter.adaptWebsocketOpenOrders(websocketOpenOrders, currencyPair);
    }).concatMapIterable(OpenOrders::getAllOpenOrders);
}
Also used : Arrays(java.util.Arrays) CoinmateWebsocketOpenOrder(info.bitrich.xchangestream.coinmate.v2.dto.CoinmateWebsocketOpenOrder) UserTrades(org.knowm.xchange.dto.trade.UserTrades) UserTrade(org.knowm.xchange.dto.trade.UserTrade) StreamingObjectMapperHelper(info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) CoinmateWebSocketUserTrade(info.bitrich.xchangestream.coinmate.v2.dto.CoinmateWebSocketUserTrade) Order(org.knowm.xchange.dto.Order) List(java.util.List) JsonNode(com.fasterxml.jackson.databind.JsonNode) Observable(io.reactivex.Observable) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) TypeReference(com.fasterxml.jackson.core.type.TypeReference) StreamingTradeService(info.bitrich.xchangestream.core.StreamingTradeService) Collections(java.util.Collections) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) CoinmateWebsocketOpenOrder(info.bitrich.xchangestream.coinmate.v2.dto.CoinmateWebsocketOpenOrder) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) List(java.util.List) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders)

Example 20 with CurrencyPair

use of org.knowm.xchange.currency.CurrencyPair in project XChange by knowm.

the class CoinmateStreamingTradeService method getUserTrades.

@Override
public Observable<UserTrade> getUserTrades(CurrencyPair currencyPair, Object... args) {
    String channelName = "private-user-trades-" + coinmateStreamingService.getUserId() + "-" + CoinmateStreamingAdapter.getChannelPostfix(currencyPair);
    ObjectReader reader = StreamingObjectMapperHelper.getObjectMapper().readerFor(new TypeReference<List<CoinmateWebSocketUserTrade>>() {
    });
    return coinmateStreamingService.subscribeChannel(channelName, true).map((message) -> {
        List<CoinmateWebSocketUserTrade> webSocketUserTrades = reader.readValue(message.get("payload"));
        return CoinmateStreamingAdapter.adaptWebSocketUserTrades(webSocketUserTrades, currencyPair);
    }).concatMapIterable(UserTrades::getUserTrades);
}
Also used : Arrays(java.util.Arrays) CoinmateWebsocketOpenOrder(info.bitrich.xchangestream.coinmate.v2.dto.CoinmateWebsocketOpenOrder) UserTrades(org.knowm.xchange.dto.trade.UserTrades) UserTrade(org.knowm.xchange.dto.trade.UserTrade) StreamingObjectMapperHelper(info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) CoinmateWebSocketUserTrade(info.bitrich.xchangestream.coinmate.v2.dto.CoinmateWebSocketUserTrade) Order(org.knowm.xchange.dto.Order) List(java.util.List) JsonNode(com.fasterxml.jackson.databind.JsonNode) Observable(io.reactivex.Observable) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) TypeReference(com.fasterxml.jackson.core.type.TypeReference) StreamingTradeService(info.bitrich.xchangestream.core.StreamingTradeService) Collections(java.util.Collections) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) UserTrades(org.knowm.xchange.dto.trade.UserTrades) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) List(java.util.List)

Aggregations

CurrencyPair (org.knowm.xchange.currency.CurrencyPair)518 BigDecimal (java.math.BigDecimal)157 Test (org.junit.Test)125 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)121 Date (java.util.Date)108 ArrayList (java.util.ArrayList)107 Ticker (org.knowm.xchange.dto.marketdata.Ticker)91 Currency (org.knowm.xchange.currency.Currency)88 OrderType (org.knowm.xchange.dto.Order.OrderType)81 Exchange (org.knowm.xchange.Exchange)76 Order (org.knowm.xchange.dto.Order)70 TradeHistoryParamCurrencyPair (org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)60 MarketDataService (org.knowm.xchange.service.marketdata.MarketDataService)58 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)57 UserTrade (org.knowm.xchange.dto.trade.UserTrade)57 List (java.util.List)54 HashMap (java.util.HashMap)53 CurrencyPairMetaData (org.knowm.xchange.dto.meta.CurrencyPairMetaData)53 UserTrades (org.knowm.xchange.dto.trade.UserTrades)51 IOException (java.io.IOException)43