Nija

NIJA Platform Architecture - Complete System Design

Version: 2.0 Last Updated: January 29, 2026 Status: βœ… Production-Ready Architecture


Table of Contents

  1. Executive Summary
  2. System Overview
  3. Architecture Layers
  4. Backend Structure
  5. API Routes
  6. Dashboard Layout
  7. Subscription Logic
  8. Scaling Blueprint
  9. Security Architecture
  10. Deployment Strategy
  11. Monitoring & Observability

Executive Summary

NIJA is an enterprise-grade autonomous cryptocurrency trading platform that combines:

Key Capabilities


System Overview

High-Level Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     CLIENT APPLICATIONS                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚
β”‚  β”‚ Web Dashboardβ”‚  β”‚  Mobile Apps β”‚  β”‚  API Clients β”‚          β”‚
β”‚  β”‚  (React.js)  β”‚  β”‚ (React Nativeβ”‚  β”‚   (REST)     β”‚          β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                  β”‚                  β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                    HTTPS/WSS (TLS 1.3)
                             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     API GATEWAY LAYER                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  Kong/Nginx API Gateway                                    β”‚  β”‚
β”‚  β”‚  β€’ SSL Termination                                         β”‚  β”‚
β”‚  β”‚  β€’ JWT Authentication                                      β”‚  β”‚
β”‚  β”‚  β€’ Rate Limiting (tier-based)                             β”‚  β”‚
β”‚  β”‚  β€’ Request Routing                                         β”‚  β”‚
β”‚  β”‚  β€’ CORS Policy Enforcement                                β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚                                       β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   PUBLIC APIs       β”‚                β”‚   ADMIN APIs        β”‚
β”‚   (FastAPI)         β”‚                β”‚   (FastAPI)         β”‚
β”‚                     β”‚                β”‚                     β”‚
β”‚  β€’ Dashboard API    β”‚                β”‚  β€’ User Management  β”‚
β”‚  β€’ Trading API      β”‚                β”‚  β€’ System Control   β”‚
β”‚  β€’ Analytics API    β”‚                β”‚  β€’ Monitoring       β”‚
β”‚  β€’ User Settings    β”‚                β”‚  β€’ Audit Logs       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                                      β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    BUSINESS LOGIC LAYER                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚
β”‚  β”‚ User Control β”‚  β”‚Subscription  β”‚  β”‚ Permission   β”‚          β”‚
β”‚  β”‚   Service    β”‚  β”‚   Engine     β”‚  β”‚  Validator   β”‚          β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                  β”‚                  β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     EXECUTION LAYER                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  Execution Router & Trade Isolation                        β”‚  β”‚
β”‚  β”‚  β€’ Per-user execution contexts                             β”‚  β”‚
β”‚  β”‚  β€’ Broker adapter factory                                  β”‚  β”‚
β”‚  β”‚  β€’ Order routing & validation                             β”‚  β”‚
β”‚  β”‚  β€’ Position tracking                                       β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚                                       β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   CORE BRAIN        β”‚                β”‚  BROKER ADAPTERS    β”‚
β”‚   (PRIVATE)         β”‚                β”‚  (MULTI-EXCHANGE)   β”‚
β”‚                     β”‚                β”‚                     β”‚
β”‚  β€’ APEX Strategy    │───signals──▢   β”‚  β€’ Coinbase         β”‚
β”‚  β€’ Meta-AI Engine   β”‚                β”‚  β€’ Kraken           β”‚
β”‚  β€’ MMIN System      β”‚                β”‚  β€’ Binance          β”‚
β”‚  β€’ GMIG System      β”‚                β”‚  β€’ OKX              β”‚
β”‚  β€’ Risk Manager     β”‚                β”‚  β€’ Alpaca           β”‚
β”‚  β€’ Indicators       β”‚                β”‚  β€’ Paper Trading    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                 β”‚
                                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                       β”‚   EXCHANGES         β”‚
                                       β”‚  (External APIs)    β”‚
                                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      DATA LAYER                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚
β”‚  β”‚ PostgreSQL   β”‚  β”‚    Redis     β”‚  β”‚  TimescaleDB β”‚          β”‚
β”‚  β”‚              β”‚  β”‚              β”‚  β”‚              β”‚          β”‚
β”‚  β”‚ β€’ Users      β”‚  β”‚ β€’ Sessions   β”‚  β”‚ β€’ Trade Data β”‚          β”‚
β”‚  β”‚ β€’ Trades     β”‚  β”‚ β€’ Cache      β”‚  β”‚ β€’ Metrics    β”‚          β”‚
β”‚  β”‚ β€’ Positions  β”‚  β”‚ β€’ Job Queue  β”‚  β”‚ β€’ Analytics  β”‚          β”‚
β”‚  β”‚ β€’ Subscript. β”‚  β”‚ β€’ PubSub     β”‚  β”‚ β€’ Time Seriesβ”‚          β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    INFRASTRUCTURE LAYER                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚
β”‚  β”‚   HashiCorp  β”‚  β”‚ Prometheus + β”‚  β”‚   Logging    β”‚          β”‚
β”‚  β”‚     Vault    β”‚  β”‚   Grafana    β”‚  β”‚  (ELK Stack) β”‚          β”‚
β”‚  β”‚              β”‚  β”‚              β”‚  β”‚              β”‚          β”‚
β”‚  β”‚ β€’ API Keys   β”‚  β”‚ β€’ Metrics    β”‚  β”‚ β€’ Audit Logs β”‚          β”‚
β”‚  β”‚ β€’ Secrets    β”‚  β”‚ β€’ Dashboards β”‚  β”‚ β€’ Error Logs β”‚          β”‚
β”‚  β”‚ β€’ Rotation   β”‚  β”‚ β€’ Alerts     β”‚  β”‚ β€’ Trade Logs β”‚          β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Architecture Layers

Layer 1: Core Brain (PRIVATE)

Purpose: Contains proprietary trading algorithms and decision-making logic.

Location: /core/ and /bot/

Components:

Access Control:

# NEVER exposed to users
# Only accessible via execution layer
from core import verify_core_access

verify_core_access(__name__)  # Raises PermissionError if unauthorized

Key Files:


Layer 2: Execution Engine (LIMITED)

Purpose: Handles broker connections and order execution with user-specific permissions.

Location: /execution/ and /bot/broker_*.py

Components:

2.1 Execution Router

Routes trades to appropriate broker adapters with user isolation.

from execution import ExecutionRouter

router = ExecutionRouter()
result = router.route_order(
    user_id="user_123",
    broker="coinbase",
    symbol="BTC-USD",
    side="buy",
    size_usd=100.0
)

2.2 Broker Adapters

Unified interface to multiple exchanges:

from execution.broker_adapter import SecureBrokerAdapter

adapter = SecureBrokerAdapter(
    user_id="user_123",
    broker_name="coinbase"
)

# Place order (automatically validated)
result = adapter.place_order(
    pair="BTC-USD",
    side="buy",
    size_usd=50.0
)

2.3 Permission Validator

Enforces user-specific trading limits.

from execution import UserPermissions, get_permission_validator

# Define user permissions
perms = UserPermissions(
    user_id="user_123",
    allowed_pairs=["BTC-USD", "ETH-USD"],
    max_position_size_usd=100.0,
    max_daily_loss_usd=50.0,
    max_positions=3,
    trade_only=True
)

# Validate trade
validator = get_permission_validator()
valid, error = validator.validate_trade(
    user_id="user_123",
    pair="BTC-USD",
    position_size_usd=50.0
)

Key Files:


Layer 3: User Interface (PUBLIC)

Purpose: Public-facing interface for monitoring and configuration.

Location: /ui/, /frontend/, /mobile/

Components:

3.1 Web Dashboard (React.js)

Single-page application for desktop browsers.

Features:

Tech Stack:

3.2 Mobile Apps (React Native)

Native iOS and Android applications.

Features:

Tech Stack:

3.3 Dashboard API

from ui import DashboardAPI

dashboard = DashboardAPI()

# Get user statistics
stats = dashboard.get_user_stats("user_123")
# Returns: {
#   'total_trades': 150,
#   'win_rate': 0.68,
#   'total_pnl': 2340.50,
#   'active_positions': 3,
#   'total_volume': 45000.00
# }

# Get active positions
positions = dashboard.get_positions("user_123")

# Get trade history
history = dashboard.get_trade_history(
    user_id="user_123",
    limit=50,
    offset=0
)

Key Files:


Layer 4: Authentication & Authorization

Purpose: Secure user authentication and API credential management.

Location: /auth/

Components:

4.1 User Manager

Manages user accounts and authentication.

from auth import get_user_manager

user_mgr = get_user_manager()

# Create user
user_mgr.create_user(
    user_id="user_123",
    email="user@example.com",
    password_hash=hash_password("secure_password"),
    subscription_tier="pro"
)

# Authenticate user
authenticated, user = user_mgr.authenticate(
    email="user@example.com",
    password="secure_password"
)

# Generate JWT token
token = user_mgr.generate_jwt_token(user_id="user_123")

4.2 API Key Manager

Securely stores user exchange API keys.

from auth import get_api_key_manager

api_manager = get_api_key_manager()

# Store encrypted API keys
api_manager.store_user_api_key(
    user_id="user_123",
    broker="coinbase",
    api_key="user_api_key",
    api_secret="user_api_secret",
    additional_params={'org_id': 'org_123'}
)

# Retrieve decrypted credentials (internal only)
creds = api_manager.get_user_api_key("user_123", "coinbase")

Security Features:

Key Files:


Layer 5: Configuration Management

Purpose: Manage user-specific trading preferences.

Location: /config/

Components:

from config import get_config_manager

config_mgr = get_config_manager()

# Get user configuration
user_config = config_mgr.get_user_config("user_123")

# Update configuration
config_mgr.update_user_config("user_123", {
    'max_position_size': 150.0,
    'max_concurrent_positions': 5,
    'risk_level': 'medium',
    'allowed_pairs': ['BTC-USD', 'ETH-USD'],
    'notifications_enabled': True
})

Default Configuration:

Key Files:


Layer 6: Hard Controls (SAFETY)

Purpose: Enforce mandatory safety limits.

Location: /controls/

Components:

from controls import get_hard_controls

controls = get_hard_controls()

# Validate position size (enforces 2-10% rule)
valid, error = controls.validate_position_size(
    user_id="user_123",
    position_size_usd=50.0,
    account_balance=1000.0
)

# Trigger kill switch
controls.trigger_user_kill_switch(
    user_id="user_123",
    reason="Excessive losses detected"
)

# Global kill switch (stops ALL trading)
controls.trigger_global_kill_switch(
    reason="Market emergency"
)

Hard Limits:

Kill Switches:

Key Files:


Backend Structure

Directory Organization

/
β”œβ”€β”€ api_gateway.py              # Main API gateway (FastAPI)
β”œβ”€β”€ api_server.py               # Legacy API server
β”œβ”€β”€ fastapi_backend.py          # FastAPI backend
β”œβ”€β”€ user_control.py             # User control backend
β”œβ”€β”€ monetization_engine.py      # Subscription & billing
β”‚
β”œβ”€β”€ core/                       # Layer 1: Private strategy logic
β”‚   └── (strategy files - private)
β”‚
β”œβ”€β”€ execution/                  # Layer 2: Execution engine
β”‚   β”œβ”€β”€ __init__.py            # Execution router
β”‚   └── broker_adapter.py      # Secure broker wrapper
β”‚
β”œβ”€β”€ ui/                        # Layer 3: User interface
β”‚   β”œβ”€β”€ __init__.py           # Dashboard API
β”‚   └── (UI components)
β”‚
β”œβ”€β”€ auth/                      # Authentication & API key management
β”‚   β”œβ”€β”€ __init__.py           # User manager
β”‚   └── user_database.py      # User data storage
β”‚
β”œβ”€β”€ config/                    # User configuration
β”‚   └── __init__.py           # Config manager
β”‚
β”œβ”€β”€ controls/                  # Hard safety controls
β”‚   └── __init__.py           # Kill switches & limits
β”‚
β”œβ”€β”€ database/                  # Database layer
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ db_connection.py      # PostgreSQL connection
β”‚   └── models.py             # SQLAlchemy models
β”‚
β”œβ”€β”€ bot/                       # Core trading engine
β”‚   β”œβ”€β”€ nija_apex_strategy_v72_upgrade.py  # Main strategy
β”‚   β”œβ”€β”€ broker_manager.py                   # Broker management
β”‚   β”œβ”€β”€ broker_integration.py               # Exchange integrations
β”‚   β”œβ”€β”€ execution_intelligence.py           # Execution optimization
β”‚   β”œβ”€β”€ risk_manager.py                     # Risk management
β”‚   β”œβ”€β”€ meta_ai/                            # Meta-AI engine
β”‚   β”œβ”€β”€ mmin/                               # Multi-market intelligence
β”‚   β”œβ”€β”€ gmig/                               # Global macro intelligence
β”‚   └── (other modules)
β”‚
β”œβ”€β”€ frontend/                  # Web dashboard (React)
β”‚   β”œβ”€β”€ static/
β”‚   └── templates/
β”‚
β”œβ”€β”€ mobile/                    # Mobile apps (React Native)
β”‚   β”œβ”€β”€ ios/
β”‚   β”œβ”€β”€ android/
β”‚   └── (app source)
β”‚
β”œβ”€β”€ scripts/                   # Utility scripts
β”‚   └── (various scripts)
β”‚
β”œβ”€β”€ k8s/                       # Kubernetes manifests
β”‚   β”œβ”€β”€ base/
β”‚   └── components/
β”‚
β”œβ”€β”€ docker-compose.yml         # Local development stack
β”œβ”€β”€ Dockerfile.api             # API container
β”œβ”€β”€ Dockerfile.dashboard       # Dashboard container
β”œβ”€β”€ Dockerfile.gateway         # Gateway container
β”‚
└── requirements.txt           # Python dependencies

Service Architecture

Microservices Breakdown

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    NIJA Platform Services                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1. API Gateway Service (Port 8000)
   - Entry point for all API requests
   - JWT authentication
   - Rate limiting
   - Request routing

2. Trading Engine Service (Port 8001)
   - Core APEX strategy execution
   - Market scanning (732+ pairs)
   - Signal generation
   - Trade execution

3. Dashboard API Service (Port 8002)
   - User statistics
   - Position tracking
   - Performance analytics
   - Settings management

4. Subscription Service (Port 8003)
   - Stripe integration
   - Tier management
   - Usage tracking
   - Billing

5. WebSocket Service (Port 8004)
   - Real-time position updates
   - Trade notifications
   - Market data streaming
   - Chat support

6. Webhook Service (Port 5000)
   - TradingView webhooks
   - Payment webhooks (Stripe)
   - Exchange webhooks

7. Admin API Service (Port 8005)
   - User management
   - System monitoring
   - Kill switches
   - Audit logs

Database Schema

See DATABASE_SCHEMA.md for detailed schema.

Core Tables:


API Routes

See API_ROUTES.md for complete API specification.

Authentication Endpoints

POST   /api/v1/auth/register        - Register new user
POST   /api/v1/auth/login           - User login (returns JWT)
POST   /api/v1/auth/logout          - User logout
POST   /api/v1/auth/refresh         - Refresh JWT token
POST   /api/v1/auth/reset-password  - Password reset request
PUT    /api/v1/auth/change-password - Change password

Trading Endpoints

POST   /api/v1/trading/start        - Start trading bot
POST   /api/v1/trading/stop         - Stop trading bot
GET    /api/v1/trading/status       - Get bot status
POST   /api/v1/trading/emergency-stop - Emergency kill switch

Account Endpoints

GET    /api/v1/account/balance      - Get account balance
GET    /api/v1/account/positions    - Get active positions
GET    /api/v1/account/history      - Get trade history
GET    /api/v1/account/performance  - Get performance metrics
GET    /api/v1/account/stats        - Get trading statistics

Configuration Endpoints

GET    /api/v1/config/settings      - Get user settings
PUT    /api/v1/config/settings      - Update settings
GET    /api/v1/config/brokers       - Get configured brokers
POST   /api/v1/config/brokers       - Add broker API keys
DELETE /api/v1/config/brokers/:id   - Remove broker

Subscription Endpoints

GET    /api/v1/subscription/plans   - Get available plans
GET    /api/v1/subscription/current - Get current subscription
POST   /api/v1/subscription/upgrade - Upgrade subscription
POST   /api/v1/subscription/cancel  - Cancel subscription
GET    /api/v1/subscription/usage   - Get usage statistics

Admin Endpoints (Protected)

GET    /api/v1/admin/users          - List all users
GET    /api/v1/admin/users/:id      - Get user details
PUT    /api/v1/admin/users/:id      - Update user
DELETE /api/v1/admin/users/:id      - Delete user
POST   /api/v1/admin/kill-switch    - Trigger global kill switch
GET    /api/v1/admin/metrics        - System metrics
GET    /api/v1/admin/audit-logs     - Audit logs

WebSocket Endpoints

WS     /ws/positions                - Real-time position updates
WS     /ws/trades                   - Real-time trade notifications
WS     /ws/market-data              - Live market data
WS     /ws/notifications            - General notifications

Dashboard Layout

See DASHBOARD_DESIGN.md for detailed UI/UX specification.

Web Dashboard Components

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  HEADER                                                      β”‚
β”‚  [NIJA Logo]  Dashboard  Trading  Analytics  Settings  [πŸ‘€] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚  OVERVIEW CARDS                                         β”‚
β”‚ β”‚                                                         β”‚
β”‚ β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚  β”‚ Balance  β”‚  β”‚   P&L    β”‚  β”‚ Win Rate β”‚  β”‚ Active  β”‚β”‚
β”‚ β”‚  β”‚ $10,450  β”‚  β”‚ +$1,234  β”‚  β”‚   68%    β”‚  β”‚ Trades  β”‚β”‚
β”‚ β”‚  β”‚  β–² 2.5%  β”‚  β”‚  β–² 15%   β”‚  β”‚  β–² 3%   β”‚  β”‚    3    β”‚β”‚
β”‚ β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚  EQUITY CURVE CHART                                     β”‚
β”‚ β”‚  [Interactive line chart showing account growth]        β”‚
β”‚ β”‚  $12k ─                                        ╭──      β”‚
β”‚ β”‚       β”‚                              ╭────────╯        β”‚
β”‚ β”‚  $10k ─                     ╭────────╯                 β”‚
β”‚ β”‚       β”‚           ╭─────────╯                          β”‚
β”‚ β”‚   $8k ─  ╭────────╯                                    β”‚
β”‚ β”‚       └──┴────┴────┴────┴────┴────┴────┴────┴─────    β”‚
β”‚ β”‚        Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep    β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚  ACTIVE POSITIONS                                       β”‚
β”‚ β”‚  β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚  β”‚ ID  β”‚ Symbol  β”‚ Side β”‚ Size β”‚  P&L β”‚ Entry  β”‚ ... β”‚β”‚
β”‚ β”‚  β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”‚
β”‚ β”‚  β”‚ 001 β”‚ BTC-USD β”‚ LONG β”‚ $500 β”‚ +$45 β”‚ 43210  β”‚ ... β”‚β”‚
β”‚ β”‚  β”‚ 002 β”‚ ETH-USD β”‚ LONG β”‚ $300 β”‚ +$28 β”‚ 2345   β”‚ ... β”‚β”‚
β”‚ β”‚  β”‚ 003 β”‚ SOL-USD β”‚ LONG β”‚ $200 β”‚ -$12 β”‚ 98.5   β”‚ ... β”‚β”‚
β”‚ β”‚  β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚  RECENT TRADES                                          β”‚
β”‚ β”‚  [Scrollable list of recent closed trades]             β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Mobile App Screens

1. Home Screen

2. Positions Screen

3. Analytics Screen

4. Settings Screen


Subscription Logic

See SUBSCRIPTION_SYSTEM.md for detailed specification.

Subscription Tiers

FREE_TIER = {
    'name': 'Free',
    'price_monthly': 0,
    'price_yearly': 0,
    'features': [
        'Paper trading only',
        'Basic strategy (APEX V7.2)',
        '1 exchange connection',
        'Community support',
        'Basic analytics'
    ],
    'limits': {
        'max_position_size_usd': 0,  # Paper only
        'max_positions': 3,
        'max_daily_trades': 10,
        'api_calls_per_minute': 10
    }
}

BASIC_TIER = {
    'name': 'Basic',
    'price_monthly': 49,
    'price_yearly': 470,  # ~20% discount
    'features': [
        'Live trading',
        'APEX V7.2 strategy',
        '2 exchange connections',
        'Email support',
        'Standard analytics',
        'Mobile app access'
    ],
    'limits': {
        'max_position_size_usd': 500,
        'max_positions': 5,
        'max_daily_trades': 30,
        'api_calls_per_minute': 30
    }
}

PRO_TIER = {
    'name': 'Pro',
    'price_monthly': 149,
    'price_yearly': 1430,  # ~20% discount
    'features': [
        'All Basic features',
        'Meta-AI optimization',
        'MMIN multi-market intelligence',
        '5 exchange connections',
        'Priority support',
        'Advanced analytics',
        'Custom risk profiles',
        'TradingView integration'
    ],
    'limits': {
        'max_position_size_usd': 2000,
        'max_positions': 10,
        'max_daily_trades': 100,
        'api_calls_per_minute': 100
    }
}

ENTERPRISE_TIER = {
    'name': 'Enterprise',
    'price_monthly': 499,
    'price_yearly': 4790,  # ~20% discount
    'features': [
        'All Pro features',
        'GMIG macro intelligence',
        'Unlimited exchanges',
        'Dedicated support',
        'Custom strategy tuning',
        'API access',
        'White-label option',
        'Multi-account management'
    ],
    'limits': {
        'max_position_size_usd': 10000,
        'max_positions': 50,
        'max_daily_trades': 500,
        'api_calls_per_minute': 500
    }
}

Subscription Flow

User Registration
       ↓
14-Day Free Trial (Pro features)
       ↓
Trial Expiration
       ↓
β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
β”‚             β”‚
Choose Tier   ↓
       ↓   Downgrade to Free
Enter Payment (Stripe)
       ↓
Active Subscription
       ↓
Usage Tracking
       ↓
Monthly/Yearly Renewal

Stripe Integration

from monetization_engine import SubscriptionEngine
import stripe

stripe.api_key = os.getenv('STRIPE_SECRET_KEY')
sub_engine = SubscriptionEngine()

# Create subscription
subscription = sub_engine.create_subscription(
    user_id="user_123",
    tier="pro",
    interval="monthly",
    payment_method_id="pm_123"
)

# Handle webhook events
@app.post("/webhooks/stripe")
async def stripe_webhook(request: Request):
    event = stripe.Webhook.construct_event(
        payload=await request.body(),
        sig_header=request.headers.get('stripe-signature'),
        secret=os.getenv('STRIPE_WEBHOOK_SECRET')
    )

    if event['type'] == 'invoice.payment_succeeded':
        # Activate subscription
        sub_engine.activate_subscription(event['data']['object'])

    elif event['type'] == 'invoice.payment_failed':
        # Suspend account
        sub_engine.suspend_subscription(event['data']['object'])

    return {'status': 'success'}

Scaling Blueprint

See SCALING_BLUEPRINT.md for detailed scaling strategy.

Horizontal Scaling

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               Load Balancer (Nginx/HAProxy)              β”‚
β”‚                    SSL Termination                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚            β”‚            β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”  β”Œβ”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
β”‚  API Pod  β”‚  β”‚ API Pod  β”‚  β”‚ API Pod  β”‚
β”‚  Instance β”‚  β”‚ Instance β”‚  β”‚ Instance β”‚
β”‚     1     β”‚  β”‚     2    β”‚  β”‚     3    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚            β”‚            β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚            β”‚            β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”  β”Œβ”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
β”‚  Trading  β”‚  β”‚ Trading  β”‚  β”‚ Trading  β”‚
β”‚  Engine   β”‚  β”‚ Engine   β”‚  β”‚ Engine   β”‚
β”‚  Worker 1 β”‚  β”‚ Worker 2 β”‚  β”‚ Worker 3 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Vertical Scaling

Compute Resources:

Auto-scaling Triggers:

Database Scaling

PostgreSQL

Redis

TimescaleDB

Caching Strategy

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Cache Layers                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Level 1: Browser Cache
- Static assets (JS, CSS, images)
- Cache duration: 7 days

Level 2: CDN Cache (Cloudflare)
- API responses (GET only)
- User settings
- Cache duration: 5 minutes

Level 3: Redis Cache
- User sessions (JWT tokens)
- Active positions
- Market data
- Cache duration: 30 seconds - 5 minutes

Level 4: Database Query Cache
- PostgreSQL query cache
- Recent trade history
- User statistics

Load Balancing Strategy

# Round-robin for API requests
api_instances = [
    'api-1.nija.com',
    'api-2.nija.com',
    'api-3.nija.com'
]

# Least-connections for WebSocket
websocket_instances = [
    'ws-1.nija.com',
    'ws-2.nija.com'
]

# User-based sharding for trading engines
def get_trading_engine(user_id: str) -> str:
    shard = hash(user_id) % NUM_TRADING_ENGINES
    return f'trading-engine-{shard}'

Geographic Distribution

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Global Distribution                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Primary Region: US-East (Virginia)
- API Gateway
- Trading Engines
- Database (Master)

Secondary Region: US-West (Oregon)
- API Gateway (failover)
- Database (Read Replica)

Tertiary Region: EU-West (Ireland)
- API Gateway (EU users)
- Database (Read Replica)

Edge Locations (Cloudflare CDN):
- 200+ locations worldwide
- Static asset delivery
- DDoS protection

Performance Targets

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Performance SLAs                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

API Response Time:
- p50: < 100ms
- p95: < 300ms
- p99: < 500ms

WebSocket Latency:
- Trade notifications: < 50ms
- Position updates: < 100ms

Database Queries:
- Simple reads: < 10ms
- Complex aggregations: < 100ms

Trade Execution:
- Signal to order: < 500ms
- Order confirmation: < 2s (depends on exchange)

Uptime:
- Target: 99.9% (8.7 hours downtime/year)
- Monitoring: 24/7 automated alerts

Security Architecture

Defense in Depth

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 Security Layers                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Layer 1: Network Security
- DDoS protection (Cloudflare)
- WAF (Web Application Firewall)
- IP whitelisting for admin endpoints
- Rate limiting (tier-based)

Layer 2: Authentication & Authorization
- JWT tokens (HS256 signing)
- Refresh token rotation
- Role-based access control (RBAC)
- Multi-factor authentication (optional)

Layer 3: Application Security
- Input validation (Pydantic)
- SQL injection prevention (SQLAlchemy ORM)
- XSS protection (React escaping)
- CSRF tokens for state-changing operations

Layer 4: Data Security
- Encryption at rest (AES-256)
- Encryption in transit (TLS 1.3)
- API key encryption (Fernet)
- Secure credential storage (HashiCorp Vault)

Layer 5: Audit & Monitoring
- Comprehensive audit logging
- Anomaly detection
- Security alerts (Slack, PagerDuty)
- Regular security audits

Encryption

# API Key Encryption (Fernet)
from cryptography.fernet import Fernet

key = Fernet.generate_key()
cipher = Fernet(key)

# Encrypt API key
encrypted_key = cipher.encrypt(api_key.encode())

# Decrypt API key
decrypted_key = cipher.decrypt(encrypted_key).decode()

# Database Encryption (at rest)
# PostgreSQL: pgcrypto extension
# Redis: redis-encryption module

# TLS/SSL (in transit)
# All HTTPS traffic uses TLS 1.3
# Certificate management: Let's Encrypt

Rate Limiting

from fastapi import FastAPI, Request
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address

limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter

# Tier-based rate limits
@app.get("/api/v1/account/balance")
@limiter.limit("30/minute")  # Basic tier
async def get_balance(request: Request):
    # Check user tier
    tier = get_user_tier(request)

    if tier == "pro":
        # 100/minute for Pro
        pass
    elif tier == "enterprise":
        # 500/minute for Enterprise
        pass

    return {"balance": 10000.0}

Deployment Strategy

Development Environment

# Local development with Docker Compose
docker-compose up -d

# Services:
# - PostgreSQL (localhost:5432)
# - Redis (localhost:6379)
# - API Gateway (localhost:8000)
# - Dashboard (localhost:3000)

# Hot reload enabled for development

Staging Environment

# Kubernetes cluster on Railway/Render
kubectl apply -f k8s/staging/

# Features:
# - Mirrors production architecture
# - Uses separate database
# - Automated testing
# - Preview deployments for PRs

Production Environment

# Kubernetes cluster on AWS EKS / GCP GKE
kubectl apply -f k8s/production/

# Features:
# - Multi-region deployment
# - Auto-scaling
# - Load balancing
# - Monitoring & alerting
# - Automated backups

CI/CD Pipeline

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   CI/CD Workflow                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Code Push (GitHub)
       ↓
Automated Tests (GitHub Actions)
 - Unit tests
 - Integration tests
 - Security scans (CodeQL)
       ↓
Build Docker Images
       ↓
Push to Container Registry
       ↓
Deploy to Staging
       ↓
Automated E2E Tests
       ↓
Manual Approval
       ↓
Blue-Green Deployment to Production
       ↓
Health Checks
       ↓
Rollback if Failed

Kubernetes Configuration

# Example deployment manifest
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nija-api-gateway
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nija-api-gateway
  template:
    metadata:
      labels:
        app: nija-api-gateway
    spec:
      containers:
      - name: api
        image: nija/api-gateway:v2.0
        ports:
        - containerPort: 8000
        env:
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: nija-secrets
              key: database-url
        resources:
          requests:
            memory: "2Gi"
            cpu: "1"
          limits:
            memory: "4Gi"
            cpu: "2"
        livenessProbe:
          httpGet:
            path: /health
            port: 8000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /ready
            port: 8000
          initialDelaySeconds: 5
          periodSeconds: 5

Monitoring & Observability

Metrics Collection

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                Monitoring Stack                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Prometheus
- Metrics collection (15s intervals)
- Time-series database
- Alerting rules

Grafana
- Dashboard visualization
- Multi-source aggregation
- Custom alerts

ELK Stack (Elasticsearch, Logstash, Kibana)
- Centralized logging
- Log aggregation
- Search & analytics

Jaeger / OpenTelemetry
- Distributed tracing
- Request flow visualization
- Performance profiling

Key Metrics

# Application Metrics
app_requests_total = Counter('app_requests_total', 'Total requests')
app_request_duration = Histogram('app_request_duration_seconds', 'Request duration')
app_errors_total = Counter('app_errors_total', 'Total errors')

# Trading Metrics
trades_executed_total = Counter('trades_executed_total', 'Total trades')
trade_pnl = Gauge('trade_pnl_usd', 'Current P&L')
positions_active = Gauge('positions_active', 'Active positions')

# System Metrics
cpu_usage = Gauge('cpu_usage_percent', 'CPU usage')
memory_usage = Gauge('memory_usage_bytes', 'Memory usage')
db_connections = Gauge('db_connections', 'Database connections')

Alerts

# Prometheus alerting rules
groups:
  - name: nija_alerts
    rules:
      # High error rate
      - alert: HighErrorRate
        expr: rate(app_errors_total[5m]) > 0.05
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High error rate detected"

      # API latency
      - alert: HighLatency
        expr: histogram_quantile(0.95, app_request_duration_seconds) > 0.5
        for: 5m
        labels:
          severity: warning

      # Database connections
      - alert: DatabaseConnectionsHigh
        expr: db_connections > 80
        for: 5m
        labels:
          severity: critical

      # Trading stopped
      - alert: TradingStopped
        expr: rate(trades_executed_total[15m]) == 0
        for: 15m
        labels:
          severity: warning

Logging

import logging
import structlog

# Structured logging
logger = structlog.get_logger()

logger.info(
    "trade_executed",
    user_id="user_123",
    symbol="BTC-USD",
    side="buy",
    size_usd=100.0,
    price=43210.50,
    trade_id="trade_456"
)

# Log levels:
# - DEBUG: Detailed debugging information
# - INFO: General informational messages
# - WARNING: Warning messages (potential issues)
# - ERROR: Error messages (failures)
# - CRITICAL: Critical issues (system failures)

Summary

Architecture Highlights

βœ… Secure Multi-User Platform

βœ… Scalable Infrastructure

βœ… Advanced Trading System

βœ… SaaS Monetization

βœ… Comprehensive Monitoring


Next Steps

Implementation Roadmap

Phase 1: Foundation (Weeks 1-2)

Phase 2: Core Services (Weeks 3-4)

Phase 3: Frontend (Weeks 5-6)

Phase 4: Testing & Security (Weeks 7-8)

Phase 5: Launch (Week 9)


Platform Architecture Documents (This PR):

Existing Documentation:

Planned Documentation:


Version: 2.0 Last Updated: January 29, 2026 Status: βœ… Production-Ready Architecture Maintained By: NIJA Engineering Team