How talha4565/metalmind-smcforge works
1. System shape
This is a three-plane trading platform: a Next.js dashboard, a Flask/SocketIO API, and a Python ML/data pipeline. The dashboard is the presentation layer, the API is the runtime control plane, and the pipeline owns ingestion, feature engineering, training, backtesting, and scheduled refreshes.
2. Frontend boundary
frontend-next/ is a Next.js app-router frontend with separate areas for auth, dashboard, backtest, risk, and watchlist. It uses reusable UI components plus domain components for TradingView charts, SHAP explanations, signal cards, and risk metrics. Client-side hooks in src/lib/hooks mediate all data access to the backend and real-time updates.
3. API boundary
api/app/ is the main backend surface. It exposes endpoints for health, predictions, market price, SHAP feature importance, backtesting, auth, and watchlist/profile operations. The API also contains model loading, prediction caching, ETL-related routes, and error handling middleware, so it acts as the integration point between UI, models, and stored data.
4. Real-time and session flow
The frontend consumes the API over HTTP and also has a WebSocket path via useWebSocket and Flask-SocketIO. That makes the app both a request/response dashboard and a live terminal for price/prediction updates. Authentication is handled through the frontend auth pages plus backend auth services, with NextAuth wiring on the client side.
5. Data ingestion and refresh
The ETL subsystem under etl/ pulls market data from Yahoo Finance and CSV sources using extractor abstractions. Data then moves through transformers for quality checks and feature generation, and into loaders that append to CSV, load to DB, or populate a feature store. The scheduler coordinates periodic refreshes and alerting.
6. Feature engineering pipeline
features/ is where domain-specific feature construction happens. It includes Smart Money Concepts features, volume features, multi-timeframe aggregation, and label generation. This is the core modeling boundary: raw OHLCV data becomes model-ready sequences with the 90-feature schema referenced in the README.
7. Model training and retraining
models/ contains training and retraining scripts for the XGBoost classifiers, with separate support for gold and silver workflows. The training pipeline consumes engineered features from data/features/*.parquet, writes model artifacts, and records metrics into reports/training_logs/. run_pipeline.py appears to be the orchestration entry point for backfill, update, retrain, and scheduled modes.
8. Inference and explainability
At runtime, the API loads trained models through api/app/model_loader.py and serves predictions to the dashboard. SHAP computation is isolated through api/app/shap_cache.py and explainability/shap_analyzer.py, so explainability is part of the inference path rather than an offline report-only step. Prediction logs are persisted under reports/predictions/.
9. Backtesting subsystem
backtesting/engine.py provides a separate evaluation path from live inference. The API exposes backtest run/results endpoints, while the engine likely operates on historical feature/data inputs to simulate trading with slippage, commission, and TP/SL rules. This subsystem is important because it validates model behavior against historical market conditions rather than live predictions.
10. Persistence and state
The project uses a relational database through SQLAlchemy/Alembic migrations, with local SQLite files present in instance/ and the repo root, while the README also documents PostgreSQL for containerized deployment. The database boundary likely stores users, auth state, profiles, watchlists, and operational metadata, while bulk market/history data stays in files and parquet artifacts.
11. Operational artifacts
The repository keeps generated data and results in file-based stores: data/features/ for model inputs, reports/ for training logs and prediction history, and miscellaneous_archive/ for older generated reports. This means the architecture is hybrid: transactional state in the DB, but large analytical artifacts on disk.
12. Deployment and orchestration
docker-compose.yml, the Dockerfiles, start_api.py, run.py, and run_pipeline.py are the main deployment/run entry points. There is also an Airflow DAG in airflow/dags/retrain.py, suggesting the training lifecycle can be automated externally. The overall deployment model is containerized, with the frontend, API, and database exposed as separate services.
13. External integrations
The important external dependencies that affect architecture are Yahoo Finance for market data, SMTP for email alerts, TradingView embedded charts in the frontend, and likely a message channel via SocketIO for live updates. These integrations are not peripheral; they directly shape the runtime flow from ingestion to signal delivery to user notification.
14. Primary end-to-end flow
The main loop is: fetch market data → build features → train/retrain models → serve predictions with SHAP → log outcomes → backtest/monitor → alert users when confidence thresholds are met. The dashboard is the consumer of this loop, while the pipeline and API are the producers and state managers.
Main entry points: main.py Flask API · gold_latest.json Reports · useWebSocket.ts Client hooks · model_loader.py Model runtime
Components 23
Frontend
Next.js dashboard and client-side data access
- Next.js app · App-router UI shell and routing entrypoints
frontend-next/src/app/layout.tsx - Auth pages · Login, register, and email verification flows
frontend-next/src/app/auth/login/page.tsx - Dashboard · Main trading dashboard and profile/risk/watchlist areas
frontend-next/src/app/dashboard/page.tsx - Backtest UI · Backtest workspace and results view
frontend-next/src/app/backtest/page.tsx - Domain widgets · TradingView charts, SHAP explainer, signal cards, risk metrics, watchlist table
frontend-next/src/components/Charts/TradingViewChart.tsx - Client hooks · HTTP and websocket access layer used by pages and widgets
frontend-next/src/lib/hooks/useWebSocket.ts - NextAuth · Client-side auth/session integration
frontend-next/src/app/api/auth/[...nextauth]/route.ts
API
Flask/SocketIO runtime control plane
- Flask API · Main backend surface and request entrypoint
api/app/main.py - Auth service · Login, registration, password, email, and security flows
api/app/auth.py - Domain routes · Predictions, market price, SHAP, backtesting, profile, and watchlist endpoints
api/app/watchlist.py - Model runtime · Model loading and prediction caching
api/app/model_loader.py - Explainability · SHAP caching and explanation serving
api/app/shap_cache.py - ETL routes · Backend surface for ETL-related operations
api/app/etl_routes.py
ML Pipeline
Feature engineering, training, backtesting, and refresh jobs
- ETL pipeline · Extraction, transformation, loading, and scheduling
etl/pipeline.py - Feature engine · SMC, volume, multi-timeframe aggregation, and labels
features/pipeline.py - Training jobs · XGBoost training and retraining for gold and silver workflows
models/retrain.py - Backtest engine · Historical simulation of trading behavior
backtesting/engine.py - Scheduler · Periodic refresh, alerts, and pipeline automation
etl/scheduler.py
State & Artifacts
Database, reports, and generated files
- SQL database · Users, auth state, profiles, watchlists, and operational metadata
api/instance/metalmind_smc.db - Reports · Prediction logs, training logs, and generated analysis outputs
reports/training_logs/gold_latest.json - Feature store · Model-ready feature files and other file-based analytical state
data/features/XAUUSD_features_latest.parquet
Other components
- Yahoo Finance · Market data feed used by ingestion
- SocketIO · Live update channel between backend and frontend
Connections 31
- Next.js app → Auth pages: routes to · App shell exposes auth screens
- Next.js app → Dashboard: routes to · App shell exposes dashboard areas
- Next.js app → Backtest UI: routes to · App shell exposes backtesting view
- Dashboard → Domain widgets: renders · Dashboard composes charts, signals, SHAP, and risk widgets
- Backtest UI → Client hooks: loads data · Backtest UI fetches results and runs
- Dashboard → Client hooks: fetches via · Dashboard data access is mediated by hooks
- Client hooks → Flask API: HTTP to · Client hooks call backend endpoints
- Client hooks → SocketIO: subscribes to · Live updates flow over websocket
- NextAuth → Auth service: auth delegates · Client auth wiring depends on backend auth services
- Flask API → Auth service: mounts · Main app includes auth surface
- Flask API → Domain routes: mounts · Main app exposes trading domain endpoints
- Flask API → Model runtime: uses · Request handling loads models and caches predictions
- Flask API → Explainability: uses · Explainability is served from runtime cache
- Flask API → ETL routes: mounts · Backend exposes ETL integration routes
- Domain routes → SQL database: reads/writes · Profile and watchlist state persists in database
- Auth service → SQL database: reads/writes · Auth records and user state persist in database
- Model runtime → Feature store: reads · Inference consumes trained feature artifacts
- Model runtime → Reports: logs to · Predictions and runtime outputs are recorded
- Explainability → Reports: stores · Explanation outputs and plots are persisted
- ETL pipeline → Yahoo Finance: ingests from · Primary market data feed
- ETL pipeline → Feature engine: feeds · Extracted data moves into feature construction
- Feature engine → Feature store: writes · Engineered features land in parquet artifacts
- Feature engine → Training jobs: supplies · Training consumes model-ready features
- Training jobs → Reports: writes logs · Training metrics and run summaries are recorded
- Training jobs → Model runtime: produces models · Trained artifacts are loaded by runtime inference
- Backtest engine → Reports: writes results · Historical evaluations produce backtest reports
- Scheduler → ETL pipeline: orchestrates · Scheduled refreshes trigger ingestion and processing
- Scheduler → Training jobs: triggers · Scheduled retraining refreshes models
- Scheduler → Reports: alerts/logs · Operational events are recorded for monitoring
- SocketIO → Flask API: served by · Realtime channel terminates in backend
- ETL routes → ETL pipeline: bridges to · Backend routes can initiate pipeline actions