System Architecture
Built with Domain-Driven Design, clean architecture, and production-grade engineering practices
Architecture Overview
Presentation Layer
Landing Page
Next.js + Vercel
Dashboard
React + Vite
Application Layer
REST API
FastAPI
RL Engine
PPO/A2C/SAC
Risk Management
Domain Services
Infrastructure
Railway
Deployment
Market Data
Tiingo API
Storage
PostgreSQL + Redis
Domain-Driven Design
The system follows DDD principles with clear bounded contexts and separation of concerns:
Trading Context
- • Position Aggregate (entry, exit, P&L)
- • Trade Domain Events
- • Portfolio Value Objects
- • Trading Rules & Policies
RL Context
- • Model Training Services
- • Prediction Aggregates
- • Environment Simulations
- • Reward Calculations
Risk Context
- • Stop-Loss Policies
- • Position Sizing (Kelly)
- • Drawdown Management
- • Risk Metrics Tracking
Market Context
- • Price Data Services
- • Technical Indicators
- • Market Regime Detection
- • Volatility Analysis
RL Pipeline
1
Data Collection
Historical price data, technical indicators, market regime features
2
Environment Setup
Custom Gym environment with realistic trading simulation
3
Model Training
PPO/A2C/SAC with Transformer/CNN-LSTM/MLP networks
4
Backtesting
Walk-forward validation with out-of-sample testing
5
Production Deployment
Railway deployment with FastAPI serving predictions
Risk Management System
Stop-Loss
Dynamic tiered stops: 7-11% based on volatility
Position Sizing
Kelly criterion for optimal bet sizing
Time Limits
Max 22-day hold period to limit exposure
API Example
The backend exposes a RESTful API for predictions and trading operations:
curl -X POST \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"symbol": "NEM"}' \
https://aistock-production-2561.up.railway.app/api/v1/rl/predict
Response:
{
"action_type": "BUY",
"position_size": 0.65,
"expected_return": 0.082,
"volatility": 0.285,
"risk_level": "moderate",
"approved": true,
"metadata": {
"current_price": 45.17,
"stop_loss": 41.95,
"take_profit": 50.59
}
}