|
Calculate volatilities / 52 week high low |
Posted by icasper on Feb-17-2012 08:36 |
|
Hi,
I am about to get the product register. The product is super great. quick questions.
1) I would like to calculate the historical volatilitie say 60 days volatilty with 256 days count, for a stocks. I have data in the following format
Date|stock price.
Not sure if arrayMap can help here, can u provide a sample php code.
2) How to calculate say 52 week high low on the above series given a date range and then be able to plot on the line graph. i.e. plot 52 week high on the chart.
Many Thanks... |
Re: Calculate volatilities / 52 week high low |
Posted by Peter Kwan on Feb-18-2012 01:55 |
|
Hi icasper,
To plot any technical indicator that is not built into ChartDirector, you would need use your own code to compute them. After that you may pass them to ChartDirector to be plotted. Your code can use ArrayMath if you like, but it is not mandatory.
For the historical volatility, as it is not a built-in technical indicator in ChartDirector, you would need to write your own code to compute it. I can write some short code for you, but it is not tested, and you would need to debug them yourself.
As far as I know, there is not a well-accepted definition of historical volatility. The following is based on the historical volatility defining as the moving standard deviation of the logarithmic return.
$result[0] = NoValue;
for ($i = 1; $i < count($myData); ++i)
$result[$i] = log($myData[$i] / myData[$i - 1]);
$a = new ArrayMath($result);
$a->movStdDev(3);
$result = $a->result();
$myIndicator = $myFinanceChart->addIndicator(70);
$myLineLayer = $myFinanceChart->addLineIndicator2($myIndicator, $result, 0xcc0000, "Historical Volatility");
For the lowest point, the code is essentially the same as the code I suggsted in:
http://www.chartdir.com/forum/download_thread.php?site=chartdir&bn=chartdir_support&thread=1329224842&new_thread=1#N1329329602
The code is:
$visibleData = array_slice($result, $extraPoint); #extraPoint is the extraPoint parameter you use in setData
$a = new ArrayMath($visibleData);
$lowIndex = $a->minIndex();
$t = $myLineLayer->addCustomDataLabel(0, $lowIndex, $visibleData[$lowIndex], "arial.ttf", 8, 0x000000);
$t->setAlignment(Bottom);
$t->setBackground(0xff8888);
The code for the highest point is essentially the same, with the minIndex replace with maxIndex, and the Bottom alignment with Top alignment.
Hope this can help.
Regards
Peter Kwan |
|