# joelvsam/ai-powered-energy-trading architecture

> Architecture diagram of the GitHub repository joelvsam/ai-powered-energy-trading, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/joelvsam/ai-powered-energy-trading
- Repository: https://github.com/joelvsam/ai-powered-energy-trading

## Overview

#### Purpose and Entry Points
This is a Python research system for European power-price forecasting and trading-strategy validation. Its primary orchestrator is `scripts/run_all.py`; focused CLI paths are `scripts/run_backtest.py` and `scripts/run_model_comparison.py`. A separate Streamlit review interface starts at `dashboard/app.py`.

#### Configuration, Data Acquisition, and Raw Cache
`src/config.py` centralizes settings from Streamlit secrets or local `.streamlit/secrets.toml`. The acquisition boundary is `src/data_sources/entsoe_client.py`, which calls ENTSO-E for market context; the pipeline also incorporates weather context. `src/data_pipeline/cache.py` maintains validated, atomic, zone-specific raw parquet caches, while `src/data_pipeline/ingest.py` and `src/data_pipeline/clean.py` fetch missing intervals, validate schemas/provenance, and explicitly label synthetic gap repair.

#### Pipeline Assembly and Feature Boundary
`src/data_pipeline/run_pipeline.py` composes ingestion, cleaning, and dataset preparation; `src/data_pipeline/merge.py` joins market and weather series. `src/features/build_features.py` transforms only raw context into causal market-structure features such as price spreads, renewable/load proxies, volatility, regime, and seasonal variables. The cache intentionally ends before this boundary: engineered features and model-ready data are regenerated rather than persisted as canonical history.

#### Forecasting and Validation
The model subsystem has a common abstraction in `src/models/base.py` and model dispatch in `src/models/model_registry.py`. Concrete forecasting paths are `src/models/train_xgb.py`, `src/models/train_lstm.py`, and `src/models/train_prophet.py`. `src/models/walk_forward.py` produces chronological training/scoring splits; `src/models/feature_selection.py` and `src/models/tuning.py` constrain pruning and tuning to the initial training window, preventing test-period leakage. Diagnostics are emitted through `src/models/diagnostics.py`.

#### Signal Construction, Execution Simulation, and Backtests
`src/trading/signal.py` converts forecast, mean-reversion, and fundamental inputs into regime-aware, capped target positions. `src/trading/backtest.py` applies realistic execution behavior, including next-step fills, spread, slippage, delays, liquidity, and position/trade limits. The broader evaluation engine in `src/backtesting/engine.py` computes results and analytics; `src/backtesting/statistics.py` supplies bootstrap confidence intervals and pairwise significance tests. `src/backtesting/strategy_comparison.py` compares the selected strategy with persistence, seasonal, mean-reversion, and zero-signal baselines.

#### Research Orchestration and Optional AI Review
`scripts/run_all.py` coordinates pipeline output through anomaly review, training, backtesting, baseline comparison, cross-model comparison, and research-note generation. `src/agents/anomaly_agent.py` and `src/agents/research_agent.py` provide optional LLM-assisted interpretation, with request/fallback handling in `src/agents/llm_utils.py`; these are advisory layers, not decision engines. `src/experiment_tracking.py` writes redacted run manifests and an append-only index, connecting configurations, provenance, revisions, and headline metrics.

#### Outputs, Runtime, and External Dependencies
The runtime is Python 3.11+ with Streamlit for the dashboard. External services include ENTSO-E, weather retrieval, and optionally HuggingFace-hosted LLMs. Raw cache data resides under `cache/`; research, model, and execution artifacts are written under `artifacts/`. `dashboard/backtesting_review.py` and `dashboard/charts.py` adapt generated artifacts into the dashboard rather than owning the research pipeline.

Main entry points: [`scripts/run_all.py`](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/scripts/run_all.py) (Research orchestrator), [`src/data_pipeline/ingest.py`](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/ingest.py) (Ingest and clean), [`src/backtesting/engine.py`](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/backtesting/engine.py) (Evaluation and statistics), [`src/features/build_features.py`](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/features/build_features.py) (Causal feature builder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data pipeline"]
  node_config["Settings and secrets<br/>configuration boundary<br/>[config.py]"]
  node_entsoe(("ENTSO-E market API<br/>external service"))
  node_weather(("Weather retrieval<br/>external service"))
  node_ingest_clean["Ingest and clean<br/>data processing<br/>[ingest.py]"]
  node_raw_cache[("Zone raw parquet cache<br/>raw data store<br/>[cache.py]")]
  node_pipeline["Pipeline assembly<br/>pipeline orchestration<br/>[run_pipeline.py]"]
  node_merge["Market-weather merge<br/>dataset preparation<br/>[merge.py]"]
  node_features["Causal feature builder<br/>feature boundary<br/>[build_features.py]"]
end

subgraph group_models["Forecasting"]
  node_validation["Walk-forward validation<br/>chronological splitter<br/>[walk_forward.py]"]
  node_model_registry["Model registry<br/>model dispatch<br/>[model_registry.py]"]
  node_forecast_models["Forecast models<br/>XGBoost, LSTM, Prophet<br/>[train_xgb.py]"]
  node_diagnostics["Model diagnostics<br/>forecast evaluation<br/>[diagnostics.py]"]
end

subgraph group_trading["Trading evaluation"]
  node_signal["Regime-aware signals<br/>position construction<br/>[signal.py]"]
  node_execution["Execution simulator<br/>trading backtest<br/>[backtest.py]"]
  node_evaluation["Evaluation and statistics<br/>backtest analytics<br/>[engine.py]"]
  node_comparison["Strategy baselines<br/>strategy comparison"]
end

subgraph group_review["Research review"]
  node_run_all{{"Research orchestrator<br/>Python CLI<br/>[run_all.py]"}}
  node_focused_clis["Focused research CLIs<br/>Python CLI<br/>[run_backtest.py]"]
  node_dashboard["Streamlit review UI<br/>Streamlit app<br/>[app.py]"]
  node_ai_review["Optional AI review<br/>advisory agents<br/>[anomaly_agent.py]"]
  node_artifacts[("Research artifacts<br/>artifact store")]
end

node_run_all -->|"runs"| node_pipeline
node_run_all -->|"trains"| node_model_registry
node_run_all -->|"backtests"| node_evaluation
node_run_all -->|"compares"| node_comparison
node_run_all -.->|"requests review"| node_ai_review
node_focused_clis -->|"runs backtest"| node_evaluation
node_config -->|"configures"| node_ingest_clean
node_entsoe -->|"market context"| node_ingest_clean
node_weather -->|"weather context"| node_ingest_clean
node_ingest_clean -->|"validates and caches"| node_raw_cache
node_raw_cache -->|"raw history"| node_pipeline
node_pipeline -->|"prepares"| node_merge
node_merge -->|"joined context"| node_features
node_features -->|"feature data"| node_validation
node_validation -->|"train/score windows"| node_model_registry
node_model_registry -->|"dispatches"| node_forecast_models
node_forecast_models -->|"forecasts and metrics"| node_diagnostics
node_forecast_models -->|"forecasts"| node_signal
node_features -->|"fundamental inputs"| node_signal
node_signal -->|"target positions"| node_execution
node_execution -->|"trades and P&amp;L"| node_evaluation
node_evaluation -->|"selected results"| node_comparison
node_diagnostics -->|"writes"| node_artifacts
node_comparison -->|"writes results"| node_artifacts
node_ai_review -.->|"advisory notes"| node_artifacts
node_artifacts -->|"review data"| node_dashboard

click node_run_all "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/scripts/run_all.py"
click node_focused_clis "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/scripts/run_backtest.py"
click node_dashboard "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/dashboard/app.py"
click node_config "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/config.py"
click node_ingest_clean "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/ingest.py"
click node_raw_cache "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/cache.py"
click node_pipeline "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/run_pipeline.py"
click node_merge "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/merge.py"
click node_features "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/features/build_features.py"
click node_validation "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/models/walk_forward.py"
click node_model_registry "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/models/model_registry.py"
click node_forecast_models "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/models/train_xgb.py"
click node_diagnostics "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/models/diagnostics.py"
click node_signal "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/trading/signal.py"
click node_execution "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/trading/backtest.py"
click node_evaluation "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/backtesting/engine.py"
click node_comparison "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/backtesting/strategy_comparison.py"
click node_ai_review "https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/agents/anomaly_agent.py"

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_config,node_entsoe,node_weather,node_ingest_clean,node_raw_cache,node_pipeline,node_merge,node_features toneBlue
class node_validation,node_model_registry,node_forecast_models,node_diagnostics toneAmber
class node_signal,node_execution,node_evaluation,node_comparison toneMint
class node_run_all,node_focused_clis,node_dashboard,node_ai_review,node_artifacts toneRose
```

## Components

### Data pipeline

Acquisition, validated raw history, and causal preparation.

- [Settings and secrets](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/config.py): configuration boundary · `src/config.py`
- **ENTSO-E market API**: external service
- **Weather retrieval**: external service
- [Ingest and clean](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/ingest.py): data processing · Fetches missing intervals, validates provenance, and labels gap repair. · `src/data_pipeline/ingest.py`
- [Zone raw parquet cache](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/cache.py): raw data store · Validated atomic canonical raw history. · `src/data_pipeline/cache.py`
- [Pipeline assembly](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/run_pipeline.py): pipeline orchestration · `src/data_pipeline/run_pipeline.py`
- [Market-weather merge](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/data_pipeline/merge.py): dataset preparation · `src/data_pipeline/merge.py`
- [Causal feature builder](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/features/build_features.py): feature boundary · Regenerates model-ready market-structure features from raw context. · `src/features/build_features.py`

### Forecasting

Leakage-controlled model training and validation.

- [Walk-forward validation](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/models/walk_forward.py): chronological splitter · Creates chronological training and scoring windows. · `src/models/walk_forward.py`
- [Model registry](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/models/model_registry.py): model dispatch · Dispatches common forecast-model implementations. · `src/models/model_registry.py`
- [Forecast models](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/models/train_xgb.py): XGBoost, LSTM, Prophet · Trained with initial-window feature selection and tuning. · `src/models/train_xgb.py`
- [Model diagnostics](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/models/diagnostics.py): forecast evaluation · `src/models/diagnostics.py`

### Trading evaluation

Signals, execution simulation, and statistical comparison.

- [Regime-aware signals](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/trading/signal.py): position construction · Caps target positions from forecast, mean-reversion, and fundamentals. · `src/trading/signal.py`
- [Execution simulator](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/trading/backtest.py): trading backtest · Models next-step fills, costs, delays, liquidity, and limits. · `src/trading/backtest.py`
- [Evaluation and statistics](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/backtesting/engine.py): backtest analytics · Computes results, confidence intervals, and significance tests. · `src/backtesting/engine.py`
- [Strategy baselines](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/backtesting/strategy_comparison.py): strategy comparison · Compares selected strategy against persistence, seasonal, mean-reversion, and zero-signal baselines. · `src/backtesting/strategy_comparison.py`

### Research review

Orchestration, optional AI interpretation, and dashboard consumption.

- [Research orchestrator](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/scripts/run_all.py): Python CLI · Coordinates the end-to-end research workflow. · `scripts/run_all.py`
- [Focused research CLIs](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/scripts/run_backtest.py): Python CLI · `scripts/run_backtest.py`
- [Streamlit review UI](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/dashboard/app.py): Streamlit app · Reviews generated artifacts without owning the pipeline. · `dashboard/app.py`
- [Optional AI review](https://github.com/joelvsam/ai-powered-energy-trading/blob/main/src/agents/anomaly_agent.py): advisory agents · Anomaly and research interpretation with fallback handling. · `src/agents/anomaly_agent.py`
- **Research artifacts**: artifact store · Stores research, model, and execution outputs plus redacted run manifests.

## Connections

- Research orchestrator → Pipeline assembly: runs
- Research orchestrator → Model registry: trains
- Research orchestrator → Evaluation and statistics: backtests
- Research orchestrator → Strategy baselines: compares
- Research orchestrator → Optional AI review: requests review
- Focused research CLIs → Evaluation and statistics: runs backtest
- Settings and secrets → Ingest and clean: configures
- ENTSO-E market API → Ingest and clean: market context
- Weather retrieval → Ingest and clean: weather context
- Ingest and clean → Zone raw parquet cache: validates and caches
- Zone raw parquet cache → Pipeline assembly: raw history
- Pipeline assembly → Market-weather merge: prepares
- Market-weather merge → Causal feature builder: joined context
- Causal feature builder → Walk-forward validation: feature data
- Walk-forward validation → Model registry: train/score windows
- Model registry → Forecast models: dispatches
- Forecast models → Model diagnostics: forecasts and metrics
- Forecast models → Regime-aware signals: forecasts
- Causal feature builder → Regime-aware signals: fundamental inputs
- Regime-aware signals → Execution simulator: target positions
- Execution simulator → Evaluation and statistics: trades and P&L
- Evaluation and statistics → Strategy baselines: selected results
- Model diagnostics → Research artifacts: writes
- Strategy baselines → Research artifacts: writes results
- Optional AI review → Research artifacts: advisory notes
- Research artifacts → Streamlit review UI: review data
