Quantcast

TTR, volatility(), historical volatility calculation methods differ

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

TTR, volatility(), historical volatility calculation methods differ

algotr8der
1) Garman-Klass volatility as defined by Euan Sinclair in his book "volatility trading" on page 23:

http://oneryng.com/library/ViolatilityTrading.pdf

            Cl1 <- lag(OHLC[, 4])
             s <- sqrt(N/n * runSum(0.5 * log(OHLC[, 2]/OHLC[, 3])^2 - (2 * log(2) - 1) * log(OHLC[, 4]/Cl1)^2, n))

In the volatility function in the TTR package has (defined by the sitmo website):

http://web.archive.org/web/20081224134053/http://www.sitmo.com/eq/402

        s <- sqrt(N/n * runSum(0.5 * log(OHLC[, 2]/OHLC[, 3])^2 - (2 * log(2) - 1) * log(OHLC[, 4]/OHLC[, 1])^2, n))

The difference is with the last term i.e. OHLC[,1] - today's open is used in the denominator in the TTR package rather than yesterday's close (Cl1). I have read various documents that use the same definition as Dr. Sinclair. Which is correct and why?

2) Yang Zhang volatility as defined by Euan Sinclair in his book "volatility trading"  on page 24-25 :

         k <- 0.34/(1 + (n + 1)/(n - 1))
        s2o <- N/(n - 1) * runSum(log(OHLC[, 1]/Cl1), n))^2
        s2c <- N/(n - 1) * runSum(log(OHLC[, 4]/OHLC[, 1]), n))^2
        s2rs <- volatility(OHLC = OHLC, n = n, calc = "rogers.satchell", N = N, ...)
        s <-  sqrt(s2o + k * s2c + (1 - k) * (s2rs)^2, n)


But the volatility function in the TTR package has:

http://web.archive.org/web/20081224134117/http://www.sitmo.com/eq/417

            k <- 0.34/(1 + (n + 1)/(n - 1))
        s2o <- N/(n - 1) * runSum(log(OHLC[, 1]/Cl1) - 1/n *
            runSum(log(OHLC[, 1]/Cl1), n))^2
        s2c <- N/(n - 1) * runSum(log(OHLC[, 4]/OHLC[, 1]) -
            1/n * runSum(log(OHLC[, 4]/OHLC[, 1]), n))^2
        s2rs <- volatility(OHLC = OHLC, n = n, calc = "rogers.satchell",
            N = N, ...)
        s <- s2o + k * s2c + (1 - k) * s2rs

There are multiple differences - firstly the mean of the uo and uc are not present in Dr. Sinclair's version and moreover the last term in the TTR package seems to add a standard deviation (s2rs) with 2 variance calculations (s2c and s2o).  

I would greatly appreciate if someone can comment on which versions are correct.  Thank you.

> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C/en_US.UTF-8/C/C/C/C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] TTR_0.21-0 xts_0.8-0  zoo_1.6-5

loaded via a namespace (and not attached):
[1] grid_2.13.0     lattice_0.19-26 tools_2.13
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: TTR, volatility(), historical volatility calculation methods differ

Joshua Ulrich
On Mon, Dec 26, 2011 at 3:13 PM, algotr8der <[hidden email]> wrote:

> 1) Garman-Klass volatility as defined by Euan Sinclair in his book
> "volatility trading" on page 23:
>
> http://oneryng.com/library/ViolatilityTrading.pdf
>
>            Cl1 <- lag(OHLC[, 4])
>             s <- sqrt(N/n * runSum(0.5 * log(OHLC[, 2]/OHLC[, 3])^2 - (2 *
> log(2) - 1) * log(OHLC[, 4]/Cl1)^2, n))
>
> In the volatility function in the TTR package has (defined by the sitmo
> website):
>
> http://web.archive.org/web/20081224134053/http://www.sitmo.com/eq/402
>
>        s <- sqrt(N/n * runSum(0.5 * log(OHLC[, 2]/OHLC[, 3])^2 - (2 *
> log(2) - 1) * log(OHLC[, 4]/OHLC[, 1])^2, n))
>
> The difference is with the last term i.e. OHLC[,1] - today's open is used in
> the denominator in the TTR package rather than yesterday's close (Cl1). I
> have read various documents that use the same definition as Dr. Sinclair.
> Which is correct and why?
>
> 2) Yang Zhang volatility as defined by Euan Sinclair in his book "volatility
> trading"  on page 24-25 :
>
>         k <- 0.34/(1 + (n + 1)/(n - 1))
>        s2o <- N/(n - 1) * runSum(log(OHLC[, 1]/Cl1), n))^2
>        s2c <- N/(n - 1) * runSum(log(OHLC[, 4]/OHLC[, 1]), n))^2
>        s2rs <- volatility(OHLC = OHLC, n = n, calc = "rogers.satchell", N =
> N, ...)
>        s <-  sqrt(s2o + k * s2c + (1 - k) * (s2rs)^2, n)
>
>
> But the volatility function in the TTR package has:
>
> http://web.archive.org/web/20081224134117/http://www.sitmo.com/eq/417
>
>            k <- 0.34/(1 + (n + 1)/(n - 1))
>        s2o <- N/(n - 1) * runSum(log(OHLC[, 1]/Cl1) - 1/n *
>            runSum(log(OHLC[, 1]/Cl1), n))^2
>        s2c <- N/(n - 1) * runSum(log(OHLC[, 4]/OHLC[, 1]) -
>            1/n * runSum(log(OHLC[, 4]/OHLC[, 1]), n))^2
>        s2rs <- volatility(OHLC = OHLC, n = n, calc = "rogers.satchell",
>            N = N, ...)
>        s <- s2o + k * s2c + (1 - k) * s2rs
>
> There are multiple differences - firstly the mean of the uo and uc are not
> present in Dr. Sinclair's version and moreover the last term in the TTR
> package seems to add a standard deviation (s2rs) with 2 variance
> calculations (s2c and s2o).
>
> I would greatly appreciate if someone can comment on which versions are
> correct.  Thank you.
>
The calculations in most recent revisions on R-forge match the
original papers.  I can't comment on Euan Sinclair's calculations.

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: TTR, volatility(), historical volatility calculation methods differ

algotr8der
Issue #1 is accurate as per the original papers
Issue #2 has been fixed in v119 i.e variance terms are added together

https://r-forge.r-project.org/scm/viewvc.php/pkg/R/volatility.R?view=markup&root=ttr&pathrev=119
Loading...