Looping with quantmod - r

I'm new to R, loops and quantmod. I'm trying to convince quantmod to skip any ticker it's unable to process and continue on to the next ticker symbol, instead of stopping. I thought I'd found my answer here how do I loop through all the stocks with quantmod and ttr? but I'm not able to get Rime's solution to work:
If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following
# Actual loop:
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51
for(i in 51:length(symbols)) {
symbols[i]-> symbol
...
Below is my original code, which only returns 8 of the many values(so I assume that 9 is the trouble spot).
library(gdata)
d = read.xls("~/Documents/TEST.xlsx", sheet = 1, stringsAsFactors=F)
library(quantmod)
sym <- as.character(d[,1])
results <- NULL
for (ii in sym){
data1 <- getSymbols(Symbols = ii,
src = "yahoo",
from = Sys.Date() - 100,
auto.assign = FALSE)
de = head(data1,150)
colnames(de) <- c("open","high","low","close","volume","adj.")
overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1
results <- rbind(results,cbind(
paste(round(min(overnightRtn,na.rm=T),5),"%",sep="")))
}
colnames(results) <- c("overnightRtn2")
rownames(results) <- sym
View(results)
When I change for(ii in sym) to for(ii in 9:length(sym)) I get an error:
could not find function "getSymbols.9"
Here is the start of d[,1] :
[1] "ABX" "ACC" "ACCO" "ACE" "ACG" "ACH" "ACI" "ACM" "ACMP" "ACN"

There are some workarounds for errors when looping in R, one way to do this will be using the tryCatchfunction, juba showed here how to do it. I also made sure that the for loop will only continue when the data1variable is assigned some value.
Change your for loop for the following code and it should work for what you are asking.
for (ii in sym){
data1 <- NULL # NULL data1
data1 <- tryCatch(getSymbols(Symbols = ii,
src = "yahoo",
from = Sys.Date() - 100,
auto.assign = FALSE),
error=function(e){}) # empty function for error handling
if(is.null(data1)) next() # if data1 is still NULL go to next ticker
de = head(data1,150)
colnames(de) <- c("open","high","low","close","volume","adj.")
overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1
results <- rbind(results,cbind(
paste(round(min(overnightRtn,na.rm=T),5),"%",sep="")))
}

You might try the tidyquant package which takes care of error handling internally. It also doesn't require for-loops so it will save you a significant amount of code. The tq_get() function is responsible for getting stock prices. You can use the complete_cases argument to adjust how errors are handled.
Example with complete_cases = TRUE: Automatically removes "bad apples"
library(tidyquant)
# get data with complete_cases = TRUE automatically removes bad apples
c("AAPL", "GOOG", "BAD APPLE", "NFLX") %>%
tq_get(get = "stock.prices", complete_cases = TRUE)
#> Warning in value[[3L]](cond): Error at BAD APPLE during call to get =
#> 'stock.prices'. Removing BAD APPLE.
#> # A tibble: 7,680 × 8
#> symbol date open high low close volume adjusted
#> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 AAPL 2007-01-03 86.29 86.58 81.90 83.80 309579900 10.85709
#> 2 AAPL 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.09807
#> 3 AAPL 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.01904
#> 4 AAPL 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.07345
#> 5 AAPL 2007-01-09 86.45 92.98 85.15 92.57 837324600 11.99333
#> 6 AAPL 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.56728
#> 7 AAPL 2007-01-11 95.94 96.78 95.10 95.80 360063200 12.41180
#> 8 AAPL 2007-01-12 94.59 95.06 93.23 94.62 328172600 12.25892
#> 9 AAPL 2007-01-16 95.68 97.25 95.45 97.10 311019100 12.58023
#> 10 AAPL 2007-01-17 97.56 97.60 94.82 94.95 411565000 12.30168
#> # ... with 7,670 more rows
Example with complete_cases = FALSE: Returns nested data frame.
library(tidyquant)
# get data with complete_cases = FALSE returns a nested data frame
c("AAPL", "GOOG", "BAD APPLE", "NFLX") %>%
tq_get(get = "stock.prices", complete_cases = FALSE)
#> Warning in value[[3L]](cond): Error at BAD APPLE during call to get =
#> 'stock.prices'.
#> Warning in value[[3L]](cond): Returning as nested data frame.
#> # A tibble: 4 × 2
#> symbol stock.prices
#> <chr> <list>
#> 1 AAPL <tibble [2,560 × 7]>
#> 2 GOOG <tibble [2,560 × 7]>
#> 3 BAD APPLE <lgl [1]>
#> 4 NFLX <tibble [2,560 × 7]>
In both cases the user gets a WARNING message. The prudent user will read them and try to determine what the issue is. Most important, the long running script will not fail.

Related

Why are daily returns all zeros using Quantmod?

I used the following code:
getSymbols(c("TSLA", "AAPL", "CSCO", "IBM"))
tsla<-TSLA['2022-01-03::2023-01-03']
aapl=AAPL['2022-01-03::2023-01-03']
csco=CSCO['2022-01-03::2023-01-03']
ibm=IBM['2022-01-03::2023-01-03']
tsla<-tsla$TSLA.Adjusted
aapl<-aapl$AAPL.Adjusted
csco<-csco$CSCO.Adjusted
ibm<-ibm$IBM.Adjusted
stkdata=cbind(tsla, aapl, csco, ibm)
n<-length(stkdata[,1])
rets<-log(stkdata[2:n,]/stkdata[1:(n-1),])
It produces all zeros.
After I assigned stkdata[2:n] to x and stkdata[1:n-1] to y, R shows
x[1,]
TSLA.Adjusted AAPL.Adjusted CSCO.Adjusted IBM.Adjusted
2022-01-04 383.1967 178.3907 59.26239 129.9028
y[1,]
TSLA.Adjusted AAPL.Adjusted CSCO.Adjusted IBM.Adjusted
2022-01-03 399.9267 180.6839 60.75242 128.0392
This is fine. But
x[1,]/y[1,]
Data:
numeric(0)
Index:
Date of length 0
What could be the problem? Thanks ahead!
This behavior is expected because arithmetic and logical operations on xts objects are done on observations that have the same date.
You should use the lag() function to change the datetime index alignment. log(stkdata / lag(stkdata)).
Note that you have to be very careful using lag() with dplyr loaded. It breaks how base R's lag() function is supposed to work, which breaks lag(my_xts). It also breaks lag() on all other types of objects that have their own lag() method (e.g. zoo).
1) getSymbols can place the results into a local environment and then we can iterate over its elements using eapply. Then use diff with arithmetic=FALSE causing diff to perform division rather than subtraction.
If x is the ratio of the current price to the prior price then
while it is true that log(x) approximately equals x-1 if the return is small we don't really need to use that approximation and can calculate the return exactly using x-1.
Regarding the question, xts objects do not combine by position but by time. Removing the first or last element of an xts object does not change the times so the code in the question is dividing stkdata by itself except for the positions on the end which have been removed.
Try the code below.
library(quantmod)
tickers <- c("TSLA", "AAPL", "CSCO", "IBM")
getSymbols(tickers, env = e <- new.env(), from = "2022-01-03", to = "2023-01-03")
stks <- do.call("merge", eapply(e, Ad))
rets <- diff(stks, arithmetic = FALSE) - 1
2) A variation is to use getSymbols to load the data into the current R workspace, as in the question, and then use mget.
library(quantmod)
tickers <- c("TSLA", "AAPL", "CSCO", "IBM")
getSymbols(tickers, from = "2022-01-03", to = "2023-01-03")
stks <- do.call("merge", lapply(mget(tickers), Ad))
rets <- diff(stks, arithmetic = FALSE) - 1
With tidyquant you can calculate daily log returns as such:
library(tidyquant)
library(tidyverse)
df = tq_get(c("TSLA", "AAPL", "CSCO", "IBM"),
from = "2022-01-03",
to = "2023-01-04")
log_return = df %>%
group_by(symbol) %>%
tq_mutate(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
type = "log",
col_rename = "log_returns")
# A tibble: 1,008 × 9
# Groups: symbol [4]
symbol date open high low close volume adjusted log_returns
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 TSLA 2022-01-03 383. 400. 379. 400. 103931400 400. 0
2 TSLA 2022-01-04 397. 403. 374. 383. 100248300 383. -0.0427
3 TSLA 2022-01-05 382. 390. 360. 363. 80119800 363. -0.0550
4 TSLA 2022-01-06 359 363. 340. 355. 90336600 355. -0.0218
5 TSLA 2022-01-07 360. 360. 337. 342. 84164700 342. -0.0361
6 TSLA 2022-01-10 333. 353. 327. 353. 91815000 353. 0.0299
7 TSLA 2022-01-11 351. 359. 346. 355. 66063300 355. 0.00592
8 TSLA 2022-01-12 360. 372. 358. 369. 83739000 369. 0.0385
9 TSLA 2022-01-13 370. 372. 342. 344. 97209900 344. -0.0699
10 TSLA 2022-01-14 340. 351. 338. 350. 72924300 350. 0.0173
# … with 998 more rows
# ℹ Use `print(n = ...)` to see more rows
Plotting
log_return %>%
ggplot() +
aes(x = date, y = log_returns, col = symbol) +
geom_line() +
facet_wrap(~ symbol) +
theme_tq()

How do I retain all the columns while using tq_transmute() function?

I am trying to replicate a trading strategy and backtest in R. However, I am having a slight problem with the tq_transmute() function. Any help would be appreciated.
So, I have the following code that I have written until now:
#Importing the etfs data
symbols<- c("SPY","XLF","XLE")
start<-as.Date("2000-01-01")
end<- as.Date("2018-12-31")
price_data<- lapply(symbols, function(symbol){
etfs<-as.data.frame(getSymbols(symbol,src="yahoo", from=start, to= end,
auto.assign = FALSE))
colnames(etfs)<- c("Open", "High","Low","Close","volume","Adjusted")
etfs$Symbol<- symbol
etfs$Date<- rownames(etfs)
etfs
})
# Next, I used do.call() with rbind() to combine the data into a single data frame
etfs_df<- do.call(rbind, price_data)
#This because of POSIXct error
daily_price<- etfs_df %>%
mutate(Date=as.Date(Date, frac=1))
# I have deleted some columns of the table as my work only concerned the "Adjusted" column.
#So, until now we have:
head(daily_price)
Adjusted Symbol Date
1 98.14607 SPY 2000-01-03
2 94.30798 SPY 2000-01-04
3 94.47669 SPY 2000-01-05
4 92.95834 SPY 2000-01-06
5 98.35699 SPY 2000-01-07
6 98.69440 SPY 2000-01-10
#Converting the daily adjusted price to monthly adjusted price
monthly_price<-
tq_transmute(daily_price,select = Adjusted, mutate_fun = to.monthly, indexAt = "lastof")
head(monthly_price)
# And now, I get the following table:
# A tibble: 6 x 2
Date Adjusted
<date> <dbl>
1 2000-01-31 16.6
2 2000-02-29 15.9
3 2000-03-31 17.9
4 2000-04-30 17.7
5 2000-05-31 19.7
6 2000-06-30 18.6
So, as you can see, the Date and Adjusted prices have been successfully converted to monthly figures but my Symbol column has disappeared. Could anyone please tell me why did that happen and how do I get it back?
Thank you.
group the data by Symbol and apply tq_transmute.
library(dplyr)
library(quantmod)
library(tidyquant)
monthly_price <- daily_price %>%
group_by(Symbol) %>%
tq_transmute(daily_price,select = Adjusted,
mutate_fun = to.monthly, indexAt = "lastof")
# Symbol Date Adjusted
# <chr> <date> <dbl>
# 1 SPY 2000-01-31 94.2
# 2 SPY 2000-02-29 92.7
# 3 SPY 2000-03-31 102.
# 4 SPY 2000-04-30 98.2
# 5 SPY 2000-05-31 96.6
# 6 SPY 2000-06-30 98.5
# 7 SPY 2000-07-31 97.0
# 8 SPY 2000-08-31 103.
# 9 SPY 2000-09-30 97.6
#10 SPY 2000-10-31 97.2
# … with 674 more rows
I would do it like this:
symbols <- c("SPY", "XLF", "XLE")
start <- as.Date("2000-01-01")
end <- as.Date("2018-12-31")
# Environment to hold data
my_data <- new.env()
# Tell getSymbols() to load the data into 'my_data'
getSymbols(symbols, from = start, to = end, env = my_data)
# Combine all the adjusted close prices into one xts object
price_data <- Reduce(merge, lapply(my_data, Ad))
# Remove "Adjusted" from column names
colnames(price_data) <- sub(".Adjusted", "", colnames(price_data), fixed = TRUE)
# Get the last price for each month
monthly_data <- apply.monthly(price_data, last)
# Convert to a long data.frame
long_data <- fortify.zoo(monthly_data,
names = c("Date", "Symbol", "Adjusted"), melt = TRUE)

Loop to transform xts into dataframe

I am using the package quantmod to get historical share prices.
I want to create a loop to pull back the prices and as part of the loop I want to create a dataframe for each share. I have been unsuccessful so far with the below code, it gets the share prices as expected but this is returned as a xts object whereas I require the information as a dataframe - the as.data.frame part of the code doesn't do anything...
library(quantmod)
shares<-c("BARC.L", "BP.L", "DLG.L")
for(i in 1:length(shares)){
#gets share prices
getSymbols((paste(shares[i])), from = "2018-01-01")
#put the data into a dataframe (doesn't work).
shares[i]<-as.data.frame(shares[i])
}
The end result that I want is 3 dataframes - 1 for each share.
Can anyone suggest modifications to the code to achieve this please?
Personally I would do it like this:
library(quantmod)
shares<-c("BARC.L", "BP.L", "DLG.L")
my_shares <- lapply(shares, function(x) getSymbols(x, from = "2018-01-01", auto.assign = FALSE))
names(my_shares) <- shares
Or if you need the dates as a column instead of rownames:
my_shares <- lapply(shares, function(x) {
out <- getSymbols(x, from = "2018-01-01", auto.assign = FALSE)
out <- data.frame(dates = index(out), coredata(out))
return(out)
})
names(my_shares) <- shares
Or if you need everything in a tidy dataset:
library(tidyquant)
my_shares <- tq_get(shares)
my_shares
# A tibble: 7,130 x 8
symbol date open high low close volume adjusted
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 BARC.L 2008-01-02 464. 483. 460. 466. 38104837 344.
2 BARC.L 2008-01-03 466. 472. 458. 470. 33215781 347.
3 BARC.L 2008-01-04 466. 476. 447. 449. 42710244 332.
4 BARC.L 2008-01-07 447. 452. 433. 436. 58213512 322.
5 BARC.L 2008-01-08 439. 447. 421. 437. 105370539 322.
6 BARC.L 2008-01-09 432. 434. 420. 424. 71059078 313.
7 BARC.L 2008-01-10 428. 431. 413. 418. 54763347 309.
8 BARC.L 2008-01-11 416. 437. 416. 430. 72467229 317.
9 BARC.L 2008-01-14 430. 448. 427. 444. 56916500 328.
10 BARC.L 2008-01-15 445. 452. 428. 429. 77094907 317.
# ... with 7,120 more rows
Firstly, I suggest you use the help() function that comes with R packages if you're not already doing so. I noticed in help(getSymbols) that you need to set env=NULL to actually return the data. With that, I've also made a list object so you can store the data as data.frames like you requested:
library(quantmod)
shares<-c("BARC.L", "BP.L", "DLG.L")
# initialize a list to store your data frames
df_list <- as.list(rep(data.frame(), length(shares)))
for (i in 1:length(shares)) {
#gets share prices
df_list[[i]] <- as.data.frame(getSymbols(shares[i], from = "2018-01-01", env=NULL))
}
# so you can access by name, e.g. df_list$DLG.L
names(df_list) <- shares

How to loop through objects in the global environment - R

I have looked far and wide for a solution to this issue, but I cannot seem to figure it out. I do not have much experience working with xts objects in R.
I have 40 xts objects (ETF data) and I want to run the quantmod function WeeklyReturn on each of them individually.
I have tried to refer to them by using the ls() function:
lapply(ls(), weeklyReturn)
I have also tried the object() function
lapply(object(), weeklyReturn)
I have also tried using as.xts() in my call to coerce the ls() objects to be used as xts but to no avail.
How can I run this function on every xts object in the environment?
Thank you,
It would be better to load all of your xts objects into a list or create them in a way that returns them in a list to begin with. Then you could do results = lapply(xts.list, weeklyReturn).
To work with objects in the global environment, you could test for whether the object is an xts object and then run weeklyReturn on it if it is. Something like this:
results = lapply(setNames(ls(), ls()), function(i) {
x = get(i)
if(is.xts(x)) {
weeklyReturn(x)
}
})
results = results[!sapply(results, is.null)]
Or you could select only the xts objects to begin with:
results = sapply(ls()[sapply(ls(), function(i) is.xts(get(i)))],
function(i) weeklyReturn(get(i)), simplify=FALSE, USE.NAMES=TRUE)
lapply(ls(), weeklyReturn) doesn't work, because ls() returns the object names as strings. The get function takes a string as an argument and returns the object with that name.
An alternate solution using the tidyquant package. Note that this is data frame based so I will not be working with xts objects. I use two core functions to scale the analysis. First, tq_get() is used to go from a vector of ETF symbols to getting the prices. Second, tq_transmute() is used to apply the weeklyReturn function to the adjusted prices.
library(tidyquant)
etf_vec <- c("SPY", "QEFA", "TOTL", "GLD")
# Use tq_get to get prices
etf_prices <- tq_get(etf_vec, get = "stock.prices", from = "2017-01-01", to = "2017-05-31")
etf_prices
#> # A tibble: 408 x 8
#> symbol date open high low close volume adjusted
#> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 SPY 2017-01-03 227.121 227.919 225.951 225.24 91366500 223.1760
#> 2 SPY 2017-01-04 227.707 228.847 227.696 226.58 78744400 224.5037
#> 3 SPY 2017-01-05 228.363 228.675 227.565 226.40 78379000 224.3254
#> 4 SPY 2017-01-06 228.625 229.856 227.989 227.21 71559900 225.1280
#> 5 SPY 2017-01-09 229.009 229.170 228.514 226.46 46265300 224.3848
#> 6 SPY 2017-01-10 228.575 229.554 228.100 226.46 63771900 224.3848
#> 7 SPY 2017-01-11 228.453 229.200 227.676 227.10 74650000 225.0190
#> 8 SPY 2017-01-12 228.595 228.847 227.040 226.53 72113200 224.4542
#> 9 SPY 2017-01-13 228.827 229.503 228.786 227.05 62717900 224.9694
#> 10 SPY 2017-01-17 228.403 228.877 227.888 226.25 61240800 224.1767
#> # ... with 398 more rows
# Use tq_transmute to apply weeklyReturn to multiple groups
etf_returns_w <- etf_prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted, mutate_fun = weeklyReturn)
etf_returns_w
#> # A tibble: 88 x 3
#> # Groups: symbol [4]
#> symbol date weekly.returns
#> <chr> <date> <dbl>
#> 1 SPY 2017-01-06 0.0087462358
#> 2 SPY 2017-01-13 -0.0007042173
#> 3 SPY 2017-01-20 -0.0013653367
#> 4 SPY 2017-01-27 0.0098350474
#> 5 SPY 2017-02-03 0.0016159256
#> 6 SPY 2017-02-10 0.0094619381
#> 7 SPY 2017-02-17 0.0154636969
#> 8 SPY 2017-02-24 0.0070186222
#> 9 SPY 2017-03-03 0.0070964211
#> 10 SPY 2017-03-10 -0.0030618336
#> # ... with 78 more rows

Convert data frame with date column to timeseries

I've got a data frame with the following data:
>PRICE
DATE CLOSE
1 20070103 54.700
2 20070104 54.770
3 20070105 55.120
4 20070108 54.870
5 20070109 54.860
6 20070110 54.270
7 20070111 54.770
8 20070112 55.360
9 20070115 55.760
...
As you can see my DATE column represents a date (yyyyMMdd) and my CLOSE column represents prices.
I now have to calculate CalmarRatio, from the PerformanceAnalytics package.
I'm new to R, so i can't understand everything, but from what i have googled to the moment i see that the R parameter to that function needs to be a time-series-like object.
Is there any way i can convert my array to a time-series object given that there might not be data for every date in a period (only for the ones i specify)?
Your DATE column may represent a date, but it is actually either a character, factor, integer, or a numeric vector.
First, you need to convert the DATE column to a Date object. Then you can create an xts object from the CLOSE and DATE columns of your PRICE data.frame. Finally, you can use the xts object to calculate returns and the Calmar ratio.
PRICE <- structure(list(
DATE = c(20070103L, 20070104L, 20070105L, 20070108L, 20070109L,
20070110L, 20070111L, 20070112L, 20070115L),
CLOSE = c(54.7, 54.77, 55.12, 54.87, 54.86, 54.27, 54.77, 55.36, 55.76)),
.Names = c("DATE", "CLOSE"), class = "data.frame",
row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9"))
library(PerformanceAnalytics) # loads/attaches xts
# Convert DATE to Date class
PRICE$DATE <- as.Date(as.character(PRICE$DATE),format="%Y%m%d")
# create xts object
x <- xts(PRICE$CLOSE,PRICE$DATE)
CalmarRatio(Return.calculate(x))
# [,1]
# Calmar Ratio 52.82026
Most people find working with the time series class to be a big pain. You should consider using the zoo class from package zoo. It will not complain about missing times , only about duplicates. The PerformanceAnalytics functions are almost certainly going to be expecting 'zoo' or its descendant class 'xts'.
pricez <- read.zoo(text=" DATE CLOSE
1 20070103 54.700
2 20070104 54.770
3 20070105 55.120
4 20070108 54.870
5 20070109 54.860
6 20070110 54.270
7 20070111 54.770
8 20070112 55.360
9 20070115 55.760
")
index(pricez) <- as.Date(as.character(index(pricez)), format="%Y%m%d")
pricez
2007-01-03 2007-01-04 2007-01-05 2007-01-08 2007-01-09 2007-01-10 2007-01-11 2007-01-12 2007-01-15
54.70 54.77 55.12 54.87 54.86 54.27 54.77 55.36 55.76
An alternative solution is to use the tidyquant package, which allows the functionality of the financial packages, including time series functionality, to be used with data frames. The following examples shows how you can get the Calmar Ratio for multiple assets. The tidyquant vignettes go into more details on how to use the package.
library(tidyquant)
# Get prices
price_tbl <- c("FB", "AMZN", "NFLX", "GOOG") %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2016-12-31")
price_tbl
#> # A tibble: 6,449 × 8
#> symbol date open high low close volume adjusted
#> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 FB 2012-05-18 42.05 45.00 38.00 38.23 573576400 38.23
#> 2 FB 2012-05-21 36.53 36.66 33.00 34.03 168192700 34.03
#> 3 FB 2012-05-22 32.61 33.59 30.94 31.00 101786600 31.00
#> 4 FB 2012-05-23 31.37 32.50 31.36 32.00 73600000 32.00
#> 5 FB 2012-05-24 32.95 33.21 31.77 33.03 50237200 33.03
#> 6 FB 2012-05-25 32.90 32.95 31.11 31.91 37149800 31.91
#> 7 FB 2012-05-29 31.48 31.69 28.65 28.84 78063400 28.84
#> 8 FB 2012-05-30 28.70 29.55 27.86 28.19 57267900 28.19
#> 9 FB 2012-05-31 28.55 29.67 26.83 29.60 111639200 29.60
#> 10 FB 2012-06-01 28.89 29.15 27.39 27.72 41855500 27.72
#> # ... with 6,439 more rows
# Convert to period returns
return_tbl <- price_tbl %>%
group_by(symbol) %>%
tq_transmute(ohlc_fun = Ad,
mutate_fun = periodReturn,
period = "daily")
return_tbl
#> Source: local data frame [6,449 x 3]
#> Groups: symbol [4]
#>
#> symbol date daily.returns
#> <chr> <date> <dbl>
#> 1 FB 2012-05-18 0.00000000
#> 2 FB 2012-05-21 -0.10986139
#> 3 FB 2012-05-22 -0.08903906
#> 4 FB 2012-05-23 0.03225806
#> 5 FB 2012-05-24 0.03218747
#> 6 FB 2012-05-25 -0.03390854
#> 7 FB 2012-05-29 -0.09620809
#> 8 FB 2012-05-30 -0.02253811
#> 9 FB 2012-05-31 0.05001770
#> 10 FB 2012-06-01 -0.06351355
#> # ... with 6,439 more rows
# Calculate performance
return_tbl %>%
tq_performance(Ra = daily.returns,
performance_fun = CalmarRatio)
#> Source: local data frame [4 x 2]
#> Groups: symbol [4]
#>
#> symbol CalmarRatio
#> <chr> <dbl>
#> 1 FB 0.50283172
#> 2 AMZN 0.91504597
#> 3 NFLX 0.14444744
#> 4 GOOG 0.05068483
Whether you want to convert a data frame (or any time series) to a xts or zoo object, as in the answers above, or to any other time series (such as a ts object) the tsbox package makes coercion easy:
PRICE <- structure(list(
DATE = c(20070103L, 20070104L, 20070105L, 20070108L, 20070109L,
20070110L, 20070111L, 20070112L, 20070115L),
CLOSE = c(54.7, 54.77, 55.12, 54.87, 54.86, 54.27, 54.77, 55.36, 55.76)),
.Names = c("DATE", "CLOSE"), class = "data.frame",
row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9"))
library(tsbox)
ts_xts(PRICE)
#> [time]: 'DATE' [value]: 'CLOSE'
#> Loading required namespace: xts
#> Registered S3 method overwritten by 'xts':
#> method from
#> as.zoo.xts zoo
#> CLOSE
#> 2007-01-03 54.70
#> 2007-01-04 54.77
#> 2007-01-05 55.12
#> 2007-01-08 54.87
#> 2007-01-09 54.86
#> 2007-01-10 54.27
#> 2007-01-11 54.77
#> 2007-01-12 55.36
#> 2007-01-15 55.76
ts_ts(PRICE)
#> [time]: 'DATE' [value]: 'CLOSE'
#> Time Series:
#> Start = 2007.00547581401
#> End = 2007.0383306981
#> Frequency = 365.2425
#> [1] 54.70 54.77 55.12 NA NA 54.87 54.86 54.27 54.77 55.36 NA
#> [12] NA 55.76
This answer based on #Joshua_Ulrich's answer creates a time series from the built-in airquality dataset containing "Daily air quality measurements in New York, May to September 1973".
> head(airquality,3)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
Convert Month and Day to a vector of class "Date"
airqualitydate = as.Date(sprintf("1973%02.0f%02.0f", airquality$Month, airquality$Day),
format="%Y%m%d")
Create the time series object
ts_airquality <- xts(airquality, airqualitydate)
head(ts_airquality, 3)
Ozone Solar.R Wind Temp Month Day
1973-05-01 41 190 7.4 67 5 1
1973-05-02 36 118 8.0 72 5 2
1973-05-03 12 149 12.6 74 5 3
Plot to illustrate the different output of the plot.xts() function. (compare to plot(airquality))
plot(ts_airquality$Ozone, main="Ozone (ppb)")
lines(ts_airquality$Temp, on=NA, main="Temperature (degrees F)")
Note, the base R ts() method is mostly suited for quarterly or yearly data.
As explained in an answer to "starting a daily time series in R":
"Time Series Object does not work well with creating daily time series. I will suggest you use the zoo library."
In particular the xts package is an extension to zoo.

Resources