Search in sources :

Example 1 with Instrument

use of org.knowm.xchange.instrument.Instrument in project XChange by knowm.

the class OkexTradeService method getOrder.

public Order getOrder(OrderQueryParams orderQueryParams) throws IOException {
    Order result = null;
    if (orderQueryParams instanceof OrderQueryParamInstrument) {
        Instrument instrument = ((OrderQueryParamInstrument) orderQueryParams).getInstrument();
        String orderId = orderQueryParams.getOrderId();
        List<OkexOrderDetails> orderResults = getOkexOrder(OkexAdapters.adaptInstrumentId(instrument), orderId).getData();
        if (!orderResults.isEmpty()) {
            result = OkexAdapters.adaptOrder(orderResults.get(0));
        }
    } else {
        throw new IOException("OrderQueryParams must implement OrderQueryParamInstrument interface.");
    }
    return result;
}
Also used : Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) OkexOrderDetails(org.knowm.xchange.okex.v5.dto.trade.OkexOrderDetails) OpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamInstrument) CancelOrderByInstrument(org.knowm.xchange.service.trade.params.CancelOrderByInstrument) TradeHistoryParamInstrument(org.knowm.xchange.service.trade.params.TradeHistoryParamInstrument) OrderQueryParamInstrument(org.knowm.xchange.service.trade.params.orders.OrderQueryParamInstrument) Instrument(org.knowm.xchange.instrument.Instrument) IOException(java.io.IOException) OrderQueryParamInstrument(org.knowm.xchange.service.trade.params.orders.OrderQueryParamInstrument)

Example 2 with Instrument

use of org.knowm.xchange.instrument.Instrument in project XChange by knowm.

the class InstrumentDeserializer method deserialize.

@Override
public Instrument deserialize(JsonParser jsonParser, final DeserializationContext ctxt) throws IOException {
    final ObjectCodec oc = jsonParser.getCodec();
    final JsonNode node = oc.readTree(jsonParser);
    final String instrumentString = node.asText();
    long count = instrumentString.chars().filter(ch -> ch == '/').count();
    // CurrencyPair (Base/Counter) i.e. BTC/USD
    if (count == 1)
        return new CurrencyPair(instrumentString);
    // Futures/Swaps (Base/Counter/Prompt) i.e. BTC/USD/200925
    if (count == 2)
        return new FuturesContract(instrumentString);
    // Options (Base/Counter/Prompt/StrikePrice/Put?Call) i.e. BTC/USD/200925/8956.67/P
    if (count == 4)
        return new OptionsContract(instrumentString);
    else
        return null;
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) FuturesContract(org.knowm.xchange.derivative.FuturesContract) JsonParser(com.fasterxml.jackson.core.JsonParser) Instrument(org.knowm.xchange.instrument.Instrument) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) OptionsContract(org.knowm.xchange.derivative.OptionsContract) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) FuturesContract(org.knowm.xchange.derivative.FuturesContract) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) OptionsContract(org.knowm.xchange.derivative.OptionsContract)

Example 3 with Instrument

use of org.knowm.xchange.instrument.Instrument in project XChange by knowm.

the class DeribitTradeService method getTradeHistory.

@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {
    String instrumentName = null;
    if (params instanceof TradeHistoryParamInstrument) {
        Instrument instrument = ((TradeHistoryParamInstrument) params).getInstrument();
        if (instrument != null) {
            instrumentName = DeribitAdapters.adaptInstrumentName(instrument);
        }
    }
    String currency = null;
    // not implemented
    Kind kind = null;
    if (params instanceof TradeHistoryParamCurrencyPair) {
        CurrencyPair currencyPair = ((TradeHistoryParamCurrencyPair) params).getCurrencyPair();
        if (currencyPair != null) {
            currency = currencyPair.base.getCurrencyCode();
        }
    }
    Date startTime = null;
    Date endTime = null;
    if (params instanceof TradeHistoryParamsTimeSpan) {
        startTime = ((TradeHistoryParamsTimeSpan) params).getStartTime();
        endTime = ((TradeHistoryParamsTimeSpan) params).getEndTime();
    }
    String startId = null;
    String endId = null;
    if (params instanceof TradeHistoryParamsIdSpan) {
        startId = ((TradeHistoryParamsIdSpan) params).getStartId();
        endId = ((TradeHistoryParamsIdSpan) params).getEndId();
    }
    Integer limit = null;
    if (params instanceof TradeHistoryParamLimit) {
        limit = ((TradeHistoryParamLimit) params).getLimit();
    }
    String sorting = null;
    if (params instanceof TradeHistoryParamsSorted) {
        TradeHistoryParamsSorted.Order order = ((TradeHistoryParamsSorted) params).getOrder();
        sorting = order == TradeHistoryParamsSorted.Order.asc ? "asc" : order == TradeHistoryParamsSorted.Order.desc ? "desc" : null;
    }
    Boolean includeOld = null;
    if (params instanceof DeribitTradeHistoryParamsOld) {
        includeOld = ((DeribitTradeHistoryParamsOld) params).isIncludeOld();
    }
    org.knowm.xchange.deribit.v2.dto.trade.UserTrades userTrades = null;
    if (startTime != null && endTime != null) {
        if (instrumentName != null) {
            userTrades = super.getUserTradesByInstrumentAndTime(instrumentName, startTime, endTime, limit, includeOld, sorting);
        } else if (currency != null) {
            userTrades = super.getUserTradesByCurrencyAndTime(currency, kind, startTime, endTime, limit, includeOld, sorting);
        }
    } else {
        if (instrumentName != null) {
            Integer startSeq = startId != null ? Integer.valueOf(startId) : null;
            Integer endSeq = endId != null ? Integer.valueOf(endId) : null;
            userTrades = super.getUserTradesByInstrument(instrumentName, startSeq, endSeq, limit, includeOld, sorting);
        } else if (currency != null) {
            userTrades = super.getUserTradesByCurrency(currency, kind, startId, endId, limit, includeOld, sorting);
        }
    }
    if (userTrades == null) {
        throw new ExchangeException("You should specify either instrument or currency pair");
    }
    return DeribitAdapters.adaptUserTrades(userTrades);
}
Also used : TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) DeribitExchange(org.knowm.xchange.deribit.v2.DeribitExchange) TradeHistoryParamsIdSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsIdSpan) Date(java.util.Date) TradeHistoryParamInstrument(org.knowm.xchange.service.trade.params.TradeHistoryParamInstrument) Kind(org.knowm.xchange.deribit.v2.dto.Kind) OpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamInstrument) DefaultOpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument) TradeHistoryParamInstrument(org.knowm.xchange.service.trade.params.TradeHistoryParamInstrument) Instrument(org.knowm.xchange.instrument.Instrument) TradeHistoryParamsSorted(org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted) NotYetImplementedForExchangeException(org.knowm.xchange.exceptions.NotYetImplementedForExchangeException) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 4 with Instrument

use of org.knowm.xchange.instrument.Instrument in project XChange by knowm.

the class DeribitTradeService method getOpenOrders.

@Override
public OpenOrders getOpenOrders(OpenOrdersParams params) throws IOException {
    List<org.knowm.xchange.deribit.v2.dto.trade.Order> openOrders;
    if (params instanceof OpenOrdersParamCurrencyPair) {
        OpenOrdersParamCurrencyPair pairParams = (OpenOrdersParamCurrencyPair) params;
        CurrencyPair pair = pairParams.getCurrencyPair();
        openOrders = super.getOpenOrdersByCurrency(pair.base.getCurrencyCode(), null, null);
    } else if (params instanceof OpenOrdersParamInstrument) {
        OpenOrdersParamInstrument instrumentParams = (OpenOrdersParamInstrument) params;
        Instrument instrument = instrumentParams.getInstrument();
        openOrders = super.getOpenOrdersByInstrument(DeribitAdapters.adaptInstrumentName(instrument), null);
    } else {
        openOrders = openOrders();
    }
    return DeribitAdapters.adaptOpenOrders(openOrders);
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) OpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamInstrument) DefaultOpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument) OpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamInstrument) DefaultOpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument) TradeHistoryParamInstrument(org.knowm.xchange.service.trade.params.TradeHistoryParamInstrument) Instrument(org.knowm.xchange.instrument.Instrument) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 5 with Instrument

use of org.knowm.xchange.instrument.Instrument in project XChange by knowm.

the class DeribitAdapters method adaptOrderBook.

public static OrderBook adaptOrderBook(DeribitOrderBook deribitOrderBook) {
    Instrument instrument = adaptInstrument(deribitOrderBook.getInstrumentName());
    List<LimitOrder> bids = adaptOrdersList(deribitOrderBook.getBids(), Order.OrderType.BID, instrument);
    List<LimitOrder> asks = adaptOrdersList(deribitOrderBook.getAsks(), Order.OrderType.ASK, instrument);
    return new OrderBook(deribitOrderBook.getTimestamp(), asks, bids);
}
Also used : OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) DeribitOrderBook(org.knowm.xchange.deribit.v2.dto.marketdata.DeribitOrderBook) DeribitInstrument(org.knowm.xchange.deribit.v2.dto.marketdata.DeribitInstrument) Instrument(org.knowm.xchange.instrument.Instrument) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder)

Aggregations

Instrument (org.knowm.xchange.instrument.Instrument)13 Test (org.junit.Test)5 FuturesContract (org.knowm.xchange.derivative.FuturesContract)5 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)4 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)4 Order (org.knowm.xchange.dto.Order)3 TradeHistoryParamInstrument (org.knowm.xchange.service.trade.params.TradeHistoryParamInstrument)3 OpenOrdersParamInstrument (org.knowm.xchange.service.trade.params.orders.OpenOrdersParamInstrument)3 IOException (java.io.IOException)2 Date (java.util.Date)2 DeribitInstrument (org.knowm.xchange.deribit.v2.dto.marketdata.DeribitInstrument)2 DeribitOrderBook (org.knowm.xchange.deribit.v2.dto.marketdata.DeribitOrderBook)2 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)2 MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)2 ExchangeException (org.knowm.xchange.exceptions.ExchangeException)2 TradeHistoryParamCurrencyPair (org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)2 DefaultOpenOrdersParamInstrument (org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument)2 OpenOrdersParamCurrencyPair (org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)1