Switch between actual and forecasted values of exogenous variables in forecast::tsCV function I am using forecast::tsCV function in R to perform a rolling origin forecast of 1 day to 1 year ahead horizons using  daily frequency data.
Suppose my outcome variable is y, and I have x1, x2 and x3 as exogenous variables.
If I want to use the actual values of x1, x2 and x3 for training and testing sample to perform  a seven day ahead out of sample forecast, then it would be as:
xreg <- data.frame(x1, x2, x3)

fit <- function(x, h, xreg, newxreg) {
  forecast(Arima(x, order=c(1,0,1), xreg=xreg), xreg=newxreg)
}

e <- forecast::tsCV(y, fit, h=7, window=1000, xreg=xreg)

However, the problem is that I want to use the actual values of x1 and x2 for the training sample and their forecasted values for the rest of the period.
So I have forecasted values of x1 and x2, lets call them f_x1 and f_x2.
If the first forecast point 1007, x1, x2 and x3 have to be used for 1 to 1000, then switch to f_x1 and f_x2 from 1001 to 1007, and this have to roll over as the windows roll.
I have tried this one:
xreg1=contains x1, x2 and x3
xreg2= contains f_x1, f_x2, x3
then:
e <tsCV(price, fit, h=7,
window =1000,
if (length(xreg) < 1001) {
xreg=xreg1
} else if (length(xreg) > 1000) {
xreg=xreg2
})

But the output shows that it doesn't convert using data from xtreg 1 to xtreg2.
Can someone help me how can I develop the forecast::tsCV function to perform this?

Thanks for the answer,
Its from PackageName::forecast

You would need to rewrite the tsCV function to implement this. The way it is written assumes that there is only one xreg row for each time period, but you are wanting to have a different xreg depending on whether the row is being used for training data or test data.
The function itself is not very long, and written only in R, so should not be difficult to adapt. Your new function will need to have two xreg arguments, one with actual values and one with forecasted values. Then you just need to switch to using the forecasted version when creating xreg_future in the function. The xreg_subset variable should use the actual values.
