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

Issue with volatility function in TTR when pulling data from excel/bloomberg #34

Closed smathaidavis closed 8 years ago

smathaidavis commented 8 years ago

I am having trouble pulling in data from Excel into T and calculating OHLC volatility using TTR. I am using the following code (I have also attached the code for your review).

The output generated by volatility(ohlc, calc="gk.yz") is downward sloping... almost like a decay function. It looks completely wrong when I compare it to the data pulled from Yahoo Finance. Am I doing something wrong with how I am pulling the data? Appreciate any help or advice you could offer.

Best, Stephen

Using the following packages: quantmod, xlsx, xts, zoo.

##R Code to pull data from Bloomberg/Excel & use quantmod/TTR
x=read.xlsx("Test OHLC Data.xlsx",sheetIndex=1)
x
x[1:756,1:5]
X=x[1:756,1:5]
X
summary(X)
typeof(X)
typeof(X$Open)
is.numeric(X$Open)
is.na(X)

str(X)

xts(X[,-1],order.by=X$NA.)
NewX=xts(X[,-1],order.by=X$NA.)
chartSeries(NewX)

is.OHLC(NewX)
has.Vo(NewX)
OHLC(NewX)

Op(NewX) ##Test - brings open data
Cl(NewX) ##Test - brings close data
HiCl(NewX) ##Test - brings in % change from Hi Low

plot(HiCl(NewX),type='l')
mean(HiCl(NewX))

ohlc = OHLC(NewX)
ohlc

volatility(ohlc,calc="garman")
IntraVol1=volatility(ohlc,calc="garman")

volatility(ohlc,calc="gk.yz")
IntraVol2=volatility(ohlc,calc="gk.yz")
joshuaulrich commented 8 years ago

After reviewing @smathaidavis' Excel file, it's clear this is a data issue. The Open is almost always outside the High-Low range. Closing as invalid.