Search in sources :

Example 1 with TradeDTO

use of tech.cassandre.trading.bot.dto.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.

the class TradeDTOTest method checkNullTrades.

@Test
@DisplayName("Check null trades")
public void checkNullTrades() {
    TradeDTO t1 = TradeDTO.builder().tradeId("0000001").build();
    TradeDTO t2 = TradeDTO.builder().tradeId("0000002").build();
    assertNotEquals(t1, t2);
    assertNotEquals(t2, t1);
}
Also used : TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with TradeDTO

use of tech.cassandre.trading.bot.dto.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.

the class BasicTa4jCassandreStrategyTestMock method tradeService.

@Bean
@Primary
public TradeService tradeService() {
    TradeService service = mock(TradeService.class);
    // Returns three values.
    Set<OrderDTO> reply = new LinkedHashSet<>();
    // Order 01.
    reply.add(OrderDTO.builder().orderId("000001").type(BID).strategy(strategyDTO).currencyPair(BTC_USDT).build());
    // Order 02.
    reply.add(OrderDTO.builder().orderId("000002").type(BID).strategy(strategyDTO).currencyPair(BTC_USDT).build());
    // Order 03.
    reply.add(OrderDTO.builder().orderId("000003").type(BID).strategy(strategyDTO).currencyPair(BTC_USDT).build());
    // Order 04.
    reply.add(OrderDTO.builder().orderId("000004").type(BID).strategy(strategyDTO).currencyPair(BTC_USDT).build());
    given(service.getOrders()).willReturn(reply);
    // Returns three values for getTrades().
    Set<TradeDTO> replyGetTrades = new LinkedHashSet<>();
    // Trade 01.
    replyGetTrades.add(TradeDTO.builder().tradeId("0000001").orderId("000001").type(BID).currencyPair(BTC_USDT).build());
    // Trade 02.
    replyGetTrades.add(TradeDTO.builder().tradeId("0000002").orderId("000002").type(BID).currencyPair(BTC_USDT).build());
    // Trade 03.
    replyGetTrades.add(TradeDTO.builder().tradeId("0000003").orderId("000003").type(BID).currencyPair(BTC_USDT).build());
    given(service.getTrades()).willReturn(replyGetTrades);
    return service;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) TradeService(tech.cassandre.trading.bot.service.TradeService) OrderDTO(tech.cassandre.trading.bot.dto.trade.OrderDTO) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 3 with TradeDTO

use of tech.cassandre.trading.bot.dto.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.

the class TradeServiceTest method checkGetTrades.

@Test
@Tag("integration")
@DisplayName("Check get trades")
public void checkGetTrades() {
    final CurrencyPairDTO cp = new CurrencyPairDTO(BTC, USD);
    // Creates two orders of the same amount (one buy, one sell).
    final OrderCreationResultDTO result1 = strategy.createBuyMarketOrder(cp, new BigDecimal("0.1"));
    final OrderCreationResultDTO result2 = strategy.createSellMarketOrder(cp, new BigDecimal("0.1"));
    // Check that the two orders appears in the trade history.
    assertTrue(result1.isSuccessful());
    assertTrue(result2.isSuccessful());
    await().untilAsserted(() -> assertTrue(tradeService.getTrades().stream().anyMatch(t -> t.getOrderId().equals(result1.getOrder().getOrderId()))));
    await().untilAsserted(() -> assertTrue(tradeService.getTrades().stream().anyMatch(t -> t.getOrderId().equals(result2.getOrder().getOrderId()))));
    // Retrieve trade & test values.
    final Optional<TradeDTO> t = tradeService.getTrades().stream().filter(trade -> trade.getOrderId().equals(result1.getOrder().getOrderId())).findFirst();
    assertTrue(t.isPresent());
    assertNull(t.get().getId());
    assertNotNull(t.get().getTradeId());
    assertEquals(BID, t.get().getType());
    assertEquals(result1.getOrderId(), t.get().getOrderId());
    assertEquals(cp, t.get().getCurrencyPair());
    assertNotNull(t.get().getAmount().getValue());
    assertEquals(BTC, t.get().getAmount().getCurrency());
    assertNotNull(t.get().getPrice().getValue());
    assertNotNull(t.get().getFee().getValue());
    assertNotNull(t.get().getFee().getCurrency());
    assertTrue(t.get().getTimestamp().isAfter(ZonedDateTime.now().minusMinutes(1)));
    assertTrue(t.get().getTimestamp().isBefore(ZonedDateTime.now().plusMinutes(1)));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BID(tech.cassandre.trading.bot.dto.trade.OrderTypeDTO.BID) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) TradeService(tech.cassandre.trading.bot.service.TradeService) Disabled(org.junit.jupiter.api.Disabled) BaseTest(tech.cassandre.trading.bot.test.util.junit.BaseTest) TestableCassandreStrategy(tech.cassandre.trading.bot.test.util.strategies.TestableCassandreStrategy) BigDecimal(java.math.BigDecimal) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BTC(tech.cassandre.trading.bot.dto.util.CurrencyDTO.BTC) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ETH(tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH) Awaitility.await(org.awaitility.Awaitility.await) USD(tech.cassandre.trading.bot.dto.util.CurrencyDTO.USD) TestPropertySource(org.springframework.test.context.TestPropertySource) TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) OrderDTO(tech.cassandre.trading.bot.dto.trade.OrderDTO) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) Optional(java.util.Optional) TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) 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) Tag(org.junit.jupiter.api.Tag)

Example 4 with TradeDTO

use of tech.cassandre.trading.bot.dto.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.

the class TradeServiceTest method checkGetTrades.

@Test
@Tag("integration")
@DisplayName("Check get trades")
@Disabled("Gemini doesn't support market order")
public void checkGetTrades() {
    final CurrencyPairDTO cp = new CurrencyPairDTO(ETH, BTC);
    // Creates two orders of the same amount (one buy, one sell).
    final OrderCreationResultDTO result1 = strategy.createBuyMarketOrder(cp, new BigDecimal("0.0001"));
    final OrderCreationResultDTO result2 = strategy.createSellMarketOrder(cp, new BigDecimal("0.0001"));
    // Check that the two orders appears in the trade history.
    assertTrue(result1.isSuccessful());
    await().untilAsserted(() -> assertTrue(tradeService.getTrades().stream().anyMatch(t -> t.getOrderId().equals(result1.getOrder().getOrderId()))));
    assertNotNull(result2.getOrder().getOrderId());
    await().untilAsserted(() -> assertTrue(tradeService.getTrades().stream().anyMatch(t -> t.getOrderId().equals(result2.getOrder().getOrderId()))));
    // Retrieve trade & test values.
    final Optional<TradeDTO> t = tradeService.getTrades().stream().filter(trade -> trade.getOrderId().equals(result1.getOrder().getOrderId())).findFirst();
    assertTrue(t.isPresent());
    assertNull(t.get().getId());
    assertNotNull(t.get().getTradeId());
    assertEquals(BID, t.get().getType());
    assertEquals(result1.getOrderId(), t.get().getOrderId());
    assertEquals(cp, t.get().getCurrencyPair());
    assertNotNull(t.get().getAmount().getValue());
    assertEquals(ETH, t.get().getAmount().getCurrency());
    assertNotNull(t.get().getPrice().getValue());
    assertEquals(BTC, t.get().getAmount().getCurrency());
    assertNotNull(t.get().getFee().getValue());
    assertNotNull(t.get().getFee().getCurrency());
    assertTrue(t.get().getTimestamp().isAfter(ZonedDateTime.now().minusMinutes(1)));
    assertTrue(t.get().getTimestamp().isBefore(ZonedDateTime.now().plusMinutes(1)));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BID(tech.cassandre.trading.bot.dto.trade.OrderTypeDTO.BID) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) TradeService(tech.cassandre.trading.bot.service.TradeService) Disabled(org.junit.jupiter.api.Disabled) BaseTest(tech.cassandre.trading.bot.test.util.junit.BaseTest) TestableCassandreStrategy(tech.cassandre.trading.bot.test.util.strategies.TestableCassandreStrategy) BigDecimal(java.math.BigDecimal) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BTC(tech.cassandre.trading.bot.dto.util.CurrencyDTO.BTC) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NEW(tech.cassandre.trading.bot.dto.trade.OrderStatusDTO.NEW) ETH(tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH) Awaitility.await(org.awaitility.Awaitility.await) ZERO(java.math.BigDecimal.ZERO) TestPropertySource(org.springframework.test.context.TestPropertySource) TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) OrderDTO(tech.cassandre.trading.bot.dto.trade.OrderDTO) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) Optional(java.util.Optional) TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) 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) Tag(org.junit.jupiter.api.Tag) Disabled(org.junit.jupiter.api.Disabled)

Example 5 with TradeDTO

use of tech.cassandre.trading.bot.dto.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.

the class UserServiceTest method checkBalancesUpdate.

@Test
@DisplayName("Check balances updates")
public void checkBalancesUpdate() {
    // We retrieve the account information in the strategy.
    assertTrue(strategy.getAccountsUpdatesReceived().isEmpty());
    accountFlux.update();
    await().untilAsserted(() -> assertEquals(3, strategy.getAccountsUpdatesReceived().size()));
    // =============================================================================================================
    // Received ticker for ETH/BTC - It means 1 ETH can be bought with 0.032661 BTC.
    // last = 0.032661 (Last trade field is the price set during the last trade)
    tickerFlux.emitValue(TickerDTO.builder().currencyPair(ETH_BTC).last(new BigDecimal("0.032666")).bid(new BigDecimal("0.032466")).ask(new BigDecimal("0.032657")).high(new BigDecimal("0.034441")).low(new BigDecimal("0.032355")).volume(new BigDecimal("33794.9795777")).quoteVolume(new BigDecimal("1146.8453384314658")).build());
    await().untilAsserted(() -> assertEquals(1, strategy.getLastTickers().size()));
    // =============================================================================================================
    // Account before buying.
    // Loaded from spring-boot-starter/autoconfigure/src/test/resources/user-trade.csv
    // BTC => 0.99962937
    // ETH => 10
    Optional<UserDTO> user = userService.getUser();
    assertTrue(user.isPresent());
    AccountDTO tradeAccount = user.get().getAccounts().get("trade");
    Optional<BalanceDTO> tradeBTC = tradeAccount.getBalance(BTC);
    assertTrue(tradeBTC.isPresent());
    assertEquals(0, new BigDecimal("0.99962937").compareTo(tradeBTC.get().getAvailable()));
    Optional<BalanceDTO> tradeETH = tradeAccount.getBalance(ETH);
    assertTrue(tradeETH.isPresent());
    assertEquals(0, new BigDecimal("10").compareTo(tradeETH.get().getAvailable()));
    // =============================================================================================================
    // Buying 0.02 ETH for 0.00065332 BTC.
    // Last price from ticker * amount ordered
    // 0.032666 * 0.02 = 0.00065332 BTC
    strategy.createBuyMarketOrder(ETH_BTC, new BigDecimal("0.02"));
    // =============================================================================================================
    // We expect one account update (with the new ETH and BTC balances).
    accountFlux.update();
    await().untilAsserted(() -> assertEquals(4, strategy.getAccountsUpdatesReceived().size()));
    // =============================================================================================================
    // Account after buying (from strategy).
    // BTC => 0.99897605 (previous amount - amount bought = 0.99962937 - 0.00065332)
    // ETH => 10.02 (we bought 0.02)
    tradeAccount = strategy.getAccounts().get("trade");
    assertNotNull(tradeAccount);
    tradeBTC = tradeAccount.getBalance(BTC);
    assertTrue(tradeBTC.isPresent());
    assertEquals(0, new BigDecimal("0.99897605").compareTo(tradeBTC.get().getAvailable()));
    tradeETH = tradeAccount.getBalance(ETH);
    assertTrue(tradeETH.isPresent());
    assertEquals(0, new BigDecimal("10.02").compareTo(tradeETH.get().getAvailable()));
    // =============================================================================================================
    // Account after buying (from user service).
    // BTC => 0.99897605 (previous amount - amount bought = 0.99962937 - 0.00065332)
    // ETH => 10.02
    user = userService.getUser();
    assertTrue(user.isPresent());
    tradeAccount = user.get().getAccounts().get("trade");
    tradeBTC = tradeAccount.getBalance(BTC);
    assertTrue(tradeBTC.isPresent());
    assertEquals(0, new BigDecimal("0.99897605").compareTo(tradeBTC.get().getAvailable()));
    tradeETH = tradeAccount.getBalance(ETH);
    assertTrue(tradeETH.isPresent());
    assertEquals(0, new BigDecimal("10.02").compareTo(tradeETH.get().getAvailable()));
    // =============================================================================================================
    // Testing the trade received.
    // Amount => 0.02
    // Price => 0.032666
    await().untilAsserted(() -> {
        orderFlux.update();
        tradeFlux.update();
        assertEquals(1, tradeRepository.count());
    });
    final Optional<TradeDTO> buyingTrade = tradeRepository.findByTradeId("DRY_TRADE_000000001").map(TRADE_MAPPER::mapToTradeDTO);
    assertTrue(buyingTrade.isPresent());
    assertEquals(BID, buyingTrade.get().getType());
    assertEquals(0, new BigDecimal("0.02").compareTo(buyingTrade.get().getAmount().getValue()));
    assertEquals(ETH, buyingTrade.get().getAmount().getCurrency());
    assertEquals(0, new BigDecimal("0.032666").compareTo(buyingTrade.get().getPrice().getValue()));
    assertEquals(BTC, buyingTrade.get().getPrice().getCurrency());
    // =============================================================================================================
    // Received ticker for ETH/BTC - It means 1 ETH can be bought with 0.032466 BTC.
    // last = 0.032466 (Last trade field is the price set during the last trade)
    tickerFlux.emitValue(TickerDTO.builder().currencyPair(ETH_BTC).last(new BigDecimal("0.032466")).bid(new BigDecimal("0.032466")).ask(new BigDecimal("0.032657")).high(new BigDecimal("0.034441")).low(new BigDecimal("0.032355")).volume(new BigDecimal("33794.9795777")).quoteVolume(new BigDecimal("1146.8453384314658")).build());
    await().untilAsserted(() -> assertEquals(2, strategy.getTickersUpdatesReceived().size()));
    // =============================================================================================================
    // Selling 0.02 ETH.
    // Amount * Last price from ticker
    // 0.02 * 0.032466 = 0.00064932 ETH
    strategy.createSellMarketOrder(ETH_BTC, new BigDecimal("0.02"));
    // =============================================================================================================
    // We expect one account update (with the new ETH and BTC balances).
    accountFlux.update();
    await().untilAsserted(() -> assertEquals(5, strategy.getAccountsUpdatesReceived().size()));
    // =============================================================================================================
    // Account values in strategy should be :
    // BTC => 0.99962537 (previous sold + amount sold = 0.99897605 + 0.00064932)
    // ETH => 10 (0.02 sold)
    tradeAccount = strategy.getAccounts().get("trade");
    assertNotNull(tradeAccount);
    tradeBTC = tradeAccount.getBalance(BTC);
    assertTrue(tradeBTC.isPresent());
    assertEquals(0, new BigDecimal("0.99962537").compareTo(tradeBTC.get().getAvailable()));
    tradeETH = tradeAccount.getBalance(ETH);
    assertTrue(tradeETH.isPresent());
    assertEquals(0, new BigDecimal("10").compareTo(tradeETH.get().getAvailable()));
    // =============================================================================================================
    // Testing the trade.
    // Amount => 0.02
    // Price => 0.032466
    await().untilAsserted(() -> {
        orderFlux.update();
        tradeFlux.update();
        assertEquals(2, tradeRepository.count());
    });
    final Optional<TradeDTO> sellingTrade = tradeRepository.findByTradeId("DRY_TRADE_000000002").map(TRADE_MAPPER::mapToTradeDTO);
    assertTrue(sellingTrade.isPresent());
    assertEquals(ASK, sellingTrade.get().getType());
    assertEquals(0, new BigDecimal("0.02").compareTo(sellingTrade.get().getAmount().getValue()));
    assertEquals(ETH, sellingTrade.get().getAmount().getCurrency());
    assertEquals(0, new BigDecimal("0.032466").compareTo(sellingTrade.get().getPrice().getValue()));
}
Also used : TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) UserDTO(tech.cassandre.trading.bot.dto.user.UserDTO) BalanceDTO(tech.cassandre.trading.bot.dto.user.BalanceDTO) 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)

Aggregations

TradeDTO (tech.cassandre.trading.bot.dto.trade.TradeDTO)22 BigDecimal (java.math.BigDecimal)16 DisplayName (org.junit.jupiter.api.DisplayName)14 Test (org.junit.jupiter.api.Test)14 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 CurrencyPairDTO (tech.cassandre.trading.bot.dto.util.CurrencyPairDTO)12 BaseTest (tech.cassandre.trading.bot.test.util.junit.BaseTest)12 OrderDTO (tech.cassandre.trading.bot.dto.trade.OrderDTO)11 Optional (java.util.Optional)10 CurrencyAmountDTO (tech.cassandre.trading.bot.dto.util.CurrencyAmountDTO)10 OrderCreationResultDTO (tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO)7 GainDTO (tech.cassandre.trading.bot.dto.util.GainDTO)7 ZERO (java.math.BigDecimal.ZERO)6 PositionDTO (tech.cassandre.trading.bot.dto.position.PositionDTO)6 FLOOR (java.math.RoundingMode.FLOOR)5 ZonedDateTime (java.time.ZonedDateTime)5 LinkedHashSet (java.util.LinkedHashSet)5 TickerDTO (tech.cassandre.trading.bot.dto.market.TickerDTO)5 PositionCreationResultDTO (tech.cassandre.trading.bot.dto.position.PositionCreationResultDTO)5 LONG (tech.cassandre.trading.bot.dto.position.PositionTypeDTO.LONG)5