Search in sources :

Example 26 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class BitfinexApiBroker method resubscribeChannels.

/**
 * Resubscribe the old ticker
 * @throws InterruptedException
 * @throws APIException
 */
private void resubscribeChannels() throws InterruptedException, APIException {
    final Map<Integer, BitfinexStreamSymbol> oldChannelIdSymbolMap = new HashMap<>();
    synchronized (channelIdSymbolMap) {
        oldChannelIdSymbolMap.putAll(channelIdSymbolMap);
        channelIdSymbolMap.clear();
        channelIdSymbolMap.notifyAll();
    }
    // Resubscribe channels
    for (BitfinexStreamSymbol symbol : oldChannelIdSymbolMap.values()) {
        if (symbol instanceof BitfinexTickerSymbol) {
            sendCommand(new SubscribeTickerCommand((BitfinexTickerSymbol) symbol));
        } else if (symbol instanceof BitfinexExecutedTradeSymbol) {
            sendCommand(new SubscribeTradesCommand((BitfinexExecutedTradeSymbol) symbol));
        } else if (symbol instanceof BitfinexCandlestickSymbol) {
            sendCommand(new SubscribeCandlesCommand((BitfinexCandlestickSymbol) symbol));
        } else if (symbol instanceof OrderbookConfiguration) {
            sendCommand(new SubscribeOrderbookCommand((OrderbookConfiguration) symbol));
        } else if (symbol instanceof RawOrderbookConfiguration) {
            sendCommand(new SubscribeRawOrderbookCommand((RawOrderbookConfiguration) symbol));
        } else {
            logger.error("Unknown stream symbol: {}", symbol);
        }
    }
    waitForChannelResubscription(oldChannelIdSymbolMap);
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) HashMap(java.util.HashMap) SubscribeOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) SubscribeTradesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTradesCommand) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) SubscribeCandlesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand) SubscribeTickerCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BitfinexStreamSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexStreamSymbol) SubscribeRawOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)

Example 27 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class BitfinexApiBroker method executeAuthentification.

/**
 * Execute the authentication and wait until the socket is ready
 * @throws InterruptedException
 * @throws APIException
 */
private void executeAuthentification() throws InterruptedException, APIException {
    connectionReadyLatch = new CountDownLatch(CONNECTION_READY_EVENTS);
    if (isAuthenticatedConnection()) {
        sendCommand(new AuthCommand());
        logger.info("Waiting for connection ready events");
        connectionReadyLatch.await(10, TimeUnit.SECONDS);
        if (!authenticated) {
            throw new APIException("Unable to perform authentification, capabilities are: " + capabilities);
        }
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) CountDownLatch(java.util.concurrent.CountDownLatch) AuthCommand(com.github.jnidzwetzki.bitfinex.v2.commands.AuthCommand)

Example 28 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class BitfinexApiBroker method handleChannelDataArray.

/**
 * Handle the channel data with has an array at first position
 * @param jsonArray
 * @param channelSymbol
 * @throws APIException
 */
private void handleChannelDataArray(final JSONArray jsonArray, final BitfinexStreamSymbol channelSymbol) throws APIException {
    final JSONArray subarray = jsonArray.getJSONArray(1);
    if (channelSymbol instanceof BitfinexCandlestickSymbol) {
        final ChannelCallbackHandler handler = new CandlestickHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof RawOrderbookConfiguration) {
        final RawOrderbookHandler handler = new RawOrderbookHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof OrderbookConfiguration) {
        final OrderbookHandler handler = new OrderbookHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof BitfinexTickerSymbol) {
        final ChannelCallbackHandler handler = new TickHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof BitfinexExecutedTradeSymbol) {
        final ChannelCallbackHandler handler = new ExecutedTradeHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else {
        logger.error("Unknown stream type: {}", channelSymbol);
    }
}
Also used : CandlestickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler) RawOrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) ChannelCallbackHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ChannelCallbackHandler) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) JSONArray(org.json.JSONArray) RawOrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler) OrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.OrderbookHandler) ExecutedTradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) TickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)

Example 29 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class OrderHandler method handleOrderCallback.

/**
 * Handle a single order callback
 * @param bitfinexApiBroker
 * @param orderArray
 * @throws APIException
 */
private void handleOrderCallback(BitfinexApiBroker bitfinexApiBroker, final JSONArray order) throws APIException {
    final ExchangeOrder exchangeOrder = new ExchangeOrder();
    exchangeOrder.setApikey(bitfinexApiBroker.getApiKey());
    exchangeOrder.setOrderId(order.getLong(0));
    exchangeOrder.setGroupId(order.optInt(1, -1));
    exchangeOrder.setCid(order.optLong(2, -1));
    exchangeOrder.setSymbol(order.getString(3));
    exchangeOrder.setCreated(order.getLong(4));
    exchangeOrder.setUpdated(order.getLong(5));
    exchangeOrder.setAmount(order.getBigDecimal(6));
    exchangeOrder.setAmountAtCreation(order.getBigDecimal(7));
    exchangeOrder.setOrderType(BitfinexOrderType.fromString(order.getString(8)));
    final ExchangeOrderState orderState = ExchangeOrderState.fromString(order.getString(13));
    exchangeOrder.setState(orderState);
    exchangeOrder.setPrice(order.optBigDecimal(16, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceAvg(order.optBigDecimal(17, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceTrailing(order.optBigDecimal(18, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceAuxLimit(order.optBigDecimal(19, BigDecimal.valueOf(-1)));
    exchangeOrder.setNotify(order.getInt(23) == 1 ? true : false);
    exchangeOrder.setHidden(order.getInt(24) == 1 ? true : false);
    bitfinexApiBroker.getOrderManager().updateOrder(exchangeOrder);
}
Also used : ExchangeOrderState(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrderState) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 30 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class TradeHandler method handleTradeCallback.

/**
 * Handle a single trade callback
 * @param bitfinexApiBroker
 * @param orderArray
 * @throws APIException
 */
private void handleTradeCallback(final BitfinexApiBroker bitfinexApiBroker, final JSONArray jsonTrade, final boolean executed) throws APIException {
    final Trade trade = new Trade();
    trade.setExecuted(executed);
    trade.setApikey(bitfinexApiBroker.getApiKey());
    trade.setId(jsonTrade.getLong(0));
    trade.setCurrency(BitfinexCurrencyPair.fromSymbolString(jsonTrade.getString(1)));
    trade.setMtsCreate(jsonTrade.getLong(2));
    trade.setOrderId(jsonTrade.getLong(3));
    trade.setExecAmount(jsonTrade.getBigDecimal(4));
    trade.setExecPrice(jsonTrade.getBigDecimal(5));
    final String orderTypeString = jsonTrade.optString(6, null);
    if (orderTypeString != null) {
        trade.setOrderType(BitfinexOrderType.fromString(orderTypeString));
    }
    trade.setOrderPrice(jsonTrade.optBigDecimal(7, BigDecimal.valueOf(-1)));
    trade.setMaker(jsonTrade.getInt(8) == 1 ? true : false);
    trade.setFee(jsonTrade.optBigDecimal(9, BigDecimal.valueOf(-1)));
    trade.setFeeCurrency(jsonTrade.optString(10, ""));
    bitfinexApiBroker.getTradeManager().updateTrade(trade);
}
Also used : Trade(com.github.jnidzwetzki.bitfinex.v2.entity.Trade)

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