Search in sources :

Example 66 with APIException

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

the class WalletHandler method handleWalletcallback.

/**
 * Handle the callback for a single wallet
 * @param bitfinexApiBroker
 * @param walletArray
 * @throws APIException
 */
private void handleWalletcallback(final BitfinexApiBroker bitfinexApiBroker, final JSONArray walletArray) throws APIException {
    final String walletType = walletArray.getString(0);
    final String currency = walletArray.getString(1);
    final BigDecimal balance = walletArray.getBigDecimal(2);
    final BigDecimal unsettledInterest = walletArray.getBigDecimal(3);
    final BigDecimal balanceAvailable = walletArray.optBigDecimal(4, BigDecimal.valueOf(-1));
    final Wallet wallet = new Wallet(walletType, currency, balance, unsettledInterest, balanceAvailable);
    final WalletManager walletManager = bitfinexApiBroker.getWalletManager();
    final Table<String, String, Wallet> walletTable = walletManager.getWalletTable();
    synchronized (walletTable) {
        walletTable.put(walletType, currency, wallet);
        walletTable.notifyAll();
    }
}
Also used : WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) BigDecimal(java.math.BigDecimal)

Example 67 with APIException

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

the class CandlestickHandler method handleChannelData.

/**
 * Handle a candlestick callback
 * @param channel
 * @param subarray
 */
@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final BitfinexStreamSymbol channelSymbol, final JSONArray jsonArray) throws APIException {
    // channel symbol trade:1m:tLTCUSD
    final List<BitfinexTick> ticksBuffer = new ArrayList<>();
    // Snapshots contain multiple Bars, Updates only one
    if (jsonArray.get(0) instanceof JSONArray) {
        for (int pos = 0; pos < jsonArray.length(); pos++) {
            final JSONArray parts = jsonArray.getJSONArray(pos);
            parseCandlestick(ticksBuffer, parts);
        }
    } else {
        parseCandlestick(ticksBuffer, jsonArray);
    }
    // Use natural ordering
    ticksBuffer.sort(null);
    final BitfinexCandlestickSymbol candlestickSymbol = (BitfinexCandlestickSymbol) channelSymbol;
    bitfinexApiBroker.getQuoteManager().handleCandlestickList(candlestickSymbol, ticksBuffer);
}
Also used : BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray)

Example 68 with APIException

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

the class BitfinexApiBroker method handleCommandCallback.

/**
 * Handle a command callback
 */
private void handleCommandCallback(final String message) {
    logger.debug("Got {}", message);
    // JSON callback
    final JSONTokener tokener = new JSONTokener(message);
    final JSONObject jsonObject = new JSONObject(tokener);
    final String eventType = jsonObject.getString("event");
    if (!commandCallbacks.containsKey(eventType)) {
        logger.error("Unknown event: {}", message);
    } else {
        try {
            final CommandCallbackHandler callback = commandCallbacks.get(eventType);
            callback.handleChannelData(this, jsonObject);
        } catch (APIException e) {
            logger.error("Got an exception while handling callback");
        }
    }
}
Also used : JSONTokener(org.json.JSONTokener) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) JSONObject(org.json.JSONObject) CommandCallbackHandler(com.github.jnidzwetzki.bitfinex.v2.callback.command.CommandCallbackHandler)

Example 69 with APIException

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

the class BitfinexApiBroker method connect.

/**
 * Open the connection
 * @throws APIException
 */
public void connect() throws APIException {
    try {
        final URI bitfinexURI = new URI(BITFINEX_URI);
        websocketEndpoint = new WebsocketClientEndpoint(bitfinexURI);
        websocketEndpoint.addConsumer(apiCallback);
        websocketEndpoint.connect();
        updateConnectionHeartbeat();
        executeAuthentification();
        heartbeatThread = new Thread(new HeartbeatThread(this));
        heartbeatThread.start();
    } catch (Exception e) {
        throw new APIException(e);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) URI(java.net.URI) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) CommandException(com.github.jnidzwetzki.bitfinex.v2.commands.CommandException)

Example 70 with APIException

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

the class BitfinexApiBroker method handleChannelData.

/**
 * Handle normal channel data
 * @param jsonArray
 * @param channel
 * @throws APIException
 */
private void handleChannelData(final JSONArray jsonArray) {
    final int channel = jsonArray.getInt(0);
    final BitfinexStreamSymbol channelSymbol = getFromChannelSymbolMap(channel);
    if (channelSymbol == null) {
        logger.error("Unable to determine symbol for channel {}", channel);
        logger.error("Data is {}", jsonArray);
        return;
    }
    try {
        if (jsonArray.get(1) instanceof String) {
            handleChannelDataString(jsonArray, channelSymbol);
        } else {
            handleChannelDataArray(jsonArray, channelSymbol);
        }
    } catch (APIException e) {
        logger.error("Got exception while handling callback", e);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexStreamSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexStreamSymbol)

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