Search in sources :

Example 1 with HttpStatusIOException

use of si.mazi.rescu.HttpStatusIOException in project XChange by knowm.

the class CoinfloorTradeServiceRaw method placeMarketOrder.

public CoinfloorMarketOrderResponse placeMarketOrder(CurrencyPair pair, OrderType side, BigDecimal amount) throws IOException {
    Currency base = normalise(pair.base);
    Currency counter = normalise(pair.counter);
    try {
        if (side == OrderType.BID) {
            return coinfloor.buyMarket(base, counter, amount);
        } else {
            return coinfloor.sellMarket(base, counter, amount);
        }
    } catch (HttpStatusIOException e) {
        if (e.getHttpStatusCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
            // contains the message: "quantity" parameter must not have more than 4 fractional digits
            throw new ExchangeException(e.getHttpBody(), e);
        } else {
            throw e;
        }
    }
}
Also used : Currency(org.knowm.xchange.currency.Currency) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) HttpStatusIOException(si.mazi.rescu.HttpStatusIOException)

Example 2 with HttpStatusIOException

use of si.mazi.rescu.HttpStatusIOException in project XChange by knowm.

the class CoinfloorTradeServiceRaw method placeLimitOrder.

public CoinfloorOrder placeLimitOrder(CurrencyPair pair, OrderType side, BigDecimal amount, BigDecimal price) throws IOException {
    Currency base = normalise(pair.base);
    Currency counter = normalise(pair.counter);
    try {
        if (side == OrderType.BID) {
            return coinfloor.buy(base, counter, amount, price);
        } else {
            return coinfloor.sell(base, counter, amount, price);
        }
    } catch (HttpStatusIOException e) {
        if (e.getHttpStatusCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
            // contains the message: "amount" parameter must not have more than 4 fractional digits
            throw new ExchangeException(e.getHttpBody(), e);
        } else {
            throw e;
        }
    }
}
Also used : Currency(org.knowm.xchange.currency.Currency) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) HttpStatusIOException(si.mazi.rescu.HttpStatusIOException)

Example 3 with HttpStatusIOException

use of si.mazi.rescu.HttpStatusIOException in project XChange by knowm.

the class QuoineTradeServiceRaw method placeLimitOrder.

public QuoineOrderResponse placeLimitOrder(CurrencyPair currencyPair, String type, BigDecimal originalAmount, BigDecimal price) throws IOException {
    int productId = productId(currencyPair);
    QuoineNewOrderRequest quoineNewOrderRequest = useMargin ? new QuoineNewMarginOrderRequest("limit", productId, type, originalAmount, price, leverageLevel, currencyPair.counter.getCurrencyCode()) : new QuoineNewOrderRequest("limit", productId, type, originalAmount, price);
    try {
        return quoine.placeOrder(QUOINE_API_VERSION, signatureCreator, contentType, new QuoineNewOrderRequestWrapper(quoineNewOrderRequest));
    } catch (HttpStatusIOException e) {
        throw handleHttpError(e);
    }
}
Also used : QuoineNewMarginOrderRequest(org.knowm.xchange.quoine.dto.trade.QuoineNewMarginOrderRequest) QuoineNewOrderRequestWrapper(org.knowm.xchange.quoine.dto.trade.QuoineNewOrderRequestWrapper) QuoineNewOrderRequest(org.knowm.xchange.quoine.dto.trade.QuoineNewOrderRequest) HttpStatusIOException(si.mazi.rescu.HttpStatusIOException)

Example 4 with HttpStatusIOException

use of si.mazi.rescu.HttpStatusIOException in project cassandre-trading-bot by cassandre-tech.

the class ExchangeAutoConfiguration method configure.

/**
 * Instantiating the exchange services based on user parameters.
 */
@PostConstruct
public void configure() {
    try {
        // Instantiate exchange class.
        Class<? extends Exchange> exchangeClass = Class.forName(getExchangeClassName()).asSubclass(Exchange.class);
        ExchangeSpecification exchangeSpecification = new ExchangeSpecification(exchangeClass);
        // Exchange configuration.
        exchangeSpecification.setUserName(exchangeParameters.getUsername());
        exchangeSpecification.setApiKey(exchangeParameters.getKey());
        exchangeSpecification.setSecretKey(exchangeParameters.getSecret());
        exchangeSpecification.getResilience().setRateLimiterEnabled(true);
        exchangeSpecification.setExchangeSpecificParametersItem("Use_Sandbox", exchangeParameters.getModes().getSandbox());
        exchangeSpecification.setExchangeSpecificParametersItem("passphrase", exchangeParameters.getPassphrase());
        exchangeSpecification.setProxyHost(exchangeParameters.getProxyHost());
        exchangeSpecification.setProxyPort(exchangeParameters.getProxyPort());
        exchangeSpecification.setSslUri(exchangeParameters.getSslUri());
        exchangeSpecification.setPlainTextUri(exchangeParameters.getPlainTextUri());
        exchangeSpecification.setHost(exchangeParameters.getHost());
        if (exchangeParameters.getPort() != null) {
            exchangeSpecification.setPort(Integer.parseInt(exchangeParameters.getPort()));
        }
        // Creates XChange services.
        xChangeExchange = ExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
        xChangeAccountService = xChangeExchange.getAccountService();
        xChangeMarketDataService = xChangeExchange.getMarketDataService();
        xChangeTradeService = xChangeExchange.getTradeService();
        // Force login to check credentials.
        logger.info("Exchange connection with driver {}", exchangeParameters.getDriverClassName());
        xChangeAccountService.getAccountInfo();
        logger.info("Exchange connection successful with username {} (Dry mode: {} / Sandbox: {})", exchangeParameters.getUsername(), exchangeParameters.getModes().getDry(), exchangeParameters.getModes().getSandbox());
    } catch (ClassNotFoundException e) {
        // If we can't find the exchange class.
        throw new ConfigurationException("Impossible to find the exchange driver class you requested: " + exchangeParameters.getDriverClassName(), "Choose and configure a valid exchange (https://trading-bot.cassandre.tech/learn/exchange-connection-configuration.html#how-does-it-works)");
    } catch (HttpStatusIOException e) {
        if (e.getHttpStatusCode() == UNAUTHORIZED_STATUS_CODE) {
            // Authorization failure.
            throw new ConfigurationException("Invalid credentials for " + exchangeParameters.getDriverClassName(), "Check your exchange credentials: " + e.getMessage() + " - login used: " + exchangeParameters.getUsername());
        } else {
            // Another HTTP failure.
            throw new ConfigurationException("Error while connecting to the exchange: " + e.getMessage());
        }
    } catch (IOException e) {
        throw new ConfigurationException("IO error: " + e.getMessage());
    }
}
Also used : ConfigurationException(tech.cassandre.trading.bot.util.exception.ConfigurationException) ExchangeSpecification(org.knowm.xchange.ExchangeSpecification) IOException(java.io.IOException) HttpStatusIOException(si.mazi.rescu.HttpStatusIOException) HttpStatusIOException(si.mazi.rescu.HttpStatusIOException) PostConstruct(javax.annotation.PostConstruct)

Example 5 with HttpStatusIOException

use of si.mazi.rescu.HttpStatusIOException in project XChange by knowm.

the class CmcErrorAdapter method adapt.

/**
 * Parse errors from HTTP exceptions
 */
public static void adapt(HttpStatusIOException httpStatusException) {
    String msg = "HTTP Status: " + httpStatusException.getHttpStatusCode();
    // if we have a HTTP body try to parse more error details from body
    if (isNotEmpty(httpStatusException.getHttpBody())) {
        ObjectMapper mapper = new ObjectMapper();
        CmcResult result;
        try {
            result = mapper.readValue(httpStatusException.getHttpBody(), CmcResult.class);
        } catch (Exception e) {
            // but ignore errors on parsing and throw generic ExchangeException instead
            throw new ExchangeException(msg, httpStatusException);
        }
        // but if it contains a parsable result, then try to parse errors from result:
        if (result.getStatus() != null && isNotEmpty(result.getStatus().getErrorMessage()) && !result.isSuccess()) {
            String error = result.getStatus().getErrorMessage();
            if (result.getStatus().getErrorCode() == 401) {
                throw new ExchangeSecurityException(error);
            }
            if (result.getStatus().getErrorCode() == 402) {
                throw new FundsExceededException(error);
            }
            if (result.getStatus().getErrorCode() == 429) {
                throw new FrequencyLimitExceededException(error);
            }
            msg = error + " - ErrorCode: " + result.getStatus().getErrorCode();
            throw new ExchangeException(msg);
        }
    }
    // else: just throw ExchangeException with causing Exception
    throw new ExchangeException(msg, httpStatusException);
}
Also used : CmcResult(org.knowm.xchange.coinmarketcap.pro.v1.dto.CmcResult) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) FrequencyLimitExceededException(org.knowm.xchange.exceptions.FrequencyLimitExceededException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FundsExceededException(org.knowm.xchange.exceptions.FundsExceededException) FrequencyLimitExceededException(org.knowm.xchange.exceptions.FrequencyLimitExceededException) ExchangeSecurityException(org.knowm.xchange.exceptions.ExchangeSecurityException) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) HttpStatusIOException(si.mazi.rescu.HttpStatusIOException) ExchangeSecurityException(org.knowm.xchange.exceptions.ExchangeSecurityException) FundsExceededException(org.knowm.xchange.exceptions.FundsExceededException)

Aggregations

HttpStatusIOException (si.mazi.rescu.HttpStatusIOException)9 CmcCurrencyInfoResponse (org.knowm.xchange.coinmarketcap.pro.v1.dto.marketdata.response.CmcCurrencyInfoResponse)3 Currency (org.knowm.xchange.currency.Currency)3 ExchangeException (org.knowm.xchange.exceptions.ExchangeException)3 IOException (java.io.IOException)2 QuoineNewMarginOrderRequest (org.knowm.xchange.quoine.dto.trade.QuoineNewMarginOrderRequest)2 QuoineNewOrderRequest (org.knowm.xchange.quoine.dto.trade.QuoineNewOrderRequest)2 QuoineNewOrderRequestWrapper (org.knowm.xchange.quoine.dto.trade.QuoineNewOrderRequestWrapper)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Exchange (org.knowm.xchange.Exchange)1 ExchangeSpecification (org.knowm.xchange.ExchangeSpecification)1 CmcErrorAdapter (org.knowm.xchange.coinmarketcap.pro.v1.CmcErrorAdapter)1 CmcResult (org.knowm.xchange.coinmarketcap.pro.v1.dto.CmcResult)1 CmcCurrency (org.knowm.xchange.coinmarketcap.pro.v1.dto.marketdata.CmcCurrency)1