Search in sources :

Example 1 with TickerDTO

use of tech.cassandre.trading.bot.dto.market.TickerDTO in project cassandre-trading-bot by cassandre-tech.

the class MarketServiceTest method checkGetTicker.

@Test
@Tag("integration")
@DisplayName("Check get ticker")
public void checkGetTicker() {
    CurrencyPairDTO cp = new CurrencyPairDTO(ETH, BTC);
    Set<TickerDTO> tickers = marketService.getTickers(Collections.singleton(cp));
    final Optional<TickerDTO> t = tickers.stream().filter(tickerDTO -> tickerDTO.getCurrencyPair().equals(cp)).findFirst();
    assertTrue(t.isPresent());
    // currencyPair.
    assertNotNull(t.get().getCurrencyPair());
    assertEquals(t.get().getCurrencyPair(), cp);
    // open.
    assertNull(t.get().getOpen());
    // last.
    assertNotNull(t.get().getLast());
    assertTrue(t.get().getLast().compareTo(ZERO) >= 0);
    // bid.
    // assertNotNull(t.get().getBid());
    // assertTrue(t.get().getBid().compareTo(ZERO) > 0);
    // ask.
    // assertNotNull(t.get().getAsk());
    // assertTrue(t.get().getAsk().compareTo(ZERO) > 0);
    // volume.
    assertNotNull(t.get().getVolume());
    // assertTrue(t.get().getVolume().compareTo(ZERO) > 0);
    // quote volume.
    assertNotNull(t.get().getQuoteVolume());
    // assertTrue(t.get().getQuoteVolume().compareTo(ZERO) > 0);
    // bidSize.
    assertNull(t.get().getBidSize());
    // askSize.
    assertNull(t.get().getAskSize());
    // timestamp.
    assertNotNull(t.get().getTimestamp());
    assertTrue(t.get().getTimestamp().isAfter(ZonedDateTime.now().minusMinutes(1)));
    assertTrue(t.get().getTimestamp().isBefore(ZonedDateTime.now().plusMinutes(1)));
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) ZERO(java.math.BigDecimal.ZERO) ActiveProfiles(org.springframework.test.context.ActiveProfiles) TestPropertySource(org.springframework.test.context.TestPropertySource) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) MarketService(tech.cassandre.trading.bot.service.MarketService) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) BTC(tech.cassandre.trading.bot.dto.util.CurrencyDTO.BTC) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) Optional(java.util.Optional) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) Collections(java.util.Collections) ETH(tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName) Tag(org.junit.jupiter.api.Tag)

Example 2 with TickerDTO

use of tech.cassandre.trading.bot.dto.market.TickerDTO in project cassandre-trading-bot by cassandre-tech.

the class Issue736Test method testImportedTickers.

@Test
@DisplayName("Imported tickers")
public void testImportedTickers() {
    // We wait for initialized to be sure all data have been imported.
    await().untilAsserted(() -> assertTrue(strategy.isInitialized()));
    // 5 tickers imported from tickers-to-import-1.csv & tickers-to-import-2.csv.
    assertEquals(5, importedTickerRepository.count());
    // =============================================================================================================
    // BTC/USDT.
    // Ticker 1 : BTC/USDT - 0.00000001 - 1508546000 (Saturday 21 October 2017 00:33:20)
    // Ticker 2 : BTC/USDT - 1.00000001 - 1508446000 (Thursday 19 October 2017 20:46:40)
    // Order expected: ticker 2, ticker 1.
    List<TickerDTO> btcUsdtTickers = strategy.getImportedTickers(BTC_USDT);
    assertEquals(2, btcUsdtTickers.size());
    // Ticker 1
    TickerDTO ticker1 = btcUsdtTickers.get(0);
    assertNotNull(ticker1);
    assertEquals(BTC_USDT, ticker1.getCurrencyPair());
    assertEquals(0, new BigDecimal("1.00000001").compareTo(ticker1.getOpen()));
    assertEquals(0, new BigDecimal("1.00000002").compareTo(ticker1.getLast()));
    assertEquals(0, new BigDecimal("1.00000003").compareTo(ticker1.getBid()));
    assertEquals(0, new BigDecimal("1.00000004").compareTo(ticker1.getAsk()));
    assertEquals(0, new BigDecimal("1.00000005").compareTo(ticker1.getHigh()));
    assertEquals(0, new BigDecimal("1.00000006").compareTo(ticker1.getLow()));
    assertEquals(0, new BigDecimal("1.00000007").compareTo(ticker1.getVwap()));
    assertEquals(0, new BigDecimal("1.00000008").compareTo(ticker1.getVolume()));
    assertEquals(0, new BigDecimal("1.00000009").compareTo(ticker1.getQuoteVolume()));
    assertEquals(0, new BigDecimal("1.00000010").compareTo(ticker1.getBidSize()));
    assertEquals(0, new BigDecimal("1.00000011").compareTo(ticker1.getAskSize()));
    assertEquals(19, ticker1.getTimestamp().getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker1.getTimestamp().getMonth());
    assertEquals(2017, ticker1.getTimestamp().getYear());
    // Ticker 2
    TickerDTO ticker2 = btcUsdtTickers.get(1);
    assertNotNull(ticker2);
    assertEquals(BTC_USDT, ticker2.getCurrencyPair());
    assertEquals(0, new BigDecimal("0.00000001").compareTo(ticker2.getOpen()));
    assertEquals(0, new BigDecimal("0.00000002").compareTo(ticker2.getLast()));
    assertEquals(0, new BigDecimal("0.00000003").compareTo(ticker2.getBid()));
    assertEquals(0, new BigDecimal("0.00000004").compareTo(ticker2.getAsk()));
    assertEquals(0, new BigDecimal("0.00000005").compareTo(ticker2.getHigh()));
    assertEquals(0, new BigDecimal("0.00000006").compareTo(ticker2.getLow()));
    assertEquals(0, new BigDecimal("0.00000007").compareTo(ticker2.getVwap()));
    assertEquals(0, new BigDecimal("0.00000008").compareTo(ticker2.getVolume()));
    assertEquals(0, new BigDecimal("0.00000009").compareTo(ticker2.getQuoteVolume()));
    assertEquals(0, new BigDecimal("0.00000010").compareTo(ticker2.getBidSize()));
    assertEquals(0, new BigDecimal("0.00000011").compareTo(ticker2.getAskSize()));
    assertEquals(21, ticker2.getTimestamp().getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker2.getTimestamp().getMonth());
    assertEquals(2017, ticker2.getTimestamp().getYear());
    // =============================================================================================================
    // ETH/USDT.
    // Ticker 1 : ETH/USDT - 2.00000001 - 1508346000 (Wednesday 18 October 2017 17:00:00)
    // Ticker 2 : ETH/USDT - 1 BID_SIZE - 1508746000 (Monday 23 October 2017 08:06:40)
    // Ticker 3 : ETH/USDT - 2 ASK_SIZE - 1508646000 (Sunday 22 October 2017 04:20:00)
    // Order expected: ticker 1, ticker 3, ticker 2.
    List<TickerDTO> ethUsdtTickers = strategy.getImportedTickers(ETH_USDT);
    assertEquals(3, ethUsdtTickers.size());
    // Ticker 3
    TickerDTO ticker3 = ethUsdtTickers.get(0);
    assertNotNull(ticker3);
    assertEquals(ETH_USDT, ticker3.getCurrencyPair());
    assertEquals(0, new BigDecimal("2.00000001").compareTo(ticker3.getOpen()));
    assertEquals(0, new BigDecimal("2.00000002").compareTo(ticker3.getLast()));
    assertEquals(0, new BigDecimal("2.00000003").compareTo(ticker3.getBid()));
    assertEquals(0, new BigDecimal("2.00000004").compareTo(ticker3.getAsk()));
    assertEquals(0, new BigDecimal("2.00000005").compareTo(ticker3.getHigh()));
    assertEquals(0, new BigDecimal("2.00000006").compareTo(ticker3.getLow()));
    assertEquals(0, new BigDecimal("2.00000007").compareTo(ticker3.getVwap()));
    assertEquals(0, new BigDecimal("2.00000008").compareTo(ticker3.getVolume()));
    assertEquals(0, new BigDecimal("2.00000009").compareTo(ticker3.getQuoteVolume()));
    assertEquals(0, new BigDecimal("2.00000010").compareTo(ticker3.getBidSize()));
    assertEquals(0, new BigDecimal("2.00000011").compareTo(ticker3.getAskSize()));
    assertEquals(18, ticker3.getTimestamp().getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker3.getTimestamp().getMonth());
    assertEquals(2017, ticker3.getTimestamp().getYear());
    // Ticker 4
    TickerDTO ticker4 = ethUsdtTickers.get(1);
    assertNotNull(ticker4);
    assertEquals(ETH_USDT, ticker4.getCurrencyPair());
    assertNull(ticker4.getOpen());
    assertNull(ticker4.getLast());
    assertNull(ticker4.getBid());
    assertNull(ticker4.getAsk());
    assertNull(ticker4.getHigh());
    assertNull(ticker4.getLow());
    assertNull(ticker4.getVwap());
    assertNull(ticker4.getOpen());
    assertNull(ticker4.getVolume());
    assertNull(ticker4.getQuoteVolume());
    assertNull(ticker4.getBidSize());
    assertEquals(0, new BigDecimal("2").compareTo(ticker4.getAskSize()));
    assertEquals(22, ticker4.getTimestamp().getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker4.getTimestamp().getMonth());
    assertEquals(2017, ticker4.getTimestamp().getYear());
    // Ticker 5
    TickerDTO ticker5 = ethUsdtTickers.get(2);
    assertNotNull(ticker5);
    assertEquals(ETH_USDT, ticker5.getCurrencyPair());
    assertNull(ticker5.getOpen());
    assertNull(ticker5.getLast());
    assertNull(ticker5.getBid());
    assertNull(ticker5.getAsk());
    assertNull(ticker5.getHigh());
    assertNull(ticker5.getLow());
    assertNull(ticker5.getVwap());
    assertNull(ticker5.getOpen());
    assertNull(ticker5.getVolume());
    assertNull(ticker5.getQuoteVolume());
    assertEquals(0, new BigDecimal("1").compareTo(ticker5.getBidSize()));
    assertNull(ticker5.getAskSize());
    assertEquals(23, ticker5.getTimestamp().getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker5.getTimestamp().getMonth());
    assertEquals(2017, ticker5.getTimestamp().getYear());
    // Test all tickers order.
    // Order: ETH/USDT-1, BTC/USDT-2, BTC/USDT-1, ETH/USDT-3, ETH/USDT-2.
    List<TickerDTO> allTickers = strategy.getImportedTickers();
    assertEquals(5, allTickers.size());
    ZonedDateTime ticker1Date = allTickers.get(0).getTimestamp();
    assertEquals(18, ticker1Date.getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker1Date.getMonth());
    assertEquals(2017, ticker1Date.getYear());
    ZonedDateTime ticker2Date = allTickers.get(1).getTimestamp();
    assertEquals(19, ticker2Date.getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker2Date.getMonth());
    assertEquals(2017, ticker2Date.getYear());
    ZonedDateTime ticker3Date = allTickers.get(2).getTimestamp();
    assertEquals(21, ticker3Date.getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker3Date.getMonth());
    assertEquals(2017, ticker3Date.getYear());
    ZonedDateTime ticker4Date = allTickers.get(3).getTimestamp();
    assertEquals(22, ticker4Date.getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker4Date.getMonth());
    assertEquals(2017, ticker4Date.getYear());
    ZonedDateTime ticker5Date = allTickers.get(4).getTimestamp();
    assertEquals(23, ticker5Date.getDayOfMonth());
    assertEquals(Month.OCTOBER, ticker5Date.getMonth());
    assertEquals(2017, ticker5Date.getYear());
}
Also used : ZonedDateTime(java.time.ZonedDateTime) TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with TickerDTO

use of tech.cassandre.trading.bot.dto.market.TickerDTO in project cassandre-trading-bot by cassandre-tech.

the class MultipleStrategiesTest method checkMultipleStrategyBehavior.

@Test
@DisplayName("Check multiple strategies behavior with dry mode")
public void checkMultipleStrategyBehavior() {
    // ==============================================================================================================
    // Strategies tested.
    // Strategy 1 - Requesting BTC/USDT.
    // Strategy 2 - Requesting BTC/ETH.
    // Strategy 3 - Requesting BTC/USDT & ETH/USDT.
    // ==============================================================================================================
    // Checking the MarketService have all the currency pairs.
    final Set<CurrencyPairDTO> availableCurrencyPairs = exchangeService.getAvailableCurrencyPairs();
    assertEquals(3, availableCurrencyPairs.size());
    assertTrue(availableCurrencyPairs.contains(BTC_USDT));
    assertTrue(availableCurrencyPairs.contains(BTC_ETH));
    assertTrue(availableCurrencyPairs.contains(ETH_USDT));
    // ==============================================================================================================
    // Checking the three strategies is stored in the database.
    assertEquals(3, strategyRepository.count());
    final Optional<Strategy> s1 = strategyRepository.findByStrategyId("01");
    assertTrue(s1.isPresent());
    assertEquals(1, s1.get().getUid());
    assertEquals("01", s1.get().getStrategyId());
    assertEquals("Strategy 1", s1.get().getName());
    final Optional<Strategy> s2 = strategyRepository.findByStrategyId("02");
    assertTrue(s2.isPresent());
    assertEquals(2, s2.get().getUid());
    assertEquals("02", s2.get().getStrategyId());
    assertEquals("Strategy 2", s2.get().getName());
    final Optional<Strategy> s3 = strategyRepository.findByStrategyId("03");
    assertTrue(s3.isPresent());
    assertEquals(3, s3.get().getUid());
    assertEquals("03", s3.get().getStrategyId());
    assertEquals("Strategy 3", s3.get().getName());
    // ==============================================================================================================
    // Check balances on each strategy & onAccountUpdate(), we should have the same values in all strategies.
    // As we are in dry mode, balances are loaded from:
    // spring-boot-starter/autoconfigure/src/test/resources/user-main.csv
    // spring-boot-starter/autoconfigure/src/test/resources/user-trade.csv
    // spring-boot-starter/autoconfigure/src/test/resources/user-savings.csv
    accountFlux.update();
    await().untilAsserted(() -> assertEquals(3, strategy3.getAccountsUpdatesReceived().size()));
    // Strategy 1 accounts data test.
    Map<String, AccountDTO> strategy1Accounts = strategy1.getAccounts();
    Optional<AccountDTO> strategy1TradeAccount = strategy1.getTradeAccount();
    assertEquals(3, strategy1Accounts.size());
    assertTrue(strategy1Accounts.containsKey("main"));
    assertTrue(strategy1Accounts.containsKey("trade"));
    assertTrue(strategy1Accounts.containsKey("savings"));
    assertTrue(strategy1TradeAccount.isPresent());
    assertEquals("trade", strategy1TradeAccount.get().getName());
    assertEquals(3, strategy1TradeAccount.get().getBalances().size());
    assertEquals(3, strategy1TradeAccount.get().getBalances().size());
    assertTrue(strategy1TradeAccount.get().getBalance(BTC).isPresent());
    assertEquals(0, new BigDecimal("0.99962937").compareTo(strategy1TradeAccount.get().getBalance(BTC).get().getAvailable()));
    assertTrue(strategy1TradeAccount.get().getBalance(USDT).isPresent());
    assertEquals(0, new BigDecimal("1000").compareTo(strategy1TradeAccount.get().getBalance(USDT).get().getAvailable()));
    assertTrue(strategy1TradeAccount.get().getBalance(ETH).isPresent());
    assertEquals(0, new BigDecimal("10").compareTo(strategy1TradeAccount.get().getBalance(ETH).get().getAvailable()));
    // Strategy 2 accounts data test.
    Map<String, AccountDTO> strategy2Accounts = strategy2.getAccounts();
    Optional<AccountDTO> strategy2TradeAccount = strategy2.getTradeAccount();
    assertEquals(3, strategy2Accounts.size());
    assertTrue(strategy2Accounts.containsKey("main"));
    assertTrue(strategy2Accounts.containsKey("trade"));
    assertTrue(strategy2Accounts.containsKey("savings"));
    assertTrue(strategy2TradeAccount.isPresent());
    assertEquals("trade", strategy2TradeAccount.get().getName());
    assertEquals(3, strategy2TradeAccount.get().getBalances().size());
    assertTrue(strategy2TradeAccount.get().getBalance(BTC).isPresent());
    assertEquals(0, new BigDecimal("0.99962937").compareTo(strategy2TradeAccount.get().getBalance(BTC).get().getAvailable()));
    assertTrue(strategy2TradeAccount.get().getBalance(USDT).isPresent());
    assertEquals(0, new BigDecimal("1000").compareTo(strategy2TradeAccount.get().getBalance(USDT).get().getAvailable()));
    assertTrue(strategy2TradeAccount.get().getBalance(ETH).isPresent());
    assertEquals(0, new BigDecimal("10").compareTo(strategy2TradeAccount.get().getBalance(ETH).get().getAvailable()));
    // Strategy 3 accounts data test.
    Map<String, AccountDTO> strategy3Accounts = strategy3.getAccounts();
    Optional<AccountDTO> strategy3TradeAccount = strategy3.getTradeAccount();
    assertEquals(3, strategy3Accounts.size());
    assertTrue(strategy3Accounts.containsKey("main"));
    assertTrue(strategy3Accounts.containsKey("trade"));
    assertTrue(strategy3Accounts.containsKey("savings"));
    assertTrue(strategy3TradeAccount.isPresent());
    assertEquals("trade", strategy3TradeAccount.get().getName());
    assertEquals(3, strategy3TradeAccount.get().getBalances().size());
    assertTrue(strategy3TradeAccount.get().getBalance(BTC).isPresent());
    assertEquals(0, new BigDecimal("0.99962937").compareTo(strategy3TradeAccount.get().getBalance(BTC).get().getAvailable()));
    assertTrue(strategy3TradeAccount.get().getBalance(USDT).isPresent());
    assertEquals(0, new BigDecimal("1000").compareTo(strategy3TradeAccount.get().getBalance(USDT).get().getAvailable()));
    assertTrue(strategy3TradeAccount.get().getBalance(ETH).isPresent());
    assertEquals(0, new BigDecimal("10").compareTo(strategy3TradeAccount.get().getBalance(ETH).get().getAvailable()));
    // ==============================================================================================================
    // Emitting tickers and checking received tickers by strategies.
    // Sending BTC/USDT - BTC/ETH - ETH/USDT.
    // Strategy 1 - Requesting BTC/USDT should receive one ticker - 1 BTC costs 50 000 USDT.
    // Strategy 2 - Requesting BTC/ETH should receive one ticker - 1 BTC costs 25 ETH.
    // Strategy 3 - Requesting BTC/USDT & ETH/USDT should receive two tickers - 1 ETH costs 2000 USDT.
    tickerFlux.emitValue(TickerDTO.builder().currencyPair(BTC_USDT).last(new BigDecimal("50000")).build());
    tickerFlux.emitValue(TickerDTO.builder().currencyPair(BTC_ETH).last(new BigDecimal("25")).build());
    tickerFlux.emitValue(TickerDTO.builder().currencyPair(ETH_USDT).last(new BigDecimal("2000")).build());
    await().untilAsserted(() -> assertEquals(2, strategy3.getTickersUpdatesReceived().size()));
    // Strategy 1.
    assertEquals(1, strategy1.getTickersUpdatesReceived().size());
    final TickerDTO strategy1Ticker1 = strategy1.getTickersUpdatesReceived().get(0);
    assertNotNull(strategy1Ticker1);
    assertEquals(BTC_USDT, strategy1Ticker1.getCurrencyPair());
    assertEquals(0, new BigDecimal("50000").compareTo(strategy1Ticker1.getLast()));
    // Strategy 2.
    assertEquals(1, strategy2.getTickersUpdatesReceived().size());
    final TickerDTO strategy2Ticker1 = strategy2.getTickersUpdatesReceived().get(0);
    assertNotNull(strategy2Ticker1);
    assertEquals(BTC_ETH, strategy2Ticker1.getCurrencyPair());
    assertEquals(0, new BigDecimal("25").compareTo(strategy2Ticker1.getLast()));
    // Strategy 3.
    assertEquals(2, strategy3.getTickersUpdatesReceived().size());
    final TickerDTO strategy3Ticker1 = strategy3.getTickersUpdatesReceived().get(0);
    assertNotNull(strategy3Ticker1);
    assertEquals(BTC_USDT, strategy3Ticker1.getCurrencyPair());
    assertEquals(0, new BigDecimal("50000").compareTo(strategy3Ticker1.getLast()));
    final TickerDTO strategy3Ticker2 = strategy3.getTickersUpdatesReceived().get(1);
    assertNotNull(strategy3Ticker2);
    assertEquals(ETH_USDT, strategy3Ticker2.getCurrencyPair());
    assertEquals(0, new BigDecimal("2000").compareTo(strategy3Ticker2.getLast()));
    // ==============================================================================================================
    // Strategy 1 - Creating 1 position on BTC/USDT (0.001 BTC for 50 USDT).
    // The price of 1 BTC is 50 000 USDT, and we buy 0.001 BTC for 50 USDT.
    // We stop at 100% gain.
    final PositionCreationResultDTO position1Result = strategy1.createLongPosition(BTC_USDT, new BigDecimal("0.001"), PositionRulesDTO.builder().stopGainPercentage(100f).build());
    assertTrue(position1Result.isSuccessful());
    final long position1Uid = position1Result.getPosition().getUid();
    final long position1PositionId = position1Result.getPosition().getPositionId();
    await().untilAsserted(() -> {
        orderFlux.update();
        tradeFlux.update();
        assertEquals(OPENED, getPositionDTO(position1Uid).getStatus());
    });
    // Check position uid & position id.
    assertEquals(1, position1Uid);
    assertEquals(1, position1PositionId);
    // Check onPositionUpdate() & onPositionStatusUpdate().
    // For strategy 1:
    // 4 positions updates:
    // - Position OPENING with opening order with PENDING_NEW status.
    // - Position OPENING with opening order with NEW status.
    // - Position OPENING with opening order with FILLED status.
    // - Position OPENED with opening order with FILLED status and the corresponding trade.
    // Positions status updates 2: OPENING and then OPENED.
    await().untilAsserted(() -> assertEquals(4, strategy1.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy1.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy2.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy2.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy3.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy3.getPositionsUpdatesReceived().size()));
    // Check onOrderUpdate().
    // - Order with NEW status.
    // - Order with FILLED status.
    // (PENDING_NEW orders are not in events).
    await().untilAsserted(() -> assertEquals(2, strategy1.getOrdersUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy2.getOrdersUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy3.getOrdersUpdatesReceived().size()));
    // Check onTradeUpdate().
    // - Trade arrives for the opening order.
    await().untilAsserted(() -> assertEquals(1, strategy1.getTradesUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy2.getTradesUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy3.getTradesUpdatesReceived().size()));
    // Check getOrders() & getOrderByOrderId().
    // Only strategy 1 should have received an order.
    assertEquals(1, strategy1.getOrders().size());
    assertTrue(strategy1.getOrderByOrderId("DRY_ORDER_000000001").isPresent());
    assertEquals(0, strategy2.getOrders().size());
    assertTrue(strategy2.getOrderByOrderId("DRY_ORDER_000000001").isEmpty());
    assertEquals(0, strategy3.getOrders().size());
    assertTrue(strategy3.getOrderByOrderId("DRY_ORDER_000000001").isEmpty());
    // Check getTrades() & getTradeByTradeId().
    // Only strategy 1 should have received a trade.
    assertEquals(1, strategy1.getTrades().size());
    assertTrue(strategy1.getTradeByTradeId("DRY_TRADE_000000001").isPresent());
    assertEquals(0, strategy2.getTrades().size());
    assertTrue(strategy2.getTradeByTradeId("DRY_TRADE_000000001").isEmpty());
    assertEquals(0, strategy3.getTrades().size());
    assertTrue(strategy3.getTradeByTradeId("DRY_TRADE_000000001").isEmpty());
    // Check getAmountsLockedByPosition().
    BigDecimal amountLockedForBTC = strategy1.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.001").compareTo(amountLockedForBTC));
    amountLockedForBTC = strategy2.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.001").compareTo(amountLockedForBTC));
    amountLockedForBTC = strategy3.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.001").compareTo(amountLockedForBTC));
    // ==============================================================================================================
    // Strategy 2 - Creating 1 position on BTC/ETH (0.2 BTC for 0.5 ETH).
    // The price of 1 BTC is 25 ETH, and we buy 0.02 BTC for 0.5 ETH.
    // We stop at 100% gain.
    final PositionCreationResultDTO position2Result = strategy2.createLongPosition(BTC_ETH, new BigDecimal("0.02"), PositionRulesDTO.builder().stopGainPercentage(100f).build());
    assertTrue(position2Result.isSuccessful());
    final long position2Uid = position2Result.getPosition().getUid();
    final long position2PositionId = position2Result.getPosition().getPositionId();
    await().untilAsserted(() -> {
        orderFlux.update();
        tradeFlux.update();
        assertEquals(OPENED, getPositionDTO(position2Uid).getStatus());
    });
    // Check position uid & position id.
    assertEquals(2, position2Uid);
    assertEquals(1, position2PositionId);
    // Check onPositionUpdate() & onPositionStatusUpdate().
    // For strategy 2:
    // 4 positions updates:
    // - Position OPENING with opening order with PENDING_NEW status.
    // - Position OPENING with opening order with NEW status.
    // - Position OPENING with opening order with FILLED status.
    // - Position OPENED with opening order with FILLED status and the corresponding trade.
    // Positions status updates 2: OPENING and then OPENED.
    await().untilAsserted(() -> assertEquals(4, strategy1.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy1.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(4, strategy2.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy2.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy3.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy3.getPositionsUpdatesReceived().size()));
    // Check onOrderUpdate().
    // - Order with NEW status.
    // - Order with FILLED status.
    // (PENDING_NEW orders are not in events).
    await().untilAsserted(() -> assertEquals(2, strategy1.getOrdersUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy2.getOrdersUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy3.getOrdersUpdatesReceived().size()));
    // Check onTradeUpdate().
    // - Trade arrives for the opening order.
    await().untilAsserted(() -> assertEquals(1, strategy1.getTradesUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(1, strategy2.getTradesUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(0, strategy3.getTradesUpdatesReceived().size()));
    // Check getOrders() & getOrderByOrderId().
    assertEquals(1, strategy1.getOrders().size());
    assertTrue(strategy1.getOrderByOrderId("DRY_ORDER_000000001").isPresent());
    assertEquals(1, strategy2.getOrders().size());
    assertTrue(strategy2.getOrderByOrderId("DRY_ORDER_000000002").isPresent());
    assertEquals(0, strategy3.getOrders().size());
    assertTrue(strategy3.getOrderByOrderId("DRY_ORDER_000000001").isEmpty());
    // Check getTrades() & getTradeByTradeId().
    assertEquals(1, strategy1.getTrades().size());
    assertTrue(strategy1.getTradeByTradeId("DRY_TRADE_000000001").isPresent());
    assertEquals(1, strategy2.getTrades().size());
    assertTrue(strategy2.getTradeByTradeId("DRY_TRADE_000000002").isPresent());
    assertEquals(0, strategy3.getTrades().size());
    assertTrue(strategy3.getTradeByTradeId("DRY_TRADE_000000001").isEmpty());
    // Check getAmountsLockedByPosition().
    amountLockedForBTC = strategy1.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.021").compareTo(amountLockedForBTC));
    amountLockedForBTC = strategy2.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.021").compareTo(amountLockedForBTC));
    amountLockedForBTC = strategy3.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.021").compareTo(amountLockedForBTC));
    // =============================================================================================================
    // Changing the price for BTC/USDT.
    // A bitcoin now costs 10000 USDT and the price of position should have change.
    tickerFlux.emitValue(TickerDTO.builder().currencyPair(BTC_USDT).last(new BigDecimal("10000")).build());
    await().untilAsserted(() -> assertEquals(3, strategy3.getTickersUpdatesReceived().size()));
    assertEquals(2, strategy1.getTickersUpdatesReceived().size());
    assertEquals(1, strategy2.getTickersUpdatesReceived().size());
    assertEquals(3, strategy3.getTickersUpdatesReceived().size());
    // =============================================================================================================
    // Strategy 3
    // - Creating one position on BTC/USDT (0.01 BTC for 100 USDT).
    final PositionCreationResultDTO position3Result = strategy3.createLongPosition(BTC_USDT, new BigDecimal("0.01"), PositionRulesDTO.builder().stopGainPercentage(100f).build());
    assertTrue(position3Result.isSuccessful());
    final long position3Uid = position3Result.getPosition().getUid();
    final long position3PositionId = position3Result.getPosition().getPositionId();
    await().untilAsserted(() -> {
        orderFlux.update();
        tradeFlux.update();
        assertEquals(OPENED, getPositionDTO(position3Uid).getStatus());
    });
    // - Creating one position on ETH/USDT (0.1 ETH for 200 USDT).
    final PositionCreationResultDTO position4Result = strategy3.createLongPosition(ETH_USDT, new BigDecimal("0.1"), PositionRulesDTO.builder().stopGainPercentage(100f).build());
    assertTrue(position4Result.isSuccessful());
    final long position4Uid = position4Result.getPosition().getUid();
    final long position4PositionId = position4Result.getPosition().getPositionId();
    await().untilAsserted(() -> {
        orderFlux.update();
        tradeFlux.update();
        assertEquals(OPENED, getPositionDTO(position4Uid).getStatus());
    });
    // Check position uid & position id.
    assertEquals(3, position3Uid);
    assertEquals(1, position3PositionId);
    assertEquals(4, position4Uid);
    assertEquals(2, position4PositionId);
    // Check onPositionUpdate() & onPositionStatusUpdate().
    // 8 positions updates:
    // - 2 positions OPENING with opening order with PENDING_NEW status.
    // - 2 positions OPENING with opening order with NEW status.
    // - 2 positions OPENING with opening order with FILLED status.
    // - 2 positions OPENED with opening order with FILLED status and the corresponding trade.
    // Positions status updates 4: 2 orders OPENING and then OPENED.
    await().untilAsserted(() -> assertEquals(5, strategy1.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy1.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(4, strategy2.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy2.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(8, strategy3.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(4, strategy3.getPositionsStatusUpdatesReceived().size()));
    // Check onOrderUpdate().
    await().untilAsserted(() -> assertEquals(2, strategy1.getOrdersUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy2.getOrdersUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(4, strategy3.getOrdersUpdatesReceived().size()));
    // Check onTradeUpdate().
    await().untilAsserted(() -> assertEquals(1, strategy1.getTradesUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(1, strategy2.getTradesUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy3.getTradesUpdatesReceived().size()));
    // Check getOrders() & getOrderByOrderId().
    // - 2 orders with NEW status.
    // - 2 orders with FILLED status.
    // (PENDING_NEW orders are not in events).
    assertEquals(1, strategy1.getOrders().size());
    assertTrue(strategy1.getOrderByOrderId("DRY_ORDER_000000001").isPresent());
    assertEquals(1, strategy2.getOrders().size());
    assertTrue(strategy2.getOrderByOrderId("DRY_ORDER_000000002").isPresent());
    assertEquals(2, strategy3.getOrders().size());
    assertTrue(strategy3.getOrderByOrderId("DRY_ORDER_000000003").isPresent());
    assertTrue(strategy3.getOrderByOrderId("DRY_ORDER_000000004").isPresent());
    // Check getTrades() & getTradeByTradeId().
    // - 2 trades arrives for the 2 opening orders.
    assertEquals(1, strategy1.getTrades().size());
    assertTrue(strategy1.getTradeByTradeId("DRY_TRADE_000000001").isPresent());
    assertEquals(1, strategy2.getTrades().size());
    assertTrue(strategy2.getTradeByTradeId("DRY_TRADE_000000002").isPresent());
    assertEquals(2, strategy3.getTrades().size());
    assertTrue(strategy3.getTradeByTradeId("DRY_TRADE_000000003").isPresent());
    assertTrue(strategy3.getTradeByTradeId("DRY_TRADE_000000004").isPresent());
    // Check getAmountsLockedByPosition().
    amountLockedForBTC = strategy1.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.031").compareTo(amountLockedForBTC));
    amountLockedForBTC = strategy2.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.031").compareTo(amountLockedForBTC));
    amountLockedForBTC = strategy3.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.031").compareTo(amountLockedForBTC));
    BigDecimal amountLockedForETH = strategy1.getAmountsLockedByCurrency(ETH);
    assertEquals(0, new BigDecimal("0.1").compareTo(amountLockedForETH));
    amountLockedForETH = strategy2.getAmountsLockedByCurrency(ETH);
    assertEquals(0, new BigDecimal("0.1").compareTo(amountLockedForETH));
    amountLockedForETH = strategy3.getAmountsLockedByCurrency(ETH);
    assertEquals(0, new BigDecimal("0.1").compareTo(amountLockedForETH));
    // ==============================================================================================================
    // Check balances, canBuy() & canSell().
    // At the beginning my balances was:
    // - 0.99962937 BTC.
    // - 1 000 USDT.
    // - 10 ETH.
    // ---
    // Position 1 - Bought 0.001 BTC for 50 USDT - BTC/USDT : 50000.
    // Position 2 - Bought 0.2 BTC for 0.5 ETH - BTC/ETH : 25.
    // Position 3 - Bought 0.01 BTC for 100 USDT - BTC/USDT : 10000
    // Position 4 - Bought 0.1 ETH for 200 USDT - ETH/USDT : 2000.
    // --
    // BTC : 0.99962937 + 0.001 (P1) + 0.02 (P2) + 0.01 (P2) = 1.03062937 BTC
    // USDT : 1000 - 50 (P1) - 100 (P3) - 200 (P4) = 650 USDT
    // ETH : 10 - 0.5 (P2) + 0.1 (P4) = 5.1 ETH
    accountFlux.update();
    await().untilAsserted(() -> assertEquals(4, strategy3.getAccountsUpdatesReceived().size()));
    // Strategy 1 balances.
    assertEquals(0, new BigDecimal("1.03062937").compareTo(strategy1.getTradeAccountBalances().get(BTC).getAvailable()));
    assertEquals(0, new BigDecimal("650").compareTo(strategy1.getTradeAccountBalances().get(USDT).getAvailable()));
    assertEquals(0, new BigDecimal("9.6").compareTo(strategy1.getTradeAccountBalances().get(ETH).getAvailable()));
    // Strategy 2 balances.
    assertEquals(0, new BigDecimal("1.03062937").compareTo(strategy2.getTradeAccountBalances().get(BTC).getAvailable()));
    assertEquals(0, new BigDecimal("650").compareTo(strategy2.getTradeAccountBalances().get(USDT).getAvailable()));
    assertEquals(0, new BigDecimal("9.6").compareTo(strategy2.getTradeAccountBalances().get(ETH).getAvailable()));
    // Strategy 3 balances.
    assertEquals(0, new BigDecimal("1.03062937").compareTo(strategy3.getTradeAccountBalances().get(BTC).getAvailable()));
    assertEquals(0, new BigDecimal("650").compareTo(strategy3.getTradeAccountBalances().get(USDT).getAvailable()));
    assertEquals(0, new BigDecimal("9.6").compareTo(strategy3.getTradeAccountBalances().get(ETH).getAvailable()));
    // ==============================================================================================================
    // New ticker on BTC USDT to close a position.
    // Before : BTC/USDT - 10 000
    // New : BTC/USDT - 20 000 => Should trigger closing Position 3 (Position 1 of strategy 3).
    // Position 1 - Bought 0.001 BTC for 50 USDT - BTC/USDT : 50000.
    // Position 2 - Bought 0.2 BTC for 0.5 ETH - BTC/ETH : 25.
    // Position 3 - Bought 0.01 BTC for 100 USDT - BTC/USDT : 20000 => Now close, sold 0.01 BTC for 200 USDT.
    // Position 4 - Bought 0.1 ETH for 200 USDT - ETH/USDT : 2000.
    tickerFlux.emitValue(TickerDTO.builder().currencyPair(BTC_USDT).last(new BigDecimal("20000")).build());
    await().untilAsserted(() -> assertEquals(CLOSING, getPositionDTO(position3Uid).getStatus()));
    await().untilAsserted(() -> {
        orderFlux.update();
        tradeFlux.update();
        assertEquals(CLOSED, getPositionDTO(position3Uid).getStatus());
    });
    // Check position status.
    assertEquals(OPENED, getPositionDTO(position1Uid).getStatus());
    assertEquals(OPENED, getPositionDTO(position2Uid).getStatus());
    assertEquals(CLOSED, getPositionDTO(position3Uid).getStatus());
    assertEquals(OPENED, getPositionDTO(position4Uid).getStatus());
    // Check onPositionUpdate() & onPositionStatusUpdate().
    // strategy1: 1 position update because of prices updates.
    // strategy3, 5 position updates:
    // - Position prices updates.
    // - Position CLOSING with opening order with PENDING_NEW status.
    // - Position CLOSING with opening order with NEW status.
    // - Position CLOSING with opening order with FILLED status.
    // - Position CLOSED with opening order with FILLED status and the corresponding trade.
    // Positions status updates : CLOSING and then CLOSED.
    await().untilAsserted(() -> assertEquals(6, strategy1.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy1.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(4, strategy2.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy2.getPositionsStatusUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(13, strategy3.getPositionsUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(6, strategy3.getPositionsStatusUpdatesReceived().size()));
    // Check onOrderUpdate().
    // - Order with NEW status.
    // - Order with FILLED status.
    // (PENDING_NEW orders are not in events).
    await().untilAsserted(() -> assertEquals(2, strategy1.getOrdersUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(2, strategy2.getOrdersUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(6, strategy3.getOrdersUpdatesReceived().size()));
    // Check onTradeUpdate().
    // - Trade arrives for the closing order.
    await().untilAsserted(() -> assertEquals(1, strategy1.getTradesUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(1, strategy2.getTradesUpdatesReceived().size()));
    await().untilAsserted(() -> assertEquals(3, strategy3.getTradesUpdatesReceived().size()));
    // Check getOrders() & getOrderByOrderId().
    assertEquals(1, strategy1.getOrders().size());
    assertTrue(strategy1.getOrderByOrderId("DRY_ORDER_000000001").isPresent());
    assertEquals(1, strategy2.getOrders().size());
    assertTrue(strategy2.getOrderByOrderId("DRY_ORDER_000000002").isPresent());
    assertEquals(3, strategy3.getOrders().size());
    assertTrue(strategy3.getOrderByOrderId("DRY_ORDER_000000003").isPresent());
    assertTrue(strategy3.getOrderByOrderId("DRY_ORDER_000000004").isPresent());
    assertTrue(strategy3.getOrderByOrderId("DRY_ORDER_000000005").isPresent());
    // Check getTrades() & getTradeByTradeId().
    assertEquals(1, strategy1.getTrades().size());
    assertTrue(strategy1.getTradeByTradeId("DRY_TRADE_000000001").isPresent());
    assertEquals(1, strategy2.getTrades().size());
    assertTrue(strategy2.getTradeByTradeId("DRY_TRADE_000000002").isPresent());
    assertEquals(3, strategy3.getTrades().size());
    assertTrue(strategy3.getTradeByTradeId("DRY_TRADE_000000003").isPresent());
    assertTrue(strategy3.getTradeByTradeId("DRY_TRADE_000000004").isPresent());
    assertTrue(strategy3.getTradeByTradeId("DRY_TRADE_000000005").isPresent());
    // Check getAmountsLockedByPosition().
    // Position 3 - Bought 0.01 BTC for 100 USDT - BTC/USDT : 20000 => Now close, sold 0.01 BTC for 200 USDT.
    amountLockedForBTC = strategy1.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.021").compareTo(amountLockedForBTC));
    amountLockedForBTC = strategy2.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.021").compareTo(amountLockedForBTC));
    amountLockedForBTC = strategy3.getAmountsLockedByCurrency(BTC);
    assertEquals(0, new BigDecimal("0.021").compareTo(amountLockedForBTC));
    amountLockedForETH = strategy1.getAmountsLockedByCurrency(ETH);
    assertEquals(0, new BigDecimal("0.1").compareTo(amountLockedForETH));
    amountLockedForETH = strategy2.getAmountsLockedByCurrency(ETH);
    assertEquals(0, new BigDecimal("0.1").compareTo(amountLockedForETH));
    amountLockedForETH = strategy3.getAmountsLockedByCurrency(ETH);
    assertEquals(0, new BigDecimal("0.1").compareTo(amountLockedForETH));
    // ==============================================================================================================
    // Check balances, canBuy() & canSell().
    // At the beginning my balances was:
    // - 0.99962937 BTC.
    // - 1 000 USDT.
    // - 10 ETH.
    // ---
    // Position 1 - Bought 0.001 BTC for 50 USDT - BTC/USDT : 50000.
    // Position 2 - Bought 0.2 BTC for 0.5 ETH - BTC/ETH : 25.
    // Position 3 - Bought 0.01 BTC for 100 USDT - BTC/USDT : 10000
    // Position 4 - Bought 0.1 ETH for 200 USDT - ETH/USDT : 2000.
    // New event.
    // Position 3 - Bought 0.01 BTC for 100 USDT - BTC/USDT : 20000 => Now close, sold 0.01 BTC for 200 USDT.
    // --
    // BTC : 0.99962937 + 0.001 (P1) + 0.02 (P2) + 0.01 (P2) - 0.01 (P3 close) = 1.02062937 BTC
    // USDT : 1000 - 50 (P1) - 100 (P3) - 200 (P4) + 200 (P3) = 850 USDT
    // ETH : 10 - 0.5 (P2) + 0.1 (P4) = 5.1 ETH
    accountFlux.update();
    await().untilAsserted(() -> assertEquals(5, strategy3.getAccountsUpdatesReceived().size()));
    // Strategy 1.
    assertEquals(0, new BigDecimal("1.02062937").compareTo(strategy1.getTradeAccountBalances().get(BTC).getAvailable()));
    assertEquals(0, new BigDecimal("850").compareTo(strategy1.getTradeAccountBalances().get(USDT).getAvailable()));
    assertEquals(0, new BigDecimal("9.6").compareTo(strategy1.getTradeAccountBalances().get(ETH).getAvailable()));
    // Strategy 2.
    assertEquals(0, new BigDecimal("1.02062937").compareTo(strategy2.getTradeAccountBalances().get(BTC).getAvailable()));
    assertEquals(0, new BigDecimal("850").compareTo(strategy2.getTradeAccountBalances().get(USDT).getAvailable()));
    assertEquals(0, new BigDecimal("9.6").compareTo(strategy2.getTradeAccountBalances().get(ETH).getAvailable()));
    // Strategy 3.
    assertEquals(0, new BigDecimal("1.02062937").compareTo(strategy3.getTradeAccountBalances().get(BTC).getAvailable()));
    assertEquals(0, new BigDecimal("850").compareTo(strategy3.getTradeAccountBalances().get(USDT).getAvailable()));
    assertEquals(0, new BigDecimal("9.6").compareTo(strategy3.getTradeAccountBalances().get(ETH).getAvailable()));
}
Also used : PositionCreationResultDTO(tech.cassandre.trading.bot.dto.position.PositionCreationResultDTO) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) Strategy(tech.cassandre.trading.bot.domain.Strategy) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) BigDecimal(java.math.BigDecimal) BaseTest(tech.cassandre.trading.bot.test.util.junit.BaseTest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with TickerDTO

use of tech.cassandre.trading.bot.dto.market.TickerDTO in project cassandre-trading-bot by cassandre-tech.

the class TickerDTOTest method checkEqualToForTickers.

@Test
@DisplayName("Check equals()")
public void checkEqualToForTickers() {
    // Dates.
    ZonedDateTime date1 = createZonedDateTime("23-09-2017");
    ZonedDateTime date2 = createZonedDateTime("02-10-2018");
    // Rules: Ticker equality is based on currency pair and date only.
    // Ticker 1 - ETH/BTC, date1, 1 BTC.
    TickerDTO t01 = TickerDTO.builder().currencyPair(ETH_BTC).timestamp(date1).last(new BigDecimal("1")).build();
    // Ticker 2 - ETH/BTC, date1, 2 BTC.
    TickerDTO t02 = TickerDTO.builder().currencyPair(ETH_BTC).timestamp(date1).last(new BigDecimal("2")).build();
    assertEquals(t01, t02);
    assertEquals(t02, t01);
    // Ticker 3 - ETH/BTC, date2, 1 BTC.
    TickerDTO t03 = TickerDTO.builder().currencyPair(ETH_BTC).timestamp(date2).last(new BigDecimal("1")).build();
    assertNotEquals(t01, t03);
    assertNotEquals(t03, t01);
    // Ticker 4 - ETH/USDT, date1, 1 USDT.
    TickerDTO t04 = TickerDTO.builder().currencyPair(ETH_USDT).timestamp(date1).last(new BigDecimal("1")).build();
    assertNotEquals(t01, t04);
    assertNotEquals(t04, t01);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test) BaseTest(tech.cassandre.trading.bot.test.util.junit.BaseTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with TickerDTO

use of tech.cassandre.trading.bot.dto.market.TickerDTO in project cassandre-trading-bot by cassandre-tech.

the class TickersFluxTest method checkReceivedData.

@Test
@DisplayName("Check received data")
public void checkReceivedData() {
    // we will call the service 9 times.
    final int numberOfTickersExpected = 14;
    final int numberOfServiceCallsExpected = 10;
    // Waiting for the service to have been called with all the test data.
    await().untilAsserted(() -> verify(marketDataService, atLeast(numberOfServiceCallsExpected)).getTickers(any()));
    // Waiting for the strategy to have received all the tickers.
    await().untilAsserted(() -> assertTrue(strategy.getTickersUpdatesReceived().size() >= numberOfTickersExpected));
    // =============================================================================================================
    // Test all values received by the strategy with update methods.
    final Iterator<TickerDTO> iterator = strategy.getTickersUpdatesReceived().iterator();
    // Call 1.
    TickerDTO t = iterator.next();
    assertEquals(ETH_BTC, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("1").compareTo(t.getLast()));
    t = iterator.next();
    assertEquals(ETH_USDT, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("10").compareTo(t.getLast()));
    // Call 2.
    t = iterator.next();
    assertEquals(ETH_BTC, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("2").compareTo(t.getLast()));
    t = iterator.next();
    assertEquals(ETH_USDT, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("20").compareTo(t.getLast()));
    // Call 3.
    t = iterator.next();
    assertEquals(ETH_BTC, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("3").compareTo(t.getLast()));
    t = iterator.next();
    assertEquals(ETH_USDT, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("30").compareTo(t.getLast()));
    // Call 4.
    t = iterator.next();
    assertEquals(ETH_USDT, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("40").compareTo(t.getLast()));
    // Call 5.
    t = iterator.next();
    assertEquals(ETH_BTC, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("4").compareTo(t.getLast()));
    t = iterator.next();
    assertEquals(ETH_USDT, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("50").compareTo(t.getLast()));
    // Call 6.
    t = iterator.next();
    assertEquals(ETH_BTC, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("40").compareTo(t.getLast()));
    // Call 7.
    t = iterator.next();
    assertEquals(ETH_BTC, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("5").compareTo(t.getLast()));
    t = iterator.next();
    assertEquals(ETH_USDT, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("60").compareTo(t.getLast()));
    // Call 8.
    t = iterator.next();
    assertEquals(ETH_BTC, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("6").compareTo(t.getLast()));
    // Call 9.
    t = iterator.next();
    assertEquals(ETH_USDT, t.getCurrencyPair());
    assertEquals(0, new BigDecimal("70").compareTo(t.getLast()));
    // =============================================================================================================
    // Check data we have in the strategy.
    assertEquals(2, strategy.getLastTickers().size());
    // For ETH/BTC.
    final Optional<TickerDTO> lastTickerForETHBTC = strategy.getLastTickerByCurrencyPair(ETH_BTC);
    assertTrue(lastTickerForETHBTC.isPresent());
    assertEquals(ETH_BTC, lastTickerForETHBTC.get().getCurrencyPair());
    assertEquals(0, new BigDecimal("6").compareTo(lastTickerForETHBTC.get().getLast()));
    // For ETH/USDT.
    final Optional<TickerDTO> lastTickerForETHUSDT = strategy.getLastTickerByCurrencyPair(ETH_USDT);
    assertTrue(lastTickerForETHUSDT.isPresent());
    assertEquals(ETH_USDT, lastTickerForETHUSDT.get().getCurrencyPair());
    assertEquals(0, new BigDecimal("70").compareTo(lastTickerForETHUSDT.get().getLast()));
}
Also used : TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test) BaseTest(tech.cassandre.trading.bot.test.util.junit.BaseTest) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

TickerDTO (tech.cassandre.trading.bot.dto.market.TickerDTO)19 BigDecimal (java.math.BigDecimal)13 DisplayName (org.junit.jupiter.api.DisplayName)10 Test (org.junit.jupiter.api.Test)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 CurrencyPairDTO (tech.cassandre.trading.bot.dto.util.CurrencyPairDTO)8 OrderCreationResultDTO (tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO)6 AccountDTO (tech.cassandre.trading.bot.dto.user.AccountDTO)5 BaseTest (tech.cassandre.trading.bot.test.util.junit.BaseTest)5 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Set (java.util.Set)4 Around (org.aspectj.lang.annotation.Around)4 Tag (org.junit.jupiter.api.Tag)4 BalanceDTO (tech.cassandre.trading.bot.dto.user.BalanceDTO)4 DryModeException (tech.cassandre.trading.bot.util.exception.DryModeException)4 IOException (java.io.IOException)3 ZERO (java.math.BigDecimal.ZERO)3 ZonedDateTime (java.time.ZonedDateTime)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3