[R-SIG-Finance] Select best worst
Brian G. Peterson
br|@n @end|ng |rom br@verock@com
Wed Jul 29 16:34:15 CEST 2020
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 using 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.
More information about the R-SIG-Finance
mailing list