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

Attachment: signature.asc
Description: PGP signature

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to