Search in sources :

Example 56 with BitfinexApiBroker

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

the class CommandsCallbackTest method testAuthCommandCallback2.

/**
 * Test the auth callback
 * @throws APIException
 */
@Test
public void testAuthCommandCallback2() throws APIException {
    final String authCallback = "{\"event\":\"auth\",\"status\":\"FAILED\",\"chanId\":0,}";
    final BitfinexApiBroker bitfinexApiBroker = Mockito.spy(BitfinexApiBroker.class);
    final JSONObject jsonObject = new JSONObject(authCallback);
    final AuthCallbackHandler authCallbackHandler = new AuthCallbackHandler();
    Assert.assertFalse(bitfinexApiBroker.isAuthenticated());
    authCallbackHandler.handleChannelData(bitfinexApiBroker, jsonObject);
    Assert.assertFalse(bitfinexApiBroker.isAuthenticated());
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONObject(org.json.JSONObject) AuthCallbackHandler(com.github.jnidzwetzki.bitfinex.v2.callback.command.AuthCallbackHandler) Test(org.junit.Test)

Example 57 with BitfinexApiBroker

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

the class CommandsCallbackTest method testConnectionHeartbeat.

/**
 * Test the pong callback
 * @throws APIException
 */
@Test
public void testConnectionHeartbeat() throws APIException {
    final String jsonString = "{\"event\":\"pong\",\"ts\":1515023251265}";
    final JSONObject jsonObject = new JSONObject(jsonString);
    final BitfinexApiBroker bitfinexApiBroker = new BitfinexApiBroker();
    final long oldHeartbeat = bitfinexApiBroker.getLastHeatbeat().get();
    final ConnectionHeartbeatCallback connectionHeartbeatCallback = new ConnectionHeartbeatCallback();
    connectionHeartbeatCallback.handleChannelData(bitfinexApiBroker, jsonObject);
    final long newHeartbeat = bitfinexApiBroker.getLastHeatbeat().get();
    Assert.assertTrue(oldHeartbeat < newHeartbeat);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONObject(org.json.JSONObject) ConnectionHeartbeatCallback(com.github.jnidzwetzki.bitfinex.v2.callback.command.ConnectionHeartbeatCallback) Test(org.junit.Test)

Example 58 with BitfinexApiBroker

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

the class PositionHandler method handlePositionCallback.

/**
 * Handle a position update
 * @param bitfinexApiBroker
 * @param positions
 */
private void handlePositionCallback(final BitfinexApiBroker bitfinexApiBroker, final JSONArray positions) {
    final String currencyString = positions.getString(0);
    BitfinexCurrencyPair currency = BitfinexCurrencyPair.fromSymbolString(currencyString);
    final Position position = new Position(currency);
    position.setStatus(positions.getString(1));
    position.setAmount(positions.getBigDecimal(2));
    position.setBasePrice(positions.getBigDecimal(3));
    position.setMarginFunding(positions.getBigDecimal(4));
    position.setMarginFundingType(positions.getBigDecimal(5));
    position.setPl(positions.optBigDecimal(6, BigDecimal.valueOf(-1)));
    position.setPlPercent(positions.optBigDecimal(7, BigDecimal.valueOf(-1)));
    position.setPriceLiquidation(positions.optBigDecimal(8, BigDecimal.valueOf(-1)));
    position.setLeverage(positions.optBigDecimal(9, BigDecimal.valueOf(-1)));
    bitfinexApiBroker.getPositionManager().updatePosition(position);
}
Also used : Position(com.github.jnidzwetzki.bitfinex.v2.entity.Position) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)

Example 59 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker 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 60 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker 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)

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