Search in sources :

Example 61 with BitfinexApiBroker

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

the class ExecutedTradeHandler method handleEntry.

/**
 * Handle a new executed trade entry
 * @param bitfinexApiBroker
 * @param symbol
 * @param jsonArray
 */
private void handleEntry(final BitfinexApiBroker bitfinexApiBroker, final BitfinexExecutedTradeSymbol symbol, final JSONArray jsonArray) {
    final ExecutedTrade executedTrade = new ExecutedTrade();
    final long id = jsonArray.getNumber(0).longValue();
    executedTrade.setId(id);
    final long timestamp = jsonArray.getNumber(1).longValue();
    executedTrade.setTimestamp(timestamp);
    final BigDecimal amount = jsonArray.getBigDecimal(2);
    executedTrade.setAmount(amount);
    // Funding or Currency
    if (jsonArray.optNumber(4) != null) {
        final BigDecimal rate = jsonArray.getBigDecimal(3);
        executedTrade.setRate(rate);
        final int period = jsonArray.getNumber(4).intValue();
        executedTrade.setPeriod(period);
    } else {
        final BigDecimal price = jsonArray.getBigDecimal(3);
        executedTrade.setPrice(price);
    }
    bitfinexApiBroker.getQuoteManager().handleExecutedTradeEntry(symbol, executedTrade);
}
Also used : ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) BigDecimal(java.math.BigDecimal)

Example 62 with BitfinexApiBroker

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

the class ExecutedTradeHandler method handleChannelData.

@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final BitfinexStreamSymbol channelSymbol, final JSONArray jsonArray) throws APIException {
    final BitfinexExecutedTradeSymbol configuration = (BitfinexExecutedTradeSymbol) channelSymbol;
    try {
        // Snapshots contain multiple executes entries, updates only one
        if (jsonArray.get(0) instanceof JSONArray) {
            for (int pos = 0; pos < jsonArray.length(); pos++) {
                final JSONArray parts = jsonArray.getJSONArray(pos);
                handleEntry(bitfinexApiBroker, configuration, parts);
            }
        } else {
            handleEntry(bitfinexApiBroker, configuration, jsonArray);
        }
    } catch (JSONException e) {
        throw new APIException(e);
    }
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 63 with BitfinexApiBroker

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

the class OrderbookHandler method handleChannelData.

@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final BitfinexStreamSymbol channelSymbol, final JSONArray jsonArray) throws APIException {
    final OrderbookConfiguration configuration = (OrderbookConfiguration) channelSymbol;
    // Example: [13182,1,-0.1]
    try {
        // Snapshots contain multiple Orderbook entries, updates only one
        if (jsonArray.get(0) instanceof JSONArray) {
            for (int pos = 0; pos < jsonArray.length(); pos++) {
                final JSONArray parts = jsonArray.getJSONArray(pos);
                handleEntry(bitfinexApiBroker, configuration, parts);
            }
        } else {
            handleEntry(bitfinexApiBroker, configuration, jsonArray);
        }
    } catch (JSONException e) {
        throw new APIException(e);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 64 with BitfinexApiBroker

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

the class RawOrderbookHandler method handleEntry.

/**
 * Handle a new orderbook entry
 * @param bitfinexApiBroker
 * @param configuration
 * @param jsonArray
 */
private void handleEntry(final BitfinexApiBroker bitfinexApiBroker, final RawOrderbookConfiguration configuration, final JSONArray jsonArray) {
    final long orderId = jsonArray.getNumber(0).longValue();
    final BigDecimal price = jsonArray.getBigDecimal(1);
    final BigDecimal amount = jsonArray.getBigDecimal(2);
    final RawOrderbookEntry orderbookEntry = new RawOrderbookEntry(orderId, price, amount);
    bitfinexApiBroker.getRawOrderbookManager().handleNewOrderbookEntry(configuration, orderbookEntry);
}
Also used : RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) BigDecimal(java.math.BigDecimal)

Example 65 with BitfinexApiBroker

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

the class AuthCallbackHandler method authSuccessfully.

/**
 * Auth was successfully
 *
 * @param bitfinexApiBroker
 * @param jsonObject
 * @param connectionReadyLatch
 */
private void authSuccessfully(final BitfinexApiBroker bitfinexApiBroker, final JSONObject jsonObject, final CountDownLatch connectionReadyLatch) {
    bitfinexApiBroker.setAuthenticated(true);
    final ConnectionCapabilities capabilities = new ConnectionCapabilities(jsonObject);
    bitfinexApiBroker.setCapabilities(capabilities);
    if (connectionReadyLatch != null) {
        connectionReadyLatch.countDown();
    }
}
Also used : ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)46 Test (org.junit.Test)36 JSONArray (org.json.JSONArray)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)15 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)15 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)13 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)12 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)12 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)11 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)11 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)9 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)9 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)9 ExecutorService (java.util.concurrent.ExecutorService)9 ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)7 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)7 Assert (org.junit.Assert)7 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)6