Search in sources :

Example 36 with APIException

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

the class PortfolioOrderManager method closeTrade.

/**
 * Close a trade
 * @param trade
 */
public void closeTrade(final Trade trade) {
    if (!bitfinexApiBroker.isAuthenticated()) {
        logger.error("Unable to execute trade {} on marketplace, conecction is not authenticated", trade);
        return;
    }
    final double amount = trade.getAmount() * -1.0;
    final BitfinexOrder order = BitfinexOrderBuilder.create(trade.getSymbol(), BitfinexOrderType.EXCHANGE_MARKET, amount).build();
    try {
        trade.setTradeState(TradeState.CLOSING);
        trade.addCloseOrder(order);
        bitfinexApiBroker.getOrderManager().placeOrder(order);
        trade.setTradeState(TradeState.CLOSED);
    } catch (APIException e) {
        logger.error("Got an exception while closing trade {}", trade);
        trade.setTradeState(TradeState.ERROR);
    } finally {
        persistTrade(trade);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)

Example 37 with APIException

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

the class PortfolioOrderManager method openTrade.

/**
 * Open a new trade
 * @param trade
 */
public void openTrade(final Trade trade) {
    if (!bitfinexApiBroker.isAuthenticated()) {
        logger.error("Unable to execute trade {} on marketplace, conecction is not authenticated", trade);
        return;
    }
    final double amount = trade.getAmount();
    final BitfinexOrder order = BitfinexOrderBuilder.create(trade.getSymbol(), BitfinexOrderType.EXCHANGE_MARKET, amount).build();
    try {
        trade.setTradeState(TradeState.OPENING);
        trade.addOpenOrder(order);
        bitfinexApiBroker.getOrderManager().placeOrder(order);
        trade.setTradeState(TradeState.OPEN);
    } catch (APIException e) {
        logger.error("Got an exception while opening trade {}", trade);
        trade.setTradeState(TradeState.ERROR);
    } finally {
        persistTrade(trade);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)

Example 38 with APIException

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

the class BasePortfolioManager method getTotalPortfolioValueInUSD.

@Override
protected double getTotalPortfolioValueInUSD() throws APIException {
    final List<Wallet> wallets = getAllWallets();
    double totalValue = 0;
    for (final Wallet wallet : wallets) {
        final String curreny = wallet.getCurreny();
        if (curreny.equals("USD")) {
            totalValue = totalValue + wallet.getBalance();
        } else {
            final String symbol = "t" + curreny + "USD";
            try {
                final BitfinexCurrencyPair bitfinexCurrencyPair = BitfinexCurrencyPair.fromSymbolString(symbol);
                final BitfinexTickerSymbol bitfinexSymbol = new BitfinexTickerSymbol(bitfinexCurrencyPair);
                final BitfinexTick lastTick = bitfinexApiBroker.getQuoteManager().getLastTick(bitfinexSymbol);
                if (lastTick != null) {
                    final double rate = lastTick.getClose();
                    final double value = rate * wallet.getBalance();
                    totalValue = totalValue + value;
                } else {
                    logger.debug("Unable to find tick for {}, appraise wallet with 0 USD", symbol);
                }
            } catch (IllegalArgumentException e) {
                logger.debug("Unkown symbol {}, ignoring wallet", symbol);
            }
        }
    }
    return totalValue;
}
Also used : BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)

Example 39 with APIException

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

the class MarginPortfolioManager method getOpenPositionSizeForCurrency.

/**
 * Get the position size for the symbol
 * @param symbol
 * @return
 * @throws APIException
 */
@Override
protected double getOpenPositionSizeForCurrency(final String currency) throws APIException {
    final List<Position> positions = bitfinexApiBroker.getPositionManager().getPositions();
    final Position position = positions.stream().filter(p -> p.getCurreny().getCurrency1().equals(currency)).findAny().orElse(null);
    if (position == null) {
        return 0;
    }
    return position.getAmount();
}
Also used : Position(com.github.jnidzwetzki.bitfinex.v2.entity.Position)

Example 40 with APIException

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

the class MarginPortfolioManager 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.getBalanceAvailable();
        }
    }
    logger.error("Unable to find USD wallet");
    return 0;
}
Also used : Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet)

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