Page 1 of 1

Add moving average to candle stick

Posted: Fri Oct 31, 2014 9:25 pm
by paphus
I'm trying to add a moving average to a candlestick chart. It should be simple, but doesn't seem to work.

Code: Select all

JFreeChart chart = ChartFactory.createCandlestickChart(title, "date", "price", dataSet, true);
 XYPlot xyplot = (XYPlot)chart.getPlot();
xyplot.setRangeAxis(1, new NumberAxis("Value 1")); 

XYDataset xydataset = MovingAverage.createMovingAverage(dataSet, "-MA5", 50, 0);
xyplot.setDataset(1, xydataset);
xyplot.setRenderer(1, new StandardXYItemRenderer());
This draw a blue line, but it seems to be an average, not a moving average. It does not lag the candle price, but just connect the average price in each candle?

Help pls.

Re: Add moving average to candle stick

Posted: Fri Nov 07, 2014 8:16 pm
by paphus
I think I found the issue. The time period for the moving average is in milliseconds, not candles, so for 5 day it would be 1000L * 60L *60L * 24L * 5L.