Search in sources :

Example 1 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.

the class BasePortfolioManager method getAvailablePortfolioValueInUSD.

@Override
protected double getAvailablePortfolioValueInUSD() throws APIException {
    final List<Wallet> wallets = getAllWallets();
    for (final Wallet wallet : wallets) {
        if (wallet.getCurreny().equals("USD")) {
            return wallet.getBalance();
        }
    }
    logger.error("Unable to find USD wallet");
    return 0;
}
Also used : Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet)

Example 2 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.

the class MarginPortfolioManager method getTotalPortfolioValueInUSD.

@Override
protected double getTotalPortfolioValueInUSD() throws APIException {
    final List<Wallet> wallets = getAllWallets();
    for (final Wallet wallet : wallets) {
        if (wallet.getCurreny().equals("USD")) {
            return wallet.getBalance();
        }
    }
    logger.error("Unable to find USD wallet");
    return 0;
}
Also used : Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet)

Example 3 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.

the class PortfolioManager method cancelRemovedEntryOrders.

/**
 * Cancel the removed entry orders
 * Position is at the moment not interesting for an entry
 *
 * @param entries
 * @throws APIException
 * @throws InterruptedException
 */
private void cancelRemovedEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
    final List<ExchangeOrder> entryOrders = getAllOpenEntryOrders();
    for (final ExchangeOrder order : entryOrders) {
        final String symbol = order.getSymbol();
        final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.fromSymbolString(symbol);
        if (!entries.containsKey(currencyPair)) {
            logger.info("Entry order for {} is not contained, canceling", currencyPair);
            cancelOrder(order);
        }
    }
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 4 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.

the class PortfolioManager method placeNewExitOrders.

/**
 * Place the exit orders
 * @param exits
 * @throws APIException
 * @throws InterruptedException
 */
private void placeNewExitOrders(final Map<BitfinexCurrencyPair, Double> exits) throws APIException, InterruptedException {
    for (final BitfinexCurrencyPair currency : exits.keySet()) {
        final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
        final double exitPrice = exits.get(currency);
        // Check old orders
        if (order != null) {
            final double orderPrice = order.getPrice();
            if (orderPrice >= exitPrice || MathHelper.almostEquals(orderPrice, exitPrice)) {
                logger.info("Old order price for {} is fine (price: order {} model {})", currency, orderPrice, exitPrice);
                continue;
            }
            logger.info("Exit price for {} has moved form {} to {}, canceling old order", currency, orderPrice, exitPrice);
            cancelOrder(order);
        }
        final double positionSize = getOpenPositionSizeForCurrency(currency.getCurrency1());
        // * -1.0 for sell order
        final double positionSizeSell = positionSize * -1.0;
        final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSizeSell).withPrice(exitPrice).setPostOnly().build();
        placeOrder(newOrder);
    }
}
Also used : BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 5 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.

the class PortfolioManager method cleanupOldExitOrders.

/**
 * Cleanup the old exit orders (remove duplicates, unknown orders)
 * @param exits
 * @throws APIException
 * @throws InterruptedException
 */
private void cleanupOldExitOrders(final Map<BitfinexCurrencyPair, Double> exits) throws APIException, InterruptedException {
    final List<ExchangeOrder> oldExitOrders = getAllOpenExitOrders();
    // Remove unknown orders
    for (final ExchangeOrder order : oldExitOrders) {
        final BitfinexCurrencyPair symbol = BitfinexCurrencyPair.fromSymbolString(order.getSymbol());
        if (!exits.containsKey(symbol)) {
            logger.error("Found old and unknown order {}, canceling", order);
            cancelOrder(order);
        }
    }
    // Remove duplicates
    final Map<String, List<ExchangeOrder>> oldOrders = oldExitOrders.stream().collect(Collectors.groupingBy(ExchangeOrder::getSymbol));
    for (final String symbol : oldOrders.keySet()) {
        final List<ExchangeOrder> symbolOrderList = oldOrders.get(symbol);
        if (symbolOrderList.size() > 1) {
            logger.error("Found duplicates {}", symbolOrderList);
            for (final ExchangeOrder order : symbolOrderList) {
                cancelOrder(order);
            }
        }
    }
}
Also used : List(java.util.List) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)33 Test (org.junit.Test)33 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)25 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)20 JSONArray (org.json.JSONArray)20 CountDownLatch (java.util.concurrent.CountDownLatch)14 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)12 HashMap (java.util.HashMap)11 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)10 BitfinexOrder (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)9 ConnectionCapabilities (com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)9 Wallet (com.github.jnidzwetzki.bitfinex.v2.entity.Wallet)9 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)8 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)8 CurrencyEntry (com.github.jnidzwetzki.cryptobot.CurrencyEntry)8 BasePortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager)8 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)7 PortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager)7 ArrayList (java.util.ArrayList)7