# libao3128/neat_trader architecture

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

- Interactive diagram: https://gitdiagram.com/libao3128/neat_trader
- Repository: https://github.com/libao3128/neat_trader

## Overview

#### Purpose and Entry Points
This is a Python research/backtesting package that evolves neural-network stock-trading strategies with NEAT-Python. Primary API entry points are package exports in `neat_trader/__init__.py`, example usage in `neat_trader/example.py`, and the orchestration class in `neat_trader/algorithm/neat_trader.py`. A notebook, `neat_trading_strategy_development.ipynb`, serves as an exploratory workflow.

#### Evolution Orchestrator
`NeatTrader` coordinates a NEAT population run: it receives a NEAT configuration and an evaluator, runs generations (optionally multiprocess), preserves the winning genome, and generates reports/checkpoints/visual outputs as supported by the package.
- `neat_trader/algorithm/neat_trader.py`
- `model/config-feedforward`
- `model/config-test`

#### Data Acquisition and Feature Boundary
`DataHandler` is the market-data boundary. It loads historical stock data from SQLite, selects symbol or random training windows, and prepares technical-indicator inputs for strategy evaluation. The repository also includes a downloader script and an S&P 500 constituent submodule/dataset.
- `neat_trader/utils/data_handler.py`
- `download_sp500_data.py`
- `s-and-p-500-companies`

#### Genome-to-Strategy Execution
A genome is converted into a feed-forward neural trading policy. The policy consumes 11 indicator/state inputs (including SMA, stochastic, MACD, CCI, RSI, ADOSC, and current positions) and emits buy, sell, and volume signals. Strategy logic applies those signals to backtesting/risk and position behavior.
- `neat_trader/algorithm/strategy.py`
- `neat_trader/config.py`
- `model/config-feedforward`

#### Evaluation and Fitness Pipeline
The evaluator is the central per-genome pipeline: obtain a data period, execute the neural strategy through the backtesting layer, collect performance metrics, then assign genome fitness. Fitness functions support return, benchmark outperformance, Sharpe ratio, and drawdown-oriented multi-objective scoring.
- `neat_trader/algorithm/evaluate.py`
- `neat_trader/algorithm/fitness_fn.py`
- `neat_trader/algorithm/strategy.py`

#### Configuration and Failure Boundaries
NEAT topology/mutation/population settings live in the NEAT config files; application-level trading defaults such as cash, commissions, periods, risk thresholds, and node names are centralized separately. Custom exceptions distinguish data, evaluation, visualization, and checkpoint failures.
- `neat_trader/config.py`
- `model/config-feedforward`
- `neat_trader/exceptions.py`

#### Reporting and Runtime Dependencies
The runtime is Python 3.7+ and integrates NEAT-Python for evolution, TA-Lib for indicators, a backtesting framework for simulated execution, SQLite for stock history, and Pandas/NumPy for processing. Visualization uses Matplotlib and Graphviz to render training statistics, species trends, and winner topology.
- `neat_trader/utils/visualize.py`
- `requirements.txt`
- `report/SmaCross.html`

Main entry points: [`neat_trader/algorithm/neat_trader.py`](https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/neat_trader.py) (NeatTrader), [`neat_trader/algorithm/evaluate.py`](https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/evaluate.py) (Genome evaluator), [`neat_trader/utils/visualize.py`](https://github.com/libao3128/neat_trader/blob/master/neat_trader/utils/visualize.py) (Training visualizations), [`neat_trader/algorithm/strategy.py`](https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/strategy.py) (Neural strategy)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry Points"]
  node_package_exports["Package API<br/>Python package<br/>[__init__.py]"]
  node_example["Example workflow<br/>Python example<br/>[example.py]"]
  node_notebook["Research notebook"]
end

subgraph group_evolution["Evolution"]
  node_neat_trader["NeatTrader<br/>evolution orchestrator<br/>[neat_trader.py]"]
  node_neat_config["NEAT configuration"]
end

subgraph group_evaluation["Strategy Evaluation"]
  node_evaluator["Genome evaluator<br/>evaluation pipeline<br/>[evaluate.py]"]
  node_strategy["Neural strategy<br/>trading policy<br/>[strategy.py]"]
  node_fitness["Fitness functions<br/>scoring<br/>[fitness_fn.py]"]
  node_app_config["Trading defaults<br/>application config<br/>[config.py]"]
end

subgraph group_data["Market Data"]
  node_data_handler["DataHandler<br/>data boundary<br/>[data_handler.py]"]
  node_sqlite_history[("Historical stock data<br/>SQLite data")]
  node_data_downloader["S&amp;P 500 downloader<br/>ingestion script"]
  node_sp500_source[("S&amp;P 500 constituents<br/>dataset submodule")]
end

subgraph group_reporting["Reporting"]
  node_visualize["Training visualizations<br/>report renderer<br/>[visualize.py]"]
  node_report["Backtest report<br/>HTML report<br/>[SmaCross.html]"]
end

node_exceptions["Custom exceptions<br/>failure boundary<br/>[exceptions.py]"]
node_dependencies["Runtime dependencies<br/>Python dependencies<br/>[requirements.txt]"]

node_package_exports -->|"exports"| node_neat_trader
node_example -->|"runs"| node_neat_trader
node_notebook -->|"explores"| node_neat_trader
node_neat_trader -->|"loads"| node_neat_config
node_neat_trader -->|"evaluates genomes"| node_evaluator
node_neat_trader -->|"generates outputs"| node_visualize
node_evaluator -->|"gets data period"| node_data_handler
node_evaluator -->|"executes genome"| node_strategy
node_evaluator -->|"assigns metrics"| node_fitness
node_strategy -->|"uses defaults"| node_app_config
node_data_handler -->|"loads history"| node_sqlite_history
node_data_handler -->|"provides indicators"| node_strategy
node_data_downloader -->|"uses constituents"| node_sp500_source
node_data_downloader -->|"stores history"| node_sqlite_history
node_neat_trader -.->|"raises"| node_exceptions
node_evaluator -.->|"raises"| node_exceptions
node_visualize -.->|"raises"| node_exceptions
node_visualize -->|"produces reports"| node_report
node_dependencies -.->|"supports runtime"| node_neat_trader
node_dependencies -.->|"provides NEAT, TA-Lib, backtesting"| node_strategy
node_dependencies -.->|"provides plotting"| node_visualize

click node_package_exports "https://github.com/libao3128/neat_trader/blob/master/neat_trader/__init__.py"
click node_example "https://github.com/libao3128/neat_trader/blob/master/neat_trader/example.py"
click node_notebook "https://github.com/libao3128/neat_trader/blob/master/neat_trading_strategy_development.ipynb"
click node_neat_trader "https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/neat_trader.py"
click node_neat_config "https://github.com/libao3128/neat_trader/blob/master/model/config-feedforward"
click node_evaluator "https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/evaluate.py"
click node_strategy "https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/strategy.py"
click node_fitness "https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/fitness_fn.py"
click node_app_config "https://github.com/libao3128/neat_trader/blob/master/neat_trader/config.py"
click node_data_handler "https://github.com/libao3128/neat_trader/blob/master/neat_trader/utils/data_handler.py"
click node_data_downloader "https://github.com/libao3128/neat_trader/blob/master/download_sp500_data.py"
click node_sp500_source "https://github.com/libao3128/neat_trader/tree/master/s-and-p-500-companies"
click node_visualize "https://github.com/libao3128/neat_trader/blob/master/neat_trader/utils/visualize.py"
click node_report "https://github.com/libao3128/neat_trader/blob/master/report/SmaCross.html"
click node_exceptions "https://github.com/libao3128/neat_trader/blob/master/neat_trader/exceptions.py"
click node_dependencies "https://github.com/libao3128/neat_trader/blob/master/requirements.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_package_exports,node_example,node_notebook toneBlue
class node_neat_trader,node_neat_config toneAmber
class node_evaluator,node_strategy,node_fitness,node_app_config toneMint
class node_data_handler,node_sqlite_history,node_data_downloader,node_sp500_source toneRose
class node_visualize,node_report toneIndigo
class node_exceptions,node_dependencies toneNeutral
```

## Components

### Entry Points

- [Package API](https://github.com/libao3128/neat_trader/blob/master/neat_trader/__init__.py): Python package · Public package exports. · `neat_trader/__init__.py`
- [Example workflow](https://github.com/libao3128/neat_trader/blob/master/neat_trader/example.py): Python example · `neat_trader/example.py`
- [Research notebook](https://github.com/libao3128/neat_trader/blob/master/neat_trading_strategy_development.ipynb): Notebook · Exploratory strategy-development workflow. · `neat_trading_strategy_development.ipynb`

### Evolution

- [NeatTrader](https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/neat_trader.py): evolution orchestrator · Runs populations and retains the winning genome. · `neat_trader/algorithm/neat_trader.py`
- [NEAT configuration](https://github.com/libao3128/neat_trader/blob/master/model/config-feedforward): NEAT config · Topology, mutation, and population settings. · `model/config-feedforward`

### Strategy Evaluation

- [Genome evaluator](https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/evaluate.py): evaluation pipeline · Executes each genome and assigns fitness. · `neat_trader/algorithm/evaluate.py`
- [Neural strategy](https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/strategy.py): trading policy · Maps indicator and position inputs to buy, sell, and volume signals. · `neat_trader/algorithm/strategy.py`
- [Fitness functions](https://github.com/libao3128/neat_trader/blob/master/neat_trader/algorithm/fitness_fn.py): scoring · Scores return, benchmark performance, Sharpe ratio, and drawdown. · `neat_trader/algorithm/fitness_fn.py`
- [Trading defaults](https://github.com/libao3128/neat_trader/blob/master/neat_trader/config.py): application config · Cash, commissions, periods, risk thresholds, and node names. · `neat_trader/config.py`

### Market Data

- [DataHandler](https://github.com/libao3128/neat_trader/blob/master/neat_trader/utils/data_handler.py): data boundary · Selects periods and prepares market inputs. · `neat_trader/utils/data_handler.py`
- **Historical stock data**: SQLite data
- [S&P 500 downloader](https://github.com/libao3128/neat_trader/blob/master/download_sp500_data.py): ingestion script · `download_sp500_data.py`
- [S&P 500 constituents](https://github.com/libao3128/neat_trader/tree/master/s-and-p-500-companies): dataset submodule · `s-and-p-500-companies`

### Reporting

- [Training visualizations](https://github.com/libao3128/neat_trader/blob/master/neat_trader/utils/visualize.py): report renderer · Renders statistics, species trends, and winner topology. · `neat_trader/utils/visualize.py`
- [Backtest report](https://github.com/libao3128/neat_trader/blob/master/report/SmaCross.html): HTML report · `report/SmaCross.html`

### Other components

- [Custom exceptions](https://github.com/libao3128/neat_trader/blob/master/neat_trader/exceptions.py): failure boundary · Defines data, evaluation, visualization, and checkpoint failures. · `neat_trader/exceptions.py`
- [Runtime dependencies](https://github.com/libao3128/neat_trader/blob/master/requirements.txt): Python dependencies · NEAT-Python, TA-Lib, backtesting, SQLite, Pandas, NumPy, Matplotlib, and Graphviz. · `requirements.txt`

## Connections

- Package API → NeatTrader: exports
- Example workflow → NeatTrader: runs
- Research notebook → NeatTrader: explores
- NeatTrader → NEAT configuration: loads
- NeatTrader → Genome evaluator: evaluates genomes
- NeatTrader → Training visualizations: generates outputs
- Genome evaluator → DataHandler: gets data period
- Genome evaluator → Neural strategy: executes genome
- Genome evaluator → Fitness functions: assigns metrics
- Neural strategy → Trading defaults: uses defaults
- DataHandler → Historical stock data: loads history
- DataHandler → Neural strategy: provides indicators
- S&P 500 downloader → S&P 500 constituents: uses constituents
- S&P 500 downloader → Historical stock data: stores history
- NeatTrader → Custom exceptions: raises
- Genome evaluator → Custom exceptions: raises
- Training visualizations → Custom exceptions: raises
- Training visualizations → Backtest report: produces reports
- Runtime dependencies → NeatTrader: supports runtime
- Runtime dependencies → Neural strategy: provides NEAT, TA-Lib, backtesting
- Runtime dependencies → Training visualizations: provides plotting
