Back to home

Project 01 of 06

Nebula

Virtual stock trading and learning platform with a real-time market engine

NestJSTypeScriptReactPostgreSQLRedisPrismaSocket.IONode.js
deployable processes
3
deployable processes
server layers
4
server layers
minute access tokens
15
minute access tokens
circuit breaker band
10%
circuit breaker band

Architecture

4 stages
nebula-trading-platformsignal flow
React ClientNestJS ServerSimulation EnginePostgreSQL & Redis
Readout

Virtual stock trading and learning platform with a real-time market engine

Overview

3 min read

Nebula is a virtual stock trading and market-education platform built on NestJS and React, combining a custom real-time market simulation engine, broker-mediated collateral management, and full administrative oversight under a strict layered architecture. The platform is split into three independently deployable processes with non-overlapping responsibilities: a React SPA client, a NestJS server that is the sole authority on business rules and financial state, and a standalone market simulation engine that never talks to the client directly.

Every trading action, price update, and notification passes through the server. Authentication supports email/password and Google OAuth 2.0 registration, with short-lived 15-minute JWT access tokens held in memory only, 7-day refresh tokens delivered as HTTP-only cookies and rotated on every use, and refresh-token reuse detection that invalidates every active session on an account the moment a stolen token is replayed.

Each device holds an independent, individually revocable refresh token, and access tokens are blacklisted on logout before any database lookup occurs. The Trader Portal covers Market and Limit order placement with idempotency-key support against duplicate submissions, a live order book depth ladder, order history with cancellation and automatic release of reserved funds, a portfolio view with cost basis and unrealized P&L, a watchlist with self-clearing one-time price alerts, a paginated wallet transaction ledger, and broker-routed collateral top-up requests.

The Broker Dashboard gives brokers a list of assigned traders with balances and activity summaries, per-trader detail views, collateral top-up processing under a weekly cap enforced through database aggregation rather than a cache counter, mandatory receipt upload with duplicate-reference prevention, and suspicious-activity flagging with resolution tracking.

The Admin Panel adds platform-wide statistics with a composite market index chart, user suspension that automatically cancels pending orders and releases reserved balances, broker application review and reassignment, top-up oversight with cap-override authority, suspicious-flag resolution, learning-content management, and a full audit log.

At the core of the platform is a market simulation engine using Geometric Brownian Motion with sector-specific volatility and drift, a Box-Muller transform for normally distributed price movement, a circuit breaker that halts a stock once it moves beyond 10% of the previous close, and a Redis-backed order book with price-time priority and self-trade prevention.

The engine is completely independent of the rest of the platform, with no Prisma, no NestJS, no PostgreSQL, and no knowledge of users or wallets. It communicates with the server only through Redis pub/sub and a polled HTTP health endpoint that gates order placement when the engine is unreachable. The server enforces a strict four-layer separation in every module: controllers handle routing only, services own all business logic, repositories own all Prisma and Redis access, and DTOs handle request validation, with a single global exception filter as the only place in the codebase that constructs an error response.

All monetary values are stored as integer paise, never floating point, and any wallet read-then-write acquires a Redis distributed lock ahead of a Prisma transaction, backstopped by a PostgreSQL CHECK constraint against negative balances. Real-time updates reach the client through room-scoped Socket.IO events: stock-specific rooms for price updates, user-specific rooms for fills and portfolio changes, and platform-wide broadcasts for circuit-breaker and market-status events.

Highlights

7 items

  1. 01Dual auth: email/password and Google OAuth 2.0, 15-minute in-memory JWT access tokens paired with rotating 7-day httpOnly refresh cookies, reuse detection that invalidates every session on theft, and per-device revocation
  2. 02Trader Portal: Market/Limit orders with idempotency-key deduplication, live order book depth ladder, portfolio with cost basis and unrealized P&L, self-clearing watchlist price alerts, broker-routed collateral top-ups
  3. 03Broker Dashboard: assigned-trader oversight, weekly-capped top-up processing enforced via database aggregation (not cache), mandatory receipt upload with duplicate-reference prevention, suspicious activity flagging
  4. 04Admin Panel: platform-wide stats with composite market index chart, user suspension with automatic order cancellation, broker application review, top-up cap overrides, audit log, and read-only engine health status
  5. 05Market simulation engine: Geometric Brownian Motion price simulation with a Box-Muller transform, a 10% circuit breaker, and a Redis-backed order book with price-time priority. Fully independent of Prisma, NestJS, and PostgreSQL, communicating only via Redis pub/sub
  6. 06Strict four-layer server architecture (Controller, Service, Repository, DTO) with a single global exception filter, integer-paise money handling, and Redis-locked wallet transactions backstopped by a PostgreSQL CHECK constraint
  7. 07Three independently deployable processes (client, server, engine) with room-scoped Socket.IO events for price ticks, fills, and platform-wide circuit-breaker and market-status broadcasts