How joelvsam/ai-powered-energy-trading works
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: run_all.py Research orchestrator · ingest.py Ingest and clean · engine.py Evaluation and statistics · build_features.py Causal feature builder
Components 21
Data pipeline
Acquisition, validated raw history, and causal preparation.
- Settings and secrets
src/config.py - ENTSO-E market API
- Weather retrieval
- Ingest and clean · Fetches missing intervals, validates provenance, and labels gap repair.
src/data_pipeline/ingest.py - Zone raw parquet cache · Validated atomic canonical raw history.
src/data_pipeline/cache.py - Pipeline assembly
src/data_pipeline/run_pipeline.py - Market-weather merge
src/data_pipeline/merge.py - Causal feature builder · Regenerates model-ready market-structure features from raw context.
src/features/build_features.py
Forecasting
Leakage-controlled model training and validation.
- Walk-forward validation · Creates chronological training and scoring windows.
src/models/walk_forward.py - Model registry · Dispatches common forecast-model implementations.
src/models/model_registry.py - Forecast models · Trained with initial-window feature selection and tuning.
src/models/train_xgb.py - Model diagnostics
src/models/diagnostics.py
Trading evaluation
Signals, execution simulation, and statistical comparison.
- Regime-aware signals · Caps target positions from forecast, mean-reversion, and fundamentals.
src/trading/signal.py - Execution simulator · Models next-step fills, costs, delays, liquidity, and limits.
src/trading/backtest.py - Evaluation and statistics · Computes results, confidence intervals, and significance tests.
src/backtesting/engine.py - Strategy baselines · 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 · Coordinates the end-to-end research workflow.
scripts/run_all.py - Focused research CLIs
scripts/run_backtest.py - Streamlit review UI · Reviews generated artifacts without owning the pipeline.
dashboard/app.py - Optional AI review · Anomaly and research interpretation with fallback handling.
src/agents/anomaly_agent.py - Research artifacts · Stores research, model, and execution outputs plus redacted run manifests.
Connections 26
- 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