use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project crypto-bot by jnidzwetzki.
the class EMABot method updateScreen.
public synchronized void updateScreen() {
if (!UPDATE_SCREEN) {
return;
}
final QuoteManager tickerManager = bitfinexApiBroker.getQuoteManager();
CliTools.clearScreen();
System.out.println("");
System.out.println("==========");
System.out.println("Last ticks");
System.out.println("==========");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
final String symbol = currency.toBitfinexString();
final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
System.out.println(symbol + " " + tickerManager.getLastTick(tickerSymbol));
}
System.out.println("");
System.out.println("==========");
System.out.println("Last bars");
System.out.println("==========");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
System.out.println(currency + " " + timeSeries.get(currency).getLastBar());
}
System.out.println("");
System.out.println("==========");
System.out.println("P/L");
System.out.println("==========");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
final String symbol = currency.toBitfinexString();
final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
final Trade trade = getOpenTrade(currency);
if (trade != null) {
final double priceIn = trade.getExpectedPriceOpen();
final double currentPrice = tickerManager.getLastTick(tickerSymbol).getClose();
System.out.println(symbol + ": price in " + priceIn + " / " + (currentPrice - priceIn));
}
}
System.out.println("");
System.out.println("==========");
System.out.println("Trades");
System.out.println("==========");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
final String symbol = currency.toBitfinexString();
final List<Trade> lastTrades = trades.get(currency);
if (lastTrades == null) {
continue;
}
lastTrades.sort((t1, t2) -> Long.compare(t2.getTid(), t1.getTid()));
final List<Trade> lastTwoTrades = lastTrades.subList(Math.max(lastTrades.size() - 2, 0), lastTrades.size());
for (final Trade trade : lastTwoTrades) {
System.out.println(symbol + " " + trade);
}
}
try {
System.out.println("");
System.out.println("==========");
System.out.println("Orders");
System.out.println("==========");
final List<ExchangeOrder> orders = new ArrayList<>(bitfinexApiBroker.getOrderManager().getOrders());
orders.sort((o1, o2) -> Long.compare(o2.getCid(), o1.getCid()));
final List<ExchangeOrder> lastOrders = orders.subList(Math.max(orders.size() - 3, 0), orders.size());
for (final ExchangeOrder order : lastOrders) {
System.out.println(order);
}
} catch (APIException e) {
logger.error("Got eception while reading wallets", e);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project crypto-bot by jnidzwetzki.
the class EMABot method registerTicker.
protected void registerTicker(final BitfinexApiBroker bitfinexApiBroker) throws InterruptedException, APIException {
logger.info("Register ticker");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
tickMerger.put(currency, new BarMerger(currency, TIMEFRAME, (s, t) -> barDoneCallback(s, t)));
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
bitfinexApiBroker.getQuoteManager().subscribeTicker(symbol);
logger.info("Wait for ticker");
while (!bitfinexApiBroker.isTickerActive(symbol)) {
Thread.sleep(100);
}
bitfinexApiBroker.getQuoteManager().registerTickCallback(symbol, (s, c) -> handleTickCallback(s, c));
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class IntegrationTest method testTickerStream.
/**
* Test the tick stream
*/
@Test(timeout = 60000)
public void testTickerStream() {
final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
// Await at least 2 callbacks
final CountDownLatch latch = new CountDownLatch(2);
try {
bitfinexClient.connect();
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(BitfinexCurrencyPair.BTC_USD);
final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
final BiConsumer<BitfinexTickerSymbol, BitfinexTick> callback = (c, o) -> {
latch.countDown();
};
orderbookManager.registerTickCallback(symbol, callback);
orderbookManager.subscribeTicker(symbol);
latch.await();
Assert.assertTrue(bitfinexClient.isTickerActive(symbol));
orderbookManager.unsubscribeTicker(symbol);
Assert.assertFalse(bitfinexClient.isTickerActive(symbol));
Assert.assertTrue(orderbookManager.removeTickCallback(symbol, callback));
Assert.assertFalse(orderbookManager.removeTickCallback(symbol, callback));
} catch (Exception e) {
// Should not happen
e.printStackTrace();
Assert.assertTrue(false);
} finally {
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 unsubscribeTicker.
/**
* Unsubscribe a ticker
* @param tickerSymbol
*/
public void unsubscribeTicker(final BitfinexTickerSymbol tickerSymbol) {
final int channel = bitfinexApiBroker.getChannelForSymbol(tickerSymbol);
if (channel == -1) {
throw new IllegalArgumentException("Unknown symbol: " + tickerSymbol);
}
final UnsubscribeChannelCommand command = new UnsubscribeChannelCommand(channel);
bitfinexApiBroker.sendCommand(command);
bitfinexApiBroker.removeChannelForSymbol(tickerSymbol);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class TickHandlerTest method testTickUpdateAndNotify.
/**
* Test the parsing of one tick
* @throws APIException
* @throws InterruptedException
*/
@Test(timeout = 20000)
public void testTickUpdateAndNotify() throws APIException, InterruptedException {
final String callbackValue = "[26123,41.4645776,26129,33.68138507,2931,0.2231,26129,144327.10936387,26149,13139]";
final JSONArray jsonArray = new JSONArray(callbackValue);
final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.BTC_USD;
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currencyPair);
final ExecutorService executorService = Executors.newFixedThreadPool(10);
final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
final QuoteManager tickerManager = new QuoteManager(bitfinexApiBroker);
Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(tickerManager);
final CountDownLatch latch = new CountDownLatch(1);
tickerManager.registerTickCallback(symbol, (s, c) -> {
try {
Assert.assertEquals(symbol, s);
Assert.assertEquals(26129, c.getOpen().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getClose().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getHigh().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getLow().doubleValue(), DELTA);
Assert.assertEquals(BitfinexTick.INVALID_VOLUME.doubleValue(), c.getVolume().doubleValue(), DELTA);
} catch (Throwable e) {
System.out.println(e);
throw e;
}
latch.countDown();
});
Assert.assertEquals(-1, tickerManager.getHeartbeatForSymbol(symbol));
Assert.assertEquals(null, tickerManager.getLastTick(symbol));
final TickHandler tickHandler = new TickHandler();
final long now = System.currentTimeMillis();
tickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
// Tick callbacks are handled async
latch.await();
Assert.assertTrue(now <= tickerManager.getHeartbeatForSymbol(symbol));
Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
}
Aggregations