construct portfolio over rolling window for daily 6 stocks market

Hi,

I have daily 6 stocks prices data. start from jan 2019 to Dec 2022.. How can I calculate the log return of this data over a rolling window?

I want my first training set start from Jan to Nov 2019 ,
Then I move one month a head. my second training set is from Feb 2019 to Dec 2019 ,and so on. so I will have 48 rolling window if I am correct.


data<-NULL
tickers  <- c("^GSPC" ,"^IXIC",'^DJI','EMHY','AGG','TIP') 
data<-NULL
for (ticker in tickers) 
data <- cbind(data,getSymbols(ticker, from = "2019-01-01", to="2022-11-30", auto.assign = F)[,4]) 
colnames(data)<-tickers  # just change closing price name to tickers  name 


log.return<-na.omit(Return.calculate(data, method = "log")*100) 

for each training set I want to construct my portfolio which depends of course on the return .

Thank you in advance

Check out the package {RccpRoll}.

I have used for rolling windows like this:

library(dplyr)
library(RccpRoll)
daily_sessions %>%
  mutate(
    weekly_length(length, 7, fill = NA, align = "right")
  )

can you please explain your code more ? Sorry I did not understand.

@safara I corrected the code and added comments

# Load in packages
library(dplyr)
library(RccpRoll)

# 'daily_sessions' is the dataset
daily_sessions %>%
  mutate(
# Weekly length is the rolling mean of 7 'lengths'
    weekly_length = roll_mean(length, 7, fill = NA, align = "right")
  )

thank you. basically what I am looking for is the following sets for example

return.1=window(log.return[1:230])  # this is the return from "2019-01-01", to="2022-11-30"
return.2=window(log.return[21:251]) # this is the return from Feb to Dec  
return.3=window(log.return[40:271]) # this is the return from March to Jan 
return.4=window(log.return[61:291]) # this is the return from Apr to Feb 
return.5=window(log.return[82:312]) # this is the return from May to March 
return.6=window(log.return[104:334])
return.7=window(log.return[124:354])
return.8=window(log.return[146:376])
return.9=window(log.return[168:398])
return.10=window(log.return[188:419])
return.11=window(log.return[211:440])
return.12=window(log.return[231:462])
return.13=window(log.return[252:482])
return.14=window(log.return[273:504])
return.15=window(log.return[292:523])
return.16=window(log.return[314:542])
return.17=window(log.return[335:564])
return.18=window(log.return[355:586])
return.19=window(log.return[377:606])
return.20=window(log.return[399:628])
return.21=window(log.return[420:649])
return.22=window(log.return[441:671])
return.23=window(log.return[463:692])
return.24=window(log.return[483:713])
return.25=window(log.return[505:734])
return.26=window(log.return[524:756])
return.27=window(log.return[543:776])
return.28=window(log.return[566:795])
return.29=window(log.return[587:818])
return.30=window(log.return[607:838])
return.31=window(log.return[629:859])
return.32=window(log.return[650:880])
return.33=window(log.return[672:900])
return.34=window(log.return[693:923])
return.35=window(log.return[714:944])
return.36=window(log.return[735:965])
return.37=window(log.return[757:986]) # this is the return from Jan 2022 to Nov 2022 

so rather than writing all of the above code I want to split it immediately

@safara The 'weekly_length' argument in my example above would be those sets for 7 instances. Yours would just be 230 instead of 7.

thank you. once I run the code I recived error

Error in `mutate()`:
! Problem while computing `weekly_length = roll_mean(length, 230, fill
  = NA, align = "right")`.
Caused by error in `roll_mean_impl()`:
! Not compatible with requested type: [type=builtin; target=double].
Run `rlang::last_error()` to see where the error occurred.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.