# netanelshoshan/freqai-lstm architecture

> Architecture diagram of the GitHub repository netanelshoshan/freqai-lstm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/netanelshoshan/freqai-lstm
- Repository: https://github.com/netanelshoshan/freqai-lstm

## Overview

##### Main Components
- Trading Strategy Component – Represented by files like ExampleLSTMStrategy.py. This module is responsible for calculating technical indicators, normalizing them, applying dynamic weights, and generating the final target score for determining entry/exit signals.
- Model Implementations – Two primary implementations, one under the “tensorflow” directory (e.g., LSTMRegressor.py) and one under the “torch” directory. Since the PyTorch version is the updated focus, include these files: BasePyTorchModel.py, PyTorchLSTMModel.py, PyTorchLSTMRegressor.py, and PyTorchModelTrainer.py.
- Configuration and Integration – The config-example.json and instructions in the README show how to integrate the model with the freqtrade framework. Also, note that files need to be copied into specific freqtrade directories.

##### Detail the Relationships and Interactions
- Data Flow: Start by illustrating how historical market data is downloaded (using freqtrade commands) and passed into the strategy component for processing.
- Strategy Processing: Show that the ExampleLSTMStrategy processes input data by computing indicators (SMA, MACD, RSI, etc.), normalizing them, and applying dynamic weighting to generate an aggregate “target score.”
- Model Training and Prediction: Next, detail how that target score becomes part of the input for the LSTM network. The PyTorchLSTMModel takes these inputs through multiple layers (LSTM layers with Batch Normalization, Dropout, fully connected layers, etc.), and the PyTorchModelTrainer coordinates model training (with configurable hyperparameters like learning rate, batch size, n_epochs).
- Integration with Freqtrade: The trained model then feeds back into the freqtrade bot for live or backtesting trading decisions.
- Containerization: Use Docker (as per torch/Dockerfile) to indicate that the PyTorch model can run in a containerized environment, ensuring that dependencies and runtime environments are isolated.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The project separates the logic into distinct modules (strategy definition, model definition, training, and deployment). Emphasize the decoupling between the strategy logic (which computes indicators and scores) and the AI model’s logic (which is responsible for learning and prediction).
- Configuration-Driven Design: The model’s hyperparameters are easily configurable (via JSON configuration files) which ensures flexibility and adaptability across different market conditions.
- Containerization and Deployment: The presence of a Dockerfile highlights a modern approach to deployment, ensuring consistency across development and production environments.

##### Incorporate Relevant Technologies
- PyTorch and (briefly) TensorFlow for the model implementation.
- Docker for containerized deployment.
- Technical analysis libraries (implied by the use of TA functions) which feed into the strategy.
- Freqtrade (from GitHub/freqtrade and FreqAI module) as the overarching trading bot framework.
- Standard configuration files (JSON) and version control (.gitignore, LICENSE).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Data Ingestion & Strategy Layer
    subgraph "Data Ingestion & Strategy"
        MD("Market Data"):::data
        STR("Technical Indicators & Strategy\n(ExampleLSTMStrategy)"):::strategy
    end

    MD -->|"downloaded_data"| STR

    %% Model Implementation Layer
    subgraph "Model Implementation"
        TFL("TensorFlow LSTMRegressor"):::model_tf
        subgraph "PyTorch Implementation"
            BTM("BasePyTorchModel"):::model_pt
            PLM("PyTorchLSTMModel"):::model_pt
            PLR("PyTorchLSTMRegressor"):::model_pt
            PTM("PyTorchModelTrainer"):::model_pt
        end
    end

    %% Data flow from Strategy to Models
    STR -->|"generates_target_score"| TFL
    STR -->|"generates_target_score"| PLM

    %% PyTorch internal flow
    BTM -->|"feeds_into"| PLM
    PLM -->|"computes_regression"| PLR
    PLR -->|"trains_model"| PTM

    %% Predictions flow to Freqtrade Integration
    TFL -->|"predictions"| FREQ
    PTM -->|"predictions"| FREQ

    %% Integration & Deployment Layer
    subgraph "Integration & Deployment"
        FREQ("Freqtrade Integration"):::integration
        CONFIG("Configuration\n(config-example.json)"):::config
    end

    CONFIG -->|"configures"| STR
    CONFIG -->|"configures"| PTM

    %% Docker Container for PyTorch
    DOCK("Docker Container"):::container
    DOCK -->|"containerizes"| PTM

    %% Styles
    classDef data fill:#e8f4fd,stroke:#333,stroke-width:2px;
    classDef strategy fill:#fde2e4,stroke:#333,stroke-width:2px;
    classDef model_tf fill:#e3f9e5,stroke:#333,stroke-width:2px;
    classDef model_pt fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef integration fill:#d0e1f9,stroke:#333,stroke-width:2px;
    classDef config fill:#fce5cd,stroke:#333,stroke-width:2px;
    classDef container fill:#d9d2e9,stroke:#333,stroke-width:2px;

    %% Click Events
    click STR "https://github.com/netanelshoshan/freqai-lstm/blob/main/ExampleLSTMStrategy.py"
    click TFL "https://github.com/netanelshoshan/freqai-lstm/blob/main/tensorflow/LSTMRegressor.py"
    click BTM "https://github.com/netanelshoshan/freqai-lstm/blob/main/torch/BasePyTorchModel.py"
    click PLM "https://github.com/netanelshoshan/freqai-lstm/blob/main/torch/PyTorchLSTMModel.py"
    click PLR "https://github.com/netanelshoshan/freqai-lstm/blob/main/torch/PyTorchLSTMRegressor.py"
    click PTM "https://github.com/netanelshoshan/freqai-lstm/blob/main/torch/PyTorchModelTrainer.py"
    click CONFIG "https://github.com/netanelshoshan/freqai-lstm/blob/main/config-example.json"
    click DOCK "https://github.com/netanelshoshan/freqai-lstm/tree/main/torch/Dockerfile"
```
