use of io.bitsquare.trade.statistics.TradeStatistics in project bitsquare by bitsquare.
the class PublishTradeStatistics method run.
@Override
protected void run() {
try {
runInterceptHook();
// Taker only publishes if the offerer uses an old version
processModel.getP2PService().getNetworkNode().getConfirmedConnections().stream().filter(c -> c.getPeersNodeAddressOptional().isPresent() && c.getPeersNodeAddressOptional().get().equals(trade.getTradingPeerNodeAddress())).findAny().ifPresent(c -> {
TradeStatistics tradeStatistics = new TradeStatistics(trade.getOffer(), trade.getTradePrice(), trade.getTradeAmount(), trade.getDate(), (trade.getDepositTx() != null ? trade.getDepositTx().getHashAsString() : ""), processModel.getPubKeyRing());
final List<Integer> requiredCapabilities = tradeStatistics.getRequiredCapabilities();
final List<Integer> supportedCapabilities = c.getSupportedCapabilities();
boolean matches = false;
if (supportedCapabilities != null) {
for (int messageCapability : requiredCapabilities) {
for (int connectionCapability : supportedCapabilities) {
if (messageCapability == connectionCapability) {
matches = true;
break;
}
}
}
}
if (!matches) {
log.debug("We publish tradeStatistics because the offerer uses an old version so we publish to have at least 1 data item published.");
processModel.getP2PService().addData(tradeStatistics, true);
} else {
log.trace("We do not publish tradeStatistics because the offerer support the capabilities.");
}
});
complete();
} catch (Throwable t) {
failed(t);
}
}
use of io.bitsquare.trade.statistics.TradeStatistics in project bitsquare by bitsquare.
the class TradesChartsViewModelTest method testGetCandleData.
@Test
public void testGetCandleData() {
TradesChartsViewModel model = new TradesChartsViewModel();
long low = Fiat.parseFiat("EUR", "500").value;
long open = Fiat.parseFiat("EUR", "520").value;
long close = Fiat.parseFiat("EUR", "580").value;
long high = Fiat.parseFiat("EUR", "600").value;
long average = Fiat.parseFiat("EUR", "550").value;
long amount = Coin.parseCoin("4").value;
long volume = Fiat.parseFiat("EUR", "2200").value;
boolean isBullish = true;
Set<TradeStatistics> set = new HashSet<>();
final Date now = new Date();
Offer offer = new Offer(null, null, null, null, 0, 0, false, 0, 0, "EUR", null, null, null, null, null, null, null, null);
set.add(new TradeStatistics(offer, Fiat.parseFiat("EUR", "520"), Coin.parseCoin("1"), new Date(now.getTime()), null, null));
set.add(new TradeStatistics(offer, Fiat.parseFiat("EUR", "500"), Coin.parseCoin("1"), new Date(now.getTime() + 100), null, null));
set.add(new TradeStatistics(offer, Fiat.parseFiat("EUR", "600"), Coin.parseCoin("1"), new Date(now.getTime() + 200), null, null));
set.add(new TradeStatistics(offer, Fiat.parseFiat("EUR", "580"), Coin.parseCoin("1"), new Date(now.getTime() + 300), null, null));
CandleData candleData = model.getCandleData(model.getTickFromTime(now.getTime(), TradesChartsViewModel.TickUnit.DAY), set);
assertEquals(open, candleData.open);
assertEquals(close, candleData.close);
assertEquals(high, candleData.high);
assertEquals(low, candleData.low);
assertEquals(average, candleData.average);
assertEquals(amount, candleData.accumulatedAmount);
assertEquals(volume, candleData.accumulatedVolume);
assertEquals(isBullish, candleData.isBullish);
}
use of io.bitsquare.trade.statistics.TradeStatistics in project bitsquare by bitsquare.
the class TradeManager method publishTradeStatistics.
private void publishTradeStatistics(List<Trade> trades) {
for (int i = 0; i < trades.size(); i++) {
Trade trade = trades.get(i);
TradeStatistics tradeStatistics = new TradeStatistics(trade.getOffer(), trade.getTradePrice(), trade.getTradeAmount(), trade.getDate(), (trade.getDepositTx() != null ? trade.getDepositTx().getHashAsString() : ""), keyRing.getPubKeyRing());
tradeStatisticsManager.add(tradeStatistics, true);
// only re-publish in case tradeStatistics are missing.
if ((new Date().getTime() - trade.getDate().getTime()) < TimeUnit.DAYS.toMillis(10)) {
long delay = 5000;
final long minDelay = (i + 1) * delay;
final long maxDelay = (i + 2) * delay;
UserThread.runAfterRandomDelay(() -> {
if (!stopped)
p2PService.addData(tradeStatistics, true);
}, minDelay, maxDelay, TimeUnit.MILLISECONDS);
}
}
}
use of io.bitsquare.trade.statistics.TradeStatistics in project bitsquare by bitsquare.
the class TradesChartsViewModel method getCandleData.
@VisibleForTesting
CandleData getCandleData(long tick, Set<TradeStatistics> set) {
long open = 0;
long close = 0;
long high = 0;
long low = 0;
long accumulatedVolume = 0;
long accumulatedAmount = 0;
long numTrades = set.size();
for (TradeStatistics item : set) {
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;
}
// 100000000 -> Coin.COIN.value;
final double value = MathUtils.scaleUpByPowerOf10(accumulatedVolume, 8);
long averagePrice = MathUtils.roundDoubleToLong(value / (double) accumulatedAmount);
List<TradeStatistics> list = new ArrayList<>(set);
list.sort((o1, o2) -> (o1.tradeDate < o2.tradeDate ? -1 : (o1.tradeDate == o2.tradeDate ? 0 : 1)));
if (list.size() > 0) {
open = list.get(0).tradePrice;
close = list.get(list.size() - 1).tradePrice;
}
boolean isBullish = close > open;
final Date dateFrom = new Date(getTimeFromTickIndex(tick));
final Date dateTo = new Date(getTimeFromTickIndex(tick + 1));
String dateString = tickUnit.ordinal() > TickUnit.DAY.ordinal() ? formatter.formatDateTimeSpan(dateFrom, dateTo) : formatter.formatDate(dateFrom) + " - " + formatter.formatDate(dateTo);
if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) {
return new CandleData(tick, getInvertedPrice(open), getInvertedPrice(close), getInvertedPrice(high), getInvertedPrice(low), getInvertedPrice(averagePrice), accumulatedAmount, accumulatedVolume, numTrades, isBullish, dateString);
} else {
return new CandleData(tick, open, close, high, low, averagePrice, accumulatedAmount, accumulatedVolume, numTrades, isBullish, dateString);
}
}
use of io.bitsquare.trade.statistics.TradeStatistics in project bitsquare by bitsquare.
the class PublishTradeStatistics method run.
@Override
protected void run() {
try {
runInterceptHook();
// Offerer is responsible for publishing. Only in case the offerer uses an old verison the taker publishes.
TradeStatistics tradeStatistics = new TradeStatistics(trade.getOffer(), trade.getTradePrice(), trade.getTradeAmount(), trade.getDate(), (trade.getDepositTx() != null ? trade.getDepositTx().getHashAsString() : ""), processModel.getPubKeyRing());
processModel.getP2PService().addData(tradeStatistics, true);
complete();
} catch (Throwable t) {
failed(t);
}
}
Aggregations