Dear Ilya, Thanks for the prompt reply once more! The code I used is below and its output is attached (which is in Portuguese).
tickers <- c('UNIP6.SA', 'KLBN4.SA', 'CSAN3.SA', 'BBAS3.SA', 'PETR4.SA', '
SBSP3.SA')
tickers.clean <- str_replace_all(tickers,'.SA$', '')
## weights
wts = c(0.4, 0.15, 0.15, 0.05, 0.1, 0.15)
wts_tbl <- tibble(symbol = tickers,
wts = wts)
wts_percent <- wts_tbl
wts_percent$wts <- sapply(wts_percent$wts, function(x) percent(x, accuracy
= 0.01))
print(wts_percent)
## YF download
price_data <- tq_get(tickers,
from = start,
to = end,
get = 'stock.prices')
## Daily return for each stock
ret_data <- price_data %>% group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
col_rename = "ret",
relationship = "many-to-many")
## weights, returns join on a daily basis
ret_data <- left_join(ret_data, wts_tbl, by = 'symbol', relationship =
"many-to-many")
## weighted average
ret_data <- ret_data %>%
mutate(wt_return = wts * ret)
## removal of the .SA suffix from Brazilian stocks via custom function
del.SA downloaded through tidyquant
ret_data <- del.SA(ret_data, "tq")
## total return on a given day is the sum of the prices weight of the
portfolio
port_ret <- ret_data %>%
group_by(date) %>%
summarise(port_ret = sum(wt_return))
## Portfolio cumulative return via cumprod()
port_cumulative_ret <- port_ret %>%
mutate(cum_ret = cumprod(1 + port_ret))
## filter for period of interest
port_cumulative_ret <- port_cumulative_ret[port_cumulative_ret$date >=
as.Date("2024-06-01") &
port_cumulative_ret$date <= today(),]
posrtfolio.return <-
port_cumulative_ret %>% ggplot(aes(x = date, y = cum_ret)) +
theme_gray() +
theme(plot.background = element_rect(fill = "gray86")) +
geom_line(color = "blue") +
theme(axis.text.x = element_text(angle = 50, hjust = 1)) +
labs(x = 'Data',
y = 'Retorno Acumulado',
title = paste0('Retorno Acumulado do Portfolio de
',length(tickers.clean), ' ativo(s) em ',
format(as.Date(end), format="%d/%m/%Y")),
subtitle = paste0("Ativos: ", toString(tickers.clean), "\nPesos: ",
toString(percent(wts, accuracy = 0.1)))
) +
scale_x_date(date_breaks = '2 weeks',
date_labels = '%d %b %y') +
scale_y_continuous(
breaks = seq(0, 10, 0.05),
labels = scales::percent_format(accuracy = 1)
)
Code for saving it to pdf seems quite straight forward and simple thus it
isn't shown.
I hope this work for you. If it doesn't let me know for it does for me
(OSX v11.7, R v4.1).
Regards,
--
André Luiz Tietbohl Ramos, PhD
On Mon, Feb 2, 2026 at 2:43 PM Ilya Kipnis <[email protected]> wrote:
> Andre,
>
> I am saying that those of us that aren't *you* cannot run your code
> because we don't know how you formulated the "stocks" object. Your code is
> not reproducible to anybody else. Please include a minimum *reproducible*
> example.
>
> -Ilya
>
> On Mon, Feb 2, 2026 at 12:41 PM Andre Luiz Tietbohl Ramos <
> [email protected]> wrote:
>
>> Dear Ilya,
>>
>> Thanks for the prompt reply! The code from my previous message works and
>> I uploaded as an example so you could see what my intent is. Your
>> suggestion will be considered, thank you! Its output is attached (which is
>> in Portuguese).
>>
>> Thanks,
>>
>> --
>> André Luiz Tietbohl Ramos, PhD
>>
>>
>> On Mon, Feb 2, 2026 at 12:56 PM Ilya Kipnis <[email protected]>
>> wrote:
>>
>>> This isn't a reproducible example--we don't have the stocks data frame.
>>> Also, it is bad practice to use dplyr in the R/Finance stack as it
>>> overrides multiple functions, such as lag and filter.
>>>
>>> On Mon, Feb 2, 2026 at 8:39 AM Andre Luiz Tietbohl Ramos <
>>> [email protected]> wrote:
>>>
>>>> Hello,
>>>>
>>>> Is it possible to customize the x-axis label? Using ggplot I have the
>>>> custom x-label described below,
>>>>
>>>> stocks %>%
>>>> ggplot(aes(x = date, y = cum_ret)) +
>>>> theme_gray() +
>>>> theme(plot.background = element_rect(fill = "gray86")) +
>>>> geom_line(color = "blue") +
>>>> *theme(axis.text.x = element_text(angle = 50, hjust = 1))* +
>>>> labs(x = 'Data',
>>>> y = 'Cumulative Return',
>>>> title = paste0('Portfolio Cumulative Return of ',
>>>> length(tickers.clean), ' stocks on ',
>>>> format(as.Date(end), format="%d/%m/%Y")),
>>>> subtitle = paste0("Ativos: ", toString(tickers.clean),
>>>> "\nWeights:
>>>> ",
>>>> toString(percent(wts, accuracy = 0.1)))
>>>> ) +
>>>>
>>>>
>>>>
>>>>
>>>> * scale_x_date(date_breaks = '2 weeks',
>>>> date_labels = '%d %b %y') + scale_y_continuous( breaks =
>>>> seq(0,
>>>> 10, 0.05), labels = scales::percent_format(accuracy = 1)*
>>>> )
>>>>
>>>> TIA,
>>>>
>>>> --
>>>> Andre Luiz Tietbohl Ramos, PhD
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> _______________________________________________
>>>> [email protected] 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.
>>>>
>>>
Retorno_Acumulado_do_Portfolio_UNIP6,KLBN4,CSAN3,BBAS3,PETR4,SBSP3_em_02-02-2026.pdf
Description: Adobe PDF document
port_cumulative_ret.Rdata
Description: Binary data
_______________________________________________ [email protected] 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.
