@Override public CoinbaseSpotPriceHistory deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { final List<CoinbaseHistoricalSpotPrice> historicalPrices = new ArrayList<CoinbaseHistoricalSpotPrice>(); // If there is a better way to get to the actual String returned please replace this section. final Reader reader = (Reader) ctxt.getParser().getTokenLocation().getSourceRef(); reader.reset(); final StringBuilder historyStringBuilder = new StringBuilder(); for (int c = reader.read(); c > 0; c = reader.read()) historyStringBuilder.append((char) c); // Parse in reverse because they are inconsistent with the number of decimals for the rates // which makes it difficult to differentiate from the following year. Going in reverse // we can rely on the comma. final String entireHistoryString = historyStringBuilder.reverse().toString(); final Matcher matcher = historicalRateStringPatternInReverse.matcher(entireHistoryString); while (matcher.find()) { final String rateString = new StringBuilder(matcher.group(1)).reverse().toString(); final BigDecimal spotRate = new BigDecimal(rateString); final String timestampString = new StringBuilder(matcher.group(2)).reverse().toString(); final Date timestamp = DateUtils.fromISO8601DateString(timestampString); final CoinbaseHistoricalSpotPrice historicalSpotPrice = new CoinbaseHistoricalSpotPrice(timestamp, spotRate); historicalPrices.add(historicalSpotPrice); } Collections.sort(historicalPrices, Collections.reverseOrder()); return new CoinbaseSpotPriceHistory(historicalPrices); }
@Test public void testTradeAdapter() throws IOException { // Read in the JSON from the example resources InputStream is = MtGoxAdapterTest.class.getResourceAsStream("/v0/marketdata/example-trades-data-v0.json"); // Use Jackson to parse it ObjectMapper mapper = new ObjectMapper(); MtGoxTrades[] mtGoxTrades = mapper.readValue(is, MtGoxTrades[].class); Trades trades = MtGoxAdapters.adaptTrades(mtGoxTrades); System.out.println(trades.getTrades().size()); assertTrue("Trades size should be 609", trades.getTrades().size() == 609); // verify all fields filled // System.out.println(trades.getTrades().get(0).toString()); assertTrue( "Price should be 16.75", trades.getTrades().get(0).getPrice().getAmount().doubleValue() == 16.75); assertTrue("Order type should be Bid", trades.getTrades().get(0).getType() == OrderType.BID); assertTrue( "TradableAmount should be 0.09910328", trades.getTrades().get(0).getTradableAmount().doubleValue() == 0.09910328); assertTrue( "TradableIdentifier should be BTC", trades.getTrades().get(0).getTradableIdentifier().equals("BTC")); assertTrue( "TransactionCurrency should be USD", trades.getTrades().get(0).getTransactionCurrency().equals("USD")); // Unix 1358803625 = Mon, 21 Jan 2013 21:27:05 GMT assertThat( "2013-01-21 21:27:05 GMT", is(equalTo(DateUtils.toUTCString(trades.getTrades().get(0).getTimestamp())))); }
@Test public void testDeserializeSpotRateHistory() throws IOException { // Read in the JSON from the example resources InputStream is = CoinbaseMarketDataJsonTest.class.getResourceAsStream( "/marketdata/example-spot-rate-history-data.json"); String spotPriceHistoryString; Scanner scanner = null; try { scanner = new Scanner(is); spotPriceHistoryString = scanner.useDelimiter("\\A").next(); } finally { scanner.close(); } CoinbaseSpotPriceHistory spotPriceHistory = CoinbaseSpotPriceHistory.fromRawString(spotPriceHistoryString); List<CoinbaseHistoricalSpotPrice> spotPriceHistoryList = spotPriceHistory.getSpotPriceHistory(); assertThat(spotPriceHistoryList.size()).isEqualTo(10); CoinbaseHistoricalSpotPrice historicalSpotPrice = spotPriceHistoryList.get(0); assertThat(historicalSpotPrice.getSpotRate()).isEqualTo("719.79"); assertThat(historicalSpotPrice.getTimestamp()) .isEqualTo(DateUtils.fromISO8601DateString("2014-02-08T13:21:51-08:00")); }
/** * Adapts a CexIOTrade to a Trade Object * * @param trade CexIO trade object * @param currencyPair trade currencies * @return The XChange Trade */ public static Trade adaptTrade(CexIOTrade trade, CurrencyPair currencyPair) { BigDecimal amount = trade.getAmount(); BigDecimal price = trade.getPrice(); Date date = DateUtils.fromMillisUtc(trade.getDate() * 1000L); // Cex.IO API does not return trade type return new Trade(null, amount, currencyPair, price, date, String.valueOf(trade.getTid())); }
/** * Adapts a Transaction[] to a Trades Object * * @param transactions The Bitstamp transactions * @param tradableIdentifier The tradeable identifier (e.g. BTC in BTC/USD) * @param currency The currency (e.g. USD in BTC/USD) * @return The XChange Trades */ public static Trades adaptTrades( BitstampTransaction[] transactions, String tradableIdentifier, String currency) { List<Trade> trades = new ArrayList<Trade>(); for (BitstampTransaction tx : transactions) { trades.add( new Trade( null, tx.getAmount(), tradableIdentifier, currency, BigMoney.of(CurrencyUnit.of(currency), tx.getPrice()), DateUtils.fromMillisUtc(tx.getDate() * 1000L), tx.getTid())); } return new Trades(trades); }
@Test public void testTradeAdapter() throws IOException { // Read in the JSON from the example resources InputStream is = BitcurexTradesJSONTest.class.getResourceAsStream("/marketdata/example-trades-data.json"); // Use Jackson to parse it ObjectMapper mapper = new ObjectMapper(); BitcurexTrade[] BitcurexTrades = mapper.readValue(is, BitcurexTrade[].class); Trades trades = BitcurexAdapters.adaptTrades(BitcurexTrades, CurrencyPair.BTC_EUR); assertThat(trades.getTrades().size()).isEqualTo(70); // Verify all fields filled assertThat(trades.getTrades().get(0).getPrice().doubleValue() == 70.00000000); assertThat(trades.getTrades().get(0).getTradableAmount().doubleValue() == 23.99500000); assertThat(DateUtils.toUTCString(trades.getTrades().get(0).getTimestamp())) .isEqualTo("2013-07-29 16:53:28 GMT"); }
public static OpenOrders adaptOpenOrders(List<CexIOOrder> cexIOOrderList) { List<LimitOrder> limitOrders = new ArrayList<LimitOrder>(); for (CexIOOrder cexIOOrder : cexIOOrderList) { Order.OrderType orderType = cexIOOrder.getType() == CexIOOrder.Type.buy ? Order.OrderType.BID : Order.OrderType.ASK; String id = Long.toString(cexIOOrder.getId()); limitOrders.add( new LimitOrder( orderType, cexIOOrder.getPending(), new CurrencyPair( cexIOOrder.getTradableIdentifier(), cexIOOrder.getTransactionCurrency()), id, DateUtils.fromMillisUtc(cexIOOrder.getTime()), cexIOOrder.getPrice())); } return new OpenOrders(limitOrders); }