Skip to content

Latest commit

 

History

History
75 lines (60 loc) · 3.4 KB

File metadata and controls

75 lines (60 loc) · 3.4 KB

System Architecture (SaaS v9)

NetTrader is built using a Clean Architecture / Domain-Driven Design (DDD) approach, now fully evolved into a multi-tenant SaaS platform.

Layered Architecture

  1. Domain Layer (NetTrader.Domain)

    • Core entities: User, TradeSession, UserApiKey.
    • Business logic abstractions and Enums.
    • Zero external dependencies.
  2. Application Layer (NetTrader.Application)

    • Orchestrates multi-tenant trading cycles.
    • GridTradingManager: Handles grid orchestration and order execution logic.
    • IndicatorEnrichmentService: Computes MTF technical indicators.
  3. Infrastructure Layer (NetTrader.Infrastructure)

    • Exchanges: User-scoped OrderExecutorFactory decrypts keys and creates IOrderExecutor instances for Binance/Bybit.
    • AI/ML: GeminiAdvisor (LLM-based strategy) and MLSignalService (ML.NET inference).
    • Persistence: EF Core with PostgreSQL.
  4. Presentation/Worker Layer (NetTrader.Api & NetTrader.Worker)

    • REST API: Secure endpoints for user management, settings, and trading control.
    • SignalR: Real-time data streaming to frontend.
    • Background Workers: TradingBotWorker (Main loop) and TelegramListenerWorker.

Architecture Overview

graph TD
    A[REST API / SignalR] --> B[Application Layer]
    B --> C[TradingBotWorker]
    C --> D{Cycle Type}
    D -->|Fast 10s| E[ROI Monitor & Trailing Stop]
    D -->|ML 60s| F[ML.NET Sniper Signals]
    D -->|AI 30m| G[Gemini 1.5 Pro Advisory]
    E & F & G --> H[OrderExecutorFactory]
    H --> I[AES-256 Key Decrypt]
    I --> J[Binance / Bybit API]
    B --> K[(PostgreSQL)]
Loading

Multi-Tenant Trading Loop

The TradingBotWorker executes a sequential loop over all active, subscribed users to ensure isolation and prevent API rate limiting.

graph TD
    A[Start Cycle] --> B{Manual Trigger?}
    B -- Yes --> C[Targeted User Cycle]
    B -- No --> D{Global Interval?}
    D -- Yes --> E[Fetch Market Data & ML Signals]
    E --> F[Foreach Active User]
    F --> G[Create Scoped OrderExecutor]
    G --> H[Fetch Balance & Active Sessions]
    H --> I[AI Advisory & ML Confluence]
    I --> J[Update Grids & Place Orders]
    J --> K[Wait 3-5s for Rate Limiting]
    K --> L{More Users?}
    L -- Yes --> F
    L -- No --> M[End Cycle]
    D -- No --> N[Fast Monitoring Loop]
Loading

Core Components

  • UserExchangeExecutorFactory: The security gateway. It fetches encrypted API keys from the DB, decrypts them using a system-level ENCRYPTION_KEY, and provides a transient IOrderExecutor for the current user's operation.
  • GridMathCalculator: Implements Nash Equilibrium position sizing. It analyzes the Volume Profile to place orders at High Volume Nodes (HVNs), ensuring trades are executed near peak liquidity.
  • MLSignalService: Uses a PredictionEnginePool to run ML.NET models. It applies Bayesian inference to adjust confidence thresholds based on current market volatility and ADX trend strength.

Technical Constraints & Guardrails

  • Sequential Execution: Users are processed one-by-one with an explicit delay to prevent Gemini API 429 errors and exchange rate-limit penalties.
  • Memory Management: Use of StringBuilder with capacity and AsNoTracking() in EF Core for high-frequency loops.
  • Fail-Fast DI: The system refuses to start if critical security configurations (like Encryption__Key) are missing.