private OHLCDataset getOHLCDataset(List<Candle> chartDatas) { final int size = chartDatas.size(); Date[] date = new Date[size]; double[] high = new double[size]; double[] low = new double[size]; double[] open = new double[size]; double[] close = new double[size]; double[] volume = new double[size]; int i = 0; for(Candle chartData : chartDatas) { date[i] = new Date(chartData.getStartTimeStamp()); high[i] = chartData.getHigh(); low[i] = chartData.getLow(); open[i] = chartData.getOpen(); close[i] = chartData.getClose(); volume[i] = chartData.getVolume(); i++; } return new DefaultHighLowDataset("Price", date, high, low, open, close, volume); }
/** * Builds a JFreeChart OHLC dataset from a ta4j time series. * @param series a time series * @return an Open-High-Low-Close dataset */ private static OHLCDataset createOHLCDataset(TimeSeries series) { final int nbTicks = series.getTickCount(); Date[] dates = new Date[nbTicks]; double[] opens = new double[nbTicks]; double[] highs = new double[nbTicks]; double[] lows = new double[nbTicks]; double[] closes = new double[nbTicks]; double[] volumes = new double[nbTicks]; for (int i = 0; i < nbTicks; i++) { Tick tick = series.getTick(i); dates[i] = new Date(tick.getEndTime().toEpochSecond() * 1000); opens[i] = tick.getOpenPrice().toDouble(); highs[i] = tick.getMaxPrice().toDouble(); lows[i] = tick.getMinPrice().toDouble(); closes[i] = tick.getClosePrice().toDouble(); volumes[i] = tick.getVolume().toDouble(); } OHLCDataset dataset = new DefaultHighLowDataset("btc", dates, highs, lows, opens, closes, volumes); return dataset; }
/** * Creates and returns a default instance of a candlesticks chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return a candlestick chart. */ public static JFreeChart createCandlestickChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, null); plot.setRenderer(new CandlestickRenderer()); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a high-low-open-close chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return a high-low-open-close chart. */ public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); HighLowRenderer renderer = new HighLowRenderer(); renderer.setToolTipGenerator(new HighLowItemLabelGenerator()); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a high-low-open-close chart with * a special timeline. This timeline can be a {@link org.jfree.chart.axis.SegmentedTimeline} * such as the Monday trough Friday timeline that will remove Saturdays and Sundays from * the axis. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param timeline the timeline. * @param legend a flag specifying whether or not a legend is required. * * @return a high-low-open-close chart. */ public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, Timeline timeline, boolean legend) { DateAxis timeAxis = new DateAxis(timeAxisLabel); timeAxis.setTimeline(timeline); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); HighLowRenderer renderer = new HighLowRenderer(); renderer.setToolTipGenerator(new HighLowItemLabelGenerator()); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a candlesticks chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> * permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> * permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return A candlestick chart. */ public static JFreeChart createCandlestickChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, null); plot.setRenderer(new CandlestickRenderer()); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a high-low-open-close chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> * permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> * permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return A high-low-open-close chart. */ public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); HighLowRenderer renderer = new HighLowRenderer(); renderer.setBaseToolTipGenerator(new HighLowItemLabelGenerator()); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a high-low-open-close chart * with a special timeline. This timeline can be a * {@link org.jfree.chart.axis.SegmentedTimeline} such as the Monday * through Friday timeline that will remove Saturdays and Sundays from * the axis. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> * permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> * permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param timeline the timeline. * @param legend a flag specifying whether or not a legend is required. * * @return A high-low-open-close chart. */ public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, Timeline timeline, boolean legend) { DateAxis timeAxis = new DateAxis(timeAxisLabel); timeAxis.setTimeline(timeline); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); HighLowRenderer renderer = new HighLowRenderer(); renderer.setBaseToolTipGenerator(new HighLowItemLabelGenerator()); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a high-low-open-close chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> * permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> * permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return A high-low-open-close chart. */ public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); HighLowRenderer renderer = new HighLowRenderer(); renderer.setBaseToolTipGenerator(new HighLowItemLabelGenerator()); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); currentTheme.apply(chart); return chart; }
/** * Creates and returns a default instance of a high-low-open-close chart. * * @param title the chart title ({@code null} permitted). * @param timeAxisLabel a label for the time axis ({@code null} * permitted). * @param valueAxisLabel a label for the value axis ({@code null} * permitted). * @param dataset the dataset for the chart ({@code null} permitted). * @param legend a flag specifying whether or not a legend is required. * * @return A high-low-open-close chart. */ public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); HighLowRenderer renderer = new HighLowRenderer(); renderer.setDefaultToolTipGenerator(new HighLowItemLabelGenerator()); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); currentTheme.apply(chart); return chart; }
/** * Creates and returns a default instance of a candlesticks chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> * permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> * permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return A candlestick chart. */ public static JFreeChart createCandlestickChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, null); plot.setRenderer(new CandlestickRenderer()); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); currentTheme.apply(chart); return chart; }
/** * Creates and returns a default instance of a high-low-open-close chart * with a special timeline. This timeline can be a * {@link org.jfree.chart.axis.SegmentedTimeline} such as the Monday * through Friday timeline that will remove Saturdays and Sundays from * the axis. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> * permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> * permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param timeline the timeline. * @param legend a flag specifying whether or not a legend is required. * * @return A high-low-open-close chart. */ public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, Timeline timeline, boolean legend) { DateAxis timeAxis = new DateAxis(timeAxisLabel); timeAxis.setTimeline(timeline); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); HighLowRenderer renderer = new HighLowRenderer(); renderer.setBaseToolTipGenerator(new HighLowItemLabelGenerator()); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); currentTheme.apply(chart); return chart; }
private JFreeChart createCandlestickChart(OHLCDataset priceOHLCDataset) { final String title = "Chart"; final ValueAxis timeAxis = new DateAxis("Date"); final NumberAxis valueAxis = new NumberAxis("Price"); valueAxis.setAutoRangeIncludesZero(false); valueAxis.setUpperMargin(0.0); valueAxis.setLowerMargin(0.0); XYPlot plot = new XYPlot(priceOHLCDataset, timeAxis, valueAxis, null); final CandlestickRenderer candlestickRenderer = new CandlestickRenderer(); plot.setRenderer(candlestickRenderer); //plot.getRangeAxis().setAutoRange(true); // Give good width when zoom in, but too slow in calculation. ((CandlestickRenderer)plot.getRenderer()).setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST); CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(timeAxis); cplot.add(plot, 3); cplot.setGap(8.0); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, cplot, true); applyChartTheme(chart); // Handle zooming event. chart.addChangeListener(this.getChartChangeListner()); return chart; }
private double getLowestLow(OHLCDataset dataset, int start, int end){ double lowest; lowest = dataset.getLowValue(0,start); for(int i=1;i<=end;i++){ if(dataset.getLowValue(0,i) < lowest){ lowest = dataset.getLowValue(0,i); } } return lowest; }
private double getHighestHigh(OHLCDataset dataset, int start, int end){ double highest; highest = dataset.getHighValue(0,start); for(int i=1;i<=end;i++){ if(dataset.getLowValue(0,i) > highest){ highest = dataset.getHighValue(0,i); } } return highest; }
/** * Initialises the renderer then returns the number of 'passes' through the data that the * renderer will require (usually just one). This method will be called before the first * item is rendered, giving the renderer an opportunity to initialise any * state information it wants to maintain. The renderer can do nothing if it chooses. * * @param g2 the graphics device. * @param dataArea the area inside the axes. * @param plot the plot. * @param dataset the data. * @param info an optional info collection object to return data back to the caller. * * @return The number of passes the renderer requires. */ public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, XYPlot plot, XYDataset dataset, PlotRenderingInfo info) { // calculate the maximum allowed candle width from the axis... ValueAxis axis = plot.getDomainAxis(); double x1 = axis.getLowerBound(); double x2 = x1 + this.maxCandleWidthInMilliseconds; RectangleEdge edge = plot.getDomainAxisEdge(); double xx1 = axis.valueToJava2D(x1, dataArea, edge); double xx2 = axis.valueToJava2D(x2, dataArea, edge); this.maxCandleWidth = Math.abs(xx2 - xx1); // Absolute value, since the relative x // positions are reversed for horizontal orientation // calculate the highest volume in the dataset... if (this.drawVolume) { OHLCDataset highLowDataset = (OHLCDataset) dataset; this.maxVolume = 0.0; for (int series = 0; series < highLowDataset.getSeriesCount(); series++) { for (int item = 0; item < highLowDataset.getItemCount(series); item++) { double volume = highLowDataset.getVolumeValue(series, item); if (volume > this.maxVolume) { this.maxVolume = volume; } } } } return new XYItemRendererState(info); }
/** * Generates a tooltip text item for a particular item within a series. * * @param dataset the dataset. * @param series the series (zero-based index). * @param item the item (zero-based index). * * @return the tooltip text. */ public String generateToolTip(XYDataset dataset, int series, int item) { String result = null; if (dataset instanceof OHLCDataset) { OHLCDataset d = (OHLCDataset) dataset; Number high = d.getHigh(series, item); Number low = d.getLow(series, item); Number open = d.getOpen(series, item); Number close = d.getClose(series, item); Number x = d.getX(series, item); result = d.getSeriesName(series); if (x != null) { Date date = new Date(x.longValue()); result = result + "--> Date=" + this.dateFormatter.format(date); if (high != null) { result = result + " High=" + this.numberFormatter.format(high.doubleValue()); } if (low != null) { result = result + " Low=" + this.numberFormatter.format(low.doubleValue()); } if (open != null) { result = result + " Open=" + this.numberFormatter.format(open.doubleValue()); } if (close != null) { result = result + " Close=" + this.numberFormatter.format(close.doubleValue()); } } } return result; }
/** * Iterates over the data item of the xy dataset to find * the range extent. * * @param dataset the dataset (<code>null</code> not permitted). * * @return The range (possibly <code>null</code>). */ public static Range iterateXYRangeExtent(XYDataset dataset) { double minimum = Double.POSITIVE_INFINITY; double maximum = Double.NEGATIVE_INFINITY; int seriesCount = dataset.getSeriesCount(); for (int series = 0; series < seriesCount; series++) { int itemCount = dataset.getItemCount(series); for (int item = 0; item < itemCount; item++) { double lvalue; double uvalue; if (dataset instanceof IntervalXYDataset) { IntervalXYDataset intervalXYData = (IntervalXYDataset) dataset; lvalue = intervalXYData.getStartYValue(series, item); uvalue = intervalXYData.getEndYValue(series, item); } else if (dataset instanceof OHLCDataset) { OHLCDataset highLowData = (OHLCDataset) dataset; lvalue = highLowData.getLowValue(series, item); uvalue = highLowData.getHighValue(series, item); } else { lvalue = dataset.getYValue(series, item); uvalue = lvalue; } if (!Double.isNaN(lvalue)) { minimum = Math.min(minimum, lvalue); } if (!Double.isNaN(uvalue)) { maximum = Math.max(maximum, uvalue); } } } if (minimum == Double.POSITIVE_INFINITY) { return null; } else { return new Range(minimum, maximum); } }
/** * Initialises the renderer then returns the number of 'passes' through the * data that the renderer will require (usually just one). This method * will be called before the first item is rendered, giving the renderer * an opportunity to initialise any state information it wants to maintain. * The renderer can do nothing if it chooses. * * @param g2 the graphics device. * @param dataArea the area inside the axes. * @param plot the plot. * @param dataset the data. * @param info an optional info collection object to return data back to * the caller. * * @return The number of passes the renderer requires. */ public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, XYPlot plot, XYDataset dataset, PlotRenderingInfo info) { // calculate the maximum allowed candle width from the axis... ValueAxis axis = plot.getDomainAxis(); double x1 = axis.getLowerBound(); double x2 = x1 + this.maxCandleWidthInMilliseconds; RectangleEdge edge = plot.getDomainAxisEdge(); double xx1 = axis.valueToJava2D(x1, dataArea, edge); double xx2 = axis.valueToJava2D(x2, dataArea, edge); this.maxCandleWidth = Math.abs(xx2 - xx1); // Absolute value, since the relative x // positions are reversed for horizontal orientation // calculate the highest volume in the dataset... if (this.drawVolume) { OHLCDataset highLowDataset = (OHLCDataset) dataset; this.maxVolume = 0.0; for (int series = 0; series < highLowDataset.getSeriesCount(); series++) { for (int item = 0; item < highLowDataset.getItemCount(series); item++) { double volume = highLowDataset.getVolumeValue(series, item); if (volume > this.maxVolume) { this.maxVolume = volume; } } } } return new XYItemRendererState(info); }