Search in sources :

Example 1 with XYSeries

use of org.knowm.xchart.XYSeries in project XChange by knowm.

the class DepthChartDemo method main.

public static void main(String[] args) throws IOException {
    // Use the factory to get the version 1 Bitstamp exchange API using default settings
    Exchange bitstampExchange = ExchangeFactory.INSTANCE.createExchange(BitstampExchange.class);
    // Interested in the public market data feed (no authentication)
    MarketDataService marketDataService = bitstampExchange.getMarketDataService();
    System.out.println("fetching data...");
    // Get the current orderbook
    OrderBook orderBook = marketDataService.getOrderBook(CurrencyPair.BTC_USD);
    System.out.println("orderBook = " + orderBook);
    System.out.println("received data.");
    System.out.println("plotting...");
    // Create Chart
    XYChart chart = new XYChartBuilder().width(800).height(600).title("Bitstamp Order Book").xAxisTitle("BTC").yAxisTitle("USD").build();
    // Customize Chart
    chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
    // BIDS
    List<Number> xData = new ArrayList<>();
    List<Number> yData = new ArrayList<>();
    BigDecimal accumulatedBidUnits = new BigDecimal("0");
    for (LimitOrder limitOrder : orderBook.getBids()) {
        if (limitOrder.getLimitPrice().doubleValue() > 5000) {
            xData.add(limitOrder.getLimitPrice());
            accumulatedBidUnits = accumulatedBidUnits.add(limitOrder.getOriginalAmount());
            yData.add(accumulatedBidUnits);
        }
    }
    Collections.reverse(xData);
    Collections.reverse(yData);
    // Bids Series
    XYSeries series = chart.addSeries("bids", xData, yData);
    series.setMarker(SeriesMarkers.NONE);
    // ASKS
    xData = new ArrayList<>();
    yData = new ArrayList<>();
    BigDecimal accumulatedAskUnits = new BigDecimal("0");
    for (LimitOrder limitOrder : orderBook.getAsks()) {
        if (limitOrder.getLimitPrice().doubleValue() < 20000) {
            xData.add(limitOrder.getLimitPrice());
            accumulatedAskUnits = accumulatedAskUnits.add(limitOrder.getOriginalAmount());
            yData.add(accumulatedAskUnits);
        }
    }
    // Asks Series
    series = chart.addSeries("asks", xData, yData);
    series.setMarker(SeriesMarkers.NONE);
    new SwingWrapper(chart).displayChart();
}
Also used : XYSeries(org.knowm.xchart.XYSeries) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) ArrayList(java.util.ArrayList) SwingWrapper(org.knowm.xchart.SwingWrapper) BigDecimal(java.math.BigDecimal) Exchange(org.knowm.xchange.Exchange) BitstampExchange(org.knowm.xchange.bitstamp.BitstampExchange) MarketDataService(org.knowm.xchange.service.marketdata.MarketDataService) XYChart(org.knowm.xchart.XYChart) XYChartBuilder(org.knowm.xchart.XYChartBuilder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder)

Example 2 with XYSeries

use of org.knowm.xchart.XYSeries in project XChange by knowm.

the class DepthChartDemo method main.

public static void main(String[] args) throws IOException {
    // Use the factory to get the version 1 Bitstamp exchange API using default settings
    Exchange ccexExchange = ExchangeFactory.INSTANCE.createExchange(CCEXExchange.class);
    // Interested in the public market data feed (no authentication)
    MarketDataService marketDataService = ccexExchange.getMarketDataService();
    System.out.println("fetching data...");
    // Get the current orderbook
    OrderBook orderBook = marketDataService.getOrderBook(CurrencyPair.XAUR_BTC);
    System.out.println("received data.");
    for (LimitOrder limitOrder : orderBook.getBids()) {
        System.out.println(limitOrder.getType() + " " + limitOrder.getCurrencyPair() + " Limit price: " + limitOrder.getLimitPrice() + " Amount: " + limitOrder.getOriginalAmount());
    }
    for (LimitOrder limitOrder : orderBook.getAsks()) {
        System.out.println(limitOrder.getType() + " " + limitOrder.getCurrencyPair() + " Limit price: " + limitOrder.getLimitPrice() + " Amount: " + limitOrder.getOriginalAmount());
    }
    System.out.println("plotting...");
    // Create Chart
    XYChart chart = new XYChartBuilder().width(800).height(600).title("C-CEX Order Book - Xaurum").xAxisTitle("BTC").yAxisTitle("Amount").build();
    // Customize Chart
    chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
    // BIDS
    List<Number> xData = new ArrayList<>();
    List<Number> yData = new ArrayList<>();
    BigDecimal accumulatedBidUnits = new BigDecimal("0");
    for (LimitOrder limitOrder : orderBook.getBids()) {
        xData.add(limitOrder.getLimitPrice());
        accumulatedBidUnits = accumulatedBidUnits.add(limitOrder.getOriginalAmount());
        yData.add(accumulatedBidUnits);
    }
    Collections.reverse(xData);
    Collections.reverse(yData);
    // Bids Series
    XYSeries series = chart.addSeries("bids", xData, yData);
    series.setMarker(SeriesMarkers.NONE);
    // ASKS
    xData = new ArrayList<>();
    yData = new ArrayList<>();
    BigDecimal accumulatedAskUnits = new BigDecimal("0");
    for (LimitOrder limitOrder : orderBook.getAsks()) {
        xData.add(limitOrder.getLimitPrice());
        accumulatedAskUnits = accumulatedAskUnits.add(limitOrder.getOriginalAmount());
        yData.add(accumulatedAskUnits);
    }
    // Asks Series
    series = chart.addSeries("asks", xData, yData);
    series.setMarker(SeriesMarkers.NONE);
    new SwingWrapper(chart).displayChart();
}
Also used : XYSeries(org.knowm.xchart.XYSeries) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) ArrayList(java.util.ArrayList) SwingWrapper(org.knowm.xchart.SwingWrapper) BigDecimal(java.math.BigDecimal) CCEXExchange(org.knowm.xchange.ccex.CCEXExchange) Exchange(org.knowm.xchange.Exchange) MarketDataService(org.knowm.xchange.service.marketdata.MarketDataService) XYChart(org.knowm.xchart.XYChart) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) XYChartBuilder(org.knowm.xchart.XYChartBuilder)

Example 3 with XYSeries

use of org.knowm.xchart.XYSeries in project XChange by knowm.

the class BitcoiniumRealtimeOrderbookDemo method buildPanel.

public XChartPanel<XYChart> buildPanel() throws IOException {
    System.out.println("fetching data...");
    updateData();
    // create chart
    chart = new XYChartBuilder().width(800).height(400).title("Real-time Bitcoinium Order Book - BITSTAMP_BTC_USD").xAxisTitle("BTC").yAxisTitle("USD").build();
    // Customize Chart
    chart.getStyler().setLegendPosition(LegendPosition.InsideNE);
    chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
    // add series
    XYSeries series = chart.addSeries(BIDS_SERIES_NAME, xAxisBidData, yAxisBidData);
    series.setMarker(SeriesMarkers.NONE);
    series = chart.addSeries(ASKS_SERIES_NAME, xAxisAskData, yAxisAskData);
    series.setMarker(SeriesMarkers.NONE);
    return new XChartPanel<>(chart);
}
Also used : XYSeries(org.knowm.xchart.XYSeries) XChartPanel(org.knowm.xchart.XChartPanel) XYChartBuilder(org.knowm.xchart.XYChartBuilder)

Example 4 with XYSeries

use of org.knowm.xchart.XYSeries in project XChange by knowm.

the class BitcoiniumRealtimeTickerDemo method buildPanel.

public XChartPanel<XYChart> buildPanel() throws IOException {
    System.out.println("fetching data...");
    // Get the latest order book data for BTC/USD - BITSTAMP
    BitcoiniumTickerHistory bitcoiniumTickerHistory = bitcoiniumMarketDataService.getBitcoiniumTickerHistory("BTC", "BITSTAMP_USD", "THREE_HOURS");
    System.out.println(bitcoiniumTickerHistory.toString());
    // build ticker history chart series data
    xAxisData = new ArrayList<>();
    yAxisData = new ArrayList<>();
    for (int i = 0; i < bitcoiniumTickerHistory.getCondensedTickers().length; i++) {
        BitcoiniumTicker bitcoiniumTicker = bitcoiniumTickerHistory.getCondensedTickers()[i];
        Date timestamp = new Date(bitcoiniumTicker.getTimestamp());
        float price = bitcoiniumTicker.getLast().floatValue();
        System.out.println(timestamp + ": " + price);
        xAxisData.add(timestamp);
        yAxisData.add(price);
    }
    // Create Chart
    chart = new XYChartBuilder().width(800).height(600).title("Real-time Bitstamp Price vs. Time").xAxisTitle("Time").yAxisTitle("Price").build();
    // Customize Chart
    chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
    chart.getStyler().setLegendPosition(LegendPosition.InsideNE);
    // add series
    XYSeries series = chart.addSeries(SERIES_NAME, xAxisData, yAxisData);
    series.setMarker(SeriesMarkers.NONE);
    return new XChartPanel(chart);
}
Also used : XYSeries(org.knowm.xchart.XYSeries) BitcoiniumTickerHistory(org.knowm.xchange.bitcoinium.dto.marketdata.BitcoiniumTickerHistory) BitcoiniumTicker(org.knowm.xchange.bitcoinium.dto.marketdata.BitcoiniumTicker) XChartPanel(org.knowm.xchart.XChartPanel) XYChartBuilder(org.knowm.xchart.XYChartBuilder) Date(java.util.Date)

Example 5 with XYSeries

use of org.knowm.xchart.XYSeries in project XChange by knowm.

the class BitcoiniumRealtimeTickerDemo method go.

private void go() throws IOException {
    ExchangeSpecification exchangeSpecification = new ExchangeSpecification(BitcoiniumExchange.class);
    // exchangeSpecification.setPlainTextUri("http://openexchangerates.org");
    exchangeSpecification.setApiKey("42djci5kmbtyzrvglfdw3e2dgmh5mr37");
    System.out.println(exchangeSpecification.toString());
    Exchange bitcoiniumExchange = ExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
    // Interested in the public market data feed (no authentication)
    bitcoiniumMarketDataService = (BitcoiniumMarketDataServiceRaw) bitcoiniumExchange.getMarketDataService();
    // Setup the panel
    final XChartPanel<XYChart> chartPanel = buildPanel();
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            // Create and set up the window.
            JFrame frame = new JFrame("XChart");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(chartPanel);
            // Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    });
    // Simulate a data feed
    TimerTask chartUpdaterTask = new TimerTask() {

        @Override
        public void run() {
            try {
                BitcoiniumTicker bitcoiniumTicker = bitcoiniumMarketDataService.getBitcoiniumTicker("BTC", "BITSTAMP_USD");
                System.out.println(bitcoiniumTicker.toString());
                Date timestamp = new Date(bitcoiniumTicker.getTimestamp());
                float price = bitcoiniumTicker.getLast().floatValue();
                if (xAxisData.get(xAxisData.size() - 1).getTime() != timestamp.getTime()) {
                    xAxisData.add(timestamp);
                    yAxisData.add(price);
                    XYSeries series = chart.updateXYSeries(SERIES_NAME, xAxisData, yAxisData, null);
                    chartPanel.revalidate();
                    chartPanel.repaint();
                    System.out.println(series.getXData());
                    System.out.println(series.getYData());
                } else {
                    System.out.println("No new data.");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    Timer timer = new Timer();
    // every ten
    timer.scheduleAtFixedRate(chartUpdaterTask, 0, 10000);
// seconds
}
Also used : XYSeries(org.knowm.xchart.XYSeries) IOException(java.io.IOException) Date(java.util.Date) BitcoiniumExchange(org.knowm.xchange.bitcoinium.BitcoiniumExchange) Exchange(org.knowm.xchange.Exchange) TimerTask(java.util.TimerTask) Timer(java.util.Timer) JFrame(javax.swing.JFrame) XYChart(org.knowm.xchart.XYChart) ExchangeSpecification(org.knowm.xchange.ExchangeSpecification) BitcoiniumTicker(org.knowm.xchange.bitcoinium.dto.marketdata.BitcoiniumTicker)

Aggregations

XYSeries (org.knowm.xchart.XYSeries)17 XYChart (org.knowm.xchart.XYChart)12 XYChartBuilder (org.knowm.xchart.XYChartBuilder)12 ArrayList (java.util.ArrayList)9 Exchange (org.knowm.xchange.Exchange)8 SwingWrapper (org.knowm.xchart.SwingWrapper)8 BigDecimal (java.math.BigDecimal)5 Date (java.util.Date)5 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)5 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)5 MarketDataService (org.knowm.xchange.service.marketdata.MarketDataService)5 BasicStroke (java.awt.BasicStroke)3 ExchangeSpecification (org.knowm.xchange.ExchangeSpecification)3 BitcoiniumExchange (org.knowm.xchange.bitcoinium.BitcoiniumExchange)3 BitcoiniumTicker (org.knowm.xchange.bitcoinium.dto.marketdata.BitcoiniumTicker)3 IOException (java.io.IOException)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 BitcoiniumTickerHistory (org.knowm.xchange.bitcoinium.dto.marketdata.BitcoiniumTickerHistory)2 BitcoiniumMarketDataServiceRaw (org.knowm.xchange.bitcoinium.service.BitcoiniumMarketDataServiceRaw)2