public Holding holdingOfFilledOrder() { if (!this.isFilled()) { throw new IllegalStateException("Order must be filled before Holding can be queried."); } Holding holding = this.holding; if (holding == null) { int sharesOrdered = this.execution().quantityOfSharesOrdered(); Money totalValue = this.quote().valueOfPricedShares(sharesOrdered); holding = new Holding( this.accountId(), this.orderId(), this.quote().tickerSymbol(), sharesOrdered, totalValue, this.execution().filledDate()); this.setHolding(holding); } return holding; }
public void testSaveWithOverwrite() throws Exception { BuyOrder buyOrder = this.buyOrderFixture1(); this.repository.save(buyOrder); assertTrue(buyOrder.isOpen()); assertFalse(buyOrder.isFilled()); BuyOrder changedBuyOrder = this.repository.orderOf(buyOrder.orderId()); changedBuyOrder.sharesToPurchase(changedBuyOrder.execution().quantityOfSharesOrdered()); this.repository.save(changedBuyOrder); changedBuyOrder = this.repository.orderOf(buyOrder.orderId()); assertNotNull(changedBuyOrder.holdingOfFilledOrder()); assertFalse(buyOrder.isOpen()); assertTrue(buyOrder.isFilled()); }
assertTrue(previouslyOpenOrder.isFilled()); assertFalse(previouslyOpenOrder.isOpen()); ++filledCount;
public void testUnmatchedFillMarketOrders() throws Exception { BuyOrder[] buyOrders = this.buyOrderFixture(); for (BuyOrder buyOrder : buyOrders) { this.buyOrderRepository.save(buyOrder); } QuoteBar quoteBar = this.quoteBarUnmatchedFixture(); TickerSymbol tickerSymbol = new TickerSymbol(quoteBar.symbol()); int totalQuantityAvailable = quoteBar .totalQuantity() .toBigInteger() .intValue(); Collection<BuyOrder> openOrders = this.buyOrderRepository.openOrdersOf(tickerSymbol); MarketFillService marketFillService = new MarketFillService(this.buyOrderRepository); marketFillService.fillMarketBuyOrders(openOrders, totalQuantityAvailable); int stillOpenCount = 0; for (BuyOrder previouslyOpenOrder : buyOrders) { assertFalse(previouslyOpenOrder.isFilled()); assertTrue(previouslyOpenOrder.isOpen()); ++stillOpenCount; } assertEquals(buyOrders.length, stillOpenCount); }
public void sharesToPurchase(int aQuantityOfSharesAvailable) { if (this.execution().quantityOfSharesOutstanding() > 0) { if (aQuantityOfSharesAvailable > 0) { int quantityToPurchase = Math.min( this.execution().quantityOfSharesOutstanding(), aQuantityOfSharesAvailable); if (quantityToPurchase > 0) { DomainEventPublisher.instance().publish( new BuyOrderSharePurchaseRequested( this.accountId(), this.orderId(), this.quote(), quantityToPurchase)); this.setExecution( this.execution() .withPurchasedSharesOf(quantityToPurchase)); if (this.isFilled()) { DomainEventPublisher.instance().publish( new BuyOrderFilled( this.accountId(), this.orderId(), this.quote(), this.execution().quantityOfSharesOrdered(), this.holdingOfFilledOrder())); } } } } }
public void testPurchaseForBuyOrder() throws Exception { final BuyOrder buyOrder = this.buyOrderFixture(); DomainEventPublisher.instance().subscribe(new DomainEventSubscriber<BuyOrderSharePurchaseRequested>() { @Override public void handleEvent(BuyOrderSharePurchaseRequested aDomainEvent) { buyOrderSharesToPurchase = aDomainEvent; quantityOfSharesToPurchase += aDomainEvent.quantityOfShares(); } @Override public Class<BuyOrderSharePurchaseRequested> subscribedToEventType() { return BuyOrderSharePurchaseRequested.class; } }); buyOrder.sharesToPurchase(NUMBER_OF_SHARES / 2); buyOrder.sharesToPurchase(0); buyOrder.sharesToPurchase(NUMBER_OF_SHARES / NUMBER_OF_SHARES); buyOrder.sharesToPurchase(NUMBER_OF_SHARES * 10); assertNotNull(buyOrderSharesToPurchase); assertEquals(NUMBER_OF_SHARES, quantityOfSharesToPurchase); assertEquals(0, buyOrder.quantityOfOutstandingShares()); assertFalse(buyOrder.isOpen()); assertTrue(buyOrder.isFilled()); }
public void testBuyOrderCreation() throws Exception { DomainEventPublisher.instance().subscribe(new DomainEventSubscriber<BuyOrderPlaced>() { @Override public void handleEvent(BuyOrderPlaced aDomainEvent) { buyOrderPlaced = aDomainEvent; } @Override public Class<BuyOrderPlaced> subscribedToEventType() { return BuyOrderPlaced.class; } }); BuyOrder buyOrder = this.buyOrderFixture(); assertNotNull(buyOrderPlaced); assertNotNull(buyOrder.accountId()); assertNotNull(buyOrder.execution()); assertFalse(buyOrder.isFilled()); assertTrue(buyOrder.isOpen()); assertEquals(NUMBER_OF_SHARES, buyOrder.quantityOfSharesOrdered()); assertNotNull(buyOrder.quote()); assertEquals(TICKER, buyOrder.quote().tickerSymbol().symbol()); assertEquals(PRICE, buyOrder.quote().price()); assertEquals(PRICE.multipliedBy(NUMBER_OF_SHARES), buyOrder.valueOfOrderedShares()); try { buyOrder.holdingOfFilledOrder(); fail("Holding must not have been created yet."); } catch (Exception e) { // success } }
public void testHoldingAfterFill() throws Exception { BuyOrder buyOrder = this.buyOrderFixture(); assertTrue(buyOrder.isOpen()); assertFalse(buyOrder.isFilled()); buyOrder.sharesToPurchase(buyOrder.execution().quantityOfSharesOrdered()); try { Holding holding = buyOrder.holdingOfFilledOrder(); assertNotNull(holding); assertEquals(buyOrder.accountId(), holding.accountId()); assertEquals(buyOrder.holdingOfFilledOrder().acquiredOn(), holding.acquiredOn()); assertEquals(buyOrder.orderId(), holding.orderId()); assertEquals(buyOrder.quantityOfSharesOrdered(), holding.numberOfShares()); assertEquals(buyOrder.quote().tickerSymbol(), holding.tickerSymbol()); assertEquals(buyOrder.quote().price().multipliedBy(buyOrder.quantityOfSharesOrdered()), holding.totalValue()); } catch (Exception e) { fail("Holding should be available with filled order."); } }
public void testFillBuyOrder() throws Exception { DomainEventPublisher.instance().subscribe(new DomainEventSubscriber<BuyOrderFilled>() { @Override public void handleEvent(BuyOrderFilled aDomainEvent) { buyOrderFilled = aDomainEvent; } @Override public Class<BuyOrderFilled> subscribedToEventType() { return BuyOrderFilled.class; } }); BuyOrder buyOrder = this.buyOrderFixture(); // event published here buyOrder.sharesToPurchase(NUMBER_OF_SHARES); assertNotNull(buyOrderFilled); assertTrue(buyOrder.isFilled()); assertFalse(buyOrder.isOpen()); Holding holding = null; try { holding = buyOrder.holdingOfFilledOrder(); assertEquals(buyOrder.accountId(), holding.accountId()); assertEquals(buyOrder.holdingOfFilledOrder().acquiredOn(), holding.acquiredOn()); assertEquals(buyOrder.orderId(), holding.orderId()); assertEquals(buyOrder.quantityOfSharesOrdered(), holding.numberOfShares()); assertEquals(buyOrder.quote().tickerSymbol(), holding.tickerSymbol()); assertEquals(buyOrder.quote().price().multipliedBy(buyOrder.quantityOfSharesOrdered()), holding.totalValue()); } catch (Exception e) { fail("Holding should be available with filled order."); } }