Skip to content
Prev 4914 / 15274 Next

Possible enhancement to volatility in TTR

Hi Cedrick,

Sorry for the slow response.  How about this quick change to
TTR::volatility() and the code below?  I agree that OHLC should be
allowed to be univariate in the case of calc="close".  I'm hesitant to
add the other logic though, since the same results can be achieved
with an external call to apply() -- see below.

volatility3 <-
function (OHLC, n = 10, N = 260, calc = "close", ...)
{
<snip>
    if (calc == "close") {
        # Add univariate case for calc="close"
        if (NCOL(OHLC) == 1) {
            r <- ROC(OHLC, 1, ...)
        } else {
            r <- ROC(OHLC[, 4], 1, ...)
        }
        rBar <- runSum(r, n - 1)/(n - 1)
        s <- sqrt(N/(n - 2) * runSum((r - rBar)^2, n - 1))
    }
<snip>
    reclass(s, OHLC)
}

Then you can get pretty volatilities via a command like:
Open High Low Close
2007-06-25    6    5   5     6
2007-06-26    6    6   6     6
2007-06-27    6    6   6     6
2007-06-28    6    5   6     5
2007-06-29    5    5   6     5
2007-06-30    5    5   6     5
[,1]
2007-06-25 5.656552
2007-06-26 5.729933
2007-06-27 5.732217
2007-06-28 4.771530
2007-06-29 4.764216
2007-06-30 4.818589

HTH,
Josh
--
http://www.fosstrading.com



On Mon, Sep 28, 2009 at 9:34 AM, Cedrick Johnson
<cedrick at cedrickjohnson.com> wrote: