Search in sources :

Example 1 with DryModeException

use of tech.cassandre.trading.bot.util.exception.DryModeException in project cassandre-trading-bot by cassandre-tech.

the class TradeServiceDryModeAOP method createBuyMarketOrder.

@Around(value = "execution(* tech.cassandre.trading.bot.service.TradeService.createBuyMarketOrder(..)) && args(strategy, currencyPair, amount)", argNames = "pjp, strategy, currencyPair, amount")
public final OrderCreationResultDTO createBuyMarketOrder(final ProceedingJoinPoint pjp, final CassandreStrategy strategy, final CurrencyPairDTO currencyPair, final BigDecimal amount) {
    // We check that we have the trade account.
    final Optional<AccountDTO> tradeAccount = strategy.getTradeAccount();
    if (tradeAccount.isEmpty()) {
        throw new DryModeException("Trade account was not found");
    }
    // We check if we have enough assets to buy.
    // Buying order - we buy ETH with BTC.
    // We are buying the following amount: ticker last price * amount
    Optional<BalanceDTO> balance = tradeAccount.get().getBalance(currencyPair.getQuoteCurrency());
    final Optional<TickerDTO> ticker = strategy.getLastTickerByCurrencyPair(currencyPair);
    if (balance.isPresent() && ticker.isPresent()) {
        BigDecimal ownedAssets = balance.get().getAvailable();
        BigDecimal cost = ticker.get().getLast().multiply(amount);
        if (cost.compareTo(ownedAssets) > 0) {
            final String errorMessage = "Not enough assets (costs: " + cost + " " + currencyPair.getQuoteCurrency() + " - owned assets: " + ownedAssets + " " + currencyPair.getQuoteCurrency() + ")";
            return new OrderCreationResultDTO(errorMessage, new RuntimeException());
        }
    } else {
        return new OrderCreationResultDTO("No assets (" + currencyPair.getQuoteCurrency() + ")", new RuntimeException());
    }
    // We execute the buy.
    Object result = null;
    try {
        result = pjp.proceed();
    } catch (Throwable throwable) {
        logger.error("Error in Dry mode AOP: {}", throwable.getMessage());
    }
    // We update the account.
    userService.addToBalance(strategy, CURRENCY_MAPPER.mapToCurrency(currencyPair.getBaseCurrency()), amount);
    userService.addToBalance(strategy, CURRENCY_MAPPER.mapToCurrency(currencyPair.getQuoteCurrency()), amount.multiply(ticker.get().getLast()).multiply(new BigDecimal("-1")));
    return (OrderCreationResultDTO) result;
}
Also used : TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) DryModeException(tech.cassandre.trading.bot.util.exception.DryModeException) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) BalanceDTO(tech.cassandre.trading.bot.dto.user.BalanceDTO) BigDecimal(java.math.BigDecimal) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) Around(org.aspectj.lang.annotation.Around)

Example 2 with DryModeException

use of tech.cassandre.trading.bot.util.exception.DryModeException in project cassandre-trading-bot by cassandre-tech.

the class TradeServiceDryModeAOP method createSellMarketOrder.

@Around(value = "execution(* tech.cassandre.trading.bot.service.TradeService.createSellMarketOrder(..)) && args(strategy, currencyPair, amount)", argNames = "pjp, strategy, currencyPair, amount")
public final OrderCreationResultDTO createSellMarketOrder(final ProceedingJoinPoint pjp, final CassandreStrategy strategy, final CurrencyPairDTO currencyPair, final BigDecimal amount) {
    // We check that we have the trade account.
    final Optional<AccountDTO> tradeAccount = strategy.getTradeAccount();
    if (tradeAccount.isEmpty()) {
        throw new DryModeException("Trade account was not found");
    }
    // Selling order - we sell ETH to buy BTC.
    // We are selling the amount
    Optional<BalanceDTO> balance = tradeAccount.get().getBalance(currencyPair.getBaseCurrency());
    final Optional<TickerDTO> ticker = strategy.getLastTickerByCurrencyPair(currencyPair);
    if (balance.isPresent() && ticker.isPresent()) {
        BigDecimal ownedAssets = balance.get().getAvailable();
        if (amount.compareTo(ownedAssets) > 0) {
            final String errorMessage = "Not enough assets (amount: " + amount + " " + currencyPair.getQuoteCurrency() + " - owned assets: " + ownedAssets + " " + currencyPair.getBaseCurrency();
            return new OrderCreationResultDTO(errorMessage, new RuntimeException());
        }
    } else {
        return new OrderCreationResultDTO("No assets (" + currencyPair.getBaseCurrency() + ")", new RuntimeException());
    }
    // We execute the sell.
    Object result = null;
    try {
        result = pjp.proceed();
    } catch (Throwable throwable) {
        logger.error("Error in Dry mode AOP: {}", throwable.getMessage());
    }
    // We update the account.
    userService.addToBalance(strategy, CURRENCY_MAPPER.mapToCurrency(currencyPair.getBaseCurrency()), amount.multiply(new BigDecimal("-1")));
    userService.addToBalance(strategy, CURRENCY_MAPPER.mapToCurrency(currencyPair.getQuoteCurrency()), amount.multiply(ticker.get().getLast()));
    return (OrderCreationResultDTO) result;
}
Also used : TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) DryModeException(tech.cassandre.trading.bot.util.exception.DryModeException) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) BalanceDTO(tech.cassandre.trading.bot.dto.user.BalanceDTO) BigDecimal(java.math.BigDecimal) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) Around(org.aspectj.lang.annotation.Around)

Example 3 with DryModeException

use of tech.cassandre.trading.bot.util.exception.DryModeException in project cassandre-trading-bot by cassandre-tech.

the class TradeServiceDryModeAOP method createBuyMarketOrder.

@Around(value = "execution(* tech.cassandre.trading.bot.service.TradeService.createBuyMarketOrder(..)) && args(strategy, currencyPair, amount)", argNames = "pjp, strategy, currencyPair, amount")
public final OrderCreationResultDTO createBuyMarketOrder(final ProceedingJoinPoint pjp, final GenericCassandreStrategy strategy, final CurrencyPairDTO currencyPair, final BigDecimal amount) {
    // We check that we have the trade account.
    final Optional<AccountDTO> tradeAccount = strategy.getTradeAccount();
    if (tradeAccount.isEmpty()) {
        throw new DryModeException("Trade account was not found");
    }
    // We check if we have enough assets to buy.
    // Buying order - we buy ETH with BTC.
    // We are buying the following amount: ticker last price * amount
    Optional<BalanceDTO> balance = tradeAccount.get().getBalance(currencyPair.getQuoteCurrency());
    final Optional<TickerDTO> ticker = strategy.getLastTickerByCurrencyPair(currencyPair);
    if (balance.isPresent() && ticker.isPresent()) {
        BigDecimal ownedAssets = balance.get().getAvailable();
        BigDecimal cost = ticker.get().getLast().multiply(amount);
        if (cost.compareTo(ownedAssets) > 0) {
            final String errorMessage = "Not enough assets (costs: " + cost + " " + currencyPair.getQuoteCurrency() + " - owned assets: " + ownedAssets + " " + currencyPair.getQuoteCurrency() + ")";
            return new OrderCreationResultDTO(errorMessage, new RuntimeException());
        }
    } else {
        return new OrderCreationResultDTO("No assets (" + currencyPair.getQuoteCurrency() + ")", new RuntimeException());
    }
    // We execute the buy.
    Object result = null;
    try {
        result = pjp.proceed();
    } catch (Throwable throwable) {
        logger.error("Error in Dry mode AOP: {}", throwable.getMessage());
    }
    // We update the account.
    userService.addToBalance(strategy, CURRENCY_MAPPER.mapToCurrency(currencyPair.getBaseCurrency()), amount);
    userService.addToBalance(strategy, CURRENCY_MAPPER.mapToCurrency(currencyPair.getQuoteCurrency()), amount.multiply(ticker.get().getLast()).multiply(new BigDecimal("-1")));
    return (OrderCreationResultDTO) result;
}
Also used : TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) DryModeException(tech.cassandre.trading.bot.util.exception.DryModeException) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) BalanceDTO(tech.cassandre.trading.bot.dto.user.BalanceDTO) BigDecimal(java.math.BigDecimal) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) Around(org.aspectj.lang.annotation.Around)

Example 4 with DryModeException

use of tech.cassandre.trading.bot.util.exception.DryModeException in project cassandre-trading-bot by cassandre-tech.

the class TradeServiceDryModeAOP method createSellMarketOrder.

@Around(value = "execution(* tech.cassandre.trading.bot.service.TradeService.createSellMarketOrder(..)) && args(strategy, currencyPair, amount)", argNames = "pjp, strategy, currencyPair, amount")
public final OrderCreationResultDTO createSellMarketOrder(final ProceedingJoinPoint pjp, final GenericCassandreStrategy strategy, final CurrencyPairDTO currencyPair, final BigDecimal amount) {
    // We check that we have the trade account.
    final Optional<AccountDTO> tradeAccount = strategy.getTradeAccount();
    if (tradeAccount.isEmpty()) {
        throw new DryModeException("Trade account was not found");
    }
    // Selling order - we sell ETH to buy BTC.
    // We are selling the amount
    Optional<BalanceDTO> balance = tradeAccount.get().getBalance(currencyPair.getBaseCurrency());
    final Optional<TickerDTO> ticker = strategy.getLastTickerByCurrencyPair(currencyPair);
    if (balance.isPresent() && ticker.isPresent()) {
        BigDecimal ownedAssets = balance.get().getAvailable();
        if (amount.compareTo(ownedAssets) > 0) {
            final String errorMessage = "Not enough assets (amount: " + amount + " " + currencyPair.getQuoteCurrency() + " - owned assets: " + ownedAssets + " " + currencyPair.getBaseCurrency();
            return new OrderCreationResultDTO(errorMessage, new RuntimeException());
        }
    } else {
        return new OrderCreationResultDTO("No assets (" + currencyPair.getBaseCurrency() + ")", new RuntimeException());
    }
    // We execute the sell.
    Object result = null;
    try {
        result = pjp.proceed();
    } catch (Throwable throwable) {
        logger.error("Error in Dry mode AOP: {}", throwable.getMessage());
    }
    // We update the account.
    userService.addToBalance(strategy, CURRENCY_MAPPER.mapToCurrency(currencyPair.getBaseCurrency()), amount.multiply(new BigDecimal("-1")));
    userService.addToBalance(strategy, CURRENCY_MAPPER.mapToCurrency(currencyPair.getQuoteCurrency()), amount.multiply(ticker.get().getLast()));
    return (OrderCreationResultDTO) result;
}
Also used : TickerDTO(tech.cassandre.trading.bot.dto.market.TickerDTO) DryModeException(tech.cassandre.trading.bot.util.exception.DryModeException) OrderCreationResultDTO(tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO) BalanceDTO(tech.cassandre.trading.bot.dto.user.BalanceDTO) BigDecimal(java.math.BigDecimal) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) Around(org.aspectj.lang.annotation.Around)

Aggregations

BigDecimal (java.math.BigDecimal)4 Around (org.aspectj.lang.annotation.Around)4 TickerDTO (tech.cassandre.trading.bot.dto.market.TickerDTO)4 OrderCreationResultDTO (tech.cassandre.trading.bot.dto.trade.OrderCreationResultDTO)4 AccountDTO (tech.cassandre.trading.bot.dto.user.AccountDTO)4 BalanceDTO (tech.cassandre.trading.bot.dto.user.BalanceDTO)4 DryModeException (tech.cassandre.trading.bot.util.exception.DryModeException)4