Search in sources :

Example 1 with ETH

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

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

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

the class TradeServiceTest method checkCreateBuyLimitOrder.

@Test
@Tag("integration")
@DisplayName("Check creates a buy limit order")
public void checkCreateBuyLimitOrder() {
    final CurrencyPairDTO cp = new CurrencyPairDTO(ETH, BTC);
    // =============================================================================================================
    // Making a buy limit order (Buy 0.0001 ETH).
    final OrderCreationResultDTO result1 = strategy.createBuyLimitOrder(cp, new BigDecimal("0.0001"), new BigDecimal("0.1"));
    assertTrue(result1.isSuccessful());
    assertNull(result1.getErrorMessage());
    assertNull(result1.getException());
    assertNotNull(result1.getOrder().getOrderId());
    // =============================================================================================================
    // Getting a non-existing order.
    assertFalse(tradeService.getOrders().stream().anyMatch(o -> o.getOrderId().equals("")));
    // =============================================================================================================
    // Getting the order and testing the data.
    final Optional<OrderDTO> order1 = tradeService.getOrders().stream().filter(o -> o.getOrderId().equals(result1.getOrder().getOrderId())).findFirst();
    assertTrue(order1.isPresent());
    assertNotNull(order1.get().getOrderId());
    assertEquals(result1.getOrder().getOrderId(), order1.get().getOrderId());
    assertEquals(BID, order1.get().getType());
    assertEquals(cp, order1.get().getCurrencyPair());
    assertEquals(0, order1.get().getAmount().getValue().compareTo(new BigDecimal("0.0001")));
    assertEquals(cp.getBaseCurrency(), order1.get().getAmount().getCurrency());
    assertEquals(0, order1.get().getLimitPrice().getValue().compareTo(new BigDecimal("0.000001")));
    assertEquals(cp.getQuoteCurrency(), order1.get().getLimitPrice().getCurrency());
    assertNull(order1.get().getLeverage());
    assertNull(order1.get().getUserReference());
    assertNotNull(order1.get().getTimestamp());
    assertTrue(order1.get().getTimestamp().isAfter(ZonedDateTime.now().minusMinutes(1)));
    assertTrue(order1.get().getTimestamp().isBefore(ZonedDateTime.now().plusMinutes(1)));
    // Cancel the order.
    tradeService.cancelOrder(result1.getOrder().getOrderId());
}
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) 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) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) OrderDTO(tech.cassandre.trading.bot.dto.trade.OrderDTO) 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 ETH

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

the class AccountDataFetcherTest method accountByAccountId.

@Test
@DisplayName("accountByAccountId(accountId: String): Account")
void accountByAccountId() {
    // Query and fields definition.
    GraphQLQueryRequest graphQLQueryRequest = new GraphQLQueryRequest(new AccountByAccountIdGraphQLQuery.Builder().accountId("trade").build(), new AccountByAccountIdProjectionRoot().accountId().name().balances().currency().code().getParent().total().available().frozen().loaned().borrowed().withdrawing().depositing());
    // Query execution.
    Account account = dgsQueryExecutor.executeAndExtractJsonPathAsObject(graphQLQueryRequest.serialize(), "data." + DgsConstants.QUERY.AccountByAccountId, new TypeRef<>() {
    });
    // Tests
    assertNotNull(account);
    assertEquals("trade", account.getAccountId());
    assertEquals("trade account name", account.getName());
    assertEquals(2, account.getBalances().size());
    // Testing BTC balance.
    final Optional<Balance> btcBalance = account.getBalances().stream().filter(balance -> balance.getCurrency().equals(BTC)).findAny();
    assertTrue(btcBalance.isPresent());
    assertEquals(BTC, btcBalance.get().getCurrency());
    assertEquals(0, new BigDecimal("1").compareTo(btcBalance.get().getTotal()));
    assertEquals(0, new BigDecimal("2").compareTo(btcBalance.get().getAvailable()));
    assertEquals(0, new BigDecimal("3").compareTo(btcBalance.get().getFrozen()));
    assertEquals(0, new BigDecimal("4").compareTo(btcBalance.get().getBorrowed()));
    assertEquals(0, new BigDecimal("5").compareTo(btcBalance.get().getLoaned()));
    assertEquals(0, new BigDecimal("6").compareTo(btcBalance.get().getWithdrawing()));
    assertEquals(0, new BigDecimal("7").compareTo(btcBalance.get().getDepositing()));
    // Testing ETH balance.
    final Optional<Balance> ethBalance = account.getBalances().stream().filter(balance -> balance.getCurrency().equals(ETH)).findAny();
    assertTrue(ethBalance.isPresent());
    assertEquals(ETH, ethBalance.get().getCurrency());
    assertEquals(0, new BigDecimal("11").compareTo(ethBalance.get().getTotal()));
    assertEquals(0, new BigDecimal("22").compareTo(ethBalance.get().getAvailable()));
    assertEquals(0, new BigDecimal("33").compareTo(ethBalance.get().getFrozen()));
    assertEquals(0, new BigDecimal("44").compareTo(ethBalance.get().getBorrowed()));
    assertEquals(0, new BigDecimal("55").compareTo(ethBalance.get().getLoaned()));
    assertEquals(0, new BigDecimal("66").compareTo(ethBalance.get().getWithdrawing()));
    assertEquals(0, new BigDecimal("77").compareTo(ethBalance.get().getDepositing()));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Balance(tech.cassandre.trading.bot.api.graphql.client.generated.types.Balance) AccountByAccountIdProjectionRoot(tech.cassandre.trading.bot.api.graphql.client.generated.client.AccountByAccountIdProjectionRoot) AccountsGraphQLQuery(tech.cassandre.trading.bot.api.graphql.client.generated.client.AccountsGraphQLQuery) BaseMock(tech.cassandre.trading.bot.api.graphql.test.util.mock.BaseMock) Account(tech.cassandre.trading.bot.api.graphql.client.generated.types.Account) AccountByAccountIdGraphQLQuery(tech.cassandre.trading.bot.api.graphql.client.generated.client.AccountByAccountIdGraphQLQuery) BEFORE_EACH_TEST_METHOD(org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) BigDecimal(java.math.BigDecimal) DgsConstants(tech.cassandre.trading.bot.api.graphql.client.generated.DgsConstants) BTC(tech.cassandre.trading.bot.dto.util.CurrencyDTO.BTC) DgsAutoConfiguration(com.netflix.graphql.dgs.autoconfig.DgsAutoConfiguration) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ETH(tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH) AccountsProjectionRoot(tech.cassandre.trading.bot.api.graphql.client.generated.client.AccountsProjectionRoot) CassandreTradingBot(tech.cassandre.trading.bot.api.graphql.test.CassandreTradingBot) BaseDataFetcherTest(tech.cassandre.trading.bot.api.graphql.test.util.base.BaseDataFetcherTest) TypeRef(com.jayway.jsonpath.TypeRef) AccountDataFetcher(tech.cassandre.trading.bot.api.graphql.data.AccountDataFetcher) Import(org.springframework.context.annotation.Import) GraphQLQueryRequest(com.netflix.graphql.dgs.client.codegen.GraphQLQueryRequest) TestPropertySource(org.springframework.test.context.TestPropertySource) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) List(java.util.List) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DgsQueryExecutor(com.netflix.graphql.dgs.DgsQueryExecutor) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) AccountByAccountIdProjectionRoot(tech.cassandre.trading.bot.api.graphql.client.generated.client.AccountByAccountIdProjectionRoot) Account(tech.cassandre.trading.bot.api.graphql.client.generated.types.Account) AccountByAccountIdGraphQLQuery(tech.cassandre.trading.bot.api.graphql.client.generated.client.AccountByAccountIdGraphQLQuery) Balance(tech.cassandre.trading.bot.api.graphql.client.generated.types.Balance) BigDecimal(java.math.BigDecimal) GraphQLQueryRequest(com.netflix.graphql.dgs.client.codegen.GraphQLQueryRequest) BaseDataFetcherTest(tech.cassandre.trading.bot.api.graphql.test.util.base.BaseDataFetcherTest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with ETH

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

the class PositionGainsServiceTest method checkGainsCalculation.

@Test
@DisplayName("Check gains calculation")
public void checkGainsCalculation() {
    // =============================================================================================================
    /*
            Position 1 - Bought 10 BTC with USDT.
            TRADE_11 - Bought 7 for 11 = 77.
            TRADE_12 - Bought 3 for 12 = 36.
            TRADE_13 - Sold 1 for 13 = 13.
            TRADE_14 - Sold 1 for 14 = 14.
            TRADE_15 - Sold 8 for 15 = 120.
            We bought 10 BTC for 113 USDT and sold them for 147 USDT.
            Amount gain : 34 USDT.
            Amount percentage : 30.08 % - ((147 - 113) / 113) * 100.
            Fees : 15 USDT.
        */
    final Optional<PositionDTO> p1 = positionService.getPositionByUid(1L);
    assertTrue(p1.isPresent());
    final GainDTO gain1 = p1.get().getGain();
    // Gain (amount).
    assertEquals(0, new BigDecimal("34").compareTo(gain1.getAmount().getValue()));
    assertEquals(USDT, gain1.getAmount().getCurrency());
    // Gain (percentage).
    assertEquals(30.09, gain1.getPercentage());
    // Gain (fees).
    final Map<CurrencyDTO, CurrencyAmountDTO> gain1Fees = gain1.getFeesByCurrency();
    assertEquals(1, gain1Fees.size());
    assertNotNull(gain1Fees.get(USDT));
    assertEquals(0, new BigDecimal("15").compareTo(gain1Fees.get(USDT).getValue()));
    assertEquals(USDT, gain1Fees.get(USDT).getCurrency());
    // =============================================================================================================
    /*
            Position 2 - Bought 20 ETH with BTC.
            TRADE_21 - Bought 20 ETH for 100 = 2000.
            TRADE_22 - Sold 20 ETH for 50 = 1000.
            We bought 20 BTC for 2 000 BTC and sold them for 1 000 BTC.
            Amount gain : -1 000 BTC.
            Amount percentage : -50 % - ((1 000 - 2 000) / 2 000) * 100.
            Fees : 10 BTC.
         */
    final Optional<PositionDTO> p2 = positionService.getPositionByUid(2L);
    assertTrue(p2.isPresent());
    final GainDTO gain2 = p2.get().getGain();
    // Gain (amount).
    assertEquals(0, new BigDecimal("-1000").compareTo(gain2.getAmount().getValue()));
    assertEquals(BTC, gain2.getAmount().getCurrency());
    // Gain (percentage).
    assertEquals(-50, gain2.getPercentage());
    // Gain (fees).
    final Map<CurrencyDTO, CurrencyAmountDTO> gain2Fees = gain2.getFeesByCurrency();
    assertEquals(1, gain2Fees.size());
    assertNotNull(gain2Fees.get(BTC));
    assertEquals(0, new BigDecimal("10").compareTo(gain2Fees.get(BTC).getValue()));
    assertEquals(BTC, gain2Fees.get(BTC).getCurrency());
    // =============================================================================================================
    /*
            Position 3 - Bought 30 BTC with USDT.
            TRADE_31 - Bought 30 BTC for 20 = 600.
            TRADE_32 - Bought 30 BTC for 25 = 750.
            We bought 30 BTC for 600 USDT and sold them for 750 USDT.
            Amount gain : 150 USDT.
            Amount percentage : 25% - ((750 - 600) / 600) * 100.
            Fees : 11 USDT.
         */
    final Optional<PositionDTO> p3 = positionService.getPositionByUid(3L);
    assertTrue(p3.isPresent());
    final GainDTO gain3 = p3.get().getGain();
    // Gain (amount).
    assertEquals(0, new BigDecimal("150").compareTo(gain3.getAmount().getValue()));
    assertEquals(USDT, gain3.getAmount().getCurrency());
    // Gain (percentage).
    assertEquals(25, gain3.getPercentage());
    // Gain (fees).
    final Map<CurrencyDTO, CurrencyAmountDTO> gain3Fees = gain3.getFeesByCurrency();
    assertEquals(1, gain3Fees.size());
    assertNotNull(gain3Fees.get(USDT));
    assertEquals(0, new BigDecimal("11").compareTo(gain3Fees.get(USDT).getValue()));
    assertEquals(USDT, gain3Fees.get(USDT).getCurrency());
    // There should be no gain for positions 4,5 & 6.
    final Optional<PositionDTO> p4 = positionService.getPositionByUid(4L);
    assertTrue(p4.isPresent());
    assertEquals(0, p4.get().getGain().getPercentage());
    assertEquals(0, ZERO.compareTo(p4.get().getGain().getAmount().getValue()));
    final Optional<PositionDTO> p5 = positionService.getPositionByUid(5L);
    assertTrue(p5.isPresent());
    assertEquals(0, p5.get().getGain().getPercentage());
    assertEquals(0, ZERO.compareTo(p5.get().getGain().getAmount().getValue()));
    final Optional<PositionDTO> p6 = positionService.getPositionByUid(6L);
    assertTrue(p6.isPresent());
    assertEquals(0, p6.get().getGain().getPercentage());
    assertEquals(0, ZERO.compareTo(p6.get().getGain().getAmount().getValue()));
    // =============================================================================================================
    /*
            Position 7 (SHORT) - Sold 10 ETH for USDT.
            TRADE_63 - Sold 10 ETH with a price of 5 = 50 USDT.
            TRADE_64 - Bought ETH with 50 USDT with a price of 10 = 5 ETH
            Amount gain : -5 ETH.
            Amount percentage : -50%.
            Fees : 4 USDT.
         */
    final Optional<PositionDTO> p7 = positionService.getPositionByUid(7L);
    assertTrue(p7.isPresent());
    final GainDTO gain7 = p7.get().getGain();
    // Gain (amount).
    assertEquals(0, new BigDecimal("-5").compareTo(gain7.getAmount().getValue()));
    assertEquals(ETH, gain7.getAmount().getCurrency());
    // Gain (percentage).
    assertEquals(-50, gain7.getPercentage());
    // Gain (fees).
    final Map<CurrencyDTO, CurrencyAmountDTO> gain7Fees = gain7.getFeesByCurrency();
    assertEquals(1, gain7Fees.size());
    assertNotNull(gain7Fees.get(ETH));
    assertEquals(0, new BigDecimal("4").compareTo(gain7Fees.get(ETH).getValue()));
    assertEquals(ETH, gain7Fees.get(ETH).getCurrency());
    // =============================================================================================================
    // Check all gains.
    final Map<CurrencyDTO, GainDTO> gains = positionService.getGains();
    assertEquals(3, gains.size());
    // USDT Gains.
    final GainDTO usdtGain = gains.get(USDT);
    assertNotNull(usdtGain);
    assertEquals(25.81, usdtGain.getPercentage());
    assertEquals(0, new BigDecimal("184").compareTo(usdtGain.getAmount().getValue()));
    assertEquals(USDT, usdtGain.getAmount().getCurrency());
    // BTC Gains.
    final GainDTO btcGain = gains.get(BTC);
    assertNotNull(btcGain);
    assertEquals(-50, btcGain.getPercentage());
    assertEquals(0, new BigDecimal("-1000").compareTo(btcGain.getAmount().getValue()));
    assertEquals(BTC, btcGain.getAmount().getCurrency());
    // ETH Gains.
    final GainDTO ethGain = gains.get(ETH);
    assertNotNull(ethGain);
    assertEquals(-50, ethGain.getPercentage());
    assertEquals(0, new BigDecimal("-5").compareTo(ethGain.getAmount().getValue()));
    assertEquals(ETH, ethGain.getAmount().getCurrency());
    // ALl fees with getOrdersFees().
    final Map<CurrencyDTO, CurrencyAmountDTO> fees = ethGain.getFeesByCurrency();
    assertEquals(3, fees.size());
    // USDT.
    assertNotNull(fees.get(USDT));
    assertEquals(0, new BigDecimal("26").compareTo(fees.get(USDT).getValue()));
    assertEquals(USDT, fees.get(USDT).getCurrency());
    // BTC.
    assertNotNull(fees.get(BTC));
    assertEquals(0, new BigDecimal("10").compareTo(fees.get(BTC).getValue()));
    assertEquals(BTC, fees.get(BTC).getCurrency());
    // ETH.
    assertNotNull(fees.get(ETH));
    assertEquals(0, new BigDecimal("4").compareTo(fees.get(ETH).getValue()));
    assertEquals(ETH, fees.get(ETH).getCurrency());
    // ALl fees with getOrdersFees().
    assertEquals(3, ethGain.getFees().size());
    // USDT.
    Optional<CurrencyAmountDTO> usdtFees = ethGain.getFees().stream().filter(currencyAmountDTO -> currencyAmountDTO.getCurrency().equals(USDT)).findAny();
    assertTrue(usdtFees.isPresent());
    assertEquals(0, new BigDecimal("26").compareTo(usdtFees.get().getValue()));
    assertEquals(USDT, usdtFees.get().getCurrency());
    // BTC.
    Optional<CurrencyAmountDTO> btcFees = ethGain.getFees().stream().filter(currencyAmountDTO -> currencyAmountDTO.getCurrency().equals(BTC)).findAny();
    assertTrue(btcFees.isPresent());
    assertEquals(0, new BigDecimal("10").compareTo(btcFees.get().getValue()));
    assertEquals(BTC, btcFees.get().getCurrency());
    // ETH.
    Optional<CurrencyAmountDTO> ethFees = ethGain.getFees().stream().filter(currencyAmountDTO -> currencyAmountDTO.getCurrency().equals(ETH)).findAny();
    assertTrue(ethFees.isPresent());
    assertEquals(0, new BigDecimal("4").compareTo(ethFees.get().getValue()));
    assertEquals(ETH, ethFees.get().getCurrency());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) PositionDTO(tech.cassandre.trading.bot.dto.position.PositionDTO) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Configuration(tech.cassandre.trading.bot.test.util.junit.configuration.Configuration) CurrencyDTO(tech.cassandre.trading.bot.dto.util.CurrencyDTO) BEFORE_EACH_TEST_METHOD(org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) GainDTO(tech.cassandre.trading.bot.dto.util.GainDTO) PositionService(tech.cassandre.trading.bot.service.PositionService) Autowired(org.springframework.beans.factory.annotation.Autowired) ZERO(java.math.BigDecimal.ZERO) ActiveProfiles(org.springframework.test.context.ActiveProfiles) CurrencyAmountDTO(tech.cassandre.trading.bot.dto.util.CurrencyAmountDTO) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) BigDecimal(java.math.BigDecimal) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Map(java.util.Map) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) BTC(tech.cassandre.trading.bot.dto.util.CurrencyDTO.BTC) USDT(tech.cassandre.trading.bot.dto.util.CurrencyDTO.USDT) Optional(java.util.Optional) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Property(tech.cassandre.trading.bot.test.util.junit.configuration.Property) ETH(tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH) CurrencyAmountDTO(tech.cassandre.trading.bot.dto.util.CurrencyAmountDTO) GainDTO(tech.cassandre.trading.bot.dto.util.GainDTO) PositionDTO(tech.cassandre.trading.bot.dto.position.PositionDTO) BigDecimal(java.math.BigDecimal) CurrencyDTO(tech.cassandre.trading.bot.dto.util.CurrencyDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Optional (java.util.Optional)12 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)12 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)12 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)12 DisplayName (org.junit.jupiter.api.DisplayName)12 Test (org.junit.jupiter.api.Test)12 Autowired (org.springframework.beans.factory.annotation.Autowired)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 ActiveProfiles (org.springframework.test.context.ActiveProfiles)12 BTC (tech.cassandre.trading.bot.dto.util.CurrencyDTO.BTC)12 ETH (tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH)12 CurrencyPairDTO (tech.cassandre.trading.bot.dto.util.CurrencyPairDTO)10 BigDecimal (java.math.BigDecimal)9 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)9 TestPropertySource (org.springframework.test.context.TestPropertySource)9 ZonedDateTime (java.time.ZonedDateTime)8 Awaitility.await (org.awaitility.Awaitility.await)8 Tag (org.junit.jupiter.api.Tag)8 BaseTest (tech.cassandre.trading.bot.test.util.junit.BaseTest)8 TestableCassandreStrategy (tech.cassandre.trading.bot.test.util.strategies.TestableCassandreStrategy)8