|
Volume Moving Average |
Posted by Steve on Jun-30-2010 08:56 |
|
Hi Peter,
I'm trying to add a moving average line to overlap the volume bars. Can you take a look at the sample code and the changes I have made and see why the linelayer is not showing up?
Thank you
protected void Page_Load(object sender, EventArgs e)
{
int noOfDays = 100;
int extraDays = 30;
RanTable rantable = new RanTable(9, 6, noOfDays + extraDays);
rantable.setDateCol(0, new DateTime(2002, 9, 4), 86400, true);
rantable.setHLOCCols(1, 100, -5, 5);
rantable.setCol(5, 50000000, 250000000);
// Now we read the data from the table into arrays
double[] timeStamps = rantable.getCol(0);
double[] highData = rantable.getCol(1);
double[] lowData = rantable.getCol(2);
double[] openData = rantable.getCol(3);
double[] closeData = rantable.getCol(4);
double[] volData = rantable.getCol(5);
FinanceChart c = new FinanceChart(640);
c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays);
XYChart mc = c.addMainChart(240);
c.addVolBars(75, 0x99ff99, 0xff9999, 0x808080);
//add 50 day volume average line
ArrayMath am = new ArrayMath(volData);
double[] myMovingAvg = am.movAvg(50).result();
mc.addLineLayer(myMovingAvg, 0x000000);
c.addLineIndicator2(mc, myMovingAvg, 0xcc6600, "").moveFront();
//hide Volume Bar Scale
mc.yAxis2().setColors(Chart.Transparent, Chart.Transparent, Chart.Transparent, Chart.Transparent);
mc.yAxis().copyAxis(mc.yAxis2());
WebChartViewer1.Image = c.makeWebImage(Chart.PNG);
}
|
Re: Volume Moving Average |
Posted by Steve on Jul-01-2010 00:27 |
|
Hi Peter,
When I change the chart to use
mc.yAxis2().setLinearScale(0, xxx);
the moving average line shows up.. (depending on the data and what xxx is)
But, I am a bit confused about setting the upperlimit..
Is the range from 0-100 only and I'd have to scale the values based on the data?
Many times I am ending up with a blank chart.
When my scale is large, the moving average volume line shows up but the volume bars are too small and don't show up. |
Re: Volume Moving Average |
Posted by Peter Kwan on Jul-01-2010 01:15 |
|
Hi Steve,
There are two issues:
(a) The volume bars in the FinanceChart is not actually added using the original data, but is adding using the "scaled data". For example, if the maximum volume is 300000000, the FinanceChart will actually use 300 as the data value (that is, divide the value by 1000000), and append an "M" to the axis label (so the axis label reads 300M).
If you add a line layer, the line layer will need to use the same scaled value to be consistent with the bar layer.
(b) In the FinanceChart, the price information uses the primary y-axis, while the volume information uses the secondary y-axis. So the line layer should bind to the secondary y-axis.
I have come up with the following code, which should work:
protected void Page_Load(object sender, EventArgs e)
{
int noOfDays = 100;
int extraDays = 30;
RanTable rantable = new RanTable(9, 6, noOfDays + extraDays);
rantable.setDateCol(0, new DateTime(2002, 9, 4), 86400, true);
rantable.setHLOCCols(1, 100, -5, 5);
rantable.setCol(5, 50000000, 250000000);
// Now we read the data from the table into arrays
double[] timeStamps = rantable.getCol(0);
double[] highData = rantable.getCol(1);
double[] lowData = rantable.getCol(2);
double[] openData = rantable.getCol(3);
double[] closeData = rantable.getCol(4);
double[] volData = rantable.getCol(5);
FinanceChart c = new FinanceChart(640);
c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays);
XYChart mc = c.addMainChart(240);
c.addVolBars(75, 0x99ff99, 0xff9999, 0x808080);
ArrayMath am = new ArrayMath(volData);
double maxVol = am.max();
if (maxVol >= 1000000000)
am.div(1000000000);
else if (maxVol >= 1000000)
am.div(1000000);
else if (maxVol >= 1000)
am.div(1000);
//add 50 day volume average line
double[] myMovingAvg = am.movAvg(50).result();
Layer layer = c.addLineIndicator2(mc, myMovingAvg, 0xcc6600, "");
layer.moveFront();
layer.setUseYAxis2();
WebChartViewer1.Image = c.makeWebImage(Chart.PNG);
}
Hope this can help.
Regards
Peter Kwan |
Re: Volume Moving Average |
Posted by Steve on Jul-01-2010 04:14 |
|
Thanks Peter! That cleared up a lot of my questions! |
|