Implementation PM Perspective: This document outlines the canonical structure of the Quant-Lab platform. The architecture was designed to solve three core problems in quantitative software: Data Integrity, Logic Reusability, and Scalable Presentation. By enforcing strict boundaries between data fetching, mathematical computation, and HTTP routing, we ensure that the system remains robust as new strategies are introduced.
The system follows a modern, decoupled client-server architecture.
graph TD
A[React Frontend SPA] --> B["Typed API Clients + React Query"]
B -->|REST API| C[FastAPI Backend]
C --> D{Router + Service Layer}
D --> E[Strategy Modules]
D --> F[Core Indicators / Backtest]
D --> G[(CSV + Live OHLCV Loader)]
G --> H["Short-lived Snapshot Cache"]
G --> I["Binance / Local CSV"]
C --> J["Production-only Basic Auth"]
The backend is structured using Domain-Driven Design (DDD) principles to prevent "spaghetti code" as the number of trading strategies grows.
backend/modules/(The API Layer):- Responsibility: HTTP routing, request validation (Pydantic), and response formatting.
- Rule: No business logic here. Routers only orchestrate calls to the Service layer.
backend/services/(The Orchestration Layer):- Responsibility: Coordinates between data fetching, strategy execution, and caching.
backend/strategy/(The Business Logic Layer):- Responsibility: Contains the specific rules for trading strategies (e.g.,
streak,hybrid,bb_mid). - Design Choice: Strategies are isolated. The
streakmodule does not know about thehybridmodule.
- Responsibility: Contains the specific rules for trading strategies (e.g.,
core/(The Pure Math Layer):- Responsibility: Stateless calculation of financial indicators (RSI, MACD, ATR) and core backtesting loops.
- Design Choice: Functions here take Pandas DataFrames and return DataFrames. They have zero knowledge of APIs or HTTP requests.
backend/utils/data_loader.py+backend/utils/data_service.py(The Data Access Layer):- Responsibility: Unified OHLCV loading from CSV or live exchange data, plus request-scoped snapshot caching and market-data helper caching.
- Design Choice: Analytical services call one shared loader instead of re-implementing exchange/file access per endpoint.
The frontend uses a Feature-Sliced Design (FSD) approach to manage complexity.
src/features/: Contains domain-specific logic and components (e.g.,streak-analysis,ai-lab). This prevents thecomponents/folder from becoming a dumping ground.src/api/: Centralized Axios clients. Components never make directfetchcalls, and API helpers normalize envelopes into typed success/error behavior for React Query.src/store/: Zustand is used for global state (like the currently selected Coin and Timeframe) to ensure the UI remains synchronized across different analysis views and backtest defaults.
- Development: Local development bypasses HTTP Basic auth automatically.
- Production:
backend/main.pyenforces HTTP Basic auth at the app boundary whenAPP_ENV=production.
- Backend:
utils/decorators.pywraps router handlers and converts unhandled exceptions into a consistent API envelope. - Frontend:
src/api/config.tsexposes shared envelope unwrapping and typed API errors so request failures surface as actual query/mutation errors rather than silent null payloads.
- Market Helpers:
DataCachebacks fear-greed, ticker, and AI response caching with diskcache or in-memory fallback. - Live OHLCV Snapshots:
utils/data_loader.pykeeps a short-lived snapshot cache for repeated live-candle requests, which reduces duplicate exchange calls during multi-page analysis workflows.
To demonstrate the architecture in action, here is the flow when a user asks the AI to analyze a condition:
- User Input: "Probability that the next candle is bullish when RSI < 30" (Frontend
ChatInterface.tsx) - API Call: POST
/api/ai/research(Frontendapi/ai_lab.ts) - Routing:
backend/modules/ai_lab/router.pyreceives the request. - Service & LLM:
service.pysends the prompt to the LLM Gateway to parse the natural language into a structured JSON condition. - Parsing:
parser.pyconverts the JSON into executable Pandas boolean masks. - Data Fetching: The system retrieves the required OHLCV data through the shared loader, reusing cached live snapshots when available.
- Calculation:
core/indicators.pycalculates the RSI. - Analysis:
analyzer.pyapplies the mask, calculates the Wilson Confidence Interval and p-value. - Response: The structured data is returned to the frontend, normalized by the API client layer, and rendered via Plotly charts.
As a PM, ensuring reliability in financial software is paramount.
- Backend Test Suite:
pytestcurrently collects 124 tests across 20 files inbackend/tests/, covering core math, AI orchestration, caching, auth flow, and architecture contracts. - Type Safety: Strict TypeScript on the frontend and Pydantic models on the backend ensure data contracts are honored.
- Architecture Guards:
scripts/check_core_imports.pyandscripts/check_route_imports.pyprevent forbidden layer coupling from creeping into the codebase. - Error Handling: Centralized decorators (
utils/decorators.py) catch and format exceptions before they reach the client, while frontend API helpers preserve those failures as actionable query/mutation errors.