A web application to maintain equity positions based on transaction processing.
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.
- 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
Transaction: Represents a transaction with TradeID, Version, SecurityCode, Quantity, Action, and TypePosition: Represents current position for a securityTransactionAction: Enum for Insert, Update, CancelTransactionType: Enum for Buy, Sell
IPositionRepository: Interface for data operationsPositionRepository: In-memory implementation with business logic- Handles transaction processing and position calculation
TransactionsController: CRUD operations for transactionsPositionsController: Read operations for positionsTestController: Utility endpoints for testing
- INSERT: Always version 1 of a trade
- UPDATE: Can change SecurityCode, Quantity, or Buy/Sell
- CANCEL: Always the last version of a trade, ignores changes
- Position Calculation:
- Buy transactions add positive quantity
- Sell transactions add negative quantity
- Positions are recalculated when newer versions arrive
- Out-of-Order Processing: Only the latest version of each trade affects positions
POST /api/transactions- Create a new transactionGET /api/transactions- Get all transactionsGET /api/transactions/{id}- Get transaction by ID
GET /api/positions- Get all positionsGET /api/positions/{securityCode}- Get position for specific security
POST /api/test/load-sample-data- Load sample transactionsPOST /api/test/clear-data- Clear all 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
-
Navigate to the API project:
cd src/PositionManagement.API -
Run the application:
dotnet run
-
Access the web interface:
- Open browser to
http://localhost:5114 - Or use Swagger UI at
http://localhost:5114/swagger
- Open browser to
- Click "Load Sample Data" to load test transactions
- View transactions and positions in the respective sections
- Add new transactions using the form
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"
}'- When a transaction is added, it's stored in memory
- The system finds all transactions for the same TradeID
- Only the latest version (highest version number) is processed
- Previous versions are removed from positions
- The latest version is added to positions (unless it's a CANCEL)
- Buy transactions:
+quantity - Sell transactions:
-quantity - Positions are stored as net quantities
- Zero positions are removed from the system
The system handles transactions arriving in any order by:
- Storing all transactions
- Only processing the latest version of each trade
- Recalculating positions when newer versions arrive
This ensures that regardless of arrival order, the final positions are always correct.