Skip to content

Production-quality order execution and risk management system built with Java Spring Boot. Features REST API, JPA/Hibernate, Docker support, and comprehensive testing.

License

Notifications You must be signed in to change notification settings

fizzexual/order-execution-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Š Order Execution & Risk Management System

A production-quality order execution platform built with Java Spring Boot, demonstrating real-world trading system architecture with comprehensive risk management, transaction handling, and audit trails.

Java Spring Boot License Build

🎯 Overview

This system simulates a stock trading platform's order execution engine, handling order creation, validation, execution, and comprehensive audit logging. Built with enterprise-grade architecture and best practices, it showcases modern Java backend development skills.

Key Features

  • πŸ”„ Real-time Order Execution - Market and limit orders with instant processing
  • πŸ’° Account Management - Multi-account support with balance tracking and validation
  • πŸ›‘οΈ Risk Management - Pre-trade validation, balance checks, and account status verification
  • πŸ“Š Audit Trail - Complete execution history with detailed logging
  • 🌐 Web Dashboard - Modern UI for order management and monitoring
  • πŸ”Œ RESTful API - Clean REST endpoints with proper HTTP semantics
  • πŸ§ͺ Comprehensive Testing - Unit and integration tests with 70%+ coverage
  • 🐳 Docker Ready - Containerized deployment with Docker Compose

πŸ—οΈ Architecture

Technology Stack

Layer Technology
Backend Java 17, Spring Boot 3.2
Data Access Spring Data JPA, Hibernate
Database H2 (dev), PostgreSQL (prod)
Frontend HTML5, CSS3, Vanilla JavaScript
Testing JUnit 5, Mockito, Spring Boot Test
Build Maven
Containerization Docker, Docker Compose

Layered Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Controllers (REST API Layer)      β”‚  ← HTTP Endpoints
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Services (Business Logic)         β”‚  ← Order Execution, Validation
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Repositories (Data Access)        β”‚  ← JPA/Hibernate
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Entities (Domain Model)           β”‚  ← User, Account, Order, Log
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Domain Model

User (1) ──→ (N) Account (1) ──→ (N) Order (1) ──→ (N) ExecutionLog

πŸš€ Quick Start

Prerequisites

  • Java 17 or higher
  • Maven 3.6+
  • Docker (optional)

Running Locally

git clone https://github.com/yourusername/order-execution-system.git
cd order-execution-system

# Build the project
mvn clean install

# Run the application
mvn spring-boot:run

The application will start on http://localhost:8080

Access Points

Service URL Description
Web UI http://localhost:8080 Interactive dashboard
REST API http://localhost:8080/api API endpoints
H2 Console http://localhost:8080/h2-console Database viewer
Health Check http://localhost:8080/actuator/health Application status

H2 Console Credentials:

  • JDBC URL: jdbc:h2:mem:orderdb
  • Username: sa
  • Password: (leave empty)

πŸ“‘ API Documentation

Orders API

Create Order

POST /api/orders
Content-Type: application/json

{
  "accountId": 1,
  "symbol": "AAPL",
  "type": "MARKET",
  "side": "BUY",
  "quantity": 100
}

Get All Orders

GET /api/orders

Get Order by ID

GET /api/orders/{id}

Cancel Order

PUT /api/orders/{id}/cancel

Accounts API

Get All Accounts

GET /api/accounts

Get Account by ID

GET /api/accounts/{id}

Execution Logs API

Get Execution Logs

GET /api/execution-logs

Get Logs by Order

GET /api/execution-logs/order/{orderId}

πŸ§ͺ Testing

Run All Tests

mvn test

Test Coverage

  • Unit Tests: Service layer business logic
  • Integration Tests: Repository queries and controller endpoints
  • Coverage: 70%+ across all layers

Example Test Scenarios

  • βœ… Order creation with valid account
  • βœ… Order rejection with insufficient balance
  • βœ… Account status validation
  • βœ… Transaction rollback on failure
  • βœ… Execution log creation

🐳 Docker Deployment

Using Docker Compose

# Run with H2 (Development)
docker-compose up app-dev

# Run with PostgreSQL (Production)
docker-compose up postgres app-prod

Build Docker Image

docker build -t order-execution-system:latest .

οΏ½ Database Schema

Tables

users

  • Stores user information and status
  • Indexes: email, status

accounts

  • Trading accounts with balance tracking
  • Indexes: user_id, account_number, status

orders

  • Order details and execution status
  • Indexes: account_id, symbol, status, created_at

execution_logs

  • Audit trail of all executions
  • Indexes: order_id, executed_at

🎨 Web Dashboard Features

The included web UI provides:

  • πŸ“Š Real-time Dashboard - View accounts, orders, and execution logs
  • πŸ“ Order Creation - Interactive form for placing orders
  • πŸ”„ Auto-refresh - Updates every 10 seconds
  • 🎨 Responsive Design - Works on desktop and mobile
  • βœ… Status Indicators - Color-coded order and account statuses

πŸ”§ Configuration

Application Profiles

Development (default)

spring.profiles.active=dev
# Uses H2 in-memory database
# Auto-creates schema
# Initializes test data

Production

spring.profiles.active=prod
# Uses PostgreSQL
# Requires database setup
# No test data initialization

Environment Variables

Variable Description Default
DB_USERNAME Database username postgres
DB_PASSWORD Database password postgres
SPRING_PROFILES_ACTIVE Active profile dev

οΏ½ Project Structure

order-execution-system/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main/
β”‚   β”‚   β”œβ”€β”€ java/com/portfolio/orderexecution/
β”‚   β”‚   β”‚   β”œβ”€β”€ config/          # Configuration classes
β”‚   β”‚   β”‚   β”œβ”€β”€ controller/      # REST controllers
β”‚   β”‚   β”‚   β”œβ”€β”€ dto/             # Data Transfer Objects
β”‚   β”‚   β”‚   β”œβ”€β”€ entity/          # JPA entities
β”‚   β”‚   β”‚   β”œβ”€β”€ exception/       # Exception handling
β”‚   β”‚   β”‚   β”œβ”€β”€ mapper/          # Entity-DTO mappers
β”‚   β”‚   β”‚   β”œβ”€β”€ repository/      # Data access layer
β”‚   β”‚   β”‚   └── service/         # Business logic
β”‚   β”‚   └── resources/
β”‚   β”‚       β”œβ”€β”€ static/          # Web UI files
β”‚   β”‚       └── application.yml  # Configuration
β”‚   └── test/                    # Test classes
β”œβ”€β”€ docker-compose.yml           # Docker setup
β”œβ”€β”€ Dockerfile                   # Container image
β”œβ”€β”€ pom.xml                      # Maven dependencies
└── README.md                    # This file

πŸŽ“ Key Design Decisions

1. Layered Architecture

Clean separation of concerns with distinct layers for presentation, business logic, and data access.

2. DTO Pattern

Separate API contracts from domain models for flexibility and security.

3. Transaction Management

ACID compliance for financial operations using Spring's @Transactional.

4. Exception Handling

Global exception handler providing consistent error responses.

5. Database Indexing

Strategic indexes on frequently queried columns for optimal performance.

6. Immediate Execution

Orders execute synchronously for simplicity (can be extended to async).

πŸš€ Future Enhancements

  • Spring Security with JWT authentication
  • WebSocket for real-time order updates
  • Advanced order matching engine
  • Position tracking and portfolio management
  • Market data integration
  • Performance metrics and monitoring
  • Database migration with Flyway/Liquibase

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘€ Author

Fizzexual

πŸ™ Acknowledgments

  • Built with Spring Boot
  • Inspired by real-world trading systems
  • Created as a portfolio demonstration project

⭐ Star this repository if you find it helpful!

This is a demonstration project showcasing backend development skills. It is not intended for production trading.

About

Production-quality order execution and risk management system built with Java Spring Boot. Features REST API, JPA/Hibernate, Docker support, and comprehensive testing.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published