Online Stock Brokerage System: Low-Level Design and Java Class Structure

This document outlines the low-level design and Java classes for an online stock brokerage system. The system caters to user needs for trading stocks, managing watchlists, and receiving trade notifications. Key functionalities include:

  • Trading: Users can buy or sell stocks through various order types (market, limit, stop-loss, stop-limit).
  • Watchlists: Users can create and manage personalized lists of stocks they want to track.
  • Portfolio Management: The system tracks user holdings and calculates average purchase price for each stock.
  • User: Represents a system user with login details and holdings.
  • Stock: Stores information about a stock including symbol, price, and company details.
  • Watchlist: User-defined list of stocks they are interested in following.
  • Order: Abstract class representing a buy or sell order with quantity and type (market, limit, etc.).
  • MarketOrder: Fills at current market price.
  • LimitOrder: Fills only at or below (buy) or at or above (sell) a specified price.
  • StopLossOrder: Triggers order when price reaches a certain level.
  • StopLimitOrder: Triggers order when price reaches a level and fills at a specified limit price.
  • Portfolio: Tracks user holdings (quantity and average price) for each stock.
  • Lot: Represents a specific purchase of a stock with quantity and average price.
  • OrderType: Enum defining different order types (market, limit, stop-loss, stop-limit).
public class User {
    private String userId;
    private String name;
    private List<Watchlist> watchlists;
    private Portfolio portfolio;
    // Getters and setters
}

public class Stock {
    private String symbol;
    private double currentPrice;
    private String companyName;
    private String sector;
    // Getters and setters
}

public class Watchlist {
    private String name;
    private List<Stock> stocks;
    private User user; // Reference to the user owning this watchlist
    // Getters and setters
}

public abstract class Order {
    private User user;
    private Stock stock;
    private int quantity;
    private OrderType type; // Enum representing different order types (Market, Limit, StopLoss, StopLimit)

    public abstract double calculatePrice(double currentPrice); // This method's implementation varies based on order type

    // Getters and setters
}

public class MarketOrder extends Order {
    @Override
    public double calculatePrice(double currentPrice) {
        return currentPrice; // Market order uses current market price
    }
}

public class LimitOrder extends Order {
    private double limitPrice;

    @Override
    public double calculatePrice(double currentPrice) {
        return type == OrderType.BUY ? Math.min(limitPrice, currentPrice) : Math.max(limitPrice, currentPrice); // Fills only at or below (buy) or at or above (sell) the limit price
    }

    // Getters and setters for limitPrice
}

public class StopLossOrder extends Order {
    private double stopPrice;

    @Override
    public double calculatePrice(double currentPrice) {
        return type == OrderType.BUY ? Math.max(stopPrice, currentPrice) : Math.min(stopPrice, currentPrice); // Triggers order when price reaches or surpasses (buy) or falls below (sell) the stop price
    }

    // Getters and setters for stopPrice
}

public class StopLimitOrder extends Order {
    private double stopPrice;
    private double limitPrice;

    @Override
    public double calculatePrice(double currentPrice) {
        // Implement logic to determine price based on stop price reaching and then filling at the limit price
    }

    // Getters and setters for stopPrice and limitPrice
}

public class Portfolio {
    private User user; // Reference to the user owning this portfolio
    private Map<Stock, List<Lot>> holdings; // Map of stock to a list of lots owned by the user

    public void addOrder(Order order) {
        // Process buy or sell order and update holdings
    }

    // Getters and setters
}

public class Lot {
    private int quantity;
    private double averagePrice;

    // Getters and setters
}

public enum OrderType {
    MARKET,
    LIMIT,
    STOP_LOSS,
    STOP_LIMIT
}

You may also like...