Skip to content

Commit

Permalink
bitrich-info#448 Kraken exchange orderbook timestamp parse incorrect …
Browse files Browse the repository at this point in the history
…- fixed
  • Loading branch information
Pavel Chertalev committed Nov 27, 2019
1 parent 8133322 commit 1e4c04b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package info.bitrich.xchangestream.kraken;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import info.bitrich.xchangestream.kraken.dto.KrakenOrderBook;
import info.bitrich.xchangestream.kraken.dto.enums.KrakenOrderBookMessageType;
import info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper;
import org.apache.commons.lang3.StringUtils;
import org.knowm.xchange.kraken.dto.marketdata.KrakenPublicOrder;
import org.slf4j.Logger;
Expand Down Expand Up @@ -89,12 +86,12 @@ public static KrakenPublicOrder extractKrakenPublicOrder(List<String> list) {
return new KrakenPublicOrder(
new BigDecimal(list.get(0)).stripTrailingZeros(),
new BigDecimal(list.get(1)).stripTrailingZeros(),
timestampToMs(list.get(2))
);
}

private static long timestampToMs(String timestamp) {
return new BigDecimal(timestamp).multiply(new BigDecimal(1000)).longValue();
//TODO: The XChange Kraken orderbook timestamp is a seconds format since epoch.
// But websocket order's timestamp is more accurate. It is required to improve the XChange Kraken for supporting higher precision.
// XChange method to fix: org.knowm.xchange.kraken.KrakenAdapters.adaptOrders
new BigDecimal(list.get(2)).longValue()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper;
import org.junit.Assert;
import org.junit.Test;
import org.knowm.xchange.kraken.dto.marketdata.KrakenPublicOrder;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

public class KrakenOrderBookParseTest {
Expand All @@ -25,6 +27,14 @@ public void testOrderBookSnapshot() throws IOException {
Assert.assertEquals(25, krakenOrderBook.getAsk().length);
Assert.assertNotNull(krakenOrderBook.getBid());
Assert.assertEquals(25, krakenOrderBook.getBid().length);
KrakenPublicOrder firstAsk = krakenOrderBook.getAsk()[0];
Assert.assertEquals(0, new BigDecimal("8692").compareTo(firstAsk.getPrice()));
Assert.assertEquals(0, new BigDecimal("2.01122372").compareTo(firstAsk.getVolume()));
Assert.assertEquals(1561120269L, firstAsk.getTimestamp());
KrakenPublicOrder firstBid = krakenOrderBook.getBid()[0];
Assert.assertEquals(0, new BigDecimal("8691.9").compareTo(firstBid.getPrice()));
Assert.assertEquals(0, new BigDecimal("1.45612927").compareTo(firstBid.getVolume()));
Assert.assertEquals(1561120266L, firstBid.getTimestamp());
}

@Test
Expand All @@ -40,5 +50,9 @@ public void testOrderBookUpdate() throws IOException {
Assert.assertEquals(2, krakenOrderBook.getAsk().length);
Assert.assertNotNull(krakenOrderBook.getBid());
Assert.assertEquals(0, krakenOrderBook.getBid().length);
KrakenPublicOrder firstAsk = krakenOrderBook.getAsk()[0];
Assert.assertEquals(0, new BigDecimal("9618.6").compareTo(firstAsk.getPrice()));
Assert.assertEquals(0, BigDecimal.ZERO.compareTo(firstAsk.getVolume()));
Assert.assertEquals(1561372908L, firstAsk.getTimestamp());
}
}

0 comments on commit 1e4c04b

Please sign in to comment.