use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol 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;
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class CommandsTest method testCommandsJSON.
/**
* Call all commands and check for excepion
* @throws CommandException
*/
@Test
public void testCommandsJSON() throws CommandException {
final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.EXCHANGE_STOP, 2).build();
final BitfinexCandlestickSymbol candleSymbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BCH_USD, Timeframe.HOUR_1);
final OrderbookConfiguration orderbookConfiguration = new OrderbookConfiguration(BitfinexCurrencyPair.BCH_USD, OrderBookPrecision.P0, OrderBookFrequency.F0, 50);
final RawOrderbookConfiguration rawOrderbookConfiguration = new RawOrderbookConfiguration(BitfinexCurrencyPair.BAT_BTC);
final List<AbstractAPICommand> commands = Arrays.asList(new AuthCommand(), new CancelOrderCommand(123), new CancelOrderGroupCommand(1), new OrderCommand(order), new PingCommand(), new SubscribeCandlesCommand(candleSymbol), new SubscribeTickerCommand(new BitfinexTickerSymbol(BitfinexCurrencyPair.BCH_USD)), new SubscribeTradesCommand(new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BAT_BTC)), new SubscribeOrderbookCommand(orderbookConfiguration), new SubscribeRawOrderbookCommand(rawOrderbookConfiguration), new UnsubscribeChannelCommand(12));
final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
for (final AbstractAPICommand command : commands) {
final String commandValue = command.getCommand(bitfinexApiBroker);
Assert.assertNotNull(commandValue);
Assert.assertTrue(commandValue.length() > 10);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class IntegrationTest method testReconnect.
/**
* Test the session reconnect
* @throws APIException
* @throws InterruptedException
*/
@Test
public void testReconnect() throws APIException, InterruptedException {
final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
bitfinexClient.connect();
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(BitfinexCurrencyPair.BTC_USD);
final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
orderbookManager.subscribeTicker(symbol);
Thread.sleep(1000);
bitfinexClient.reconnect();
// Await at least 2 callbacks
final CountDownLatch latch = new CountDownLatch(2);
final BiConsumer<BitfinexTickerSymbol, BitfinexTick> callback = (c, o) -> {
latch.countDown();
};
orderbookManager.registerTickCallback(symbol, callback);
latch.await();
Assert.assertTrue(bitfinexClient.isTickerActive(symbol));
orderbookManager.unsubscribeTicker(symbol);
Assert.assertFalse(bitfinexClient.isTickerActive(symbol));
bitfinexClient.close();
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class QuoteManager method subscribeTicker.
/**
* Subscribe a ticker
* @param tickerSymbol
*/
public void subscribeTicker(final BitfinexTickerSymbol tickerSymbol) {
final SubscribeTickerCommand command = new SubscribeTickerCommand(tickerSymbol);
bitfinexApiBroker.sendCommand(command);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class CommandsCallbackTest method testSubscribeAndUnsubscribeCallback.
/**
* Test the subscribed callback
* @throws APIException
*/
@Test
public void testSubscribeAndUnsubscribeCallback() throws APIException {
final String jsonString = "{\"event\":\"subscribed\",\"channel\":\"ticker\",\"chanId\":30,\"symbol\":\"tNEOUSD\",\"pair\":\"NEOUSD\"}";
final JSONObject jsonObject = new JSONObject(jsonString);
final BitfinexApiBroker bitfinexApiBroker = new BitfinexApiBroker();
Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) == null);
final SubscribedCallback subscribedCallback = new SubscribedCallback();
subscribedCallback.handleChannelData(bitfinexApiBroker, jsonObject);
Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) instanceof BitfinexTickerSymbol);
final String unsubscribedJsonString = "{\"event\":\"unsubscribed\",\"status\":\"OK\",\"chanId\":30}";
final JSONObject jsonUnsubscribedObject = new JSONObject(unsubscribedJsonString);
final UnsubscribedCallback unsubscribedCallback = new UnsubscribedCallback();
unsubscribedCallback.handleChannelData(bitfinexApiBroker, jsonUnsubscribedObject);
Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) == null);
}
Aggregations