Skip to content
All library documents

Validating Freqtrade Backtests with a Simple Moving Average Crossover

Code Freqtrade

Summary

This Freqtrade strategy is designed to compare its backtest with a corresponding strategy run on another platform for the same coin, period, and resolution. It calculates fast and slow simple moving averages over 14 and 28 periods on hourly candles. A long entry is signaled whenever the fast average is above the slow average, and an exit is signaled when it falls below the slow average.

The script also sets minimal return on investment thresholds and a wide stop-loss limit, which affect simulated exits alongside the crossover signals. The document describes a validation setup, not a completed cross-platform comparison: it provides no data sample, backtest output, or evidence that the platforms produce matching results. Differences in candle data, fees, execution assumptions, and strategy handling could affect equivalence.

Key ideas

  • The strategy uses a 14-period and a 28-period simple moving average on hourly candles.
  • It enters long when the faster average exceeds the slower average.
  • It signals an exit when the faster average falls below the slower average.
  • The intended validation compares backtests across platforms using the same market and timeframe.
  • No comparative results are included, and implementation assumptions may affect agreement.

Tags

Full text
# Freqtrade_backtest_validation_freqtrade1.py


```py
# Freqtrade_backtest_validation_freqtrade1.py
# This script is 1 of a pair the other being freqtrade_backtest_validation_tradingview1
# These should be executed on their respective platforms for the same coin/period/resolution
# The purpose is to test Freqtrade backtest provides like results to a known industry platform.
#
# --- Do not remove these libs ---
from freqtrade.strategy import IStrategy
from pandas import DataFrame
# --------------------------------

# Add your lib to import here
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib


class Freqtrade_backtest_validation_freqtrade1(IStrategy):
    INTERFACE_VERSION: int = 3
    # Minimal ROI designed for the strategy.
    minimal_roi = {
        "40": 2.0,
        "30": 2.01,
        "20": 2.02,
        "0": 2.04
    }

    stoploss = -0.90
    timeframe = '1h'

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # SMA - Simple Moving Average
        dataframe['fastMA'] = ta.SMA(dataframe, timeperiod=14)
        dataframe['slowMA'] = ta.SMA(dataframe, timeperiod=28)
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                (dataframe['fastMA'] > dataframe['slowMA'])
            ),
            'enter_long'] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                (dataframe['fastMA'] < dataframe['slowMA'])
            ),
            'exit_long'] = 1
        return dataframe

```

Shown in full with attribution under the source's licence. Licence: GPL-3.0

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