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

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

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

import info.bitrich.xchangestream.krakenfutures.dto.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.marketdata.FundingRate;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trade;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.UserTrade;
import org.knowm.xchange.krakenfutures.KrakenFuturesAdapters;

public class KrakenFuturesStreamingAdapters {

  public static OrderBook adaptKrakenFuturesSnapshot(
      KrakenFuturesStreamingOrderBookSnapshotResponse snapshot) {
    List asks = new ArrayList<>();
    List bids = new ArrayList<>();

    snapshot
        .getBids()
        .forEach(
            krakenFuturesSnapShotOrder ->
                bids.add(
                    new LimitOrder.Builder(
                            Order.OrderType.BID,
                            KrakenFuturesAdapters.adaptInstrument(snapshot.getProduct_id()))
                        .limitPrice(krakenFuturesSnapShotOrder.getPrice())
                        .originalAmount(krakenFuturesSnapShotOrder.getQuantity())
                        .build()));
    snapshot
        .getAsks()
        .forEach(
            krakenFuturesSnapShotOrder ->
                asks.add(
                    new LimitOrder.Builder(
                            Order.OrderType.ASK,
                            KrakenFuturesAdapters.adaptInstrument(snapshot.getProduct_id()))
                        .limitPrice(krakenFuturesSnapShotOrder.getPrice())
                        .originalAmount(krakenFuturesSnapShotOrder.getQuantity())
                        .build()));

    return new OrderBook(snapshot.getTimestamp(), asks, bids);
  }

  public static Ticker adaptTicker(KrakenFuturesStreamingTickerResponse tickerResponse) {
    return new Ticker.Builder()
        .instrument(KrakenFuturesAdapters.adaptInstrument(tickerResponse.getProduct_id()))
        .ask(tickerResponse.getAsk())
        .bid(tickerResponse.getBid())
        .last(tickerResponse.getLast())
        .volume(tickerResponse.getVolume())
        .timestamp(tickerResponse.getTime())
        .quoteVolume(tickerResponse.getVolumeQuote())
        .percentageChange(tickerResponse.getChange())
        .askSize(tickerResponse.getAsk_size())
        .bidSize(tickerResponse.getBid_size())
        .build();
  }

  public static FundingRate adaptFundingRate(KrakenFuturesStreamingTickerResponse tickerResponse) {
    return new FundingRate.Builder()
        .instrument(KrakenFuturesAdapters.adaptInstrument(tickerResponse.getProduct_id()))
        .fundingRate1h(tickerResponse.getRelative_funding_rate())
        .fundingRate8h(
            (tickerResponse.getRelative_funding_rate() == null)
                ? null
                : tickerResponse.getRelative_funding_rate().multiply(BigDecimal.valueOf(8)))
        .fundingRateDate(tickerResponse.getNextFundingRateTime())
        .build();
  }

  public static Trade adaptTrade(KrakenFuturesStreamingTradeResponse trade) {
    return new Trade.Builder()
        .price(trade.getPrice())
        .instrument(KrakenFuturesAdapters.adaptInstrument(trade.getProduct_id()))
        .timestamp(trade.getTime())
        .type(
            (trade
                    .getSide()
                    .equals(
                        KrakenFuturesStreamingOrderBookDeltaResponse.KrakenFuturesStreamingSide
                            .sell)
                ? Order.OrderType.ASK
                : Order.OrderType.BID))
        .id(trade.getUid())
        .originalAmount(trade.getQty())
        .build();
  }

  public static List adaptUserTrades(KrakenFuturesStreamingFillsDeltaResponse fills) {
    List userTrades = new ArrayList<>();

    fills
        .getFills()
        .forEach(
            krakenFuturesStreamingFill ->
                userTrades.add(
                    UserTrade.builder()
                        .price(krakenFuturesStreamingFill.getPrice())
                        .originalAmount(krakenFuturesStreamingFill.getQty())
                        .id(krakenFuturesStreamingFill.getFill_id())
                        .orderId(krakenFuturesStreamingFill.getOrder_id())
                        .orderUserReference(krakenFuturesStreamingFill.getCli_ord_id())
                        .feeCurrency(new Currency(krakenFuturesStreamingFill.getFee_currency()))
                        .feeAmount(krakenFuturesStreamingFill.getFee_paid())
                        .type(
                            (krakenFuturesStreamingFill.isBuy())
                                ? Order.OrderType.BID
                                : Order.OrderType.ASK)
                        .instrument(
                            KrakenFuturesAdapters.adaptInstrument(
                                krakenFuturesStreamingFill.getInstrument()))
                        .timestamp(krakenFuturesStreamingFill.getTime())
                        .build()));

    return userTrades;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy