Search in sources :

Example 1 with OptionsContract

use of org.knowm.xchange.derivative.OptionsContract 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 2 with OptionsContract

use of org.knowm.xchange.derivative.OptionsContract in project XChange by knowm.

the class DeribitAdapters method adaptOptionsContract.

public static OptionsContract adaptOptionsContract(DeribitInstrument instrument) {
    CurrencyPair currencyPair = new CurrencyPair(instrument.getBaseCurrency(), IMPLIED_COUNTER);
    Date expireDate = instrument.getExpirationTimestamp();
    String[] parts = instrument.getInstrumentName().split("-");
    if (parts.length != 4) {
        throw new IllegalArgumentException("Could not parse options contract from '" + instrument.getInstrumentName() + "'");
    }
    BigDecimal strike = new BigDecimal(parts[2]);
    OptionsContract.OptionType type = OptionsContract.OptionType.fromString(parts[3]);
    return new OptionsContract.Builder().currencyPair(currencyPair).expireDate(expireDate).strike(strike).type(type).build();
}
Also used : Date(java.util.Date) BigDecimal(java.math.BigDecimal) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) OptionsContract(org.knowm.xchange.derivative.OptionsContract)

Example 3 with OptionsContract

use of org.knowm.xchange.derivative.OptionsContract in project XChange by knowm.

the class DeribitAdaptersTest method adaptTicker.

// @Test
public void adaptTicker() throws IOException {
    // given
    InputStream is = DeribitTrade.class.getResourceAsStream("/org/knowm/xchange/deribit/v2/dto/marketdata/example-ticker.json");
    ObjectMapper mapper = new ObjectMapper();
    DeribitTicker deribitTicker = mapper.readValue(is, DeribitTicker.class);
    // when
    Ticker ticker = DeribitAdapters.adaptTicker(deribitTicker);
    // then
    assertThat(ticker).isNotNull();
    assertThat(ticker.getInstrument()).isEqualTo(new OptionsContract("BTC/USD/190503/5000/P"));
    assertThat(ticker.getOpen()).isEqualTo(new BigDecimal("0.5"));
    assertThat(ticker.getLast()).isEqualTo(new BigDecimal("0.0075"));
    assertThat(ticker.getBid()).isEqualTo(new BigDecimal("0.01"));
    assertThat(ticker.getAsk()).isEqualTo(new BigDecimal("0.0135"));
    assertThat(ticker.getHigh()).isEqualTo(new BigDecimal("0.0625"));
    assertThat(ticker.getLow()).isEqualTo(new BigDecimal("0.0005"));
    assertThat(ticker.getVolume()).isEqualTo(new BigDecimal("0.5"));
    assertThat(ticker.getBidSize()).isEqualTo(new BigDecimal("5"));
    assertThat(ticker.getAskSize()).isEqualTo(new BigDecimal("5"));
    assertThat(ticker.getTimestamp().getTime()).isEqualTo(1556125162701L);
}
Also used : InputStream(java.io.InputStream) DeribitTicker(org.knowm.xchange.deribit.v2.dto.marketdata.DeribitTicker) Ticker(org.knowm.xchange.dto.marketdata.Ticker) DeribitTicker(org.knowm.xchange.deribit.v2.dto.marketdata.DeribitTicker) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BigDecimal(java.math.BigDecimal) OptionsContract(org.knowm.xchange.derivative.OptionsContract)

Example 4 with OptionsContract

use of org.knowm.xchange.derivative.OptionsContract in project XChange by knowm.

the class DeribitExchange method updateExchangeMetaData.

public void updateExchangeMetaData() throws IOException {
    Map<Currency, CurrencyMetaData> currencies = exchangeMetaData.getCurrencies();
    Map<FuturesContract, DerivativeMetaData> futures = exchangeMetaData.getFutures();
    Map<OptionsContract, DerivativeMetaData> options = exchangeMetaData.getOptions();
    List<DeribitCurrency> activeDeribitCurrencies = ((DeribitMarketDataServiceRaw) marketDataService).getDeribitCurrencies();
    currencies.clear();
    futures.clear();
    options.clear();
    for (DeribitCurrency deribitCurrency : activeDeribitCurrencies) {
        currencies.put(new Currency(deribitCurrency.getCurrency()), DeribitAdapters.adaptMeta(deribitCurrency));
        List<DeribitInstrument> deribitInstruments = ((DeribitMarketDataServiceRaw) marketDataService).getDeribitInstruments(deribitCurrency.getCurrency(), null, null);
        for (DeribitInstrument deribitInstrument : deribitInstruments) {
            if (deribitInstrument.getKind() == Kind.future) {
                futures.put(DeribitAdapters.adaptFuturesContract(deribitInstrument), DeribitAdapters.adaptMeta(deribitInstrument));
            } else {
                options.put(DeribitAdapters.adaptOptionsContract(deribitInstrument), DeribitAdapters.adaptMeta(deribitInstrument));
            }
        }
    }
}
Also used : DerivativeMetaData(org.knowm.xchange.dto.meta.DerivativeMetaData) DeribitInstrument(org.knowm.xchange.deribit.v2.dto.marketdata.DeribitInstrument) CurrencyMetaData(org.knowm.xchange.dto.meta.CurrencyMetaData) FuturesContract(org.knowm.xchange.derivative.FuturesContract) OptionsContract(org.knowm.xchange.derivative.OptionsContract) DeribitCurrency(org.knowm.xchange.deribit.v2.dto.marketdata.DeribitCurrency) Currency(org.knowm.xchange.currency.Currency) DeribitCurrency(org.knowm.xchange.deribit.v2.dto.marketdata.DeribitCurrency) DeribitMarketDataServiceRaw(org.knowm.xchange.deribit.v2.service.DeribitMarketDataServiceRaw)

Aggregations

OptionsContract (org.knowm.xchange.derivative.OptionsContract)4 BigDecimal (java.math.BigDecimal)2 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)2 FuturesContract (org.knowm.xchange.derivative.FuturesContract)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 Currency (org.knowm.xchange.currency.Currency)1 DeribitCurrency (org.knowm.xchange.deribit.v2.dto.marketdata.DeribitCurrency)1 DeribitInstrument (org.knowm.xchange.deribit.v2.dto.marketdata.DeribitInstrument)1 DeribitTicker (org.knowm.xchange.deribit.v2.dto.marketdata.DeribitTicker)1 DeribitMarketDataServiceRaw (org.knowm.xchange.deribit.v2.service.DeribitMarketDataServiceRaw)1 Ticker (org.knowm.xchange.dto.marketdata.Ticker)1 CurrencyMetaData (org.knowm.xchange.dto.meta.CurrencyMetaData)1