Backtesting with ML-based investment strategies

Learn to integrate a Machine Learning model into an investment strategy and evaluate its performance using the library with Python.

If you create a Machine Learning model to predict the price of a stock:

Graphical representation of a decision tree model used to predict stock prices, illustrating how the feature space is divided to reach more accurate predictions.
F1. Representation of a decision tree model

How can you evaluate its performance if you apply it to your investment strategy?

Illustrative scheme of applying a Machine Learning model in an investment strategy, showing the decision-making process based on price predictions.
F2. Integration of Machine Learning in investment strategy


We start with the stock data of NVIDIA with its ticker NVDA.

Check out this tutorial to learn how to preprocess the daily return of a stock.

import pandas as pd

df = pd.read_csv('data.csv', index_col='Date', parse_dates=True)
Capture of NVIDIA's stock data, showing a dataset ready for analysis and application of Machine Learning models, focusing on data preparation and cleaning.
F3. NVIDIA data prepared for ML analysis


  1. How is a Machine Learning model implemented to predict the change in closing price?
  2. What is the role of the min_samples_leaf parameter in the DecisionTreeRegressor algorithm?
  3. How do we measure the model’s error and what does it tell us about its performance?
  4. How do we introduce a Machine Learning model into an investment strategy?
  5. How do we evaluate the performance of the Machine Learning investment strategy?

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to datons.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.