Search in sources :

Example 31 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project ta4j by ta4j.

the class CandlestickChartWithChopIndicator method main.

public static void main(String[] args) {
    series = CsvTradesLoader.loadBitstampSeries();
    /*
         * Create the OHLC dataset from the data series
         */
    OHLCDataset ohlcDataset = createOHLCDataset(series);
    /*
         * Create volume dataset
         */
    TimeSeriesCollection xyDataset = createAdditionalDataset(series);
    /*
         * add the CHOP Indicator
         */
    TimeSeriesCollection chopSeries = createChopDataset(series);
    /*
         * Display the chart
         */
    displayChart(ohlcDataset, xyDataset, chopSeries);
}
Also used : OHLCDataset(org.jfree.data.xy.OHLCDataset) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection)

Example 32 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project ta4j by ta4j.

the class CandlestickChart method main.

public static void main(String[] args) {
    /*
         * Getting bar series
         */
    BarSeries series = CsvTradesLoader.loadBitstampSeries();
    /*
         * Creating the OHLC dataset
         */
    OHLCDataset ohlcDataset = createOHLCDataset(series);
    /*
         * Creating the additional dataset
         */
    TimeSeriesCollection xyDataset = createAdditionalDataset(series);
    /*
         * Creating the chart
         */
    JFreeChart chart = ChartFactory.createCandlestickChart("Bitstamp BTC price", "Time", "USD", ohlcDataset, true);
    // Candlestick rendering
    CandlestickRenderer renderer = new CandlestickRenderer();
    renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
    XYPlot plot = chart.getXYPlot();
    plot.setRenderer(renderer);
    // Additional dataset
    int index = 1;
    plot.setDataset(index, xyDataset);
    plot.mapDatasetToRangeAxis(index, 0);
    XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true, false);
    renderer2.setSeriesPaint(index, Color.blue);
    plot.setRenderer(index, renderer2);
    // Misc
    plot.setRangeGridlinePaint(Color.lightGray);
    plot.setBackgroundPaint(Color.white);
    NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
    numberAxis.setAutoRangeIncludesZero(false);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    /*
         * Displaying the chart
         */
    displayChart(chart);
}
Also used : OHLCDataset(org.jfree.data.xy.OHLCDataset) NumberAxis(org.jfree.chart.axis.NumberAxis) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) BarSeries(org.ta4j.core.BarSeries) CandlestickRenderer(org.jfree.chart.renderer.xy.CandlestickRenderer) JFreeChart(org.jfree.chart.JFreeChart)

Example 33 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project ta4j by ta4j.

the class CashFlowToChart method main.

public static void main(String[] args) {
    // Getting the bar series
    BarSeries series = CsvTradesLoader.loadBitstampSeries();
    // Building the trading strategy
    Strategy strategy = MovingMomentumStrategy.buildStrategy(series);
    // Running the strategy
    BarSeriesManager seriesManager = new BarSeriesManager(series);
    TradingRecord tradingRecord = seriesManager.run(strategy);
    // Getting the cash flow of the resulting positions
    CashFlow cashFlow = new CashFlow(series, tradingRecord);
    /*
         * Building chart datasets
         */
    TimeSeriesCollection datasetAxis1 = new TimeSeriesCollection();
    datasetAxis1.addSeries(buildChartBarSeries(series, new ClosePriceIndicator(series), "Bitstamp Bitcoin (BTC)"));
    TimeSeriesCollection datasetAxis2 = new TimeSeriesCollection();
    datasetAxis2.addSeries(buildChartBarSeries(series, cashFlow, "Cash Flow"));
    /*
         * Creating the chart
         */
    JFreeChart chart = // title
    ChartFactory.createTimeSeriesChart(// title
    "Bitstamp BTC", // x-axis label
    "Date", // y-axis label
    "Price", // data
    datasetAxis1, // create legend?
    true, // generate tooltips?
    true, // generate URLs?
    false);
    XYPlot plot = (XYPlot) chart.getPlot();
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("MM-dd HH:mm"));
    /*
         * Adding the cash flow axis (on the right)
         */
    addCashFlowAxis(plot, datasetAxis2);
    /*
         * Displaying the chart
         */
    displayChart(chart);
}
Also used : BarSeriesManager(org.ta4j.core.BarSeriesManager) CashFlow(org.ta4j.core.analysis.CashFlow) DateAxis(org.jfree.chart.axis.DateAxis) TradingRecord(org.ta4j.core.TradingRecord) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) BarSeries(org.ta4j.core.BarSeries) MovingMomentumStrategy(ta4jexamples.strategies.MovingMomentumStrategy) Strategy(org.ta4j.core.Strategy) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) SimpleDateFormat(java.text.SimpleDateFormat) JFreeChart(org.jfree.chart.JFreeChart)

Example 34 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project idempiere by idempiere.

the class ChartBuilder method getXYDataset.

public IntervalXYDataset getXYDataset() {
    dataset = new TimeSeriesCollection();
    loadData();
    return (IntervalXYDataset) dataset;
}
Also used : TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) IntervalXYDataset(org.jfree.data.xy.IntervalXYDataset)

Example 35 with TimeSeriesCollection

use of org.jfree.data.time.TimeSeriesCollection in project idempiere by idempiere.

the class ChartBuilder method addData.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void addData(MChartDatasource ds) {
    String value = ds.getValueColumn();
    String category;
    String unit = "D";
    if (!chartModel.isTimeSeries())
        category = ds.getCategoryColumn();
    else {
        if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Week)) {
            unit = "W";
        } else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Month)) {
            unit = "MM";
        } else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Quarter)) {
            unit = "Q";
        } else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Year)) {
            unit = "Y";
        }
        category = " TRUNC(" + ds.getDateColumn() + ", '" + unit + "') ";
    }
    String series = DB.TO_STRING(ds.getName());
    boolean hasSeries = false;
    if (ds.getSeriesColumn() != null) {
        series = ds.getSeriesColumn();
        hasSeries = true;
    }
    String where = ds.getWhereClause();
    if (!Util.isEmpty(where)) {
        where = Env.parseContext(Env.getCtx(), chartModel.getWindowNo(), where, true);
    }
    boolean hasWhere = false;
    String sql = "SELECT " + value + ", " + category + ", " + series + " FROM " + ds.getFromClause();
    if (!Util.isEmpty(where)) {
        sql += " WHERE " + where;
        hasWhere = true;
    }
    Date currentDate = Env.getContextAsDate(Env.getCtx(), Env.DATE);
    Date startDate = null;
    Date endDate = null;
    int scope = chartModel.getTimeScope();
    int offset = ds.getTimeOffset();
    if (chartModel.isTimeSeries() && scope != 0) {
        offset += -scope;
        startDate = increment(currentDate, chartModel.getTimeUnit(), offset);
        endDate = increment(startDate, chartModel.getTimeUnit(), scope);
    }
    if (startDate != null && endDate != null) {
        sql += hasWhere ? " AND " : " WHERE ";
        sql += category + ">=TRUNC(" + DB.TO_DATE(new Timestamp(startDate.getTime())) + ", '" + unit + "') AND ";
        sql += category + "<=TRUNC(" + DB.TO_DATE(new Timestamp(endDate.getTime())) + ", '" + unit + "') ";
    }
    if (sql.indexOf('@') >= 0) {
        sql = Env.parseContext(Env.getCtx(), 0, sql, false, true);
    }
    MRole role = MRole.getDefault(Env.getCtx(), false);
    sql = role.addAccessSQL(sql, null, true, false);
    if (hasSeries)
        sql += " GROUP BY " + series + ", " + category + " ORDER BY " + series + ", " + category;
    else
        sql += " GROUP BY " + category + " ORDER BY " + category;
    log.log(Level.FINE, sql);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    TimeSeries tseries = null;
    Dataset dataset = getDataset();
    try {
        pstmt = DB.prepareStatement(sql, null);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            String key = rs.getString(2);
            String seriesName = rs.getString(3);
            if (seriesName == null)
                seriesName = ds.getName();
            String queryWhere = "";
            if (hasWhere)
                queryWhere += where + " AND ";
            queryWhere += series + " = " + DB.TO_STRING(seriesName) + " AND " + category + " = ";
            if (chartModel.isTimeSeries() && dataset instanceof TimeSeriesCollection) {
                if (tseries == null || !tseries.getKey().equals(seriesName)) {
                    if (tseries != null)
                        ((TimeSeriesCollection) dataset).addSeries(tseries);
                    tseries = new TimeSeries(seriesName);
                }
                Date date = rs.getDate(2);
                RegularTimePeriod period = null;
                if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Day))
                    period = new Day(date);
                else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Week))
                    period = new Week(date);
                else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Month))
                    period = new Month(date);
                else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Quarter))
                    period = new Quarter(date);
                else if (chartModel.getTimeUnit().equals(MChart.TIMEUNIT_Year))
                    period = new Year(date);
                tseries.add(period, rs.getBigDecimal(1));
                key = period.toString();
                queryWhere += DB.TO_DATE(new Timestamp(date.getTime()));
            } else {
                queryWhere += DB.TO_STRING(key);
            }
            MQuery query = new MQuery(ds.getAD_Table_ID());
            String keyCol = MTable.get(Env.getCtx(), ds.getAD_Table_ID()).getKeyColumns()[0];
            String whereClause = keyCol + " IN (SELECT " + ds.getKeyColumn() + " FROM " + ds.getFromClause() + " WHERE " + queryWhere + " )";
            query.addRestriction(whereClause.toString());
            query.setRecordCount(1);
            HashMap<String, MQuery> map = getQueries();
            if (dataset instanceof DefaultPieDataset) {
                ((DefaultPieDataset) dataset).setValue(key, rs.getBigDecimal(1));
                map.put(key, query);
            } else if (dataset instanceof DefaultCategoryDataset) {
                ((DefaultCategoryDataset) dataset).addValue(rs.getBigDecimal(1), seriesName, key);
                map.put(seriesName + "__" + key, query);
            } else if (dataset instanceof TimeSeriesCollection) {
                map.put(seriesName + "__" + key, query);
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    if (tseries != null)
        ((TimeSeriesCollection) dataset).addSeries(tseries);
}
Also used : Quarter(org.jfree.data.time.Quarter) DBException(org.adempiere.exceptions.DBException) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) TimeSeries(org.jfree.data.time.TimeSeries) SQLException(java.sql.SQLException) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) Dataset(org.jfree.data.general.Dataset) PieDataset(org.jfree.data.general.PieDataset) IntervalXYDataset(org.jfree.data.xy.IntervalXYDataset) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) CategoryDataset(org.jfree.data.category.CategoryDataset) MRole(org.compiere.model.MRole) PreparedStatement(java.sql.PreparedStatement) MQuery(org.compiere.model.MQuery) Timestamp(java.sql.Timestamp) Date(java.util.Date) Month(org.jfree.data.time.Month) Year(org.jfree.data.time.Year) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ResultSet(java.sql.ResultSet) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) Day(org.jfree.data.time.Day) Week(org.jfree.data.time.Week) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset)

Aggregations

TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)90 TimeSeries (org.jfree.data.time.TimeSeries)66 JFreeChart (org.jfree.chart.JFreeChart)35 XYPlot (org.jfree.chart.plot.XYPlot)25 Date (java.util.Date)23 Day (org.jfree.data.time.Day)20 DateAxis (org.jfree.chart.axis.DateAxis)17 Month (org.jfree.data.time.Month)14 SimpleDateFormat (java.text.SimpleDateFormat)13 NumberAxis (org.jfree.chart.axis.NumberAxis)12 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)12 RegularTimePeriod (org.jfree.data.time.RegularTimePeriod)11 Map (java.util.Map)9 Color (java.awt.Color)7 Font (java.awt.Font)7 File (java.io.File)7 List (java.util.List)7 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)7 LocalDate (java.time.LocalDate)6 BasicStroke (java.awt.BasicStroke)5