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);
}
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);
}
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);
}
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;
}
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);
}
Aggregations