Skip to content

Commit

Permalink
Merge pull request #2976 from niyid/master
Browse files Browse the repository at this point in the history
Fixes for issues #2741, #2944, #2955
  • Loading branch information
sqrrm authored Aug 9, 2019
2 parents 1c5c633 + 11f4fe3 commit fb1c96d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
3 changes: 2 additions & 1 deletion core/src/main/resources/i18n/displayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ market.trades.tooltip.candle.close=Close:
market.trades.tooltip.candle.high=High:
market.trades.tooltip.candle.low=Low:
market.trades.tooltip.candle.average=Average:
market.trades.tooltip.candle.median=Median:
market.trades.tooltip.candle.date=Date:

####################################################################
Expand Down Expand Up @@ -614,7 +615,7 @@ message.state.SENT=Message sent
# suppress inspection "UnusedProperty"
message.state.ARRIVED=Message arrived at peer
# suppress inspection "UnusedProperty"
message.state.STORED_IN_MAILBOX=Message stored in mailbox
message.state.STORED_IN_MAILBOX=Message of payment sent but not yet received by peer
# suppress inspection "UnusedProperty"
message.state.ACKNOWLEDGED=Peer confirmed message receipt
# suppress inspection "UnusedProperty"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import java.time.temporal.ChronoUnit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -304,20 +305,20 @@ CandleData getCandleData(long tick, Set<TradeStatistics2> set) {
long accumulatedVolume = 0;
long accumulatedAmount = 0;
long numTrades = set.size();
List<Long> tradePrices = new ArrayList<>(set.size());

for (TradeStatistics2 item : set) {
long tradePriceAsLong = item.getTradePrice().getValue();
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;
}
// Previously a check was done which inverted the low and high for
// crytocurrencies.
low = (low != 0) ? Math.min(low, tradePriceAsLong) : tradePriceAsLong;
high = (high != 0) ? Math.max(high, tradePriceAsLong) : tradePriceAsLong;

accumulatedVolume += (item.getTradeVolume() != null) ? item.getTradeVolume().getValue() : 0;
accumulatedAmount += item.getTradeAmount().getValue();
tradePrices.add(item.getTradePrice().getValue());
}
Collections.sort(tradePrices);

List<TradeStatistics2> list = new ArrayList<>(set);
list.sort((o1, o2) -> (o1.getTradeDate().getTime() < o2.getTradeDate().getTime() ? -1 : (o1.getTradeDate().getTime() == o2.getTradeDate().getTime() ? 0 : 1)));
Expand All @@ -327,6 +328,9 @@ CandleData getCandleData(long tick, Set<TradeStatistics2> set) {
}

long averagePrice;
Long[] prices = new Long[tradePrices.size()];
tradePrices.toArray(prices);
long medianPrice = findMedian(prices);
boolean isBullish;
if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) {
isBullish = close < open;
Expand All @@ -343,9 +347,20 @@ CandleData getCandleData(long tick, Set<TradeStatistics2> set) {
String dateString = tickUnit.ordinal() > TickUnit.DAY.ordinal() ?
formatter.formatDateTimeSpan(dateFrom, dateTo) :
formatter.formatDate(dateFrom) + " - " + formatter.formatDate(dateTo);
return new CandleData(tick, open, close, high, low, averagePrice, accumulatedAmount, accumulatedVolume,
return new CandleData(tick, open, close, high, low, averagePrice, medianPrice, accumulatedAmount, accumulatedVolume,
numTrades, isBullish, dateString);
}

Long findMedian(Long[] prices) {
int middle = prices.length / 2;
long median;
if (prices.length % 2 == 1) {
median = prices[middle];
} else {
median = MathUtils.roundDoubleToLong((prices[middle - 1] + prices[middle]) / 2.0);
}
return median;
}

Date roundToTick(Date time, TickUnit tickUnit) {
ZonedDateTime zdt = time.toInstant().atZone(ZoneId.systemDefault());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ public class CandleData {
public final long high;
public final long low;
public final long average;
public final long median;
public final long accumulatedAmount;
public final long accumulatedVolume;
public final long numTrades;
public final boolean isBullish;
public final String date;

public CandleData(long tick, long open, long close, long high, long low, long average,
public CandleData(long tick, long open, long close, long high, long low, long average, long median,
long accumulatedAmount, long accumulatedVolume, long numTrades,
boolean isBullish, String date) {
this.tick = tick;
Expand All @@ -39,6 +40,7 @@ public CandleData(long tick, long open, long close, long high, long low, long av
this.high = high;
this.low = low;
this.average = average;
this.median = median;
this.accumulatedAmount = accumulatedAmount;
this.accumulatedVolume = accumulatedVolume;
this.numTrades = numTrades;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class CandleTooltip extends GridPane {
private final Label highValue = new AutoTooltipLabel();
private final Label lowValue = new AutoTooltipLabel();
private final Label averageValue = new AutoTooltipLabel();
private final Label medianValue = new AutoTooltipLabel();
private final Label dateValue = new AutoTooltipLabel();

CandleTooltip(StringConverter<Number> priceStringConverter) {
Expand All @@ -88,6 +89,7 @@ public class CandleTooltip extends GridPane {
Label high = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.high"));
Label low = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.low"));
Label average = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.average"));
Label median = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.median"));
Label date = new AutoTooltipLabel(Res.get("market.trades.tooltip.candle.date"));
setConstraints(open, 0, 0);
setConstraints(openValue, 1, 0);
Expand All @@ -99,8 +101,10 @@ public class CandleTooltip extends GridPane {
setConstraints(lowValue, 1, 3);
setConstraints(average, 0, 4);
setConstraints(averageValue, 1, 4);
setConstraints(date, 0, 5);
setConstraints(dateValue, 1, 5);
setConstraints(median, 0, 5);
setConstraints(medianValue, 1, 5);
setConstraints(date, 0, 6);
setConstraints(dateValue, 1, 6);

ColumnConstraints columnConstraints1 = new ColumnConstraints();
columnConstraints1.setHalignment(HPos.RIGHT);
Expand All @@ -109,7 +113,7 @@ public class CandleTooltip extends GridPane {
columnConstraints2.setHgrow(Priority.ALWAYS);
getColumnConstraints().addAll(columnConstraints1, columnConstraints2);

getChildren().addAll(open, openValue, close, closeValue, high, highValue, low, lowValue, average, averageValue, date, dateValue);
getChildren().addAll(open, openValue, close, closeValue, high, highValue, low, lowValue, average, averageValue, median, medianValue, date, dateValue);
}

public void update(CandleData candleData) {
Expand All @@ -118,6 +122,7 @@ public void update(CandleData candleData) {
highValue.setText(priceStringConverter.toString(candleData.high));
lowValue.setText(priceStringConverter.toString(candleData.low));
averageValue.setText(priceStringConverter.toString(candleData.average));
medianValue.setText(priceStringConverter.toString(candleData.median));
dateValue.setText(candleData.date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public void testGetCandleData() {
long close = Fiat.parseFiat("EUR", "580").value;
long high = Fiat.parseFiat("EUR", "600").value;
long average = Fiat.parseFiat("EUR", "550").value;
long median = Fiat.parseFiat("EUR", "550").value;
long amount = Coin.parseCoin("4").value;
long volume = Fiat.parseFiat("EUR", "2200").value;
boolean isBullish = true;
Expand All @@ -161,6 +162,7 @@ public void testGetCandleData() {
assertEquals(high, candleData.high);
assertEquals(low, candleData.low);
assertEquals(average, candleData.average);
assertEquals(median, candleData.median);
assertEquals(amount, candleData.accumulatedAmount);
assertEquals(volume, candleData.accumulatedVolume);
assertEquals(isBullish, candleData.isBullish);
Expand Down

0 comments on commit fb1c96d

Please sign in to comment.