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:
Try a *minimal* example...
I'm not going to take the time to sort through your code, especially
with all the totally unnecessary mutates and pipes.
cret <- rnorm(10) # dummy cumulative returns
names(cret) <- c('a','b','c','d','e','f','g','h','i','j')
sort(cret)
You should be able to easily use this to get the order of the columns
that you want.
Regards,
Brian
--
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock
On Wed, 2020-07-29 at 15:37 +0200, Pedro p?ramo wrote:
Hi all,
I have differente stocks
AAcciona<- tq_get("ANA.MC",from = '2019-12-31',get = "stock.prices")
ACS<- tq_get("ACS.MC",from = '2019-12-31',get = "stock.prices")
Aena<- tq_get("AENA.MC",from = '2019-12-31',get = "stock.prices")
Amadeus<- tq_get("AMS.MC",from = '2019-12-31',get = "stock.prices")
ArcelorMittal<- tq_get("MTS.MC",from = '2019-12-31',get =
"stock.prices")
BBVA<- tq_get("BBVA.MC",from = '2019-12-31',get = "stock.prices")
Sabadell<- tq_get("SAB.MC",from = '2019-12-31',get = "stock.prices")
Santander<- tq_get("SAN.MC",from = '2019-12-31',get = "stock.prices")
Bankinter<- tq_get("BKT.MC",from = '2019-12-31',get = "stock.prices")
Indra<- tq_get("IDR.MC",from = '2019-12-31',get = "stock.prices")
And I have applied this code to know the year to date accumulated
returns,
the thing is that I want to order them by the last observation (year
to
date) and make a plot of the 3 best/worst (bigger/lower
accumulated returns).
?Is there a way to compare and order the last observation of
STOCKcum_return of each stock and select the 3 top and 3 bottom so I
can
plot like worst/best without going manually?
Many thanks in advance
Hope I can explain
Indra_daily_returns <- Indra %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
Indra_cum_returns <- Indra_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
AAcciona_daily_returns <- AAcciona %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
AAcciona_cum_returns <- AAcciona_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
ACS_daily_returns <- ACS %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
ACS_cum_returns <- ACS_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
AENA_daily_returns <- AENA %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
Aena_cum_returns <- Aaena_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
Amadeus_daily_returns <- Amadeus %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
Amadeus_cum_returns <- Amadeus_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
ArcerlorMittal_daily_returns <- ArcerlorMittal %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
ArcerlorMittal_cum_returns <- ArcerlorMittal_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
BBVA_daily_returns <- BBVA %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
BBVA_cum_returns <- BBVA_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
Sabadell_daily_returns <- Sabadell %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
Sabadell_cum_returns <- Sabadell_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
Santander_daily_returns <- Santander %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
Santander_cum_returns <- Santander_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
Bankinter_daily_returns <- Bankinter %>%
tq_transmute(select = adjusted, # this specifies which
column
to select
mutate_fun = periodReturn, # This specifies what to
do
with that column
period = "daily", # This argument calculates
Daily
returns
col_rename = "idr_returns") # renames the column
Bankinter_cum_returns <- Bankinter_daily_returns %>%
mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod
function
mutate(cumulative_returns = cr - 1)
[[alternative HTML version deleted]]
_______________________________________________ R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
cret <- rnorm(10) # dummy data to represent your returns
names(cret) <- c('a','b','c','d','e','f','g','h','i','j')
sort(cret, decreasing=TRUE)
You can use your column names to easily get the order you want to rearrange things.
_______________________________________________ R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
-- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com