QuantRocket logo

Disclaimer

Walk-forward Optimization

Install strategy file

Moonshot looks for strategies inside the "moonshot" directory, so execute the following cell to "install" the strategy by moving the file to that location:

The ! sytax below lets us execute terminal commands from inside the notebook.

Run walk-forward optimization

Now we can run a walk-forward optimization, which iteratively trains and tests the machine learning model over successive periods.

In this example, we retrain the model annually (train="Y").

Because we do not specify a model parameter, the default model will be used, which is a regression model using Stochastic Gradient Descent. See the usage guide for more details about the default model.

The parameter progress=True causes the moonshot service to log the walk-forward progress during the analysis. To view the progress, open a separate Terminal window and stream the logs (using cut to format the logs to fit better in the window): quantrocket flightlog stream | cut -d ' ' -f 5-

As always, it is also a good idea to stream the detailed logs (quantrocket flightlog stream -d) to see more granular logging during the walk-forward optimization.

In the terminal window, you'll see output resembling the following:

[kitchensink-ml] Walk-forward analysis progress
                train                    test             progress
                start         end       start         end   status Sharpe
iteration
0          1998-12-31  2001-12-30  2001-12-31  2002-12-30        ✓  -0.66
1          2001-12-31  2002-12-30  2002-12-31  2003-12-30        ✓   4.13
2          2002-12-31  2003-12-30  2003-12-31  2004-12-30        ✓   0.59
3          2003-12-31  2004-12-30  2004-12-31  2005-12-30        -
4          2004-12-31  2005-12-30  2005-12-31  2006-12-30
5          2005-12-31  2006-12-30  2006-12-31  2007-12-30
6          2006-12-31  2007-12-30  2007-12-31  2008-12-30
...

Walk-forward results

The walk-forward optimization returns a backtests results CSV and the trained model.

Backtest results CSV

Open the backtest results CSV like you would for any Moonshot backtest, using Moonchart (and/or pyfolio).

Trained model

The trained model can be opened and inspected with joblib:

To perform live trading, you would need to update your ML strategy to point to this trained model:

class TheKitchenSinkML(MoonshotML):
        ...
        MODEL = "/codeload/kitchensink_ml/kitchensink_ml_trained_model.joblib"

See the usage guide for more details.


Next Up

Part 5: Dimensionality Reduction with PCA