[R-SIG-Finance] Select best worst

Pedro páramo percent||101 @end|ng |rom gm@||@com
Wed Jul 29 15:37:47 CEST 2020


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]]



More information about the R-SIG-Finance mailing list