# mnsrulz/mytradingview architecture

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

- Interactive diagram: https://gitdiagram.com/mnsrulz/mytradingview
- Repository: https://github.com/mnsrulz/mytradingview

## Overview

##### Project Type and Purpose
- Type: Full-stack serverless web application built on Next.js (App Router)
- Purpose: Provide in-depth stock options analytics (profit views, Greeks, hedging, screener) with real-time and historical data via external APIs and a PostgreSQL database.

##### Main Components
A. Client (Browser/UI)
– Next.js frontend (src/app pages + React components)
– SSR/SSG/Client-side navigation, CSS (globals.css), theming (theme.ts)
– Interactive charts, dialogs, data tables (components/*)
B. Next.js Server/API Layer
– API Routes (src/app/api/*) implemented as serverless functions
- auth: NextAuth handlers for login/ session
- contact, schedules (cron-style endpoints), symbols, trades, watchlist, timesales
– Middleware (src/middleware.ts) for auth, request logging
– Socket service (src/lib/socket.ts) for real-time updates
C. Business Logic & Services (src/lib)
– prisma.ts: Prisma Client for PostgreSQL
– dataService, mzDataService: DB abstractions
– tradierService, cboeService, stockService: External market data APIs
– optionPriceHelper, dgHedgingHelper: domain calculations
– logger, utils, formatters, hooks
D. Database
– PostgreSQL (managed via Prisma ORM, schema.prisma + migrations)
E. External Services
– Tradier Brokerage API
– CBOE delayed quotes (scraping or REST)
– (Potential) WebSocket or SSE providers for live tick data
F. Infrastructure & CI/CD
– Devcontainer for local dev
– GitHub Actions (sync-branch, dependabot)
– Netlify for staging/production deployment
– Environment variables (.env.local_SAMPLE)

##### Relationships & Data Flows
- Browser → Next.js Frontend → SSR (page.tsx) or Client API calls (/app/api/…)
- API Route Handler →
– Prisma Client → PostgreSQL
– External Service Clients (Tradier/CBOE) → fetch market data
– Business-logic helpers → compute Greeks, exposures, returns
- API Response → Frontend → Render visualizations
- NextAuth Middleware → Protect routes, manage sessions
- Socket Service → Push live updates from server to client
- Scheduler Endpoint → Triggered by external cron (Netlify functions) to update open trades’ last contract price

##### Architectural Patterns & Principles
- Monolithic serverless functions (Next.js API routes)
- Separation of concerns:
– UI components vs. page routes vs. API handlers vs. business logic libs
- Domain-driven modules: Each feature (symbols, trades, watchlist) has its own route folder
- Infrastructure as code: Prisma migrations, devcontainer, CI pipelines
- Responsive design + progressive enhancement (SSR + CSR)

##### Final Checklist
– Each component labeled (UI, API route, lib service, DB, external API)
– Notes on update frequencies for scheduled jobs
– Authentication boundary via NextAuth

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client (Browser/UI)"
        direction TB
        UI_Pages["Next.js Pages & SSR/CSR"]:::ui
        UI_Components["React UI Components"]:::ui
        UI_Styles["Global Styles"]:::ui
        UI_Theme["Theming"]:::ui
    end

    %% Server Layer
    subgraph "Next.js App Server"
        direction TB
        Frontend["Frontend (App Router)"]:::server
        Middleware["Middleware (Auth, Logging)"]:::server
        Socket["Socket Service"]:::server

        subgraph "API Routes"
            direction TB
            AuthAPI["Auth API"]:::server
            ContactAPI["Contact API"]:::server
            SchedulerAPI["Scheduler API"]:::server
            SymbolsAPI["Symbols APIs"]:::server
            TimesalesAPI["Time & Sales API"]:::server
            TradesAPI["Trades APIs"]:::server
            WatchlistAPI["Watchlist API"]:::server
        end
    end

    %% Services Layer
    subgraph "Business Logic & External Services"
        direction TB
        PrismaClient["Prisma Client"]:::services
        DataService["Data Services"]:::services
        TradierService["Tradier Service"]:::external
        CBOEService["CBOE Service"]:::external
        YFOptions["Yahoo Finance Options"]:::external
        OptionHelper["Option Price Helper"]:::services
        HedgingHelper["Delta-Gamma Hedging"]:::services
        StockService["Stock Service"]:::services
        LoggerUtils["Logger & Utils"]:::services
    end

    %% Database
    Database["PostgreSQL Database"]:::db

    %% External Cloud Services
    TradierAPICloud["Tradier Brokerage API"]:::external
    CBOEAPICloud["CBOE Delayed Quotes"]:::external
    WebSocketProvider["WebSocket/SSE Provider"]:::external

    %% Relationships
    UI_Pages -->|"Navigates/SSR"| Frontend
    UI_Components --> Frontend
    UI_Styles --> Frontend
    UI_Theme --> Frontend

    Frontend -->|"Client API Calls"| AuthAPI
    Frontend -->|"Client API Calls"| ContactAPI
    Frontend -->|"Client API Calls"| SchedulerAPI
    Frontend -->|"Client API Calls"| SymbolsAPI
    Frontend -->|"Client API Calls"| TimesalesAPI
    Frontend -->|"Client API Calls"| TradesAPI
    Frontend -->|"Client API Calls"| WatchlistAPI

    AuthAPI --> Middleware
    ContactAPI --> Middleware
    SchedulerAPI --> Middleware
    SymbolsAPI --> Middleware
    TimesalesAPI --> Middleware
    TradesAPI --> Middleware
    WatchlistAPI --> Middleware

    AuthAPI -->|Reads/Writes| Database
    ContactAPI -->|Writes| Database
    SchedulerAPI -->|Reads/Writes| Database
    SymbolsAPI -->|Reads| Database
    TradesAPI -->|Reads/Writes| Database
    WatchlistAPI -->|Reads/Writes| Database
    TimesalesAPI -->|Reads/Writes| Database

    AuthAPI -->|Session| Database

    SymbolsAPI --> TradierService
    SymbolsAPI --> CBOEService
    SymbolsAPI --> YFOptions

    TradierService --> TradierAPICloud
    CBOEService --> CBOEAPICloud
    TradierService --> WebSocketProvider

    AuthAPI --> OptionHelper
    SymbolsAPI --> OptionHelper
    TradesAPI --> HedgingHelper
    TradesAPI --> StockService

    AuthAPI --> LoggerUtils
    ContactAPI --> LoggerUtils
    SchedulerAPI --> LoggerUtils
    SymbolsAPI --> LoggerUtils
    TimesalesAPI --> LoggerUtils
    TradesAPI --> LoggerUtils
    WatchlistAPI --> LoggerUtils

    Middleware --> LoggerUtils

    SchedulerAPI -.->|Cron Trigger| SchedulerAPI

    Socket --> Frontend
    Socket --> TradierService
    Socket --> WebSocketProvider

    %% Click Events
    click UI_Pages "https://github.com/mnsrulz/mytradingview/tree/main/src/app"
    click UI_Components "https://github.com/mnsrulz/mytradingview/tree/main/src/components"
    click UI_Styles "https://github.com/mnsrulz/mytradingview/blob/main/src/app/globals.css"
    click UI_Theme "https://github.com/mnsrulz/mytradingview/blob/main/src/theme.ts"
    click AuthAPI "https://github.com/mnsrulz/mytradingview/blob/main/src/app/api/auth/[...nextauth]/route.ts"
    click ContactAPI "https://github.com/mnsrulz/mytradingview/blob/main/src/app/api/contact/route.ts"
    click SchedulerAPI "https://github.com/mnsrulz/mytradingview/blob/main/src/app/api/schedules/updateOpenTradesLastContractPrice/route.ts"
    click TimesalesAPI "https://github.com/mnsrulz/mytradingview/blob/main/src/app/api/timesales/route.ts"
    click WatchlistAPI "https://github.com/mnsrulz/mytradingview/blob/main/src/app/api/watchlist/route.ts"
    click Middleware "https://github.com/mnsrulz/mytradingview/blob/main/src/middleware.ts"
    click Socket "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/socket.ts"
    click PrismaClient "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/prisma.ts"
    click TradierService "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/tradierService.ts"
    click CBOEService "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/cboeService.ts"
    click YFOptions "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/yfOptions.ts"
    click OptionHelper "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/optionPriceHelper.ts"
    click HedgingHelper "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/dgHedgingHelper.ts"
    click StockService "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/stockService.ts"
    click LoggerUtils "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/logger.ts"
    click LoggerUtils "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/utils.ts"
    click LoggerUtils "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/formatters.ts"
    click LoggerUtils "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/hooks.ts"
    click LoggerUtils "https://github.com/mnsrulz/mytradingview/blob/main/src/lib/constants.ts"
    click Database "https://github.com/mnsrulz/mytradingview/blob/main/prisma/schema.prisma"
    click Database "https://github.com/mnsrulz/mytradingview/tree/main/prisma/migrations/"
    
    %% Styles
    classDef ui fill:#D6EAFE,stroke:#0366d6,stroke-width:1px
    classDef server fill:#E2F0CB,stroke:#228B22,stroke-width:1px
    classDef services fill:#FFE4B3,stroke:#FF8C00,stroke-width:1px
    classDef external fill:#FBE4E1,stroke:#DC143C,stroke-width:1px
    classDef db fill:#C8D6E5,stroke:#1f78b4,stroke-width:1px,shape:cylinder
```
