Skip to content
Prev 14937 / 15274 Next

Select best worst

Note that this was also posted on R-help:
https://stat.ethz.ch/pipermail/r-help/2020-July/468145.html

I answered there, and I include my answer below for others' reference.

--------------------

I don't know how to do this in tidyquant, but here's how you can do it
with quantmod:

# all tickers
tk <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC", "SAB.MC",
  "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", "ENC.MC", "ELE.MC",
  "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", "COL.MC", "IAG.MC", "MAP.MC",
  "MEL.MC", "MRL.MC", "NTGY.MC", "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC",
  "VIS.MC", "ACX.MC", "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC")

# download them into an environment ('e')
require(quantmod)
getSymbols(tk, from = "2019-12-31", env = (e <- new.env()))

# extract adjusted close column
adj <- lapply(e, Ad)
# calculate daily returns from adjusted data,
# merge into a xts matrix, and fill NA with 0
ret <- do.call(merge, c(lapply(adj, dailyReturn), fill = 0))
# cumulative returns
cumret <- cumprod(1 + ret) - 1
# set names
colnames(cumret) <- names(adj)
last(cumret)
# calculate histogram for period-to-date returns
hist(drop(last(cumret)))

I'm not sure that's the histogram you're looking for, but I hope it
gives you a start toward a solution.

Best,
Josh
On Wed, Jul 29, 2020 at 9:34 AM Brian G. Peterson <brian at braverock.com> wrote:
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com