public static synchronized BuyOrderApplicationService buyOrderApplicationService() { if (buyOrderApplicationService == null) { buyOrderApplicationService = new BuyOrderApplicationService( InMemoryBuyOrderRepository.instance(), new MarketFillService(InMemoryBuyOrderRepository.instance())); } return buyOrderApplicationService; }
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 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 fillMarketOrdersUsing(QuoteBar aQuoteBar) { TickerSymbol tickerSymbol = new TickerSymbol(aQuoteBar.symbol()); int totalQuantityAvailable = aQuoteBar .totalQuantity() .toBigInteger() .intValue(); Collection<BuyOrder> openOrders = this.buyOrderRepository().openOrdersOf(tickerSymbol); this.marketFillService().fillMarketBuyOrders(openOrders, totalQuantityAvailable); }
private boolean trySharesToPurchase( BuyOrder aBuyOrder, int aTotalQuantityAvailable) { try { aBuyOrder.sharesToPurchase(aTotalQuantityAvailable); this.buyOrderRepository().save(aBuyOrder); return true; } catch (Exception e) { return false; } }
this.buyOrderRepository.openOrdersOf(tickerSymbol); MarketFillService marketFillService = new MarketFillService(this.buyOrderRepository); marketFillService.fillMarketBuyOrders(openOrders, totalQuantityAvailable);