Search in sources :

Example 1 with USD

use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.USD 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 2 with USD

use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.USD in project cassandre-trading-bot by cassandre-tech.

the class PositionTest method checkLoadPositionFromDatabase.

@Test
@DisplayName("Check load position from database")
public void checkLoadPositionFromDatabase() {
    // =============================================================================================================
    // Check position 1 - OPENING.
    PositionDTO position1 = strategy.getPositions().get(1L);
    assertNotNull(position1);
    assertEquals(1L, position1.getUid());
    assertEquals(1L, position1.getPositionId());
    assertEquals(LONG, position1.getType());
    assertNotNull(position1.getStrategy());
    assertEquals(1, position1.getStrategy().getUid());
    assertEquals("01", position1.getStrategy().getStrategyId());
    assertEquals(new CurrencyPairDTO("BTC/USDT"), position1.getCurrencyPair());
    assertEquals(0, new BigDecimal("10").compareTo(position1.getAmount().getValue()));
    assertEquals(BTC, position1.getAmount().getCurrency());
    assertFalse(position1.getRules().isStopGainPercentageSet());
    assertFalse(position1.getRules().isStopLossPercentageSet());
    assertEquals(OPENING, position1.getStatus());
    assertEquals("BACKUP_OPENING_ORDER_01", position1.getOpeningOrder().getOrderId());
    assertTrue(position1.getOpeningOrder().getTrades().isEmpty());
    assertNull(position1.getClosingOrder());
    assertNull(position1.getLowestGainPrice());
    assertNull(position1.getHighestGainPrice());
    assertNull(position1.getLatestGainPrice());
    // Test equals.
    Optional<PositionDTO> position1Bis = strategy.getPositionByPositionId(1L);
    assertTrue(position1Bis.isPresent());
    assertEquals(position1, position1Bis.get());
    // =============================================================================================================
    // Check position 2 - OPENED.
    PositionDTO position2 = strategy.getPositions().get(2L);
    assertNotNull(position2);
    assertEquals(2L, position2.getUid());
    assertEquals(2L, position2.getPositionId());
    assertEquals(LONG, position2.getType());
    assertNotNull(position2.getStrategy());
    assertEquals(1, position2.getStrategy().getUid());
    assertEquals("01", position2.getStrategy().getStrategyId());
    assertEquals(new CurrencyPairDTO("BTC/USDT"), position2.getCurrencyPair());
    assertEquals(0, new BigDecimal("20").compareTo(position2.getAmount().getValue()));
    assertEquals(BTC, position2.getAmount().getCurrency());
    assertTrue(position2.getRules().isStopGainPercentageSet());
    assertEquals(10, position2.getRules().getStopGainPercentage());
    assertFalse(position2.getRules().isStopLossPercentageSet());
    assertEquals(OPENED, position2.getStatus());
    assertEquals("BACKUP_OPENING_ORDER_02", position2.getOpeningOrder().getOrderId());
    assertEquals(1, position2.getOpeningOrder().getTrades().size());
    assertTrue(position2.getOpeningOrder().getTrade("BACKUP_TRADE_01").isPresent());
    assertNull(position2.getClosingOrder());
    assertEquals(0, new BigDecimal("1").compareTo(position2.getLowestGainPrice().getValue()));
    assertEquals(USDT, position2.getLowestGainPrice().getCurrency());
    assertEquals(0, new BigDecimal("2").compareTo(position2.getHighestGainPrice().getValue()));
    assertEquals(USDT, position2.getHighestGainPrice().getCurrency());
    assertEquals(0, new BigDecimal("3").compareTo(position2.getLatestGainPrice().getValue()));
    assertEquals(USDT, position2.getLatestGainPrice().getCurrency());
    // =============================================================================================================
    // Check position 3 - CLOSING.
    PositionDTO position3 = strategy.getPositions().get(3L);
    assertNotNull(position3);
    assertEquals(3L, position3.getUid());
    assertEquals(3L, position3.getPositionId());
    assertEquals(LONG, position3.getType());
    assertNotNull(position3.getStrategy());
    assertEquals(1, position3.getStrategy().getUid());
    assertEquals("01", position3.getStrategy().getStrategyId());
    assertEquals(new CurrencyPairDTO("BTC/USDT"), position3.getCurrencyPair());
    assertEquals(0, new BigDecimal("30").compareTo(position3.getAmount().getValue()));
    assertEquals(BTC, position3.getAmount().getCurrency());
    assertFalse(position3.getRules().isStopGainPercentageSet());
    assertTrue(position3.getRules().isStopLossPercentageSet());
    assertEquals(20, position3.getRules().getStopLossPercentage());
    assertEquals(CLOSING, position3.getStatus());
    assertEquals("BACKUP_OPENING_ORDER_03", position3.getOpeningOrder().getOrderId());
    assertEquals(1, position3.getOpeningOrder().getTrades().size());
    assertTrue(position3.getOpeningOrder().getTrade("BACKUP_TRADE_02").isPresent());
    assertEquals("BACKUP_CLOSING_ORDER_01", position3.getClosingOrder().getOrderId());
    assertEquals(1, position3.getClosingOrder().getTrades().size());
    assertTrue(position3.getClosingOrder().getTrade("BACKUP_TRADE_04").isPresent());
    assertEquals(0, new BigDecimal("17").compareTo(position3.getLowestGainPrice().getValue()));
    assertEquals(USDT, position3.getLowestGainPrice().getCurrency());
    assertEquals(0, new BigDecimal("68").compareTo(position3.getHighestGainPrice().getValue()));
    assertEquals(USDT, position3.getHighestGainPrice().getCurrency());
    assertEquals(0, new BigDecimal("92").compareTo(position3.getLatestGainPrice().getValue()));
    assertEquals(USDT, position3.getLatestGainPrice().getCurrency());
    // =============================================================================================================
    // Check position 4 - CLOSED.
    PositionDTO position4 = strategy.getPositions().get(4L);
    assertNotNull(position4);
    assertEquals(4L, position4.getUid());
    assertEquals(4L, position4.getPositionId());
    assertEquals(LONG, position4.getType());
    assertNotNull(position4.getStrategy());
    assertEquals(1, position4.getStrategy().getUid());
    assertEquals("01", position4.getStrategy().getStrategyId());
    assertEquals(new CurrencyPairDTO("BTC/USDT"), position4.getCurrencyPair());
    assertEquals(0, new BigDecimal("40").compareTo(position4.getAmount().getValue()));
    assertEquals(BTC, position4.getAmount().getCurrency());
    assertTrue(position4.getRules().isStopGainPercentageSet());
    assertEquals(30, position4.getRules().getStopGainPercentage());
    assertTrue(position4.getRules().isStopLossPercentageSet());
    assertEquals(40, position4.getRules().getStopLossPercentage());
    assertEquals(CLOSED, position4.getStatus());
    assertEquals("BACKUP_OPENING_ORDER_04", position4.getOpeningOrder().getOrderId());
    assertEquals(1, position4.getOpeningOrder().getTrades().size());
    assertTrue(position4.getOpeningOrder().getTrade("BACKUP_TRADE_03").isPresent());
    assertEquals("BACKUP_CLOSING_ORDER_02", position4.getClosingOrder().getOrderId());
    assertEquals(1, position4.getClosingOrder().getTrades().size());
    assertTrue(position4.getClosingOrder().getTrade("BACKUP_TRADE_05").isPresent());
    assertEquals(0, new BigDecimal("17").compareTo(position4.getLowestGainPrice().getValue()));
    assertEquals(USDT, position4.getLowestGainPrice().getCurrency());
    assertEquals(0, new BigDecimal("68").compareTo(position4.getHighestGainPrice().getValue()));
    assertEquals(USDT, position4.getHighestGainPrice().getCurrency());
    assertEquals(0, new BigDecimal("93").compareTo(position4.getLatestGainPrice().getValue()));
    assertEquals(USDT, position4.getLatestGainPrice().getCurrency());
    // =============================================================================================================
    // Check position 5 - CLOSED with several trades.
    PositionDTO position5 = strategy.getPositions().get(5L);
    assertNotNull(position5);
    assertEquals(5L, position5.getUid());
    assertEquals(5L, position5.getPositionId());
    assertEquals(LONG, position5.getType());
    assertNotNull(position5.getStrategy());
    assertEquals(1, position5.getStrategy().getUid());
    assertEquals("01", position5.getStrategy().getStrategyId());
    assertEquals(new CurrencyPairDTO("ETH/USD"), position5.getCurrencyPair());
    assertEquals(0, new BigDecimal("50").compareTo(position5.getAmount().getValue()));
    assertEquals(ETH, position5.getAmount().getCurrency());
    assertTrue(position5.getRules().isStopGainPercentageSet());
    assertEquals(30, position5.getRules().getStopGainPercentage());
    assertTrue(position5.getRules().isStopLossPercentageSet());
    assertEquals(40, position5.getRules().getStopLossPercentage());
    assertEquals(CLOSED, position5.getStatus());
    assertEquals("BACKUP_OPENING_ORDER_05", position5.getOpeningOrder().getOrderId());
    assertEquals("BACKUP_CLOSING_ORDER_03", position5.getClosingOrder().getOrderId());
    assertEquals(0, new BigDecimal("17").compareTo(position5.getLowestGainPrice().getValue()));
    assertEquals(USD, position5.getLowestGainPrice().getCurrency());
    assertEquals(0, new BigDecimal("68").compareTo(position5.getHighestGainPrice().getValue()));
    assertEquals(USD, position5.getHighestGainPrice().getCurrency());
    assertEquals(0, new BigDecimal("94").compareTo(position5.getLatestGainPrice().getValue()));
    assertEquals(USD, position5.getLatestGainPrice().getCurrency());
    // Open trades.
    assertEquals(2, position5.getOpeningOrder().getTrades().size());
    assertTrue(position5.getOpeningOrder().getTrade("BACKUP_TRADE_06").isPresent());
    assertEquals("BACKUP_TRADE_06", position5.getOpeningOrder().getTrade("BACKUP_TRADE_06").get().getTradeId());
    assertTrue(position5.getOpeningOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_06".equals(t.getTradeId())));
    assertTrue(position5.getOpeningOrder().getTrade("BACKUP_TRADE_07").isPresent());
    assertTrue(position5.getOpeningOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_07".equals(t.getTradeId())));
    assertEquals("BACKUP_TRADE_07", position5.getOpeningOrder().getTrade("BACKUP_TRADE_07").get().getTradeId());
    // Close trades.
    assertEquals(3, position5.getClosingOrder().getTrades().size());
    assertTrue(position5.getClosingOrder().getTrade("BACKUP_TRADE_08").isPresent());
    assertTrue(position5.getClosingOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_08".equals(t.getTradeId())));
    assertEquals("BACKUP_TRADE_08", position5.getClosingOrder().getTrade("BACKUP_TRADE_08").get().getTradeId());
    assertTrue(position5.getClosingOrder().getTrade("BACKUP_TRADE_09").isPresent());
    assertTrue(position5.getClosingOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_09".equals(t.getTradeId())));
    assertEquals("BACKUP_TRADE_09", position5.getClosingOrder().getTrade("BACKUP_TRADE_09").get().getTradeId());
    assertTrue(position5.getClosingOrder().getTrade("BACKUP_TRADE_10").isPresent());
    assertTrue(position5.getClosingOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_10".equals(t.getTradeId())));
    assertEquals("BACKUP_TRADE_10", position5.getClosingOrder().getTrade("BACKUP_TRADE_10").get().getTradeId());
    // Check trade orders.
    final Iterator<TradeDTO> openTradesIterator = position5.getOpeningOrder().getTrades().iterator();
    assertEquals("BACKUP_TRADE_06", openTradesIterator.next().getTradeId());
    assertEquals("BACKUP_TRADE_07", openTradesIterator.next().getTradeId());
    final Iterator<TradeDTO> closeTradesIterator = position5.getClosingOrder().getTrades().iterator();
    assertEquals("BACKUP_TRADE_08", closeTradesIterator.next().getTradeId());
    assertEquals("BACKUP_TRADE_09", closeTradesIterator.next().getTradeId());
    assertEquals("BACKUP_TRADE_10", closeTradesIterator.next().getTradeId());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) ASK(tech.cassandre.trading.bot.dto.trade.OrderTypeDTO.ASK) ZonedDateTime(java.time.ZonedDateTime) BEFORE_EACH_TEST_METHOD(org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) Position(tech.cassandre.trading.bot.domain.Position) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) CurrencyAmountDTO(tech.cassandre.trading.bot.dto.util.CurrencyAmountDTO) BigDecimal(java.math.BigDecimal) TradeFlux(tech.cassandre.trading.bot.batch.TradeFlux) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) USDT(tech.cassandre.trading.bot.dto.util.CurrencyDTO.USDT) OrderFlux(tech.cassandre.trading.bot.batch.OrderFlux) NEW(tech.cassandre.trading.bot.dto.trade.OrderStatusDTO.NEW) OPENED(tech.cassandre.trading.bot.dto.position.PositionStatusDTO.OPENED) ETH(tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH) PositionDTO(tech.cassandre.trading.bot.dto.position.PositionDTO) Awaitility.await(org.awaitility.Awaitility.await) USD(tech.cassandre.trading.bot.dto.util.CurrencyDTO.USD) PositionRepository(tech.cassandre.trading.bot.repository.PositionRepository) PositionService(tech.cassandre.trading.bot.service.PositionService) TickerFlux(tech.cassandre.trading.bot.batch.TickerFlux) TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) OrderDTO(tech.cassandre.trading.bot.dto.trade.OrderDTO) Test(org.junit.jupiter.api.Test) PositionCreationResultDTO(tech.cassandre.trading.bot.dto.position.PositionCreationResultDTO) OrderRepository(tech.cassandre.trading.bot.repository.OrderRepository) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) Property(tech.cassandre.trading.bot.test.util.junit.configuration.Property) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CLOSED(tech.cassandre.trading.bot.dto.position.PositionStatusDTO.CLOSED) PositionException(tech.cassandre.trading.bot.util.exception.PositionException) LONG(tech.cassandre.trading.bot.dto.position.PositionTypeDTO.LONG) BaseTest(tech.cassandre.trading.bot.test.util.junit.BaseTest) TestableCassandreStrategy(tech.cassandre.trading.bot.test.util.strategies.TestableCassandreStrategy) OPENING(tech.cassandre.trading.bot.dto.position.PositionStatusDTO.OPENING) PositionFlux(tech.cassandre.trading.bot.batch.PositionFlux) BTC(tech.cassandre.trading.bot.dto.util.CurrencyDTO.BTC) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Configuration(tech.cassandre.trading.bot.test.util.junit.configuration.Configuration) Iterator(java.util.Iterator) CLOSING(tech.cassandre.trading.bot.dto.position.PositionStatusDTO.CLOSING) DisplayName(org.junit.jupiter.api.DisplayName) PositionRulesDTO(tech.cassandre.trading.bot.dto.position.PositionRulesDTO) PARAMETER_EXCHANGE_DRY(tech.cassandre.trading.bot.test.util.junit.configuration.ConfigurationExtension.PARAMETER_EXCHANGE_DRY) TradeDTO(tech.cassandre.trading.bot.dto.trade.TradeDTO) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) PositionDTO(tech.cassandre.trading.bot.dto.position.PositionDTO) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) BaseTest(tech.cassandre.trading.bot.test.util.junit.BaseTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

BigDecimal (java.math.BigDecimal)2 ZonedDateTime (java.time.ZonedDateTime)2 Optional (java.util.Optional)2 Awaitility.await (org.awaitility.Awaitility.await)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)2 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)2 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 DisplayName (org.junit.jupiter.api.DisplayName)2 Test (org.junit.jupiter.api.Test)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ActiveProfiles (org.springframework.test.context.ActiveProfiles)2 OrderDTO (tech.cassandre.trading.bot.dto.trade.OrderDTO)2 TradeDTO (tech.cassandre.trading.bot.dto.trade.TradeDTO)2 BTC (tech.cassandre.trading.bot.dto.util.CurrencyDTO.BTC)2 ETH (tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH)2 USD (tech.cassandre.trading.bot.dto.util.CurrencyDTO.USD)2 Iterator (java.util.Iterator)1