QuantDinger Backend Module Boundaries

July 13, 2026 ยท View on GitHub

This document defines target ownership for the backend. It does not require an immediate rewrite. It is the contract to follow as existing code is decomposed.

Layer Model

LayerOwnsMust Not Own
routesHTTP parsing, auth checks, status codes, request/response mappingtrading loops, exchange-specific rules, long-running jobs
openapischema registration, OpenAPI export, operation metadatabusiness logic
servicesbusiness workflows and use-case orchestrationraw Flask request objects except at route boundary
services/live_tradingexchange and broker adapters, order API normalizationstrategy lifecycle, user auth, HTTP responses
services/gridgrid engine, cell state, fill normalization, reconciliationroute parsing, frontend-specific formatting
data_sourcesmarket data adapters and fetch policystrategy execution or account mutation
data_providersdashboard/global-market aggregation and cache policytrading decisions or order placement
utilslow-level auth, db, cache, logging, time, crypto helpersfeature workflows
configenvironment and settings resolutionruntime mutation side effects
migrationsschema and seed dataPython runtime behavior

Current Hotspots

These files mix multiple responsibilities and should be decomposed gradually:

FileCurrent RiskTarget Split
app/__init__.pyapp factory plus core Flask wiringkeep app factory only
app/startup.pyworker boot, strategy restore, process-local singletonsdistributed worker ownership guards
app/routes/strategy.pylifecycle, templates, AI generationroute modules per subdomain
app/routes/strategy_account_routes.pyaccount snapshot and account position mirroraccount service facade
app/routes/backtest_center.pyunified V2 backtest endpoint facadeone request schema and one simulation service facade
app/routes/strategy_deviation_routes.pydry-run deviation endpointdeviation service facade
app/routes/strategy_grid_routes.pygrid resting order endpointgrid route facade
app/routes/strategy_ledger_routes.pytrades, equity curve, performance endpointsledger/read-model service facade
app/routes/strategy_positions_routes.pystrategy live position read modelposition query service facade
app/routes/strategy_review_routes.pyAI strategy review endpointsreview request schema and service facade
app/routes/strategy_logs_routes.pyruntime log query endpointobservability route facade
app/routes/strategy_notifications.pystrategy notification endpointsnotification service facade
app/routes/quick_trade.pyHTTP, credential handling, exchange selection, order formattingroute facade plus quick-trade service
app/routes/ai_chat.pymemory, skills, tools, chat, streamingseparate AI route modules
app/routes/settings.pyschema, config values, brand, connection testingsettings service plus config schemas
app/services/trading_executor.pysignal loops, order placement, sync, persistenceexecutor core, order intents, locks, recorders
app/services/backtest.pydata loading, strategy execution, simulation, metricsbacktest pipeline components
app/services/pending_order_worker.pyworker loop plus dispatch and sync detailsworker shell, dispatcher, reconciliation

Route Layer Rules

  • Routes may validate input, call services, and shape HTTP responses.
  • Routes must not start background threads directly.
  • Routes must not contain exchange-specific order sizing logic.
  • Routes must not perform multi-step database transactions inline unless the logic is being migrated and covered by tests.
  • Streaming routes must define timeout, heartbeat, and cancellation behavior.

Service Layer Rules

  • Services own use-case flow and can coordinate repositories/adapters.
  • Services should accept plain Python values, not Flask request objects.
  • Services should return plain dicts/dataclasses or typed result objects.
  • Services should define idempotency behavior when they mutate state.
  • Long-running service work should be runnable as a job or worker task.

Adapter Rules

  • Exchange and broker adapters normalize external APIs into internal contracts.
  • Adapters should not know about Flask, users, or frontend response shapes.
  • Adapter methods should accept explicit client_order_id when the venue supports it.
  • Adapter-specific rate limits and retry rules should be isolated from business logic.

Startup Boundary

Target structure:

  • create_app() creates Flask, configures JSON/CORS, registers routes.
  • startup.py owns worker startup and process-local service singletons.
  • Worker startup is disabled for OpenAPI export, tests, and one-off scripts.
  • Multi-process deployments must have an explicit owner for each background worker.

Documentation Boundary

  • Root README.md: user-facing quick start and product overview.
  • docs/: architecture, API, operations, integration guides.
  • docs/agent/: agent-facing docs only, English only.
  • Generated API files must say how they were generated.
  • Deprecated docs must be moved to an archive or marked with a date and replacement.

Language Boundary

  • Code comments, docstrings, log messages, internal error details, module names, function names, variables, and engineering documentation should be English by default.
  • Chinese is allowed only for user-facing localized text, prompts, translation dictionaries, examples that intentionally demonstrate Chinese output, exchange or market terminology that is inherently Chinese, and backward-compatible API fields that already expose localized content.
  • Do not mix Chinese comments into core trading, concurrency, API, database, or deployment code. If localized text is needed, keep it behind an explicit language selector or i18n structure.
  • When replacing legacy Chinese comments, preserve the technical meaning and avoid changing runtime behavior in the same edit.

Compatibility Rule

When moving code, keep the old import path or route path until all callers are verified. If compatibility cannot be preserved, document the break and update frontend/API tests in the same change.