JAVA
How to System Design Trade Position Aggregator? [Solution]
import org.junit.Ignore; import java.util.Iterator; import java.util.List; import java.util.ArrayList; import org.junit.Test; import static org.junit.Assert.; public class PositionManagerTest { public PositionManagerTest() { } @Ignore public void testGetRealTimePosition() { assertEquals(1, 1); } @Test public void testGetPositionForAccountAndSecurity() { PositionManager pMgr = new PositionManager(); List<Trade> tradeStream = createTrades(); Iterator<Trade> itr = tradeStream.iterator(); while(itr.hasNext()) { pMgr.process(itr.next()); itr.remove(); } assertEquals(200, pMgr.getPositionForAccountAndSecurity("ACC-101", "MSFT")); assertEquals(150, pMgr.getPositionForAccountAndSecurity("ACC-102", "INFY")); assertEquals(250, pMgr.getPositionForAccountAndSecurity("ACC-103", "REP")); assertEquals(150, pMgr.getPositionForAccountAndSecurity("ACC-104", "HKJ")); assertEquals(200, pMgr.getPositionForAccountAndSecurity("ACC-105", "FVE")); assertEquals(50, pMgr.getPositionForAccountAndSecurity("ACC-105", "AMZ")); assertEquals(150, pMgr.getPositionForAccountAndSecurity("ACC-106", "ABC")); assertEquals(-50, pMgr.getPositionForAccountAndSecurity("ACC-107", "META")); assertEquals(200, pMgr.getPositionForAccountAndSecurity("ACC-108", "OLA")); assertEquals(200, pMgr.getPositionForAccountAndSecurity("ACC-109", "BCD")); assertEquals(-200,pMgr.getPositionForAccountAndSecurity("ACC-110", "BCD")); assertEquals(100, pMgr.getPositionForAccountAndSecurity("ACC-111", "EFG")); assertEquals(0, pMgr.getPositionForAccountAndSecurity("ACC-112", "BES")); assertEquals(0, pMgr.getPositionForAccountAndSecurity("ACC-113", "KBC")); //validate the total number of trades //validate correct account contain correct trades } private List<Trade> createTrades(){ List<Trade> trades = new ArrayList<Trade>(); //simple increment trades.add(new Trade(5001,"ACC-101", 1, "MSFT", 100, Direction.BUY, Operation.NEW)); trades.add(new Trade(5002,"ACC-101", 1, "MSFT", 100, Direction.BUY, Operation.NEW)); //simple increment for another security trades.add(new Trade(5003,"ACC-102", 1, "INFY", 50, Direction.BUY, Operation.NEW)); trades.add(new Trade(5004,"ACC-102", 1, "INFY", 100, Direction.BUY, Operation.NEW)); //simple NEW - AMEND - NEW (quantity) trades.add(new Trade(5005,"ACC-103", 1, "REP", 100, Direction.BUY, Operation.NEW)); trades.add(new Trade(5005,"ACC-103", 2, "REP", 50, Direction.BUY, Operation.AMEND)); trades.add(new Trade(5006,"ACC-103", 1, "REP", 200, Direction.BUY, Operation.NEW)); //simpel BUY SELL BUY Scenario trades.add(new Trade(5007, "ACC-104", 1, "HKJ", 100, Direction.BUY, Operation.NEW)); trades.add(new Trade(5008, "ACC-104", 1, "HKJ", 50, Direction.SELL, Operation.NEW)); trades.add(new Trade(5009, "ACC-104", 1, "HKJ", 100, Direction.BUY, Operation.NEW)); //security amendment scenario trades.add(new Trade(5010, "ACC-105", 1, "FVE", 100, Direction.BUY, Operation.NEW)); trades.add(new Trade(5010, "ACC-105", 2, "AMZ", 50, Direction.BUY, Operation.AMEND)); trades.add(new Trade(5011, "ACC-105", 1, "FVE", 200, Direction.BUY, Operation.NEW)); //direction amendment scenario trades.add(new Trade(5012, "ACC-106", 1, "ABC", 100, Direction.BUY, Operation.NEW)); trades.add(new Trade(5012, "ACC-106", 2, "ABC", 50, Direction.SELL, Operation.AMEND)); trades.add(new Trade(5014, "ACC-106", 1, "ABC", 200, Direction.BUY, Operation.NEW)); //security + direction amendment scenario trades.add(new Trade(5015, "ACC-107", 1, "OLA", 100, Direction.BUY, Operation.NEW)); trades.add(new Trade(5015, "ACC-107", 2, "META", 50, Direction.SELL, Operation.AMEND)); trades.add(new Trade(5016, "ACC-108", 1, "OLA", 200, Direction.BUY, Operation.NEW)); trades.add(new Trade(5016, "ACC-108", 2, "META", 50, Direction.BUY, Operation.AMEND)); trades.add(new Trade(5016, "ACC-108", 3, "OLA", 200, Direction.BUY, Operation.AMEND)); //same security - one accoutn sale other buy trades.add(new Trade(5017, "ACC-109", 1, "BCD", 200, Direction.BUY, Operation.NEW)); trades.add(new Trade(5018, "ACC-110", 2, "BCD", 200, Direction.SELL, Operation.NEW)); //same security - multiple amendments on quantity trades.add(new Trade(5019, "ACC-111", 1, "EFG", 50, Direction.BUY, Operation.NEW)); trades.add(new Trade(5019, "ACC-111", 2, "EFG", 200, Direction.BUY, Operation.AMEND)); trades.add(new Trade(5019, "ACC-111", 3, "EFG", 250, Direction.BUY, Operation.AMEND)); trades.add(new Trade(5019, "ACC-111", 4, "EFG", 100, Direction.BUY, Operation.AMEND)); //Same Security - NEW MOD CANCEL test trades.add(new Trade(5020, "ACC-112", 1, "BES", 50, Direction.BUY, Operation.NEW)); trades.add(new Trade(5020, "ACC-112", 2, "BES", 200,
Direction.BUY, Operation.AMEND)); trades.add(new Trade(5020, "ACC-112", 3, "BES", 200,
Direction.BUY, Operation.CANCEL)); //outofsequence trade events trades.add(new Trade(5019, "ACC-113", 1, "KBC", 100,
Direction.BUY, Operation.NEW)); trades.add(new Trade(5019, "ACC-113", 2, "KBC", 150,
Direction.BUY, Operation.AMEND)); trades.add(new Trade(5019, "ACC-113", 4, "KBC", 150,
Direction.BUY, Operation.CANCEL)); trades.add(new Trade(5019, "ACC-113", 3, "KBC", 250,
Direction.BUY, Operation.AMEND)); return trades; } }