Skip to content
All library documents

Walk-Forward Backtesting for Machine-Learning Trading Strategies

Article Quant Q&A · Author: Cairan Van Rooyen

Summary

The document asks how to backtest a stock-direction model that is retrained on historical price windows and used to generate trades. The author reports prediction accuracy and wants to test a strategy over multiple years, including risk controls such as stop losses. The response points to a Python framework designed for machine-learning strategies, describing support for rolling and expanding walk-forward optimization and integration with common machine-learning libraries.

The key practical idea is to evaluate a model-driven strategy through a repeated train-and-predict process that respects the passage of time, rather than relying on prediction accuracy alone. A backtest can then assess trading outcomes and risk rules. The source offers a framework recommendation, not evidence that the model or any resulting strategy is profitable. It does not specify order timing, transaction costs, data leakage safeguards, stop-loss design, or validation results, so those choices still need to be addressed in an implementation.

Key ideas

  • A machine-learning backtest can retrain on rolling or expanding historical windows before producing new predictions.
  • Model predictions need to be translated into explicit orders to evaluate a trading strategy.
  • Backtests can incorporate risk controls such as stop losses and span multiple years.
  • Prediction accuracy alone does not establish whether a strategy is profitable after trading costs and risk controls.

Tags

Full text
# Suggestions for backtesting machine learning 'model'/strategy in Python


# Suggestions for backtesting machine learning 'model'/strategy in Python












I have coded a machine learning algo (sklearn) in Python, that uses different 'look back periods' for training a model, which is then used to predict future prices of a stock.

It has a 52% accuracy in predicting the future price. I now wish to build a simple strategy around this for backtesting. I am specifically interested in applying risk management, stoplosses, etc to test over a number of years.

Can anyone recommend a suitable Python-based backtesting platform for use with a sklearn ML algo that for each period looks back ata number of prior periods prices, trains a model, predicts future direction, and then issues orders to the backtesting platform?

Google has returned a whole range of options and so I am really after advice from anyone else that might have backtested a sklearn ML algo using a Python-based backtesting platform for recommendations...

Failing this, i might build a simple version myself.

Thank you!

## Answer by Brian from QuantRocket (score 1)

https://quant.stackexchange.com/a/71471

MoonshotML is a backtesting framework specifically for machine learning strategies. It is part of QuantRocket. It supports rolling and expanding walk-forward optimization and integrates with scikit-learn among other Python ML libraries.

Disclaimer: I'm affiliated with QuantRocket.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.