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;
}
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();
}
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);
}
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));
}
}
}
}
Aggregations