use of io.bitsquare.gui.main.market.trades.charts.CandleData 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.gui.main.market.trades.charts.CandleData 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.gui.main.market.trades.charts.CandleData in project bitsquare by bitsquare.
the class CandleStickChart method updateAxisRange.
/**
* This is called when the range has been invalidated and we need to update it. If the axis are auto
* ranging then we compile a list of all data that the given axis has to plot and call invalidateRange() on the
* axis passing it that data.
*/
@Override
protected void updateAxisRange() {
// For candle stick chart we need to override this method as we need to let the axis know that they need to be able
// to cover the whole area occupied by the high to low range not just its center data value
final Axis<Number> xa = getXAxis();
final Axis<Number> ya = getYAxis();
List<Number> xData = null;
List<Number> yData = null;
if (xa.isAutoRanging()) {
xData = new ArrayList<>();
}
if (ya.isAutoRanging()) {
yData = new ArrayList<>();
}
if (xData != null || yData != null) {
for (XYChart.Series<Number, Number> series : getData()) {
for (XYChart.Data<Number, Number> data : series.getData()) {
if (xData != null) {
xData.add(data.getXValue());
}
if (yData != null) {
if (data.getExtraValue() instanceof CandleData) {
CandleData candleData = (CandleData) data.getExtraValue();
yData.add(candleData.high);
yData.add(candleData.low);
} else {
yData.add(data.getYValue());
}
}
}
}
if (xData != null) {
xa.invalidateRange(xData);
}
if (yData != null) {
ya.invalidateRange(yData);
}
}
}
use of io.bitsquare.gui.main.market.trades.charts.CandleData in project bitsquare by bitsquare.
the class CandleStickChart method layoutPlotChildren.
// -------------- METHODS ------------------------------------------------------------------------------------------
/**
* Called to update and layout the content for the plot
*/
@Override
protected void layoutPlotChildren() {
// we have nothing to layout if no data is present
if (getData() == null) {
return;
}
// update candle positions
for (int seriesIndex = 0; seriesIndex < getData().size(); seriesIndex++) {
XYChart.Series<Number, Number> series = getData().get(seriesIndex);
Iterator<XYChart.Data<Number, Number>> iter = getDisplayedDataIterator(series);
Path seriesPath = null;
if (series.getNode() instanceof Path) {
seriesPath = (Path) series.getNode();
seriesPath.getElements().clear();
}
while (iter.hasNext()) {
XYChart.Data<Number, Number> item = iter.next();
double x = getXAxis().getDisplayPosition(getCurrentDisplayedXValue(item));
double y = getYAxis().getDisplayPosition(getCurrentDisplayedYValue(item));
Node itemNode = item.getNode();
CandleData candleData = (CandleData) item.getExtraValue();
if (itemNode instanceof Candle && candleData != null) {
Candle candle = (Candle) itemNode;
double close = getYAxis().getDisplayPosition(candleData.close);
double high = getYAxis().getDisplayPosition(candleData.high);
double low = getYAxis().getDisplayPosition(candleData.low);
// calculate candle width
double candleWidth = -1;
if (getXAxis() instanceof NumberAxis) {
NumberAxis xa = (NumberAxis) getXAxis();
// use 90% width between ticks
candleWidth = xa.getDisplayPosition(xa.getTickUnit()) * 0.90;
}
// update candle
candle.update(close - y, high - y, low - y, candleWidth);
candle.updateTooltip(candleData);
// position the candle
candle.setLayoutX(x);
candle.setLayoutY(y);
}
if (seriesPath != null && candleData != null) {
final double displayPosition = getYAxis().getDisplayPosition(candleData.average);
if (seriesPath.getElements().isEmpty())
seriesPath.getElements().add(new MoveTo(x, displayPosition));
else
seriesPath.getElements().add(new LineTo(x, displayPosition));
}
}
}
}
use of io.bitsquare.gui.main.market.trades.charts.CandleData in project bitsquare by bitsquare.
the class VolumeChart method layoutPlotChildren.
@Override
protected void layoutPlotChildren() {
if (getData() == null) {
return;
}
for (int seriesIndex = 0; seriesIndex < getData().size(); seriesIndex++) {
XYChart.Series<Number, Number> series = getData().get(seriesIndex);
Iterator<XYChart.Data<Number, Number>> iter = getDisplayedDataIterator(series);
while (iter.hasNext()) {
XYChart.Data<Number, Number> item = iter.next();
double x = getXAxis().getDisplayPosition(getCurrentDisplayedXValue(item));
double y = getYAxis().getDisplayPosition(getCurrentDisplayedYValue(item));
Node itemNode = item.getNode();
CandleData candleData = (CandleData) item.getExtraValue();
if (itemNode instanceof VolumeBar && candleData != null) {
VolumeBar volumeBar = (VolumeBar) itemNode;
double candleWidth = -1;
if (getXAxis() instanceof NumberAxis) {
NumberAxis xa = (NumberAxis) getXAxis();
// use 90% width between ticks
candleWidth = xa.getDisplayPosition(xa.getTickUnit()) * 0.90;
}
// 97 is visible chart data height if chart height is 140.
// So we subtract 43 form the height to get the height for the bar to the bottom.
// Did not find a way how to request the chart data height
final double height = getHeight() - 43;
// We want min 5px height to allow tooltips
double upperYPos = Math.min(height - 5, y);
volumeBar.update(height - upperYPos, candleWidth, candleData);
volumeBar.setLayoutX(x);
volumeBar.setLayoutY(upperYPos);
}
}
}
}
Aggregations