Skip to content

This system processes equity transactions and maintains real-time positions. It handles INSERT, UPDATE, and CANCEL operations on trades, with support for out-of-order transaction processing.

License

Notifications You must be signed in to change notification settings

shashifromearth/Position-Management-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Position Management System

A web application to maintain equity positions based on transaction processing.

Overview

This system processes equity transactions and maintains real-time positions. It handles INSERT, UPDATE, and CANCEL operations on trades, with support for out-of-order transaction processing.

Features

  • Transaction Processing: Handle INSERT, UPDATE, and CANCEL operations
  • Position Management: Real-time position calculation based on transactions
  • Out-of-Order Processing: Transactions can arrive in any sequence
  • REST API: Complete API for transaction and position management
  • Web Interface: Simple HTML interface for testing

Architecture

Domain Layer (PositionManagement.Domain)

  • Transaction: Represents a transaction with TradeID, Version, SecurityCode, Quantity, Action, and Type
  • Position: Represents current position for a security
  • TransactionAction: Enum for Insert, Update, Cancel
  • TransactionType: Enum for Buy, Sell

Data Layer (PositionManagement.Data)

  • IPositionRepository: Interface for data operations
  • PositionRepository: In-memory implementation with business logic
  • Handles transaction processing and position calculation

API Layer (PositionManagement.API)

  • TransactionsController: CRUD operations for transactions
  • PositionsController: Read operations for positions
  • TestController: Utility endpoints for testing

Business Rules

  1. INSERT: Always version 1 of a trade
  2. UPDATE: Can change SecurityCode, Quantity, or Buy/Sell
  3. CANCEL: Always the last version of a trade, ignores changes
  4. Position Calculation:
    • Buy transactions add positive quantity
    • Sell transactions add negative quantity
    • Positions are recalculated when newer versions arrive
  5. Out-of-Order Processing: Only the latest version of each trade affects positions

API Endpoints

Transactions

  • POST /api/transactions - Create a new transaction
  • GET /api/transactions - Get all transactions
  • GET /api/transactions/{id} - Get transaction by ID

Positions

  • GET /api/positions - Get all positions
  • GET /api/positions/{securityCode} - Get position for specific security

Test

  • POST /api/test/load-sample-data - Load sample transactions
  • POST /api/test/clear-data - Clear all data

Sample Data

The system comes with sample data that demonstrates the business rules:

TransactionID TradeID Version SecurityCode Quantity Action    Type
1            1       1       REL           50      INSERT    Buy
2            2       1       ITC           40      INSERT    Sell  
3            3       1       INF           70      INSERT    Buy
4            1       2       REL           60      UPDATE    Buy
5            2       2       ITC           30      CANCEL    Buy
6            4       1       INF           20      INSERT    Sell

Expected final positions:

  • REL: +60
  • ITC: 0
  • INF: +50

Running the Application

  1. Navigate to the API project:

    cd src/PositionManagement.API
  2. Run the application:

    dotnet run
  3. Access the web interface:

    • Open browser to http://localhost:5114
    • Or use Swagger UI at http://localhost:5114/swagger

Testing

Using the Web Interface

  1. Click "Load Sample Data" to load test transactions
  2. View transactions and positions in the respective sections
  3. Add new transactions using the form

Using API Directly

Load sample data:

curl -X POST "http://localhost:5114/api/test/load-sample-data" \
  -H "Content-Type: application/json"

Get positions:

curl "http://localhost:5114/api/positions"

Add a transaction:

curl -X POST "http://localhost:5114/api/transactions" \
  -H "Content-Type: application/json" \
  -d '{
    "tradeId": 5,
    "version": 1,
    "securityCode": "TCS",
    "quantity": 100,
    "action": "Insert",
    "type": "Buy"
  }'

Implementation Details

Transaction Processing Logic

  1. When a transaction is added, it's stored in memory
  2. The system finds all transactions for the same TradeID
  3. Only the latest version (highest version number) is processed
  4. Previous versions are removed from positions
  5. The latest version is added to positions (unless it's a CANCEL)

Position Calculation

  • Buy transactions: +quantity
  • Sell transactions: -quantity
  • Positions are stored as net quantities
  • Zero positions are removed from the system

Out-of-Order Handling

The system handles transactions arriving in any order by:

  1. Storing all transactions
  2. Only processing the latest version of each trade
  3. Recalculating positions when newer versions arrive

This ensures that regardless of arrival order, the final positions are always correct.

About

This system processes equity transactions and maintains real-time positions. It handles INSERT, UPDATE, and CANCEL operations on trades, with support for out-of-order transaction processing.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published