joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
330 stars 103 forks source link

Extend Cl/Cl volatility estimator to use mean 0 #8

Closed jtoll closed 9 years ago

jtoll commented 9 years ago

I'm proposing an extension to the Cl/Cl volatility estimator so that there's a version that effectively uses a fixed mean of 0 rather than the sample mean. For the case of small n with a strongly trending underlying, the sample mean can be skewed resulting in an underestimation of volatility.

I have working code and can submit a pull request if you're agreeable to extending your function. One question is the best way to select this functionality via the command-line arguments. One possibility is via the "calc" argument. This method could be designated as "mean0" or "close0". Alternatively, an additional command line argument could be added to the function such as mean0 = TRUE.

Your thoughts? Thanks.

joshuaulrich commented 9 years ago

It makes more sense to add another choice for the calc argument, since the mean0 argument would only apply in the case where calc="close". Using "close0" to indicate the calculation you describe seems reasonable.

Pull request welcome.

joshuaulrich commented 9 years ago

I've spent some time looking and the code and considering this change. The mean-zero assumption could apply to any of the current calculation methods, so adding a mean0 argument to the volatility function makes more sense.

If you could amend your pull request to support this change, I'll merge it and open an issue to add support for the mean-zero assumption to the remaining calculations. Or I can make the change myself, if you prefer.

jtoll commented 9 years ago

I will go ahead and edit my branch to add a mean0 argument, rather than the close0 method, and push that up as well. You can review and if it needs additional work, please let me know.

joshuaulrich commented 9 years ago

What are your thoughts on allowing the user to specify the mean to be used for each point in time? The user could provide a vector of means they wish to use (e.g. mean=runMean(x,n) * 0.1). I consider this a separate issue, but would appreciate your input.

jtoll commented 9 years ago

That's an interesting idea. In considering the mean0 functionality I also considered whether there were other use cases that should be considered. Even something just as simple as specifying a fixed mean other than just 0. At this point, I'm unsure. For what it's worth, most of the literature that I read on the subject simply suggests using mean 0 (usually as a footnote).

Ideally, the time series should be stationary with mean 0, as we're working with log price returns. Rather than introducing the kind of fine-grain complexity you are describing, it seems like additional work should probably just should go into making the time series stationary. I guess what I'm saying is, if they need that kind of functionality, my guess is there's a problem with their data to begin with.

Perhaps someone with a better quantitative background than myself, like @braverock, might have some thoughts on this. Are there OTC financial contracts, like for example variance swaps, that use other methods of calculating volatility that might offer some insight into methods that should be implemented?