@Override public boolean equals(Object anObject) { boolean equalObjects = false; if (anObject != null && this.getClass() == anObject.getClass()) { BuyOrder typedObject = (BuyOrder) anObject; equalObjects = this.accountId().equals(typedObject.accountId()) && this.orderId().equals(typedObject.orderId()); } return equalObjects; }
@Override public void save(BuyOrder aOrder) { this.orders().put(aOrder.orderId().id(), aOrder); }
@Override public void remove(BuyOrder aOrder) { this.orders().remove(aOrder.orderId().id()); }
@Override public int hashCode() { int hashCodeValue = + (45321 * 61) + this.accountId().hashCode() + this.orderId().hashCode(); return hashCodeValue; }
public void fillMarketBuyOrders( Collection<BuyOrder> anOpenBuyOrders, int aTotalQuantityAvailable) { BuyOrderSharePurchaseRequestedSubscriber subscriber = new BuyOrderSharePurchaseRequestedSubscriber(); DomainEventPublisher.instance().subscribe(subscriber); Iterator<BuyOrder> iterator = anOpenBuyOrders.iterator(); while (iterator.hasNext() && aTotalQuantityAvailable > 0) { BuyOrder buyOrder = iterator.next(); boolean done = false; for (int tries = 0; !done && tries < 3; ++tries) { subscriber.clear(); if (this.trySharesToPurchase(buyOrder, aTotalQuantityAvailable)) { aTotalQuantityAvailable -= subscriber.orderSharesRequested(); done = true; } else { buyOrder = this.buyOrderRepository().orderOf(buyOrder.orderId()); } } } }
public void testSave() throws Exception { List<OrderId> ids = new ArrayList<OrderId>(); for (int idx = 0; idx < 10; ++idx) { BuyOrder order = this.buyOrderFixture1(); this.repository.save(order); ids.add(order.orderId()); } for (int idx = 0; idx < 10; ++idx) { BuyOrder order = this.repository.orderOf(ids.get(idx)); assertNotNull(order); } }
public void testRemove() throws Exception { BuyOrder buyOrder = this.buyOrderFixture1(); this.repository.save(buyOrder); BuyOrder foundOrder = this.repository.orderOf(buyOrder.orderId()); assertEquals(buyOrder, foundOrder); this.repository.remove(buyOrder); foundOrder = this.repository.orderOf(buyOrder.orderId()); assertNull(foundOrder); }
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 testOrderOf() throws Exception { BuyOrder buyOrder = this.buyOrderFixture1(); this.repository.save(buyOrder); BuyOrder foundOrder = this.repository.orderOf(buyOrder.orderId()); assertNotNull(foundOrder); assertEquals(buyOrder, foundOrder); }
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 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 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 testPlaceBuyOrder() 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(); // event published here assertNotNull(buyOrderPlaced); assertEquals(new Money("9.99"), buyOrderPlaced.orderFee()); assertEquals(NUMBER_OF_SHARES, buyOrderPlaced.quantityOfSharesOrdered()); assertEquals(PRICE, buyOrderPlaced.quote().price()); assertEquals(TICKER, buyOrderPlaced.quote().tickerSymbol().symbol()); assertEquals(PRICE.multipliedBy(NUMBER_OF_SHARES), buyOrder.valueOfOrderedShares()); assertEquals(buyOrder.accountId(), buyOrderPlaced.accountId()); assertEquals(buyOrder.orderId(), buyOrderPlaced.orderId()); assertEquals(buyOrder.quantityOfSharesOrdered(), buyOrderPlaced.quantityOfSharesOrdered()); assertEquals(buyOrder.quote(), buyOrderPlaced.quote()); }
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."); } }