feature icon Machine Learning and AI

Backtest and live trade machine learning and deep learning trading strategies with QuantRocket

Walk-forward optimization

Support for rolling and expanding walk-forward optimization, widely considered the best technique for validating machine learning models in finance.

Incremental/out-of-core learning

Train models and run backtests even when your data is too large to fit in memory.

Multiple machine learning/deep learning packages

Support for multiple Python machine learning packages including scikit-learn, Keras + TensorFlow, and XGBoost.

A simple ML strategy

from moonshot import MoonshotML

class DemoMLStrategy(MoonshotML):

    CODE = "demo-ml"
    DB = "demo-stk-1d"

    def prices_to_features(self, prices):
        closes = prices.loc["Close"]
        # create a dict of DataFrame features
        features = {}
        # use past returns...
        features["returns_1d"]= closes.pct_change()
        features["returns_2d"] = (closes - closes.shift(2)) / closes.shift(2)
        # ...to predict next day returns
        targets = closes.pct_change().shift(-1)
        return features, targets

    def predictions_to_signals(self, predictions, prices):
        # buy when the model predicts a positive return
        signals = predictions > 0
        return signals.astype(int)

Run it!

>>> from quantrocket.moonshot import ml_walkforward
>>> ml_walkforward("demo-ml",
                   start_date="2006-01-01", end_date="2012-12-31",
                   train="Y", min_train="4Y",

Machine learning support is built on top of Moonshot, the backtester for data scientists.

Pandas-based: Moonshot is based on Pandas, the centerpiece of the Python data science stack. If you love Pandas you'll love Moonshot. Moonshot can be thought of as a set of conventions for organizing Pandas code for the purpose of running backtests.

Lightweight: Moonshot is simple and lightweight because it relies on the power and flexibility of Pandas and doesn't attempt to re-create functionality that Pandas can already do. No bloated codebase full of countless indicators and models to import and learn. Most of Moonshot's code is contained in a single Moonshot class.

Fast: Moonshot is fast because Pandas is fast. No event-driven backtester can match Moonshot's speed. Speed promotes alpha discovery by facilitating rapid experimentation and research iteration.

Multi-asset class, multi-time frame: Moonshot supports end-of-day and intraday strategies using equities, futures, and forex.

Live trading: Live trading with Moonshot can be thought of as running a backtest on up-to-date historical data and generating a batch of orders based on the latest signals produced by the backtest.

No black boxes, no magic: Moonshot provides many conveniences to make backtesting easier, but it eschews hidden behaviors and complex, under-the-hood simulation rules that are hard to understand or audit. What you see is what you get.

Learn more in the machine learning usage guide or see sample ML strategies in the Codeload Library.