# austin-starks/nexttrade architecture

> Architecture diagram of the GitHub repository austin-starks/nexttrade, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/austin-starks/nexttrade
- Repository: https://github.com/austin-starks/nexttrade

## Overview

##### Project type and purpose
- NextTrade is a full‑stack, monolithic trading platform.
- Frontend: React/TypeScript single‑page application for building and visualizing strategies, conditions, portfolios, backtests, optimizations, and live deployments.
- Backend: Node.js/TypeScript Express server exposing a REST API, implementing trading workflows (backtest, forward‑test, optimize, deploy) and persisting data in MongoDB.
- External integrations: Brokerage APIs (Tradier, Alpaca, Coinbase, etc.) for live deployments and data sourcing.

##### File structure
- Top‑level “client” and “app” directories separate frontend and backend.
- “client/src/pages” and “client/src/components” organize the UI into feature pages (Assets, Backtests, Optimization, Portfolios, Strategies, Authentication) and reusable components.
- “client/src/services/requests.tsx” encapsulates HTTP calls to the backend.
- “app/src/routes” → Express route definitions.
- “app/src/controller” → Controllers for each domain (backtestController, forwardtestController, optimizationController, portfolioController, etc.).
- “app/src/models” → Domain models and abstractions (Assets, Conditions, Strategies, Portfolios, History, Brokerage adapters, etc.)
- “app/src/services/db.ts” → MongoDB connection.
- “app/src/controller/*/worker.ts” → Background workers for long‑running tasks (backtest, forward‑test, optimize).
- Configuration files: tsconfig.json, nodemon.json, PM2 startup scripts (static.json).

##### README for insights
- Backend uses MongoDB for persistence, Jest for testing, PM2 for process management.
- Supports historical backtesting, genetic‑algorithm optimization, real‑time live trading via brokerage APIs.
- The platform is deployed via two processes: client and server.

##### Detail
- Break the Backend into: HTTP Server → Route Handlers → Controllers → Model Layer → DB.
- Worker processes separately, indicating they read from and write to the same DB.
- Process Manager (PM2) overseeing both client and server processes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend UI"
        direction TB
        SPA["React/TypeScript SPA"]:::frontend
        Pages["UI Feature Pages"]:::frontend
        Components["Reusable UI Components"]:::frontend
        Requests["HTTP Service (REST API calls)"]:::api
    end

    %% Backend Layer
    subgraph "Backend API Server"
        direction TB
        App["Express Server Entry Point"]:::api
        Routes["API Route Definitions"]:::api
        AuthMW["Auth Middleware"]:::api
        Controllers["Controllers (Orchestration)"]:::controller
        Models["Domain Models & Adapters"]:::model
        DBService["MongoDB Connection Service"]:::db
    end

    %% Workers
    subgraph "Background Workers"
        direction TB
        BacktestCtrl["Backtest Controller"]:::controller
        BacktestWorker["Backtest Worker"]:::worker
        ForwardCtrl["Forward‑test Controller"]:::controller
        ForwardWorker["Forward‑test Worker"]:::worker
        OptimizationCtrl["Optimization Controller"]:::controller
        OptimizationWorker["Optimization Worker"]:::worker
    end

    %% External and infra
    subgraph "Infrastructure"
        direction TB
        MongoDB[(MongoDB)]:::db
        ExternalAPIs["External Brokerage APIs"]:::external
        PM2["Process Manager (PM2)"]:::config
        StaticData["Background Data & Static Files"]:::config
    end

    %% Connections
    SPA -->|uses| Pages
    SPA -->|uses| Components
    SPA -->|calls| Requests
    Requests -->|HTTP| App
    App -->|defines| Routes
    Routes -->|uses| AuthMW
    Routes -->|delegates to| Controllers
    Controllers -->|reads/writes| Models
    Models -->|connects via| DBService
    DBService -->|connects| MongoDB

    Controllers -->|enqueue jobs| BacktestWorker
    Controllers -->|enqueue jobs| ForwardWorker
    Controllers -->|enqueue jobs| OptimizationWorker

    BacktestWorker -->|uses| Models
    BacktestWorker -->|persists| MongoDB
    ForwardWorker -->|uses| Models
    ForwardWorker -->|persists| MongoDB
    OptimizationWorker -->|uses| Models
    OptimizationWorker -->|persists| MongoDB

    Controllers -->|live deploy| ExternalAPIs
    ExternalAPIs -->|order & data| Models

    PM2 -->|manages| SPA
    PM2 -->|manages| App

    StaticData -->|feeds| Models

    %% Click Events
    click SPA "https://github.com/austin-starks/nexttrade/tree/master/client/src"
    click Pages "https://github.com/austin-starks/nexttrade/tree/master/client/src/pages"
    click Components "https://github.com/austin-starks/nexttrade/tree/master/client/src/components"
    click Requests "https://github.com/austin-starks/nexttrade/blob/master/client/src/services/requests.tsx"
    click App "https://github.com/austin-starks/nexttrade/blob/master/app/src/app.ts"
    click Routes "https://github.com/austin-starks/nexttrade/tree/master/app/src/routes"
    click AuthMW "https://github.com/austin-starks/nexttrade/blob/master/app/src/routes/middleware/auth.ts"
    click Controllers "https://github.com/austin-starks/nexttrade/tree/master/app/src/controller"
    click BacktestCtrl "https://github.com/austin-starks/nexttrade/blob/master/app/src/controller/backtestController/index.ts"
    click BacktestWorker "https://github.com/austin-starks/nexttrade/blob/master/app/src/controller/backtestController/worker.ts"
    click ForwardCtrl "https://github.com/austin-starks/nexttrade/blob/master/app/src/controller/forwardtestController/index.ts"
    click ForwardWorker "https://github.com/austin-starks/nexttrade/blob/master/app/src/controller/forwardtestController/worker.ts"
    click OptimizationCtrl "https://github.com/austin-starks/nexttrade/blob/master/app/src/controller/optimizationController/index.ts"
    click OptimizationWorker "https://github.com/austin-starks/nexttrade/blob/master/app/src/controller/optimizationController/worker.ts"

    click MongoDB "https://github.com/austin-starks/nexttrade/blob/master/app/src/services/db.ts"
    click StaticData "https://github.com/austin-starks/nexttrade/tree/master/app/data"

    classDef frontend fill:#bbdefb,stroke:#1e88e5,color:#0d47a1;
    classDef api fill:#c8e6c9,stroke:#43a047,color:#1b5e20;
    classDef controller fill:#dcedc8,stroke:#7cb342,color:#33691e;
    classDef model fill:#f0f4c3,stroke:#c0ca33,color:#827717;
    classDef worker fill:#ffe0b2,stroke:#fb8c00,color:#e65100;
    classDef db fill:#fff9c4,stroke:#fbc02d,color:#f57f17;
    classDef external fill:#ffcdd2,stroke:#e53935,color:#b71c1c;
    classDef config fill:#e0e0e0,stroke:#757575,color:#424242;
```
