@Test public void testPrepareBuyOrder() { String coinId = "BTC"; String coinName = "Bitcoin"; CurrencyPair currencyPair = CurrencyPair.BTC_AUD; BigMoney lowestSellPrice = BigMoney.of(currencyPair.getCounterCurrencyUnit(), 100D); BigMoney balance = BigMoney.of(currencyPair.getCounterCurrencyUnit(), 100); BigMoney reserved = BigMoney.of(currencyPair.getCounterCurrencyUnit(), 12); CoinEntry coin = new CoinEntry(); coin.setName(coinName); coin.setPrimaryKey(coinId); OrderBookEntry orderBookEntry = new OrderBookEntry(); orderBookEntry.setCurrencyPair(currencyPair); orderBookEntry.setLowestSellPrice(lowestSellPrice); PortfolioEntry portfolioEntry = new PortfolioEntry(); portfolioEntry.setAmountOfMoney(balance); portfolioEntry.setReservedAmountOfMoney(reserved); CoinQueryRepository coinRepository = mock(CoinQueryRepository.class); when(coinRepository.findOne(eq(coinId))).thenReturn(coin); TradeServiceFacadeImpl facade = new TradeServiceFacadeImpl(); facade.setCoinRepository(coinRepository); final BuyOrder buyOrder = facade.prepareBuyOrder(coinId, currencyPair, orderBookEntry, portfolioEntry); assertThat(buyOrder, notNullValue()); assertThat(buyOrder.getCoinId(), equalTo(coinId)); assertThat(buyOrder.getCoinName(), equalTo(coinName)); assertThat(buyOrder.getAmountCcy(), equalTo(currencyPair.getBaseCurrency())); assertThat(buyOrder.getPriceCcy(), equalTo(currencyPair.getCounterCurrency())); assertThat(buyOrder.getSuggestedPrice(), notNullValue()); assertThat(buyOrder.getSuggestedPrice(), is(closeTo(lowestSellPrice.getAmount(), BigDecimal.valueOf(0.00000000001d)))); assertThat(buyOrder.getBalance(), notNullValue()); assertThat(buyOrder.getBalance(), is(closeTo(balance.minus(reserved).getAmount(), BigDecimal.valueOf(0.00000000001d)))); verify(coinRepository).findOne(eq(coinId)); }
@Override public BuyOrder prepareBuyOrder(String coinId, CurrencyPair currencyPair, OrderBookEntry orderBookEntry, PortfolioEntry portfolioEntry) { hasLength(coinId); notNull(currencyPair); isTrue(coinId.equalsIgnoreCase(currencyPair.getBaseCurrency())); BuyOrder order = new BuyOrder(); initCoinInfo(coinId, currencyPair, order); if (orderBookEntry == null) { orderBookEntry = loadOrderBookByCurrencyPair(currencyPair); } if (orderBookEntry == null) { return order; } BigDecimal amount = BigDecimal.ZERO; if (orderBookEntry.getLowestSellPrice() != null) { amount = orderBookEntry.getLowestSellPrice().getAmount(); } order.setSuggestedPrice(amount); if (portfolioEntry == null) { portfolioEntry = userServiceFacade.obtainPortfolioForUser(); } if (portfolioEntry != null) { order.setBalance(portfolioEntry.obtainMoneyToSpend().getAmount()); } return order; }
TradeServiceFacadeImpl implements TradeServiceFacade { @Override public BuyOrder prepareBuyOrder(String coinId, CurrencyPair currencyPair, OrderBookEntry orderBookEntry, PortfolioEntry portfolioEntry) { hasLength(coinId); notNull(currencyPair); isTrue(coinId.equalsIgnoreCase(currencyPair.getBaseCurrency())); BuyOrder order = new BuyOrder(); initCoinInfo(coinId, currencyPair, order); if (orderBookEntry == null) { orderBookEntry = loadOrderBookByCurrencyPair(currencyPair); } if (orderBookEntry == null) { return order; } BigDecimal amount = BigDecimal.ZERO; if (orderBookEntry.getLowestSellPrice() != null) { amount = orderBookEntry.getLowestSellPrice().getAmount(); } order.setSuggestedPrice(amount); if (portfolioEntry == null) { portfolioEntry = userServiceFacade.obtainPortfolioForUser(); } if (portfolioEntry != null) { order.setBalance(portfolioEntry.obtainMoneyToSpend().getAmount()); } return order; } }
TradeServiceFacadeImpl implements TradeServiceFacade { @Override public BuyOrder prepareBuyOrder(String coinId, CurrencyPair currencyPair, OrderBookEntry orderBookEntry, PortfolioEntry portfolioEntry) { hasLength(coinId); notNull(currencyPair); isTrue(coinId.equalsIgnoreCase(currencyPair.getBaseCurrency())); BuyOrder order = new BuyOrder(); initCoinInfo(coinId, currencyPair, order); if (orderBookEntry == null) { orderBookEntry = loadOrderBookByCurrencyPair(currencyPair); } if (orderBookEntry == null) { return order; } BigDecimal amount = BigDecimal.ZERO; if (orderBookEntry.getLowestSellPrice() != null) { amount = orderBookEntry.getLowestSellPrice().getAmount(); } order.setSuggestedPrice(amount); if (portfolioEntry == null) { portfolioEntry = userServiceFacade.obtainPortfolioForUser(); } if (portfolioEntry != null) { order.setBalance(portfolioEntry.obtainMoneyToSpend().getAmount()); } return order; }  }
TradeServiceFacadeImpl implements TradeServiceFacade { @Override public BuyOrder prepareBuyOrder(String coinId, CurrencyPair currencyPair, OrderBookEntry orderBookEntry, PortfolioEntry portfolioEntry) { hasLength(coinId); notNull(currencyPair); isTrue(coinId.equalsIgnoreCase(currencyPair.getBaseCurrency())); BuyOrder order = new BuyOrder(); initCoinInfo(coinId, currencyPair, order); if (orderBookEntry == null) { orderBookEntry = loadOrderBookByCurrencyPair(currencyPair); } if (orderBookEntry == null) { return order; } BigDecimal amount = BigDecimal.ZERO; if (orderBookEntry.getLowestSellPrice() != null) { amount = orderBookEntry.getLowestSellPrice().getAmount(); } order.setSuggestedPrice(amount); if (portfolioEntry == null) { portfolioEntry = userServiceFacade.obtainPortfolioForUser(); } if (portfolioEntry != null) { order.setBalance(portfolioEntry.obtainMoneyToSpend().getAmount()); } return order; }  @Override OrderBookEntry loadOrderBookByCurrencyPair(CurrencyPair currencyPair); CoinEntry loadCoin(String coinId); @Override BigMoney calculateSellOrderEffectiveAmount(SellOrder order); @Override BigMoney calculateBuyOrderEffectiveAmount(BuyOrder order); @Override BuyOrder prepareBuyOrder(String coinId,
                                    CurrencyPair currencyPair,
                                    OrderBookEntry orderBookEntry,
                                    PortfolioEntry portfolioEntry); @Override SellOrder prepareSellOrder(String coinId,
                                      CurrencyPair currencyPair,
                                      OrderBookEntry orderBookEntry,
                                      PortfolioEntry portfolioEntry); @Override List<TradeExecutedEntry> findExecutedTrades(String orderBookIdentifier); @Override List<OrderEntry> findUserActiveOrders(String userId, String orderBookId); @Override List<PriceAggregate> findOrderAggregatedPrice(String orderBookIdentifier,
                                                         OrderType type,
                                                         Date toDate); @Override List<OrderEntry> findOrderForOrderBook(String orderBookIdentifier,
                                                  OrderType type,
                                                  OrderStatus orderStatus,
                                                  Pageable pageable); @Override void sellOrder(final TransactionId transactionId,
                          String coinId,
                          CurrencyPair currencyPair,
                          String orderBookId,
                          String portfolioId,
                          BigMoney tradeAmount,
                          BigMoney price); @Override void buyOrder(final TransactionId transactionId,
                         String coinId,
                         CurrencyPair currencyPair,
                         String orderBookId,
                         String portfolioId,
                         BigMoney tradeAmount,
                         BigMoney price); @Override void refreshOrderBookPrice(); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserServiceFacade(UserServiceFacade userServiceFacade); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setCoinRepository(CoinQueryRepository coinRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setOrderBookRepository(OrderBookQueryRepository orderBookRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setTradeExecutedRepository(TradeExecutedQueryRepository tradeExecutedRepository); @Autowired void setCommandGateway(CommandGateway commandGateway); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setOrderQueryRepository(OrderQueryRepository orderQueryRepository); @Autowired void setCommissionPolicyFactory(CommissionPolicyFactory commissionPolicyFactory); }
TradeServiceFacadeImpl implements TradeServiceFacade { @Override public BuyOrder prepareBuyOrder(String coinId, CurrencyPair currencyPair, OrderBookEntry orderBookEntry, PortfolioEntry portfolioEntry) { hasLength(coinId); notNull(currencyPair); isTrue(coinId.equalsIgnoreCase(currencyPair.getBaseCurrency())); BuyOrder order = new BuyOrder(); initCoinInfo(coinId, currencyPair, order); if (orderBookEntry == null) { orderBookEntry = loadOrderBookByCurrencyPair(currencyPair); } if (orderBookEntry == null) { return order; } BigDecimal amount = BigDecimal.ZERO; if (orderBookEntry.getLowestSellPrice() != null) { amount = orderBookEntry.getLowestSellPrice().getAmount(); } order.setSuggestedPrice(amount); if (portfolioEntry == null) { portfolioEntry = userServiceFacade.obtainPortfolioForUser(); } if (portfolioEntry != null) { order.setBalance(portfolioEntry.obtainMoneyToSpend().getAmount()); } return order; }  @Override OrderBookEntry loadOrderBookByCurrencyPair(CurrencyPair currencyPair); CoinEntry loadCoin(String coinId); @Override BigMoney calculateSellOrderEffectiveAmount(SellOrder order); @Override BigMoney calculateBuyOrderEffectiveAmount(BuyOrder order); @Override BuyOrder prepareBuyOrder(String coinId,
                                    CurrencyPair currencyPair,
                                    OrderBookEntry orderBookEntry,
                                    PortfolioEntry portfolioEntry); @Override SellOrder prepareSellOrder(String coinId,
                                      CurrencyPair currencyPair,
                                      OrderBookEntry orderBookEntry,
                                      PortfolioEntry portfolioEntry); @Override List<TradeExecutedEntry> findExecutedTrades(String orderBookIdentifier); @Override List<OrderEntry> findUserActiveOrders(String userId, String orderBookId); @Override List<PriceAggregate> findOrderAggregatedPrice(String orderBookIdentifier,
                                                         OrderType type,
                                                         Date toDate); @Override List<OrderEntry> findOrderForOrderBook(String orderBookIdentifier,
                                                  OrderType type,
                                                  OrderStatus orderStatus,
                                                  Pageable pageable); @Override void sellOrder(final TransactionId transactionId,
                          String coinId,
                          CurrencyPair currencyPair,
                          String orderBookId,
                          String portfolioId,
                          BigMoney tradeAmount,
                          BigMoney price); @Override void buyOrder(final TransactionId transactionId,
                         String coinId,
                         CurrencyPair currencyPair,
                         String orderBookId,
                         String portfolioId,
                         BigMoney tradeAmount,
                         BigMoney price); @Override void refreshOrderBookPrice(); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserServiceFacade(UserServiceFacade userServiceFacade); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setCoinRepository(CoinQueryRepository coinRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setOrderBookRepository(OrderBookQueryRepository orderBookRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setTradeExecutedRepository(TradeExecutedQueryRepository tradeExecutedRepository); @Autowired void setCommandGateway(CommandGateway commandGateway); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setOrderQueryRepository(OrderQueryRepository orderQueryRepository); @Autowired void setCommissionPolicyFactory(CommissionPolicyFactory commissionPolicyFactory);  }