On 2020-07-31 10:07 -0500, Joshua Ulrich wrote:
| On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas wrote:
| | ?s 15:44 de 31/07/2020, Michael Dewey escreveu:
| | | Dear Pedro
| | |
| | | Some comments in-line
| | |
| | | On 30/07/2020 21:16, Pedro p?ramo wrote:
| | | | Hi all,
| | | |
| | | | I attach my code, the think is I
| | | | want to make a bar plot the last
| | | | variable called "bwchist" so the
| | | | X axis are "Accion" and the y
| | | | axis are "reval" values.
| | | |
| | | | I have prove class(bwchist) and
| | | | says dataframe but its still a
| | | | list because it says me I have
| | | | prove to unlist, but it doesnt
| | | | work
| | | |
| | | | hist(bwchist)
| | | | Error in hist.default(bwchist) : 'x' must be numeric
| | |
| | | So bwchist is not a numeric
| | | variable as hist needs. Aboce you
| | | said it is a data frame but data
| | | frames are not numeric.
| | |
| | | For future reference your example
| | | is way too long for anyone to go
| | | through and try to help you. Try
| | | next time to reduce it to the
| | | absolute minimum by removing
| | | sections while you still get the
| | | error. It is also easier to get
| | | help if you can remove unnecessary
| | | packages.
| | |
| | | It is also unreadable because you
| | | are posting in HTML and that makes
| | | the post unreadable as this is a
| | | plain text list.
| |
| | Hello,
| |
| | I second Michael's opinion. When the
| | post's code is very long, there is a
| | tendency to have less answers.
| |
| | Please post the output of
| |
| | dput(head(bwchist, 30))
| |
| | It's much shorter code and it
| | recreates the data so we will be
| | able to see what's wrong and try to
| | find a solution.
|
| Hi Pedro,
|
| Another 'best practice' and polite
| thing to do is link to other places
| you may have cross-posted. That will
| give people the opportunity to see if
| your questions has been answered in
| another forum.
|
| I saw your post on R-SIG-Finance
| (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html),
| and started to work on a solution.
|
| 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
Wow Josh! That's very elegant.
Myself now, I just plowed through the
original code to make it simpler, but am
at a loss as to how this histogram looks
...
x <- 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")
stock.prices <-
lapply(x, function(stock) {
tidyquant::tq_get(x=stock,from = '2019-12-31',get =
"stock.prices")
})
names(stock.prices) <- x
library(tidyquant)
returns <- lapply(stock.prices, function(data) {
tab <-
tq_transmute(
data = data,
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
tab[,"cr"] <- cumprod(1 + tab[,"idr_returns"])
tab[,"cumulative_returns"] <- tab[,"cr"] - 1
dplyr::pull(
tab[nrow(tab[,"cumulative_returns"]),
"cumulative_returns"]
)
})
bestworst <- simplify2array(returns)
namebw <-
c("Acciona", "ACS", "Aena",
"Amadeus",
"ArcelorMittal", "BBVA", "Sabadell",
"Santander", "Bankinter",
"CaixaBank", "Cellnex", "Enagas",
"ENCE", "Endesa", "Ferrovial",
"Grifols", "Iberdrola", "Inditex",
"Colonial", "IAG", "Mapfre",
"Melia", "Merlin", "Naturgy",
"REE",
"Repsol", "SGamesa", "Telefonica",
"Viscofan", "Acerinox", "Bankia",
"CIE", "MasMovil", "Almirall",
"Indra")
bwc <- data.frame(
symbol=names(bestworst),
Accion=namebw,
reval=bestworst)
| | | | bwc<-cbind(bwfinal2,bwfinal)
| | | | colnames(bwc)=c("Accion","reval")
| | | | bwc <- as.data.frame(bwc)
... aaaand you know something's
happening between here (where bwchist is
created), but you don't know what it is,
do you, Mr p?ramo?
| | | | colnames(bwchist)=c("Accion","reval")
| | | | bwchist <-as.data.frame(bwc[order(bwc$reval), ])
Best,
Rasmus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20200731/a8fcb94d/attachment.sig>
Hi Rasmus, Josh and Rui,
First of all many thanks in advance about your help.
The first thig is sometimes you say " you are posting in HTML and that
makes the
post unreadable as this is a plain text list" how can I put the code in the
correct way, not html (attaching in txt?)
The second about the code:
I have used this:
bwc <- cbind(bwfinal2,bwfinal)
colnames(bwc)=c("Accion","reval")
df <- matrix(unlist(bwc), nrow=nrow(bwc), byrow=F)
colnames(bwchist)=c("Accion","reval")
bwchist <-as.data.frame(bwc[order(df[,2]), ])
bwchist is the ordered cum stock returns in the year but because is a list
it is not possible to plot and histogram with x (names of stocks) and the x
axist the value of cum stocks (reval)
when I put dput(bwchist) the console says:
dput(bwchist)
structure(list(Accion = list("REE", "Enagas",
"Grifols", "Ferrovial",
"Acerinox", "Naturgy", "Inditex",
"Bankia", "ENCE", "Aena",
"Bankinter", "Mapfre", "CaixaBank",
"CIE", "Colonial", "Almirall",
"Indra", "ArcelorMittal", "ACS",
"Telefonica", "Amadeus",
"BBVA", "Merlin", "Santander",
"Repsol", "Melia", "Sabadell",
"IAG", "Acciona", "Endesa",
"MasMovil", "Iberdrola", "SGamesa",
"Viscofan", "Cellnex"), reval =
list(-0.0200827282700085,
-0.0590294115600855, -0.214126598790964, -0.220773677809979,
-0.229653300324357, -0.257944379583984, -0.283942789063822,
-0.285159347392533, -0.303814713896458, -0.30734460425763,
-0.309408155539818, -0.319912221435868, -0.322790949659181,
-0.344047579452905, -0.347919538415482, -0.356898907103825,
-0.374263261296661, -0.40147247119078, -0.405150043834815,
-0.406022775042175, -0.413786100987797, -0.440679109311707,
-0.442603156492871, -0.491634140733524, -0.499254932434042,
-0.6, -0.709737357505148, -0.724461258850966, 0.0220528711420083,
0.0462767672643172, 0.115044247787611, 0.238734548714937,
0.274578114644054, 0.343422896082666, 0.387826126094928)), class
"data.frame", row.names = c(NA,
-35L))
I try to make an hist or barplot but because it is a list no way to obtain
the plot.
Many thanks again for your help.
I have printed two manuals to improve my level, but if you can help me, I
would be very very gratefull.
El vie., 31 jul. 2020 a las 18:28, Rasmus Liland (<jral at posteo.no>)
escribi?:
> On 2020-07-31 10:07 -0500, Joshua Ulrich wrote:
> | On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas wrote:
> | | ?s 15:44 de 31/07/2020, Michael Dewey escreveu:
> | | | Dear Pedro
> | | |
> | | | Some comments in-line
> | | |
> | | | On 30/07/2020 21:16, Pedro p?ramo wrote:
> | | | | Hi all,
> | | | |
> | | | | I attach my code, the think is I
> | | | | want to make a bar plot the last
> | | | | variable called "bwchist" so the
> | | | | X axis are "Accion" and the y
> | | | | axis are "reval" values.
> | | | |
> | | | | I have prove class(bwchist) and
> | | | | says dataframe but its still a
> | | | | list because it says me I have
> | | | | prove to unlist, but it doesnt
> | | | | work
> | | | |
> | | | | hist(bwchist)
> | | | | Error in hist.default(bwchist) : 'x' must be numeric
> | | |
> | | | So bwchist is not a numeric
> | | | variable as hist needs. Aboce you
> | | | said it is a data frame but data
> | | | frames are not numeric.
> | | |
> | | | For future reference your example
> | | | is way too long for anyone to go
> | | | through and try to help you. Try
> | | | next time to reduce it to the
> | | | absolute minimum by removing
> | | | sections while you still get the
> | | | error. It is also easier to get
> | | | help if you can remove unnecessary
> | | | packages.
> | | |
> | | | It is also unreadable because you
> | | | are posting in HTML and that makes
> | | | the post unreadable as this is a
> | | | plain text list.
> | |
> | | Hello,
> | |
> | | I second Michael's opinion. When the
> | | post's code is very long, there is a
> | | tendency to have less answers.
> | |
> | | Please post the output of
> | |
> | | dput(head(bwchist, 30))
> | |
> | | It's much shorter code and it
> | | recreates the data so we will be
> | | able to see what's wrong and try to
> | | find a solution.
> |
> | Hi Pedro,
> |
> | Another 'best practice' and polite
> | thing to do is link to other places
> | you may have cross-posted. That will
> | give people the opportunity to see if
> | your questions has been answered in
> | another forum.
> |
> | I saw your post on R-SIG-Finance
> | (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html),
> | and started to work on a solution.
> |
> | 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
>
> Wow Josh! That's very elegant.
>
> Myself now, I just plowed through the
> original code to make it simpler, but am
> at a loss as to how this histogram looks
> ...
>
> x <- 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")
> stock.prices <-
> lapply(x, function(stock) {
> tidyquant::tq_get(x=stock,from = '2019-12-31',get >
"stock.prices")
> })
> names(stock.prices) <- x
>
> library(tidyquant)
>
> returns <- lapply(stock.prices, function(data) {
> tab <-
> tq_transmute(
> data = data,
> 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
> tab[,"cr"] <- cumprod(1 +
tab[,"idr_returns"])
> tab[,"cumulative_returns"] <- tab[,"cr"] -
1
>
> dplyr::pull(
> tab[nrow(tab[,"cumulative_returns"]),
> "cumulative_returns"]
> )
> })
>
> bestworst <- simplify2array(returns)
>
> namebw <-
> c("Acciona", "ACS", "Aena",
"Amadeus",
> "ArcelorMittal", "BBVA",
"Sabadell",
> "Santander", "Bankinter",
> "CaixaBank", "Cellnex", "Enagas",
> "ENCE", "Endesa", "Ferrovial",
> "Grifols", "Iberdrola",
"Inditex",
> "Colonial", "IAG", "Mapfre",
> "Melia", "Merlin", "Naturgy",
"REE",
> "Repsol", "SGamesa",
"Telefonica",
> "Viscofan", "Acerinox", "Bankia",
> "CIE", "MasMovil", "Almirall",
> "Indra")
>
> bwc <- data.frame(
> symbol=names(bestworst),
> Accion=namebw,
> reval=bestworst)
>
> | | | | bwc<-cbind(bwfinal2,bwfinal)
> | | | | colnames(bwc)=c("Accion","reval")
> | | | | bwc <- as.data.frame(bwc)
>
> ... aaaand you know something's
> happening between here (where bwchist is
> created), but you don't know what it is,
> do you, Mr p?ramo?
>
> | | | | colnames(bwchist)=c("Accion","reval")
> | | | | bwchist <-as.data.frame(bwc[order(bwc$reval), ])
>
> Best,
> Rasmus
>
[[alternative HTML version deleted]]
Hello,
Thanks for the data in dput format.
If you run
str(bwchist)
you will see that what you have is a data.frame, yes, but, with columns
of class "list", not vectors.
So the first step is to make them vectors, to unlist the lists. I will
do it applying function unlist() to each of the columns. Since lapply
returns a list, I remake a data.frame. The original is kept unchanged,
the new object is bwch.
bwch <- lapply(bwchist, unlist, recursive = FALSE)
bwch <- do.call(cbind.data.frame, bwch)
str(bwch)
Now that everything is as it should, here are two ways of plotting bar
graphs.
#--- base R
x11(width = 11.5, height = 6)
old_par <- par(mar =? par("mar") + c(1, 0, 0, 0))
bp <- barplot(bwch$reval, yaxt = "n", ylim = c(-1, 0.4))
axis(1, at = bp, labels = bwch$Accion, las = 2)
axis(2, at = pretty(bwch$reval))
par(old_par)
#--- package ggplot2
library(ggplot2)
x11(width = 11.5, height = 6)
ggplot(bwch, aes(factor(Accion, levels = Accion), reval)) +
? geom_col() +
? theme(axis.text.x = element_text(angle = 60, hjust = 1))
Hope this helps,
Rui Barradas
?s 19:48 de 03/08/2020, Pedro p?ramo escreveu:> Hi Rasmus, Josh and Rui,
>
> First of all many thanks?in advance about your help.
>
> The first thig?is sometimes?you say " you are posting in HTML and that
> makes the
> post unreadable as this is a plain text list" how can I put the code
> in the correct way, not html (attaching in txt?)
>
> The second?about the code:
>
> I have used this:
>
> bwc <- cbind(bwfinal2,bwfinal)
> colnames(bwc)=c("Accion","reval")
> df <- matrix(unlist(bwc), nrow=nrow(bwc), byrow=F)
> colnames(bwchist)=c("Accion","reval")
> bwchist <-as.data.frame(bwc[order(df[,2]), ])
>
> bwchist is the ordered cum stock returns in the year but because is a
> list it is not possible to plot and histogram with x (names of stocks)
> and the x axist?the value of cum stocks (reval)
>
> when I put dput(bwchist) the console says:
>
> dput(bwchist)
> structure(list(Accion = list("REE", "Enagas",
"Grifols", "Ferrovial",
> ? ? "Acerinox", "Naturgy", "Inditex",
"Bankia", "ENCE", "Aena",
> ? ? "Bankinter", "Mapfre", "CaixaBank",
"CIE", "Colonial", "Almirall",
> ? ? "Indra", "ArcelorMittal", "ACS",
"Telefonica", "Amadeus",
> ? ? "BBVA", "Merlin", "Santander",
"Repsol", "Melia", "Sabadell",
> ? ? "IAG", "Acciona", "Endesa",
"MasMovil", "Iberdrola", "SGamesa",
> ? ? "Viscofan", "Cellnex"), reval =
list(-0.0200827282700085,
> ? ? -0.0590294115600855, -0.214126598790964, -0.220773677809979,
> ? ? -0.229653300324357, -0.257944379583984, -0.283942789063822,
> ? ? -0.285159347392533, -0.303814713896458, -0.30734460425763,
> ? ? -0.309408155539818, -0.319912221435868, -0.322790949659181,
> ? ? -0.344047579452905, -0.347919538415482, -0.356898907103825,
> ? ? -0.374263261296661, -0.40147247119078, -0.405150043834815,
> ? ? -0.406022775042175, -0.413786100987797, -0.440679109311707,
> ? ? -0.442603156492871, -0.491634140733524, -0.499254932434042,
> ? ? -0.6, -0.709737357505148, -0.724461258850966, 0.0220528711420083,
> ? ? 0.0462767672643172, 0.115044247787611, 0.238734548714937,
> ? ? 0.274578114644054, 0.343422896082666, 0.387826126094928)), class =
> "data.frame", row.names = c(NA,
> -35L))
>
> I try to make an hist or barplot but because it is a list no way to
> obtain the plot.
>
> Many thanks again for your help.
>
> I have printed two manuals to improve my level, but if you can help
> me, I would be very very gratefull.
>
>
>
> El vie., 31 jul. 2020 a las 18:28, Rasmus Liland (<jral at posteo.no
> <mailto:jral at posteo.no>>) escribi?:
>
> On 2020-07-31 10:07 -0500, Joshua Ulrich wrote:
> | On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas wrote:
> | | ?s 15:44 de 31/07/2020, Michael Dewey escreveu:
> | | | Dear Pedro
> | | |
> | | | Some comments in-line
> | | |
> | | | On 30/07/2020 21:16, Pedro p?ramo wrote:
> | | | | Hi all,
> | | | |
> | | | | I attach my code, the think is I
> | | | | want to make a bar plot the last
> | | | | variable called "bwchist" so the
> | | | | X axis are "Accion" and the y
> | | | | axis are "reval" values.
> | | | |
> | | | | I have prove class(bwchist) and
> | | | | says dataframe but its still a
> | | | | list because it says me I have
> | | | | prove to unlist, but it doesnt
> | | | | work
> | | | |
> | | | | hist(bwchist)
> | | | | Error in hist.default(bwchist) : 'x' must be numeric
> | | |
> | | | So bwchist is not a numeric
> | | | variable as hist needs. Aboce you
> | | | said it is a data frame but data
> | | | frames are not numeric.
> | | |
> | | | For future reference your example
> | | | is way too long for anyone to go
> | | | through and try to help you. Try
> | | | next time to reduce it to the
> | | | absolute minimum by removing
> | | | sections while you still get the
> | | | error.? It is also easier to get
> | | | help if you can remove unnecessary
> | | | packages.
> | | |
> | | | It is also unreadable because you
> | | | are posting in HTML and that makes
> | | | the post unreadable as this is a
> | | | plain text list.
> | |
> | | Hello,
> | |
> | | I second Michael's opinion. When the
> | | post's code is very long, there is a
> | | tendency to have less answers.
> | |
> | | Please post the output of
> | |
> | |? ? ?dput(head(bwchist, 30))
> | |
> | | It's much shorter code and it
> | | recreates the data so we will be
> | | able to see what's wrong and try to
> | | find a solution.
> |
> | Hi Pedro,
> |
> | Another 'best practice' and polite
> | thing to do is link to other places
> | you may have cross-posted.? That will
> | give people the opportunity to see if
> | your questions has been answered in
> | another forum.
> |
> | I saw your post on R-SIG-Finance
> | (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html),
> | and started to work on a solution.
> |
> | 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 <http://ANA.MC>", "ACS.MC
<http://ACS.MC>",
> "AENA.MC <http://AENA.MC>", "AMS.MC
<http://AMS.MC>", "MTS.MC
> <http://MTS.MC>", "BBVA.MC
<http://BBVA.MC>", "SAB.MC
> <http://SAB.MC>",
> |? ?"SAN.MC <http://SAN.MC>", "BKT.MC
<http://BKT.MC>", "CABK.MC
> <http://CABK.MC>", "CLNX.MC
<http://CLNX.MC>", "ENG.MC
> <http://ENG.MC>", "ENC.MC <http://ENC.MC>",
"ELE.MC <http://ELE.MC>",
> |? ?"FER.MC <http://FER.MC>", "GRF.MC
<http://GRF.MC>", "IBE.MC
> <http://IBE.MC>", "ITX.MC <http://ITX.MC>",
"COL.MC
> <http://COL.MC>", "IAG.MC <http://IAG.MC>",
"MAP.MC <http://MAP.MC>",
> |? ?"MEL.MC <http://MEL.MC>", "MRL.MC
<http://MRL.MC>", "NTGY.MC
> <http://NTGY.MC>", "REE.MC <http://REE.MC>",
"REP.MC
> <http://REP.MC>", "SGRE.MC
<http://SGRE.MC>", "TEF.MC
> <http://TEF.MC>",
> |? ?"VIS.MC <http://VIS.MC>", "ACX.MC
<http://ACX.MC>", "BKIA.MC
> <http://BKIA.MC>", "CIE.MC <http://CIE.MC>",
"MAS.MC
> <http://MAS.MC>", "ALM.MC <http://ALM.MC>",
"IDR.MC <http://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
>
> Wow Josh!? That's very elegant.
>
> Myself now, I just plowed through the
> original code to make it simpler, but am
> at a loss as to how this histogram looks
> ...
>
> ? ? ? ? x <- c("ANA.MC <http://ANA.MC>",
"ACS.MC <http://ACS.MC>",
> "AENA.MC <http://AENA.MC>", "AMS.MC
<http://AMS.MC>", "MTS.MC
> <http://MTS.MC>", "BBVA.MC
<http://BBVA.MC>",
> ? ? ? ? ? "SAB.MC <http://SAB.MC>", "SAN.MC
<http://SAN.MC>",
> "BKT.MC <http://BKT.MC>", "CABK.MC
<http://CABK.MC>", "CLNX.MC
> <http://CLNX.MC>", "ENG.MC <http://ENG.MC>",
> ? ? ? ? ? "ENC.MC <http://ENC.MC>", "ELE.MC
<http://ELE.MC>",
> "FER.MC <http://FER.MC>", "GRF.MC
<http://GRF.MC>", "IBE.MC
> <http://IBE.MC>", "ITX.MC <http://ITX.MC>",
> ? ? ? ? ? "COL.MC <http://COL.MC>", "IAG.MC
<http://IAG.MC>",
> "MAP.MC <http://MAP.MC>", "MEL.MC
<http://MEL.MC>", "MRL.MC
> <http://MRL.MC>", "NTGY.MC
<http://NTGY.MC>",
> ? ? ? ? ? "REE.MC <http://REE.MC>", "REP.MC
<http://REP.MC>",
> "SGRE.MC <http://SGRE.MC>", "TEF.MC
<http://TEF.MC>", "VIS.MC
> <http://VIS.MC>", "ACX.MC <http://ACX.MC>",
> ? ? ? ? ? "BKIA.MC <http://BKIA.MC>", "CIE.MC
<http://CIE.MC>",
> "MAS.MC <http://MAS.MC>", "ALM.MC
<http://ALM.MC>", "IDR.MC
> <http://IDR.MC>")
> ? ? ? ? stock.prices <-
> ? ? ? ? ? lapply(x, function(stock) {
> ? ? ? ? ? ? tidyquant::tq_get(x=stock,from = '2019-12-31',get
> "stock.prices")
> ? ? ? ? ? })
> ? ? ? ? names(stock.prices) <- x
>
> ? ? ? ? library(tidyquant)
>
> ? ? ? ? returns <- lapply(stock.prices, function(data) {
> ? ? ? ? ? tab <-
> ? ? ? ? ? ? tq_transmute(
> ? ? ? ? ? ? ? data = data,
> ? ? ? ? ? ? ? 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
> ? ? ? ? ? tab[,"cr"] <- cumprod(1 +
tab[,"idr_returns"])
> ? ? ? ? ? tab[,"cumulative_returns"] <-
tab[,"cr"] - 1
>
> ? ? ? ? ? dplyr::pull(
> ? ? ? ? ? ? tab[nrow(tab[,"cumulative_returns"]),
> ? ? ? ? ? ? ? ? ? ? ? ? ? "cumulative_returns"]
> ? ? ? ? ? )
> ? ? ? ? })
>
> ? ? ? ? bestworst <- simplify2array(returns)
>
> ? ? ? ? namebw <-
> ? ? ? ? ? c("Acciona", "ACS", "Aena",
"Amadeus",
> ? ? ? ? ? ? "ArcelorMittal", "BBVA",
"Sabadell",
> ? ? ? ? ? ? "Santander", "Bankinter",
> ? ? ? ? ? ? "CaixaBank", "Cellnex",
"Enagas",
> ? ? ? ? ? ? "ENCE", "Endesa",
"Ferrovial",
> ? ? ? ? ? ? "Grifols", "Iberdrola",
"Inditex",
> ? ? ? ? ? ? "Colonial", "IAG", "Mapfre",
> ? ? ? ? ? ? "Melia", "Merlin", "Naturgy",
"REE",
> ? ? ? ? ? ? "Repsol", "SGamesa",
"Telefonica",
> ? ? ? ? ? ? "Viscofan", "Acerinox",
"Bankia",
> ? ? ? ? ? ? "CIE", "MasMovil",
"Almirall",
> ? ? ? ? ? ? "Indra")
>
> ? ? ? ? bwc <- data.frame(
> ? ? ? ? ? symbol=names(bestworst),
> ? ? ? ? ? Accion=namebw,
> ? ? ? ? ? reval=bestworst)
>
> | | | | bwc<-cbind(bwfinal2,bwfinal)
> | | | | colnames(bwc)=c("Accion","reval")
> | | | | bwc <- as.data.frame(bwc)
>
> ... aaaand you know something's
> happening between here (where bwchist is
> created), but you don't know what it is,
> do you, Mr p?ramo?
>
> | | | | colnames(bwchist)=c("Accion","reval")
> | | | | bwchist <-as.data.frame(bwc[order(bwc$reval), ])
>
> Best,
> Rasmus
>
--
Este e-mail foi verificado em termos de v?rus pelo software antiv?rus Avast.
https://www.avast.com/antivirus
[[alternative HTML version deleted]]