Capstone Project 1

Stock Trading System

Build a comprehensive stock trading application using Java data structures and algorithms. You will implement optimal trading strategies using dynamic programming, analyze stock price trends with arrays and sliding windows, and create efficient data structures for portfolio management.

15-20 hours
Advanced
500 Points
What You Will Build
  • Stock price analysis system
  • Optimal buy/sell algorithms
  • Portfolio data structures
  • Trading strategy optimizer
  • Performance analysis tool
Contents
01

Project Overview

This capstone project brings together everything you have learned in the Java DSA course. You will work with real stock market data containing 10,000+ daily price records spanning multiple years (2020-2024), covering 50 stocks across various sectors (Technology, Healthcare, Finance, Energy, Consumer). Your goal is to build a professional trading analysis system that applies data structures and algorithms to solve classic stock trading problems and optimize investment strategies.

Skills Applied: This project tests your proficiency in Arrays (sliding window, two pointers), Dynamic Programming (optimal trading), Stacks (monotonic stacks for price spans), Heaps (top K performers), and Hash Maps (portfolio tracking).
Price Analysis

Arrays, sliding windows, prefix sums for trend analysis

Trading Optimization

Dynamic programming for maximum profit strategies

Data Structures

Heaps, stacks, hash maps for efficient operations

Performance

Algorithm complexity analysis and optimization

Learning Objectives

Technical Skills
  • Master dynamic programming for multi-transaction trading
  • Implement sliding window algorithms for trend detection
  • Use monotonic stacks for stock span problems
  • Build efficient hash maps for O(1) portfolio lookups
  • Apply heaps for top K stock selection
Problem-Solving Skills
  • Translate financial problems into algorithmic solutions
  • Analyze time and space complexity trade-offs
  • Optimize brute-force solutions with memoization
  • Handle edge cases in trading scenarios
  • Test algorithms with real-world data
Ready to submit? Already completed the project? Submit your work now!
Submit Now
02

Problem Scenario

AlgoTrade Capital

You have been hired as a Quantitative Developer at AlgoTrade Capital, an algorithmic trading firm that uses data structures and algorithms to make trading decisions. The firm manages portfolios across multiple sectors and needs efficient systems to analyze market data, identify optimal trading opportunities, and maximize returns while managing risk.

"We process millions of price points daily. We need algorithms that can determine the best times to buy and sell, calculate maximum profit strategies with transaction limits, and track stock performance efficiently. Can you build a trading system that solves these classic problems optimally?"

Sarah Chen, Chief Technology Officer

Problems to Solve

Single Transaction
  • Best time to buy and sell (one transaction)
  • Maximum profit with single buy/sell
  • Track minimum price seen so far
  • O(n) time complexity required
Multiple Transactions
  • Unlimited buy/sell transactions
  • Maximum profit with no transaction limit
  • Cannot hold multiple stocks at once
  • Greedy or DP approach
K Transactions
  • Maximum K buy/sell transactions
  • Dynamic programming with states
  • Space optimization techniques
  • Handle edge cases (k >= n/2)
Cooldown Period
  • Trading with cooldown constraint
  • Cannot buy immediately after sell
  • State machine approach
  • Transaction fee variations
Pro Tip: Think in terms of state transitions! Each trading problem can be modeled as states (holding stock, not holding, in cooldown) with transitions between them.
03

The Dataset

You will work with a comprehensive stock price dataset. Download the CSV files containing historical price data for analysis:

Dataset Download

Download stock price data directly from Kaggle. The dataset contains individual CSV files for each stock ticker with historical OHLCV (Open, High, Low, Close, Volume) data.

Recommended stocks to download: AAPL, GOOGL, MSFT, AMZN, TSLA, META, NVDA, JPM, V, JNJ
Original Data Source

This project uses the Huge Stock Market Dataset from Kaggle - one of the most comprehensive historical stock price datasets available. It contains daily stock prices for all companies traded on NYSE, NASDAQ, and other US exchanges with over 7,000+ stocks spanning decades of market data.

Dataset Info: 7,000+ stocks | Date Range: 1970 - Present | Daily OHLCV Data | Includes: Open, High, Low, Close, Volume | Format: Individual CSV per stock ticker
Dataset Schema

Each stock has its own file named {ticker}.us.txt with the following columns:

ColumnTypeDescription
DateDateTrading date (YYYY-MM-DD format)
OpenDecimalOpening price for the trading day
HighDecimalHighest price reached during the day
LowDecimalLowest price reached during the day
CloseDecimalClosing price for the trading day
VolumeIntegerTotal number of shares traded
OpenIntIntegerOpen interest (usually 0 for stocks)

The Kaggle dataset is organized into folders:

Stocks/
├── aapl.us.txt     # Apple Inc.
├── googl.us.txt    # Alphabet Inc.
├── msft.us.txt     # Microsoft
├── amzn.us.txt     # Amazon
├── tsla.us.txt     # Tesla
└── ... (7,000+ more)

Each file contains daily OHLCV data from the stock's IPO to 2017.

For LeetCode-style trading problems, extract the Close column as a price array:

// Example: Reading close prices from CSV
int[] prices = {7, 1, 5, 3, 6, 4}; // From Close column

// This becomes input for:
// - Best Time to Buy and Sell Stock I, II, III, IV
// - Stock with Cooldown
// - Stock with Transaction Fee
Sample Data Preview

Here is what the data looks like from aapl.us.txt (Apple stock):

DateOpenHighLowCloseVolumeOpenInt
2017-11-01166.91168.87166.01166.8936,590,5260
2017-11-02166.60168.50165.28168.1133,610,5800
2017-11-03174.00174.26171.12172.5059,398,6360
Dataset Stats: 7,000+ US stocks and ETFs | Date Range: 1970-2017 | Daily OHLCV data
For Algorithms: Extract Close column as int[] prices array for LeetCode problems
Data Quality Note: The dataset contains real market data with edge cases: stocks with flat prices, high volatility periods, stock splits, and gaps from market holidays. Your algorithms should handle these gracefully.
04

Project Requirements

Your project must include all of the following components. Structure your code with clear organization, documentation, and comprehensive testing.

1
Trading Algorithms (5 Required)

Implement the following classic LeetCode-style problems:

  • Best Time to Buy and Sell Stock I: Single transaction, maximum profit
  • Best Time to Buy and Sell Stock II: Unlimited transactions
  • Best Time to Buy and Sell Stock III: At most 2 transactions
  • Best Time to Buy and Sell Stock IV: At most K transactions
  • Best Time with Cooldown: Must wait 1 day after selling
Deliverable: Java classes implementing each algorithm with O(n) or O(n*k) time complexity.
2
Data Structures Implementation

Build the following custom data structures:

  • StockPriceTracker: Hash map + min/max heaps for real-time tracking
  • StockSpan: Monotonic stack for calculating price spans
  • Portfolio: Hash map with O(1) operations for holdings
  • TopKStocks: Min heap for tracking top K performers
Deliverable: Custom data structure classes with proper encapsulation and documentation.
3
Testing and Validation

Comprehensive test coverage:

  • Unit Tests: JUnit tests for each algorithm and data structure
  • Edge Cases: Empty arrays, single element, all same prices, decreasing prices
  • Performance Tests: Verify O(n) complexity with large inputs
  • Integration Tests: Test with actual CSV data
Deliverable: Test suite with 90%+ code coverage and documented test cases.
4
Documentation and Analysis

Technical documentation:

  • Algorithm Explanations: Describe approach for each trading problem
  • Complexity Analysis: Time and space complexity for each solution
  • Trade-offs: Compare different approaches (brute force vs DP)
  • Results: Performance on provided dataset
Deliverable: README.md with comprehensive documentation and analysis.
05

Algorithm Specifications

Implement the following algorithms with optimal time and space complexity. Each algorithm should be thoroughly tested and documented.

Single Transaction (Easy)

Find maximum profit with one buy and one sell.

// Example:
// prices = [7, 1, 5, 3, 6, 4]
// Output: 5 (buy at 1, sell at 6)

public int maxProfit(int[] prices) {
    int minPrice = Integer.MAX_VALUE;
    int maxProfit = 0;
    for (int price : prices) {
        minPrice = Math.min(minPrice, price);
        maxProfit = Math.max(maxProfit, price - minPrice);
    }
    return maxProfit;
}
Time: O(n) Space: O(1)
Unlimited Transactions (Medium)

Find maximum profit with unlimited transactions.

// Example:
// prices = [7, 1, 5, 3, 6, 4]
// Output: 7 (buy@1 sell@5, buy@3 sell@6)

public int maxProfit(int[] prices) {
    int profit = 0;
    for (int i = 1; i < prices.length; i++) {
        if (prices[i] > prices[i - 1]) {
            profit += prices[i] - prices[i - 1];
        }
    }
    return profit;
}
Time: O(n) Space: O(1)
K Transactions (Hard)

Find maximum profit with at most K transactions.

// Dynamic Programming approach
// dp[i][j] = max profit using i transactions 
//            up to day j

public int maxProfit(int k, int[] prices) {
    int n = prices.length;
    if (k >= n / 2) {
        return unlimitedTransactions(prices);
    }
    int[][] dp = new int[k + 1][n];
    // ... implementation
}
Time: O(n*k) Space: O(n*k)
With Cooldown (Medium)

Cannot buy the day after selling.

// State machine approach:
// hold = max profit while holding stock
// sold = max profit just sold
// rest = max profit in cooldown

public int maxProfit(int[] prices) {
    int hold = Integer.MIN_VALUE;
    int sold = 0, rest = 0;
    for (int price : prices) {
        int prevSold = sold;
        sold = hold + price;
        hold = Math.max(hold, rest - price);
        rest = Math.max(rest, prevSold);
    }
    return Math.max(sold, rest);
}
Time: O(n) Space: O(1)
Best Practice: Start with the brute-force solution, understand the recurrence relation, then optimize with memoization and finally convert to bottom-up DP.
06

Data Structure Requirements

Build custom data structures optimized for stock trading operations. Each structure should have documented methods, time complexity, and test cases.

StockSpan Calculator

Calculate consecutive days where price was ≤ current day's price.

class StockSpan {
    private Stack<int[]> stack; // [price, span]
    
    public int next(int price) {
        int span = 1;
        while (!stack.isEmpty() && 
               stack.peek()[0] <= price) {
            span += stack.pop()[1];
        }
        stack.push(new int[]{price, span});
        return span;
    }
}
Amortized O(1)
Stock Price Tracker

Track current, min, and max prices efficiently.

class StockPriceTracker {
    private Map<Integer, Integer> prices;
    private TreeMap<Integer, Integer> priceCounts;
    private int latestTime;
    
    public void update(int time, int price) {...}
    public int current() {...}
    public int maximum() {...}
    public int minimum() {...}
}
O(log n) operations
Portfolio Manager

Manage stock holdings with O(1) lookups.

class Portfolio {
    private Map<String, Holding> holdings;
    
    public void buy(String symbol, int qty, double price);
    public void sell(String symbol, int qty, double price);
    public double getValue(Map<String, Double> prices);
    public double getTotalProfit();
    public List<Holding> getHoldings();
}
O(1) buy/sell
Top K Stocks

Track top K performing stocks in real-time.

class TopKStocks {
    private PriorityQueue<Stock> minHeap;
    private int k;
    
    public void update(String symbol, double gain);
    public List<Stock> getTopK();
    public boolean isTopK(String symbol);
}
O(log k) update
07

Submission Requirements

Create a public GitHub repository with the exact name shown below:

Required Repository Name
stock-trading-system
github.com/<your-username>/stock-trading-system
Required Project Structure
stock-trading-system/
├── src/
│   ├── main/java/com/trading/
│   │   ├── algorithms/
│   │   │   ├── SingleTransaction.java
│   │   │   ├── UnlimitedTransactions.java
│   │   │   ├── KTransactions.java
│   │   │   ├── WithCooldown.java
│   │   │   └── WithTransactionFee.java
│   │   ├── datastructures/
│   │   │   ├── StockSpan.java
│   │   │   ├── StockPriceTracker.java
│   │   │   ├── Portfolio.java
│   │   │   └── TopKStocks.java
│   │   ├── models/
│   │   │   ├── Stock.java
│   │   │   └── Holding.java
│   │   └── Main.java
│   └── test/java/com/trading/
│       ├── algorithms/
│       └── datastructures/
├── data/
│   ├── stock_prices.csv
│   ├── stock_metadata.csv
│   └── sample_portfolios.csv
├── docs/
│   ├── algorithm_analysis.md
│   └── complexity_comparison.md
└── README.md
README.md Required Sections
1. Project Header
  • Project title and description
  • Your full name and submission date
  • Course and project number
2. Problem Descriptions
  • List all trading problems solved
  • LeetCode problem references
  • Difficulty levels
3. Implementation Details
  • Approach for each algorithm
  • Time and space complexity
  • Key insights and optimizations
4. Data Structures
  • Description of each custom structure
  • Methods and their complexities
  • Use cases in trading context
5. Testing
  • How to run tests
  • Test coverage summary
  • Edge cases handled
6. Results
  • Performance on provided dataset
  • Sample outputs
  • Profit calculations
Submit Your Project

Enter your GitHub username - we will verify your repository automatically

08

Grading Rubric

Your project will be graded on the following criteria. Total: 500 points.

Criteria Points Description
Trading Algorithms 150 All 5 algorithms implemented correctly with optimal complexity
Data Structures 100 Custom structures with proper encapsulation and O(1)/O(log n) operations
Code Quality 75 Clean code, proper naming, SOLID principles, no code smells
Testing 75 Comprehensive test coverage, edge cases, performance tests
Documentation 50 README quality, inline comments, complexity analysis
Project Structure 25 Proper package organization, file naming, folder structure
Bonus Features 25 Additional algorithms, visualizations, performance optimizations
Total 500
Grading Levels
Excellent
450-500

Exceeds all requirements with exceptional quality

Good
375-449

Meets all requirements with good quality

Satisfactory
300-374

Meets minimum requirements

Needs Work
< 300

Missing key requirements

Ready to Submit?

Make sure you have completed all requirements and reviewed the grading rubric above.

Submit Your Project