You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to calculate returns (arithmetic returns or log returns) for custom time period. In the package, we can calculate returns for monthly or yearly. But in my country, financial year begins from 1st April, and ends on 31st March. I want to calculate returns for this financial year. In the function tq_performance, is there a way to specify when the year begins, and when the year ends? For the time being, I have come up with a workaround. I am attaching its code below. But can we have any simple solution to this?
`# Get data
library("tibble")
library("tidyquant")
RIL<- tq_get("RELIANCE.NS")
RIL1<- RIL %>% mutate(CalYear = year(date),
Month = month(date),
FinYear = if_else(Month<4,CalYear,CalYear+1))
I want to calculate returns (arithmetic returns or log returns) for custom time period. In the package, we can calculate returns for monthly or yearly. But in my country, financial year begins from 1st April, and ends on 31st March. I want to calculate returns for this financial year. In the function tq_performance, is there a way to specify when the year begins, and when the year ends? For the time being, I have come up with a workaround. I am attaching its code below. But can we have any simple solution to this?
`# Get data
library("tibble")
library("tidyquant")
RIL<- tq_get("RELIANCE.NS")
RIL1<- RIL %>% mutate(CalYear = year(date),
Month = month(date),
FinYear = if_else(Month<4,CalYear,CalYear+1))
Get minimum and max dates in each year
start_dates = c()
end_dates = c()
for(year in format(min(RIL1$date),"%Y"):format(max(RIL1$date),"%Y")){
start_dates =
c(start_dates,
min(RIL1$date[format(RIL1$date, "%Y") == format(as.Date(ISOdate(year, 1, 1)),"%Y")])
)
end_dates =
c(end_dates,
max(RIL1$date[format(RIL1$date, "%Y") == format(as.Date(ISOdate(year, 1, 1)),"%Y")])
)
}
Get filtered data
RIL2 <- RIL1[(RIL1$date %in% start_dates | RIL1$date %in% end_dates),]
Get log returns, even indexes represent end of each year rows
end_adjusted = RIL2$adjusted[1:length(RIL2$adjusted) %% 2 == 0]
beginning_adjusted = RIL2$adjusted[1:length(RIL2$adjusted) %% 2 != 0]
log_returns = log(end_adjusted/beginning_adjusted)
Put log returns and years in a tibble.
result = tibble(log_returns ,format(RIL2$date[1:length(RIL2$date) %% 2 == 0], "%Y"))
Result
result`
The text was updated successfully, but these errors were encountered: