Risk Factors with PCA for train-test format

Hello,

Chapter 13.01.03: “PCA for Algorithmic Trading: Data-Driven Risk Factors” provides framework to estimate risk-factors with the top-2 principal component of returns data. The conclusion of the chapter says that the series of PC can be inserted as the features in a returns’ prediction model

risk_factors = pd.DataFrame(pca.transform(returns)[:, :2],
columns=[‘Principal Component 1’, ‘Principal Component 2’],
index=returns.index)
risk_factors.info()

When we test the prediction model, the features cannot use future obserations.
It means that risk-factors variables in test period can be obtained only from PCA applied to previous days. So, it is wrong to apply PCA once for all the considered window (train+test).

What do you think the correct option to apply PCA risk factors in dynamic way separating train and test periods and applicable with cross-validation?

Thanks in advance to everyone who will response this topic!