Comparing Stock Volatility Using R
In my last post, I wrote a quick Python script to download 1 years worth of a stock’s history into a CSV file for given a list of stocks. Today, we are going to take those stocks an compare the volatility between each stock’s daily price change.
Measuring volatility is a simple calculation of the standard deviation of the percentage of the stock’s price change over a given year. The higher the standard deviation of percent change means higher volatility (and conversely, lower standard deviation means lower volatility).
This program takes a list of ticker symbols, opens up their corresponding CSV files (see yesterday’s post for more information), computes the standard deviation based on percent change, sorts that list, and prints.
In the results, you can see that I’m tracking 26 stocks. The three stocks with the lowest volatility are Proctor & Gamble (0.85%), Coca-Cola (0.99%) and Kraft (1.08%). The three stocks with the highest volatility are Micron Technology (3.24%), Netflix (3.44%), and Crosstex Energy Inc (3.62%).
Disclaimer: I’m not a broker and I haven’t been investing for very long. This information should not be used as investment advice, but it should be used as a tutorial on how to pull information out of a CSV file in the R language. How you interpret this data is up to you.
Here’s the data.
R version 2.7.1 (2008-06-23)
Copyright (C) 2008 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
> source('stockSD.R')
sdList
PG 0.008494452
KO 0.009928666
KFT 0.010838839
PFE 0.013281879
MSFT 0.013841909
YUM 0.014198831
SAP 0.014843072
FTR 0.015125052
ORCL 0.015317628
INTC 0.015906689
CPNO 0.016704188
AAPL 0.016787813
EMR 0.016905197
GE 0.017227925
GOOG 0.017538399
BIG 0.018467030
YHOO 0.018651226
GS 0.019989476
AMZN 0.020597942
BAC 0.023268540
C 0.023951356
F 0.023993128
RIMM 0.024404948
MU 0.032412592
NFLX 0.034474349
XTXI 0.036222946
Here’s the code.
symbols <- c("BAC", "YHOO", "RIMM", "MU", "MSFT", "GOOG", "PFE", "INTC", "C", "GE", "SAP", "FTR", "BIG", "ORCL", "AMZN", "XTXI", "GS", "KO", "F", "PG", "EMR", "AAPL", "KFT", "NFLX", "CPNO", "YUM")
sdList <- 1:length(symbols)
for (s in 1:length(symbols)) {
infile <- paste(symbols[s], ".csv", sep="")
df <- read.csv(infile)
percent_change <- ((df$Close - c(0, df$Close[1:length(df$Close)-1]))/ c(0, df$Close[1:length(df$Close)-1]) )[-1]
sdList[s] <- sd(percent_change)
}
# Create a frame for our standard devation list
sdDF <- data.frame(symbols, sdList)
# Order the standard devation list by standard devations in ascending order
sorted.sdDF <- sdDF[order(sdDF$sdList), ]
# Apply names to the standard devation list
rownames(sorted.sdDF) <- sorted.sdDF$symbols
# Take out the names
sorted.sdDF <- sorted.sdDF[-1]
print(sorted.sdDF)