I am trying to overlay a moving average over a OHLC chart but I get no results.
Any ideas on what am I doing wrong ?
private static OHLCDataset createPriceDataset(String filename)
{
//the following data is taken from http://finance.yahoo.com/
//for demo purposes...
OHLCSeries s1 = new OHLCSeries(filename);
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String inputLine;
in.readLine();
while ((inputLine = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(inputLine, ",");
Date date = df.parse( st.nextToken() );
double open = Double.parseDouble( st.nextToken() );
double high = Double.parseDouble( st.nextToken() );
double low = Double.parseDouble( st.nextToken() );
double close = Double.parseDouble( st.nextToken() );
double volume = Double.parseDouble( st.nextToken() );
//double adjClose = Double.parseDouble( st.nextToken() );
s1.add(new Day(date), open, high, low, close);
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
OHLCSeriesCollection dataset = new OHLCSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
private static JFreeChart createCombinedChart()
{
OHLCDataset data1 = createPriceDataset(filename);
XYItemRenderer renderer1 = new HighLowRenderer();
renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
renderer1.setSeriesPaint(0, Color.blue);
DateAxis domainAxis = new DateAxis("Date");
NumberAxis rangeAxis = new NumberAxis("Price");
rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.00"));
rangeAxis.setAutoRange(true);
rangeAxis.setAutoRangeIncludesZero(false);
XYPlot plot1 = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
plot1.setBackgroundPaint(Color.lightGray);
plot1.setDomainGridlinePaint(Color.white);
plot1.setRangeGridlinePaint(Color.white);
plot1.setRangePannable(true);
//Overlay the Long-Term Trend Indicator
XYDataset dataset3 = MovingAverage.createMovingAverage(data1, "LT", 49.0, 49.0);
plot1.setDataset(1, dataset3);
plot1.setRenderer(1, new StandardXYItemRenderer());
//add a second dataset (volume) and renderer
IntervalXYDataset data2 = createVolumeDataset(filename);
XYBarRenderer renderer2 = new XYBarRenderer();
renderer2.setDrawBarOutline(false);
renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")));
renderer2.setSeriesPaint(0, Color.red);
XYPlot plot2 = new XYPlot(data2, null, new NumberAxis("Volume"), renderer2);
plot2.setBackgroundPaint(Color.lightGray);
plot2.setDomainGridlinePaint(Color.white);
plot2.setRangeGridlinePaint(Color.white);
CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(domainAxis);
cplot.add(plot1, 3);
cplot.add(plot2, 2);
cplot.setGap(8.0);
cplot.setDomainGridlinePaint(Color.white);
cplot.setDomainGridlinesVisible(true);
cplot.setDomainPannable(true);
//return the new combined chart
JFreeChart chart = new JFreeChart("Sun Microsystems (SUNW)", JFreeChart.DEFAULT_TITLE_FONT, cplot, false);
ChartUtilities.applyCurrentTheme(chart);
renderer2.setShadowVisible(false);
renderer2.setBarPainter(new StandardXYBarPainter());
return chart;
}