[R-SIG-Finance] error in yang.zhang volatility{TTR}
Joshua Ulrich
josh.m.ulrich at gmail.com
Wed May 30 05:08:44 CEST 2012
Hi James,
On Mon, May 21, 2012 at 12:40 PM, J Toll <jctoll at gmail.com> wrote:
> On Mon, May 21, 2012 at 11:32 AM, J Toll <jctoll at gmail.com> wrote:
>> Hi,
>>
>> I've been going through the code for the yang.zhang calculation method
>> of volatility in the TTR package and believe I've found the same error
>> as the one from last year on the Close to Close volatility estimator.
>>
>> https://stat.ethz.ch/pipermail/r-sig-finance/2011q2/007989.html
>>
>> Like the previous error, one of the first clues that there was a
>> problem was an excessive number of NA values generated at the
>> beginning of the output (e.g. 40 NA's for n = 20).
>>
>> I believe the errors are in these two calculations:
>>
>> s2o <- N/(n - 1) * runSum((log(OHLC[, 1]/Cl1) - 1/n *
>> runSum(log(OHLC[, 1]/Cl1), n))^2, n)
>> s2c <- N/(n - 1) * runSum((log(OHLC[, 4]/OHLC[, 1]) -
>> 1/n * runSum(log(OHLC[, 4]/OHLC[, 1]), n))^2, n)
>>
<snip>
>>
>> As a simple fix, I would suggest these corrections:
>>
>> s2o <- N * runVar(log(OHLC[, 1] / Cl1), n)
>> s2c <- N * runVar(log(OHLC[, 4] / OHLC[, 1]), n)
>
>
> Sorry, to get that to work, it should actually be:
>
> s2o <- N * runVar(log(OHLC[, 1] / Cl1), n = n)
> s2c <- N * runVar(log(OHLC[, 4] / OHLC[, 1]), n = n)
>
> It needs to be "n = n", otherwise runVar confuses n for y.
>
>
> James
>
Thank you for another very helpful patch. From looking at the svn
log, it looks like I got more things wrong than right with this
calculation. :)
I also added the ability for users to specify alpha via '...' (the
default value is 1.34).
Best,
--
Joshua Ulrich | FOSS Trading: www.fosstrading.com
More information about the R-SIG-Finance
mailing list