All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.bitrich.xchangestream.krakenfutures.KrakenFuturesStreamingTradeService Maven / Gradle / Ivy

The newest version!
package info.bitrich.xchangestream.krakenfutures;

import com.fasterxml.jackson.databind.ObjectMapper;
import info.bitrich.xchangestream.core.StreamingTradeService;
import info.bitrich.xchangestream.krakenfutures.dto.KrakenFuturesStreamingFillsDeltaResponse;
import info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper;
import io.reactivex.Observable;
import java.util.List;
import org.knowm.xchange.dto.trade.UserTrade;
import org.knowm.xchange.instrument.Instrument;

public class KrakenFuturesStreamingTradeService implements StreamingTradeService {
  private final ObjectMapper objectMapper = StreamingObjectMapperHelper.getObjectMapper();
  private final Observable> fills;

  public KrakenFuturesStreamingTradeService(KrakenFuturesStreamingService streamingService) {
    fills =
        streamingService
            .subscribeChannel(streamingService.FILLS)
            .filter(message -> message.has("feed") && message.has("fills"))
            .filter(message -> message.get("feed").asText().equals("fills"))
            .map(
                message ->
                    KrakenFuturesStreamingAdapters.adaptUserTrades(
                        objectMapper.treeToValue(
                            message, KrakenFuturesStreamingFillsDeltaResponse.class)));
  }

  @Override
  public Observable getUserTrades(Instrument instrument, Object... args) {
    return fills
        .flatMapIterable(userTrades -> userTrades)
        .filter(userTrade -> userTrade.getInstrument().equals(instrument));
  }

  @Override
  public Observable getUserTrades() {
    return fills.flatMapIterable(userTrades -> userTrades);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy