From f12abae26a97c2a7f196d5df90ca14498042678d Mon Sep 17 00:00:00 2001 From: Jure Petrovic Date: Wed, 26 Jun 2019 16:22:04 +0200 Subject: [PATCH 1/4] dont write so much data. --- .../bitrich/xchangestream/kraken/KrakenStreamingService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xchange-kraken/src/main/java/info/bitrich/xchangestream/kraken/KrakenStreamingService.java b/xchange-kraken/src/main/java/info/bitrich/xchangestream/kraken/KrakenStreamingService.java index db42002b9..3306463a7 100644 --- a/xchange-kraken/src/main/java/info/bitrich/xchangestream/kraken/KrakenStreamingService.java +++ b/xchange-kraken/src/main/java/info/bitrich/xchangestream/kraken/KrakenStreamingService.java @@ -56,7 +56,7 @@ protected void handleMessage(JsonNode message) { if (event != null && (krakenEvent = KrakenEventType.getEvent(event.textValue())) != null) { switch (krakenEvent) { case heartbeat: - LOG.info("Heartbeat received"); + //LOG.info("Heartbeat received"); break; case systemStatus: KrakenSystemStatus systemStatus = mapper.treeToValue(message, KrakenSystemStatus.class); From 84b697423264a73117125b27a65e1be8c0d4653c Mon Sep 17 00:00:00 2001 From: Jure Petrovic Date: Tue, 19 Nov 2019 11:16:17 +0100 Subject: [PATCH 2/4] Bitstamp v2 debugging --- .../bitstamp/v2/BitstampStreamingService.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingService.java b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingService.java index b0aa9384a..4b075a155 100644 --- a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingService.java +++ b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingService.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; +import java.util.ArrayList; /** * Bitstamp WebSocket V2 streaming service implementation @@ -20,9 +21,13 @@ public class BitstampStreamingService extends JsonNettyStreamingService { private static final String JSON_CHANNEL = "channel"; private static final String JSON_EVENT = "event"; private static final String JSON_DATA = "data"; - + private static final ArrayList JSON_DATA2 = new ArrayList(); + public BitstampStreamingService(String apiUrl) { super(apiUrl, Integer.MAX_VALUE); + + // add events + JSON_DATA2.add("trade"); } @Override @@ -56,8 +61,10 @@ protected void handleMessage(JsonNode message) { LOG.warn("The message has been received from disconnected channel '{}'. Skipped.", channel); return; } - if (event.equals(JSON_DATA)) { - super.handleMessage(message); + // event: trade + //if (event.equals(JSON_DATA)) { + if (JSON_DATA2.contains(event)) { + super.handleMessage(message); } } From 2dc0ec94d17330031f5a354a8c67a9a06b7a9b0d Mon Sep 17 00:00:00 2001 From: Jure Petrovic Date: Tue, 19 Nov 2019 11:48:40 +0100 Subject: [PATCH 3/4] Fixed some properties. Failing to instantiate BitstampTransaction --- .../dto/BitstampWebSocketTransaction.java | 45 +++++++++++++++++-- .../BitstampStreamingMarketDataService.java | 6 ++- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/dto/BitstampWebSocketTransaction.java b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/dto/BitstampWebSocketTransaction.java index 35d4dfc0d..795b9e849 100644 --- a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/dto/BitstampWebSocketTransaction.java +++ b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/dto/BitstampWebSocketTransaction.java @@ -1,3 +1,25 @@ +/* actual message +{ + "data":{ + "microtimestamp":"1574158935282494", + "amount":0.01000309, + "buy_order_id":4352888198, + "sell_order_id":4352886848, + "amount_str":"0.01000309", + "price_str":"7360.72", + "timestamp":"1574158935", + "price":7360.72, + "type":0, + "id":100568768 + }, + "event":"trade", + "channel":"live_trades_btceur" +} +*/ + + + + package info.bitrich.xchangestream.bitstamp.dto; import com.fasterxml.jackson.annotation.JsonProperty; @@ -7,8 +29,23 @@ import java.math.BigDecimal; public class BitstampWebSocketTransaction extends BitstampTransaction { - public BitstampWebSocketTransaction(@JsonProperty("datetime") long date, @JsonProperty("id") long tid, @JsonProperty("price") BigDecimal price, - @JsonProperty("amount") BigDecimal amount, @JsonProperty("order_type") int type) { - super(date, tid, price, amount, type); - } + + public BitstampWebSocketTransaction( + @JsonProperty("timestamp") long date, + @JsonProperty("id") long tid, + @JsonProperty("price_str") String price_str, + @JsonProperty("amount_str") String amount_str, + @JsonProperty("order_type") int type) { + + super(date, tid, getBigDecimal(price_str), getBigDecimal(amount_str), type); + + } + + // return BigDecimal object + public static BigDecimal getBigDecimal(String val) { + + BigDecimal ret = new BigDecimal(val); + return ret; + } + } diff --git a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingMarketDataService.java b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingMarketDataService.java index 84f51f917..6bf3b316f 100644 --- a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingMarketDataService.java +++ b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingMarketDataService.java @@ -65,8 +65,10 @@ public Observable getTrades(CurrencyPair currencyPair, Object... args) { .map(s -> { ObjectMapper mapper = StreamingObjectMapperHelper.getObjectMapper(); BitstampWebSocketTransaction transactions = mapper.treeToValue(s.get("data"), BitstampWebSocketTransaction.class); - transactions = new BitstampWebSocketTransaction(new Date().getTime() / 1000L, transactions.getTid(), - transactions.getPrice(), transactions.getAmount(), transactions.getType()); + + //transactions = new BitstampWebSocketTransaction(new Date().getTime() / 1000L, transactions.getTid(), + // transactions.getPrice(), transactions.getAmount(), transactions.getType()); + return BitstampAdapters.adaptTrade(transactions, currencyPair, 1000); }); } From 580ce201ab23f24daa62f40afdb53b32faeca3a5 Mon Sep 17 00:00:00 2001 From: Jure Petrovic Date: Tue, 19 Nov 2019 15:54:14 +0100 Subject: [PATCH 4/4] Fixed Bitstamp v2 trade feed --- .../dto/BitstampWebSocketTransaction.java | 45 +-------- .../BitstampStreamingMarketDataService.java | 27 ++++-- .../bitstamp/v2/BitstampStreamingService.java | 11 ++- .../v2/dto/BitstampWebSocketTrade.java | 93 +++++++++++++++++++ 4 files changed, 123 insertions(+), 53 deletions(-) create mode 100644 xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/dto/BitstampWebSocketTrade.java diff --git a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/dto/BitstampWebSocketTransaction.java b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/dto/BitstampWebSocketTransaction.java index 795b9e849..35d4dfc0d 100644 --- a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/dto/BitstampWebSocketTransaction.java +++ b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/dto/BitstampWebSocketTransaction.java @@ -1,25 +1,3 @@ -/* actual message -{ - "data":{ - "microtimestamp":"1574158935282494", - "amount":0.01000309, - "buy_order_id":4352888198, - "sell_order_id":4352886848, - "amount_str":"0.01000309", - "price_str":"7360.72", - "timestamp":"1574158935", - "price":7360.72, - "type":0, - "id":100568768 - }, - "event":"trade", - "channel":"live_trades_btceur" -} -*/ - - - - package info.bitrich.xchangestream.bitstamp.dto; import com.fasterxml.jackson.annotation.JsonProperty; @@ -29,23 +7,8 @@ import java.math.BigDecimal; public class BitstampWebSocketTransaction extends BitstampTransaction { - - public BitstampWebSocketTransaction( - @JsonProperty("timestamp") long date, - @JsonProperty("id") long tid, - @JsonProperty("price_str") String price_str, - @JsonProperty("amount_str") String amount_str, - @JsonProperty("order_type") int type) { - - super(date, tid, getBigDecimal(price_str), getBigDecimal(amount_str), type); - - } - - // return BigDecimal object - public static BigDecimal getBigDecimal(String val) { - - BigDecimal ret = new BigDecimal(val); - return ret; - } - + public BitstampWebSocketTransaction(@JsonProperty("datetime") long date, @JsonProperty("id") long tid, @JsonProperty("price") BigDecimal price, + @JsonProperty("amount") BigDecimal amount, @JsonProperty("order_type") int type) { + super(date, tid, price, amount, type); + } } diff --git a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingMarketDataService.java b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingMarketDataService.java index 6bf3b316f..2e5b50ba3 100644 --- a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingMarketDataService.java +++ b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingMarketDataService.java @@ -1,10 +1,16 @@ package info.bitrich.xchangestream.bitstamp.v2; import com.fasterxml.jackson.databind.ObjectMapper; + +// old version? import info.bitrich.xchangestream.bitstamp.dto.BitstampOrderBook; -import info.bitrich.xchangestream.bitstamp.dto.BitstampWebSocketTransaction; + + +import info.bitrich.xchangestream.bitstamp.v2.dto.BitstampWebSocketTrade; + import info.bitrich.xchangestream.core.StreamingMarketDataService; import info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper; + import io.reactivex.Observable; import org.knowm.xchange.bitstamp.BitstampAdapters; import org.knowm.xchange.currency.CurrencyPair; @@ -13,7 +19,6 @@ import org.knowm.xchange.dto.marketdata.Trade; import org.knowm.xchange.exceptions.NotAvailableFromExchangeException; -import java.util.Date; /** * Bitstamp WebSocket V2 Streaming Market Data Service implementation @@ -64,12 +69,18 @@ public Observable getTrades(CurrencyPair currencyPair, Object... args) { return service.subscribeChannel(channelName, "trade") .map(s -> { ObjectMapper mapper = StreamingObjectMapperHelper.getObjectMapper(); - BitstampWebSocketTransaction transactions = mapper.treeToValue(s.get("data"), BitstampWebSocketTransaction.class); - - //transactions = new BitstampWebSocketTransaction(new Date().getTime() / 1000L, transactions.getTid(), - // transactions.getPrice(), transactions.getAmount(), transactions.getType()); - - return BitstampAdapters.adaptTrade(transactions, currencyPair, 1000); + // v2 websocket trade data + BitstampWebSocketTrade transactions = mapper.treeToValue(s.get("data"), BitstampWebSocketTrade.class); + // convert + org.knowm.xchange.bitstamp.dto.marketdata.BitstampTransaction bitstampTransaction = + new org.knowm.xchange.bitstamp.dto.marketdata.BitstampTransaction( + transactions.getDate(), + transactions.getTid(), + transactions.getPrice(), + transactions.getAmount(), + transactions.getType()); + + return BitstampAdapters.adaptTrade(bitstampTransaction, currencyPair, 1000); }); } diff --git a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingService.java b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingService.java index 4b075a155..0ea70e21c 100644 --- a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingService.java +++ b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/BitstampStreamingService.java @@ -20,14 +20,17 @@ public class BitstampStreamingService extends JsonNettyStreamingService { private static final String JSON_CHANNEL = "channel"; private static final String JSON_EVENT = "event"; - private static final String JSON_DATA = "data"; - private static final ArrayList JSON_DATA2 = new ArrayList(); + + // can be: trade, data... + private static final ArrayList JSON_DATA = new ArrayList(); public BitstampStreamingService(String apiUrl) { super(apiUrl, Integer.MAX_VALUE); // add events - JSON_DATA2.add("trade"); + JSON_DATA.add("trade"); + // not yet tested + //JSON_DATA.add("data"); } @Override @@ -63,7 +66,7 @@ protected void handleMessage(JsonNode message) { } // event: trade //if (event.equals(JSON_DATA)) { - if (JSON_DATA2.contains(event)) { + if (JSON_DATA.contains(event)) { super.handleMessage(message); } } diff --git a/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/dto/BitstampWebSocketTrade.java b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/dto/BitstampWebSocketTrade.java new file mode 100644 index 000000000..2f56b37e1 --- /dev/null +++ b/xchange-bitstamp/src/main/java/info/bitrich/xchangestream/bitstamp/v2/dto/BitstampWebSocketTrade.java @@ -0,0 +1,93 @@ +// Bitstamp v2 - websocket api +// JSON mapping class + +/* actual message +{ + "data":{ + "microtimestamp":"1574158935282494", + "amount":0.01000309, + "buy_order_id":4352888198, + "sell_order_id":4352886848, + "amount_str":"0.01000309", + "price_str":"7360.72", + "timestamp":"1574158935", + "price":7360.72, + "type":0, + "id":100568768 + }, + "event":"trade", + "channel":"live_trades_btceur" +} +*/ + +package info.bitrich.xchangestream.bitstamp.v2.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.math.BigDecimal; + + +public class BitstampWebSocketTrade { + + private long date; + private long tid; + private BigDecimal price; + private BigDecimal amount; + private int type; + + + public BitstampWebSocketTrade( + @JsonProperty("timestamp") long date, + @JsonProperty("id") long tid, + @JsonProperty("price_str") String price_str, + @JsonProperty("amount_str") String amount_str, + @JsonProperty("order_type") int type) { + + this.date = date; + this.tid = tid; + this.price = new BigDecimal(price_str); + this.amount = new BigDecimal(amount_str); + this.type = type; + } + + // GETTERS, SETTERS + public long getDate() { + return date; + } + + public void setDate(long date) { + this.date = date; + } + + public long getTid() { + return tid; + } + + public void setTid(long tid) { + this.tid = tid; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + +}