Skip to content
This repository has been archived by the owner on Jun 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #455 from dsx-tech/448-kraken-timestamp
Browse files Browse the repository at this point in the history
#448 Kraken exchange orderbook timestamp parse incorrect - fixed
  • Loading branch information
badgerwithagun authored Dec 9, 2019
2 parents 5f46bfc + cca2620 commit 44f87c4
Show file tree
Hide file tree
Showing 2 changed files with 16 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 @@ -32,6 +29,7 @@ public class KrakenOrderBookUtils {
private static final String BID_UPDATE = "b";

private static final int EXPECTED_ORDER_BOOK_ARRAY_SIZE = 4;
private static final BigDecimal BIG_DECIMAL_1000 = new BigDecimal(1000);

@SuppressWarnings("unchecked")
public static KrakenOrderBook parse(List jsonParseResult) {
Expand Down Expand Up @@ -89,12 +87,8 @@ 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))
new BigDecimal(list.get(2)).multiply(BIG_DECIMAL_1000).longValue()
);
}

private static long timestampToMs(String timestamp) {
return new BigDecimal(timestamp).multiply(new BigDecimal(1000)).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(1561120269939L, 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(1561120266647L, 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(1561372908562L, firstAsk.getTimestamp());
}
}

0 comments on commit 44f87c4

Please sign in to comment.