Trading System Documentation¶
A Production-Grade Algorithmic Trading System¶
Built with Python, PostgreSQL, Redis, and Modern Web Technologies
Technology Stack¶
🚀 Quick Start¶
Get your trading system up and running in minutes:
# Clone and setup
git clone https://github.com/nishantnayar/trading-system.git
cd trading-system
# Activate your existing conda environment
conda activate your-environment-name
# Install dependencies
pip install -r requirements.txt
# Configure and run
cp deployment/env.example .env
python scripts/setup_databases.py
python main.py
Access your Streamlit UI: http://localhost:8501
📋 What You'll Find Here¶
Getting Started¶
- Getting Started Guide - Complete setup and configuration guide
User Guide¶
- Dashboard Overview - Navigate the interface
- Stock Screener - AI-powered stock screening
- Trading Operations - Execute trades
- Strategy Management - Create and manage strategies
- Risk Management - Control your exposure
API Reference¶
- Data Ingestion - Market data processing
- Strategy Engine - Algorithm execution
- Execution Engine - Order management
- Risk Management - Risk controls
- Analytics - Performance metrics
Development¶
- System Architecture - Complete system design
- Stock Screener Architecture - Screener technical documentation
- Database Architecture - Database design and implementation
- Database Setup - Step-by-step database setup guide
- Logging Architecture - Detailed logging strategy
- Testing Strategy - Comprehensive testing approach
- CI/CD Pipeline - Continuous integration and deployment
🏗️ System Architecture¶
graph LR
A[Data Sources] --> B[Data Ingestion]
B --> C[Core Services]
C --> D[Frontend]
B --> E[PostgreSQL]
C --> E
The Trading System follows a modular, service-oriented architecture:
Data Flow: - Data Sources → Polygon.io, Yahoo Finance, Alpaca API - Data Ingestion Service → Processes and stores market data - Core Services → Strategy Engine, Risk Management, Execution, Analytics - Storage → PostgreSQL (data persistence) and Redis (caching) - Orchestration → Prefect workflows for scheduled tasks - Frontend → Streamlit UI and FastAPI REST API
Service Interactions: - Data Ingestion feeds data to Core Services - Strategy Engine → Risk Management → Execution pipeline - All services store data in PostgreSQL - Analytics service provides metrics to the UI - Prefect orchestrates scheduled data ingestion and analytics workflows
For detailed architecture documentation, see System Architecture.
Core Components¶
| Component | Technology | Status | Purpose |
|---|---|---|---|
| Data Ingestion | Python + pandas | ✅ v1.0.0 | Market data processing (Polygon.io, Yahoo Finance, Alpaca) |
| Strategy Engine | Python + Pydantic | 🚧 v1.1.0 | Algorithm execution (planned) |
| Risk Management | Python + PostgreSQL | 🚧 v1.1.0 | Risk controls (planned) |
| Execution Engine | Python + Alpaca API | ✅ v1.0.0 | Order management (account, positions, orders) |
| Analytics | Python + Plotly | ✅ v1.0.0 | Performance tracking and visualization |
| AI Services | Python + Ollama | ✅ v1.0.0 | Natural language processing for stock screener |
| Database | PostgreSQL | ✅ v1.0.0 | Data persistence |
| Cache | Redis | ✅ v1.0.0 | High-speed access (optional) |
🎯 Key Features¶
Trading Capabilities¶
- ✅ Paper Trading - Start with Alpaca paper trading (v1.0.0)
- ✅ Historical Data - Polygon.io and Yahoo Finance integration for backtesting (v1.0.0)
- ✅ AI-Powered Screener - Natural language stock screening with Ollama LLM (v1.0.0)
- 🚧 Strategy Backtesting - Test before live deployment (v1.1.0)
- 🚧 Risk Controls - Built-in risk management (v1.1.0)
- ✅ Performance Analytics - Interactive charts and metrics with Plotly (v1.0.0)
- ✅ Order Management - Account, position, and order viewing/cancellation (v1.0.0)
- 🚧 Order Placement - Place new orders via UI (v1.1.0)
Technical Features¶
- ✅ Modular Architecture - Service-oriented design ready for scaling
- ✅ Type Safety - Pydantic for data validation
- ✅ Data Processing - pandas for analytics and manipulation
- ✅ Modern UI - Streamlit + Plotly Charts + Custom CSS
- ✅ Professional Charts - Interactive financial visualizations with Plotly
- ✅ AI Integration - Local LLM support via Ollama for natural language queries
- ✅ Session State - Persistent data sharing across pages
- ✅ Comprehensive Logging - Database-first logging with PostgreSQL storage and file fallback
- ✅ Database Design - PostgreSQL with comprehensive schema
- ✅ Data Quality - Automated validation and monitoring
- ✅ Timezone Support - UTC storage with Central Time display
Development Features¶
- ✅ Code Quality - Flake8, Black, isort, mypy
- ✅ Documentation - MkDocs
- ✅ Testing - Comprehensive test coverage
- ✅ CI/CD - Automated quality checks
📊 Dashboard Preview¶
Your Streamlit trading interface provides:
- Portfolio Management - Real-time portfolio tracking and performance metrics
- Market Analysis - Interactive charts with technical indicators using Plotly
- Stock Screener - AI-powered stock screening with natural language queries (Ollama integration)
- System Information - Team details and system architecture
- Settings - User preferences and session state management
- Session State - Persistent data sharing across all pages
- Modern UI - Clean, professional Streamlit interface
- Timezone Support - All data displayed in Central Time (stored in UTC)
🔧 Configuration¶
Environment Variables¶
# Database
POSTGRES_URL=postgresql://user:pass@localhost:5432/trading
REDIS_URL=redis://localhost:6379/0
# Alpaca API
ALPACA_API_KEY=your_api_key
ALPACA_SECRET_KEY=your_secret_key
ALPACA_BASE_URL=https://paper-api.alpaca.markets
# Ollama (Optional, for AI features)
OLLAMA_BASE_URL=http://localhost:11434
# Logging (Database-first with file fallback)
LOG_LEVEL=INFO
# Logs are stored in PostgreSQL logging schema by default
# See config/logging.yaml for detailed configuration
Service Ports¶
- Streamlit UI: http://localhost:8501
- API Docs: http://localhost:8001/docs
- PostgreSQL: localhost:5432
- Redis: localhost:6379
🚨 Important Notes¶
Paper Trading First¶
- Start with Alpaca paper trading
- Test your strategies thoroughly
- Understand risk management
- Only move to live trading when ready
Risk Management¶
- Set appropriate position sizes
- Use stop-losses and take-profits
- Monitor your exposure
- Never risk more than you can afford to lose
Development¶
- Follow the coding standards
- Write comprehensive tests
- Document your changes
- Use the logging system effectively
📞 Support¶
Documentation¶
- User Documentation (MkDocs)
- System Architecture
- Database Architecture
- Database Setup
- Logging Architecture
- Testing Strategy
- CI/CD Pipeline
- API Reference
- Troubleshooting
Getting Help¶
- Check the Troubleshooting Guide
- Join the Discussions
- Create an issue on GitHub
- Contact: nishant.nayar@hotmail.com
📄 License¶
This project is licensed under the MIT License - see the LICENSE file for details.
Built by Nishant Nayar