I have found the code snippet for volatility, var95 and shortfall95
from my earlier project and removed all code that is not needed, for
those who are interested. (in this form it has not been tested).

Input: indexValue(), an array of consecutive close prices of an index
or security
Output: volatility(), an array of the volatility. var95(), an array of
value at risk 95%. sf95(), an array of shortfall based on var95

                        // calculate volatilitiy, VAR95 and shortfall
                        int var = 95;
                        // number of trading days in one year = period of 
volatility
                        int volaBars = 252;
                        for (int pf = 0; pf < priceCount; pf++) {
                                List<Float> list = new ArrayList<Float>();
                                int n = Math.min(volaBars, pf);
                                if (n != 0) {
                                        float vBar = 0, v = 0, stddev = 0, 
variance = 0, volatility = 0;
                                        // vBar = averaged sum of log price 
changes
                                        // = average price change
                                        for (int j = pf - n + 1; j <= pf; j++) {
                                                        vBar += 
Math.log(indexValue(j) / indexValue(j - 1));
                                        }
                                        vBar /= n;
                                        // sample standard deviation would be: 
denominator = n - 1
                                        // stddev = square root of averaged sum 
of the squares
                                        // of the daily difference of log price 
changes
                                        // and the average price change
                                        for (int j = pf - n + 1; j <= pf; j++) {
                                                v = (float) 
Math.log(indexValue(j) / indexValue(j - 1));
                                                variance += (v - vBar) * (v - 
vBar);
                                                //change in the index value 
from previous day in percent
                                                list.add((Float) (indexValue(j) 
/ indexValue(j - 1) - 1F) *
100F);
                                        }
                                        // daily variance
                                        variance /= Math.max(1, n - 1);
                                        // daily standard deviation
                                        stddev = (float) Math.sqrt(variance);
                                        // volatility for period of vola bars
                                        volatility(pf) = (float) (stddev * 
Math.sqrt(volaBars));
                                        // var95
                                        Collections.sort(list);
                                        int ind = (int) (n * (1F - var / 100f));
                                        float var95(pf) = list.get(ind);
                                        // calculate shortfall as average of 
worst 5%
                                        // does not include var95 border value 
in shortfall
                                        float sf95(pf) = 0;
                                        for (int j = 0; j < ind; j++) {
                                                sf95(pf) += list.get(j);
                                        }
                                        sf95(pf) /= ind;
                                        if (sf95(pf) == 0) sf95(pf) = var95(pf);
                                }
                        }

You can change the VAR percentage to say 98% (set var = 98) or
calculate monthly instead of yearly volatility (set volaBars = 21).

-- 
You received this message because you are subscribed to the Google Groups 
"JBookTrader" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jbooktrader?hl=en.

Reply via email to