-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfutures_script.R
More file actions
executable file
·45 lines (36 loc) · 1.29 KB
/
Copy pathfutures_script.R
File metadata and controls
executable file
·45 lines (36 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
################################################
###
### script for:
### loading data from CSV file,
### calculating rolling (running) volatility
###
################################################
library(quantmod)
library(TTR)
# script for loading data - taken from FRE6871_Lecture5.pdf slide #13
datav <- read.zoo(file="data.csv",
header=TRUE, sep=",", FUN=as.POSIXct,
tz="America/New_York",
format="%m/%d/%Y")
# coerce to xts
datav <- as.xts(datav)
tail(datav)
# calculate returns - produces a list of xts
returns <- lapply(datav, dailyReturn)
length(returns)
names(returns)
tail(returns$"Jan.16")
# calculate volatility - produces a named numeric vector
sapply(returns, sd)
rolling_vol <- function(datav=datav, rangev=NULL, tseries=colnames(datav[, 1]), lookb=10) {
stopifnot("package:quantmod" %in% search() || require("quantmod", quietly=TRUE))
stopifnot("package:TTR" %in% search() || require("TTR", quietly=TRUE))
if (is.null(rangev))
rangev <- index(datav)
# calculate returns
returns <- dailyReturn(na.omit(datav[rangev, tseries]))
# calculate rolling volatility
na.omit(runSD(x=returns, n=lookb))
} # end rolling_vol
foo_bar <- rolling_vol(datav, rangev="2015-03-01/2015-11-25", tseries="Apr.16", lookb=20)
chart_Series(foo_bar)