freqAI-LSTM Trading Model

repository·main·Indexed 43 days ago

https://github.com/netanelshoshan/freqai-lstm

A regression-based trading model for the FreqAI module of Freqtrade. It utilizes PyTorch or TensorFlow/Keras-based LSTM (Long Short-Term Memory) networks with a dynamic weighting and aggregate scoring system to process technical indicators into market sentiment signals.

Tokens
3.9K
Snippets
6
Records
8
Agent score
6%

What's inside freqAI-LSTM

  1. Mitigate overfitting and noise in LSTM models

    main

    To ensure the LSTM model learns meaningful signals rather than market noise or overfitting to historical data, the following techniques are recommended:

    • Overfitting Mitigation: Use dropout layers, regularization, adjust the number of layers and neurons, and carefully tune the number of training epochs.
    • Noise Reduction: Implement thresholds and weights to filter out low-confidence signals, or employ dissimilarity measures to distinguish signal from noise.
  2. How the LSTM Trading Strategy works

    main

    The strategy uses a multi-step pipeline to generate a target score for the LSTM model to predict. The workflow is as follows:

    1. Indicator Calculation: Computes technical indicators to capture trends, momentum, and volatility.
    2. Normalization: Calculates the z-score for each indicator (subtracting rolling mean and dividing by rolling standard deviation) to make them comparable.
    3. Dynamic Weighting: Adjusts the importance of different indicators based on market conditions.
    4. Aggregate Scoring: Combines normalized indicators into a single market sentiment score.
    5. Regime & Volatility Filtering: Adjusts the target score based on the current market regime (bullish/bearish/neutral) and volatility levels.
    6. Signal Generation: Uses the predicted target score against defined thresholds to trigger entry and exit signals.
  3. Quick Start - TensorFlow Integration

    main

    To use TensorFlow/Keras with this project, follow these steps to ensure the model saving and loading logic is correctly integrated into the Freqtrade/FreqAI framework.

    1. Install Dependencies: Ensure tensorflow is installed in your environment.
    2. Configure Model Type: In your config.json, set "model_save_type": "keras".
    3. Manual Code Integration: You must manually patch the following files to support the .keras format:
    • freqtrade/freqai/data_drawer.py:
      • In save_data(): Add logic to save using model.save(save_path / f"{dk.model_filename}_model.keras") when self.model_type == 'keras'.
      • In load_data(): Add logic to load using from tensorflow.keras.models import load_model and model = load_model(dk.data_path / f"{dk.model_filename}_model.keras") when self.model_type == 'keras'.
    • freqtrade/freqai/freqai_interface.py:
      • In model_exists(): Add elif self.dd.model_type == "keras": file_type = ".keras".
    • freqtrade/configuration/config_validation.py:
      • In _validate_freqai_include_timeframes(), ensure the main timeframe is set appropriately (e.g., change conf.get('timeframe', '5m') to '1h' or your preferred minimum timeframe).
    1. Download Data: Use the freqtrade download-data command to fetch historical OHLCV data in json format.
    2. Run Backtesting: Execute the backtesting command using your configuration file.
    # 1. Download data (example for 4h and 1h timeframes)
    freqtrade download-data --exchange binance --days 450 -t 4h -p ETH/USDT:USDT ETC/USDT:USDT SOL/USDT:USDT OCEAN/USDT:USDT BTC/USDT:USDT --data-format-ohlcv json --erase --trading-mode futures
    
    freqtrade download-data --exchange binance --days 450 -t 1h -p ETH/USDT:USDT ETC/USDT:USDT SOL/USDT:USDT OCEAN/USDT:USDT BTC/USDT:USDT --data-format-ohlcv json --erase --trading-mode futures
    
    # 2. Run backtesting
    freqtrade backtesting -c config-example.json --breakdown day week month --timerange 20240301-20240401
  4. Implement the multi-factor target score strategy

    main

    The strategy generates a target signal for the LSTM model by combining technical indicators into a single score. The process follows these steps:

    1. Calculate Indicators: Use ta (Technical Analysis library) to compute indicators like SMA, MACD, RSI, Bollinger Bands, etc.
    2. Normalize Indicators: Convert indicators into z-scores (subtracting rolling mean and dividing by rolling standard deviation) so they are comparable and weightable.
    3. Apply Dynamic Weighting: Adjust weights based on market conditions (e.g., increasing momentum weight during strong trends).
    4. Calculate Aggregate Score (S): A weighted sum of all normalized indicators.
    5. Apply Market Regime Filter (R): Use Bollinger Bands or Moving Averages to determine if the market is trending or ranging (assigning values like 1, -1, or 0).
    6. Apply Volatility Adjustment (V): Scale the score based on volatility (e.g., using inverse Bollinger Band width or inverse ATR).
    7. Compute Final Target (T): Multiply the aggregate score by the regime filters and volatility adjustments.

    The final value is assigned to the &-target column for the AI model to learn.

    # Step 0: Calculate new indicators
    dataframe['ma'] = ta.SMA(dataframe, timeperiod=10)
    dataframe['roc'] = ta.ROC(dataframe, timeperiod=2)
    dataframe['macd'], dataframe['macdsignal'], dataframe['macdhist'] = ta.MACD(dataframe['close'], slowperiod=12, fastperiod=26)
    dataframe['momentum'] = ta.MOM(dataframe, timeperiod=4)
    dataframe['rsi'] = ta.RSI(dataframe, timeperiod=10)
    bollinger = ta.BBANDS(dataframe, timeperiod=20)
    dataframe['bb_upperband'] = bollinger['upperband']
    dataframe['bb_middleband'] = bollinger['middleband']
    dataframe['bb_lowerband'] = bollinger['lowerband']
    dataframe['cci'] = ta.CCI(dataframe, timeperiod=20)
    dataframe['stoch'] = ta.STOCH(dataframe)['slowk']
    dataframe['atr'] = ta.ATR(dataframe, timeperiod=14)
    dataframe['obv'] = ta.OBV(dataframe)
    
    # Step 1: Normalize Indicators (Z-Score)
    dataframe['normalized_stoch'] = (dataframe['stoch'] - dataframe['stoch'].rolling(window=14).mean()) / dataframe['stoch'].rolling(window=14).std()
    # ... (other normalizations) ...
    
    # Step 2: Calculate aggregate score S
    w = [self.w0.value, self.w1.value, self.w2.value, self.w3.value, self.w4.value, self.w5.value, self.w6.value, self.w7.value, self.w8.value]
    dataframe['S'] = w[0] * dataframe['normalized_ma'] + ... + dataframe['w_momentum'] * dataframe['normalized_momentum']
    
    # Step 3: Market Regime Filter R
    dataframe['R'] = 0
    dataframe.loc[(dataframe['close'] > dataframe['bb_middleband']) & (dataframe['close'] > dataframe['bb_upperband']), 'R'] = 1
    dataframe.loc[(dataframe['close'] < dataframe['bb_middleband']) & (dataframe['close'] < dataframe['bb_lowerband']), 'R'] = -1
    
    # Step 4: Volatility Adjustment V
    bb_width = (dataframe['bb_upperband'] - dataframe['bb_lowerband']) / dataframe['bb_middleband']
    dataframe['V'] = 1 / bb_width
    
    # Final Target
    dataframe['T'] = dataframe['S'] * dataframe['R'] * dataframe['V'] * dataframe['R2'] * dataframe['V2']
    dataframe['&-target'] = dataframe['T']
  5. Quick Start with Docker

    main

    You can run the model using Docker by building a local image from the provided Dockerfile. This approach handles the environment setup automatically.

    1. Build the image: Use the Dockerfile located in the torch/ directory.
    2. Run commands: Use docker run with a volume mount for your ./data directory to execute download-data and backtesting.
    # Build
    git clone https://github.com/Netanelshoshan/freqAI-LSTM.git
    cd freqAI-LSTM
    docker build -f torch/Dockerfile -t freqai .
    
    # Download data
    docker run -v ./data:/freqtrade/user_data/data -it freqai download-data -c user_data/config-torch.json --timerange 20230101-20240529 --timeframe 15m 30m 1h 2h 4h 8h 1d --erase
    
    # Run backtest
    docker run -v ./data:/freqtrade/user_data/data -it freqai backtesting -c user_data/config-torch.json --breakdown day week month --timerange 20240301-20240401
  6. Install and Setup freqAI-LSTM

    main

    To use this LSTM model with Freqtrade, you must clone the repository and manually copy the PyTorch-based model files into your existing freqtrade installation directory. You also need to modify the Freqtrade core validation logic to support timeframes larger than 5 minutes.

    Steps:

    1. Clone the repository
    2. Copy model files to the appropriate freqtrade subdirectories (see code for exact paths).
    3. Download data using the provided configuration.
    4. Modify Freqtrade core: Edit freqtrade/configuration/config_validation.py to change the default main_tf from '5m' to your desired minimum timeframe (e.g., '1h').
    5. Reinstall package: Run pip install -e . to ensure changes are recognized.
    # 1. Clone
    git clone https://github.com/Netanelshoshan/freqAI-LSTM.git
    
    # 2. Copy files (Replace <freqtrade dir> with your actual path)
    cp torch/BasePyTorchModel.py <freqtrade dir>/freqtrade/freqai/base_models/
    cp torch/PyTorchLSTMModel.py <freqtrade dir>/freqtrade/freqai/torch/
    cp torch/PyTorchModelTrainer.py <freqtrade dir>/freqtrade/freqai/torch/
    cp torch/PyTorchLSTMRegressor.py <freqtrade dir>/user_data/freqaimodels/
    cp config-example.json <freqtrade dir>/user_data/config.json
    cp ExampleLSTMStrategy.py <freqtrade dir>/user_data/strategies/
    
    # 3. Download data
    freqtrade download-data -c user_data/config-torch.json --timerange 20230101-20240529 --timeframe 15m 30m 1h 2h 4h 8h 1d --erase
    
    # 4. Edit freqtrade/configuration/config_validation.py to change main_tf to '1h' or higher
    
    # 5. Install editable
    pip install -e .
    
    # 6. Run backtest
    freqtrade backtesting -c user_data/config-torch.json --breakdown day week month --timerange 20240301-20240401
  7. Configure PyTorch LSTM Model Hyperparameters

    main

    The PyTorchLSTMModel architecture is configured via the model_training_parameters section in your Freqtrade configuration file. You can tune the LSTM layers, dimensionality, dropout, and training process using model_kwargs and trainer_kwargs.

    Configuration Keys:

    model_training_parameters

    • learning_rate: Step size for the optimizer.
    • trainer_kwargs: Arguments passed to PyTorchLSTMTrainer.
      • n_steps: Number of training iterations (if null, uses n_epochs).
      • batch_size: Samples per gradient update.
      • n_epochs: Total iterations over the dataset.
    • model_kwargs: Arguments passed to PyTorchLSTMModel.
      • num_lstm_layers: Number of LSTM layers.
      • hidden_dim: Dimensionality of the output space in each LSTM layer.
      • dropout_percent: Dropout rate for regularization.
      • window_size: Number of time steps to look back for predictions.
    "model_training_parameters": {
        "learning_rate": 3e-3,
        "trainer_kwargs": {
            "n_steps": null,
            "batch_size": 32,
            "n_epochs": 10
        },
        "model_kwargs": {
            "num_lstm_layers": 3,
            "hidden_dim": 128,
            "dropout_percent": 0.4,
            "window_size": 5
        }
    }
  8. Configure TensorFlow/Keras Model Parameters

    main

    You can tune the LSTM model architecture and training process via the config.json file. The parameters are nested under the freqai key.

    Key parameters include:

    • conv_width: Represents the number of timesteps.
    • model_training_parameters object:
      • num_lstm_layers: Number of LSTM layers in the network.
      • epochs: Number of training iterations.
      • batch_size: Number of samples per gradient update.
      • learning_rate: Step size for the optimizer.
      • dropout_rate: Fraction of neurons to drop to prevent overfitting.
    {
      "freqai": {
        "keras": true,
        "model_save_type": "keras",
        "conv_width": 24,
        "model_training_parameters": {
          "num_lstm_layers": 3,
          "epochs": 100,
          "batch_size": 32,
          "learning_rate": 0.001,
          "dropout_rate": 0.3
        }
      }
    }