public PurchaseExecution withPurchasedSharesOf(int aQuantity) { if (this.wouldOverrunExecutionWith(aQuantity)) { throw new IllegalArgumentException("Acquired too many shares."); } int quantityOfSharesOutstanding = this.quantityOfSharesOutstanding() - aQuantity; return new PurchaseExecution(this.openDate(), this.quantityOfSharesOrdered(), quantityOfSharesOutstanding); }
@Override public String toString() { return "PurchaseExecution [openDate=" + this.openDate() + ", filledDate=" + this.filledDate() + ", quantityOfSharesOrdered=" + this.quantityOfSharesOrdered() + ", quantityOfSharesOutstanding=" + this.quantityOfSharesOutstanding() + "]"; }
public boolean isFilled() { return this.execution().isFilled(); }
public void testFillExecution() throws Exception { PurchaseExecution purchaseExecution = new PurchaseExecution(20); for (int count = 0; count < 4; ++count) { assertTrue(purchaseExecution.isOpen()); assertFalse(purchaseExecution.isFilled()); purchaseExecution = purchaseExecution.withPurchasedSharesOf(5); } assertFalse(purchaseExecution.isOpen()); assertTrue(purchaseExecution.isFilled()); assertNotNull(purchaseExecution.filledDate()); Calendar filledDate = Calendar.getInstance(); filledDate.setTime(purchaseExecution.filledDate()); assertEquals(Calendar.getInstance().get(Calendar.MONTH), filledDate.get(Calendar.MONTH)); assertEquals(Calendar.getInstance().get(Calendar.DATE), filledDate.get(Calendar.DATE)); assertEquals(Calendar.getInstance().get(Calendar.YEAR), filledDate.get(Calendar.YEAR)); }
public BuyOrder( AccountId anAccountId, Quote aQuote, int aQuantityOfSharesOrdered, Money anOrderFee) { super(); this.setAccountId(anAccountId); this.setExecution(new PurchaseExecution(aQuantityOfSharesOrdered)); this.setOrderId(OrderId.unique()); this.setQuote(aQuote); DomainEventPublisher.instance().publish( new BuyOrderPlaced( this.accountId(), this.orderId(), this.quote(), this.execution().quantityOfSharesOrdered(), this.execution().openDate(), aQuote.price().multipliedBy(aQuantityOfSharesOrdered), anOrderFee)); }
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 testNoNegativeQuantities() throws Exception { try { new PurchaseExecution(-20); fail("Should not allow negative order quantity."); } catch (Exception e) { // success } try { PurchaseExecution purchaseExecution = new PurchaseExecution(10); purchaseExecution.withPurchasedSharesOf(11); fail("Should not allow overrun purchase execution."); } catch (Exception e) { // success } }
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 testPlaceBuyOrder() throws Exception { Money money = new Money("10000.00"); Account account = this.profileFixture().openAccount(money); Money orderFee = new Money("9.99"); Money price = new Money("723.25"); int shares = 10; TickerSymbol tickerSymbol = new TickerSymbol("GOOG"); BuyOrder buyOrder = account.placeBuyOrder(tickerSymbol, price, shares, orderFee); assertEquals(account.accountId(), buyOrder.accountId()); assertEquals(tickerSymbol, buyOrder.quote().tickerSymbol()); assertEquals(price, buyOrder.quote().price()); assertEquals(shares, buyOrder.quantityOfSharesOrdered()); assertEquals(shares, buyOrder.execution().quantityOfSharesOrdered()); assertEquals(shares, buyOrder.execution().quantityOfSharesOutstanding()); }
public int quantityOfSharesOrdered() { return this.execution().quantityOfSharesOrdered(); }
public boolean isOpen() { return this.execution().isOpen(); }
public int quantityOfOutstandingShares() { return this.execution().quantityOfSharesOutstanding(); }
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()); }
public void testOpenExecution() throws Exception { PurchaseExecution purchaseExecution = new PurchaseExecution(20); assertEquals(20, purchaseExecution.quantityOfSharesOrdered()); assertEquals(20, purchaseExecution.quantityOfSharesOutstanding()); assertNull(purchaseExecution.filledDate()); assertNotNull(purchaseExecution.openDate()); Calendar openDate = Calendar.getInstance(); openDate.setTime(purchaseExecution.openDate()); assertEquals(Calendar.getInstance().get(Calendar.MONTH), openDate.get(Calendar.MONTH)); assertEquals(Calendar.getInstance().get(Calendar.DATE), openDate.get(Calendar.DATE)); assertEquals(Calendar.getInstance().get(Calendar.YEAR), openDate.get(Calendar.YEAR)); }
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."); } }