From 7a827c970e6183953a7b94da842d3d5aa8425f5f Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Fri, 19 Aug 2016 22:55:15 +0200 Subject: [PATCH] Apply inverted price to trade charts --- .../main/market/trades/TradesChartsView.java | 15 +++++++-- .../market/trades/TradesChartsViewModel.java | 31 +++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/gui/src/main/java/io/bitsquare/gui/main/market/trades/TradesChartsView.java b/gui/src/main/java/io/bitsquare/gui/main/market/trades/TradesChartsView.java index 783a721574..1ed7bf72b4 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/market/trades/TradesChartsView.java +++ b/gui/src/main/java/io/bitsquare/gui/main/market/trades/TradesChartsView.java @@ -18,12 +18,14 @@ package io.bitsquare.gui.main.market.trades; import io.bitsquare.common.UserThread; +import io.bitsquare.common.util.MathUtils; import io.bitsquare.gui.common.view.ActivatableViewAndModel; import io.bitsquare.gui.common.view.FxmlView; import io.bitsquare.gui.main.market.trades.charts.price.CandleStickChart; import io.bitsquare.gui.main.market.trades.charts.volume.VolumeChart; import io.bitsquare.gui.util.BSFormatter; import io.bitsquare.gui.util.GUIUtil; +import io.bitsquare.locale.CurrencyUtil; import io.bitsquare.locale.TradeCurrency; import io.bitsquare.trade.statistics.TradeStatistics; import javafx.beans.property.ReadOnlyObjectWrapper; @@ -233,7 +235,11 @@ public class TradesChartsView extends ActivatableViewAndModel() { @Override public String toString(Number object) { - return formatter.formatPrice(Fiat.valueOf(model.getCurrencyCode(), new Double((double) object).longValue())); + if (CurrencyUtil.isCryptoCurrency(model.getCurrencyCode())) { + final double value = (double) object / 100000000D; + return String.valueOf(MathUtils.roundDouble(value, 8)); + } else + return formatter.formatPrice(Fiat.valueOf(model.getCurrencyCode(), new Double((double) object).longValue())); } @Override @@ -245,7 +251,12 @@ public class TradesChartsView extends ActivatableViewAndModel() { @Override public String toString(Number object) { - return formatter.formatPriceWithCode(Fiat.valueOf(model.getCurrencyCode(), (long) object)); + if (CurrencyUtil.isCryptoCurrency(model.getCurrencyCode())) { + final double value = (long) object / 100000000D; + return String.valueOf(MathUtils.roundDouble(value, 8)) + " " + formatter.getCurrencyPair(model.getCurrencyCode()); + } else { + return formatter.formatPriceWithCode(Fiat.valueOf(model.getCurrencyCode(), (long) object)); + } } @Override diff --git a/gui/src/main/java/io/bitsquare/gui/main/market/trades/TradesChartsViewModel.java b/gui/src/main/java/io/bitsquare/gui/main/market/trades/TradesChartsViewModel.java index 9a04c1a715..bfe7343855 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/market/trades/TradesChartsViewModel.java +++ b/gui/src/main/java/io/bitsquare/gui/main/market/trades/TradesChartsViewModel.java @@ -20,6 +20,7 @@ package io.bitsquare.gui.main.market.trades; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; import io.bitsquare.btc.pricefeed.PriceFeedService; +import io.bitsquare.common.util.MathUtils; import io.bitsquare.gui.Navigation; import io.bitsquare.gui.common.model.ActivatableViewModel; import io.bitsquare.gui.main.MainView; @@ -235,9 +236,10 @@ class TradesChartsViewModel extends ActivatableViewModel { // create CandleData for defined time interval List candleDataList = itemsPerInterval.entrySet().stream() + .filter(entry -> entry.getKey() >= 0) .map(entry -> getCandleData(entry.getKey(), entry.getValue())) - .filter(e -> e.tick >= 0) .collect(Collectors.toList()); + candleDataList.sort((o1, o2) -> (o1.tick < o2.tick ? -1 : (o1.tick == o2.tick ? 0 : 1))); priceItems.setAll(candleDataList.stream() @@ -259,9 +261,15 @@ class TradesChartsViewModel extends ActivatableViewModel { long accumulatedAmount = 0; for (TradeStatistics item : set) { - final long tradePriceAsLong = item.tradePrice; - low = (low != 0) ? Math.min(low, tradePriceAsLong) : tradePriceAsLong; - high = (high != 0) ? Math.max(high, tradePriceAsLong) : tradePriceAsLong; + long tradePriceAsLong = item.tradePrice; + if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) { + low = (low != 0) ? Math.max(low, tradePriceAsLong) : tradePriceAsLong; + high = (high != 0) ? Math.min(high, tradePriceAsLong) : tradePriceAsLong; + } else { + low = (low != 0) ? Math.min(low, tradePriceAsLong) : tradePriceAsLong; + high = (high != 0) ? Math.max(high, tradePriceAsLong) : tradePriceAsLong; + } + accumulatedVolume += (item.getTradeVolume() != null) ? item.getTradeVolume().value : 0; accumulatedAmount += item.tradeAmount; } @@ -279,8 +287,19 @@ class TradesChartsViewModel extends ActivatableViewModel { String dateString = tickUnit.ordinal() > TickUnit.DAY.ordinal() ? formatter.formatDateTime(date) : formatter.formatDate(date); - return new CandleData(tick, open, close, high, low, averagePrice, accumulatedAmount, accumulatedVolume, - isBullish, dateString); + if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) { + return new CandleData(tick, getInvertedPrice(open), getInvertedPrice(close), getInvertedPrice(high), + getInvertedPrice(low), getInvertedPrice(averagePrice), accumulatedAmount, accumulatedVolume, + isBullish, dateString); + } else { + return new CandleData(tick, open, close, high, low, averagePrice, accumulatedAmount, accumulatedVolume, + isBullish, dateString); + } + } + + long getInvertedPrice(long price) { + final double value = price != 0 ? 1000000000000D / price : 0; + return Math.round(MathUtils.roundDouble(value, 8)); } long getTickFromTime(long tradeDateAsTime, TickUnit tickUnit) {