use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project crypto-bot by jnidzwetzki.
the class BitfinexClientFactory method buildBifinexClient.
/**
* Build a new bitfinex client
* @return
*/
public static List<BitfinexApiBroker> buildBifinexClient() {
final Properties prop = new Properties();
final List<BitfinexApiBroker> resultList = new ArrayList<>();
final ClassLoader classLoader = BitfinexClientFactory.class.getClassLoader();
try (final InputStream input = classLoader.getResourceAsStream("auth.properties")) {
if (input != null) {
prop.load(input);
final String apiConnectionsString = prop.getProperty("apiConnections");
final int apiConnections = MathUtil.tryParseInt(apiConnectionsString, () -> "Invalid integer value: " + apiConnectionsString);
for (int i = 0; i < apiConnections; i++) {
final String apiKey = prop.getProperty("apiKey." + Integer.toString(i));
final String apiSecret = prop.getProperty("apiSecret." + Integer.toString(i));
if (apiKey == null || apiSecret == null) {
logger.warn("API key or secret are null ({})", i);
} else {
logger.info("Building authenticated client");
final BitfinexApiBroker apiBroker = new BitfinexApiBroker(apiKey, apiSecret);
resultList.add(apiBroker);
}
}
return resultList;
}
} catch (Exception e) {
logger.error("Unable to load properties", e);
}
logger.error("Properties not found");
return resultList;
}
use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project crypto-bot by jnidzwetzki.
the class TestPersistence method testGetOpenTrades.
/**
* Test get open trades from order manager
*/
@Test
public void testGetOpenTrades() {
final BitfinexApiBroker apiBroker = Mockito.mock(BitfinexApiBroker.class);
final PortfolioOrderManager ordermanager = new PortfolioOrderManager(apiBroker);
Assert.assertTrue(ordermanager.getAllOpenTrades().isEmpty());
final Trade trade = new Trade("ABC", TradeDirection.LONG, BitfinexCurrencyPair.BTC_USD, 1);
trade.setTradeState(TradeState.CREATED);
final Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(trade);
session.getTransaction().commit();
Assert.assertTrue(ordermanager.getAllOpenTrades().isEmpty());
session.beginTransaction();
trade.setTradeState(TradeState.OPEN);
session.saveOrUpdate(trade);
session.getTransaction().commit();
session.beginTransaction();
Assert.assertEquals(1, ordermanager.getAllOpenTrades().size());
session.getTransaction().commit();
final Trade trade2 = new Trade("ABC", TradeDirection.LONG, BitfinexCurrencyPair.BTC_USD, 1);
trade2.setTradeState(TradeState.OPEN);
session.beginTransaction();
session.save(trade2);
session.getTransaction().commit();
session.beginTransaction();
Assert.assertEquals(2, ordermanager.getAllOpenTrades().size());
session.getTransaction().commit();
session.close();
}
use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project crypto-bot by jnidzwetzki.
the class DonchianBot method run.
@Override
public void run() {
try {
logger.info("===============================================");
logger.info("Starting with {} API keys", apiBrokerList.size());
logger.info("===============================================");
for (final BitfinexApiBroker bitfinexApiBroker : apiBrokerList) {
bitfinexApiBroker.connect();
}
final Map<BitfinexCandlestickSymbol, TimeSeries> historicalCandles = HistoricalCandlesHelper.requestHistoricalCandles(apiBrokerList.get(0), TIMEFRAME, tradedCurrencies);
historicalCandles.forEach((k, v) -> timeSeries.put(k.getSymbol(), v));
registerTicker();
while (true) {
if (tickerLatch != null) {
tickerLatch.await();
}
executeSystem();
tickerLatch = new CountDownLatch(tradedCurrencies.size());
}
} catch (Throwable e) {
logger.error("Got exception", e);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker 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.BitfinexApiBroker in project crypto-bot by jnidzwetzki.
the class HistoricalCandlesHelper method requestHistoricalCandles.
/**
* Request historical candles
*
* @param bitfinexApiBroker
* @param timeframe
* @param tradedCurrencies
* @return
* @throws InterruptedException
* @throws APIException
*/
public static Map<BitfinexCandlestickSymbol, TimeSeries> requestHistoricalCandles(final BitfinexApiBroker bitfinexApiBroker, final Timeframe timeframe, final List<BitfinexCurrencyPair> tradedCurrencies) throws InterruptedException, APIException {
logger.info("Request historical candles");
final Map<BitfinexCandlestickSymbol, TimeSeries> timeSeries = new HashMap<>();
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
final String bitfinexString = currency.toBitfinexString();
final BaseTimeSeries currencyTimeSeries = new BaseTimeSeries(bitfinexString);
final BitfinexCandlestickSymbol barSymbol = new BitfinexCandlestickSymbol(currency, timeframe);
timeSeries.put(barSymbol, currencyTimeSeries);
final CountDownLatch tickCountdown = new CountDownLatch(100);
// Add bars to timeseries callback
final BiConsumer<BitfinexCandlestickSymbol, BitfinexTick> callback = (channelSymbol, tick) -> {
final TimeSeries timeSeriesToAdd = timeSeries.get(channelSymbol);
final Bar bar = BarConverter.convertBitfinexTick(tick);
try {
timeSeriesToAdd.addBar(bar);
tickCountdown.countDown();
} catch (IllegalArgumentException e) {
logger.error("Unable to add tick {} to time series, last tick is {}", bar, timeSeriesToAdd.getLastBar());
}
};
bitfinexApiBroker.getQuoteManager().registerCandlestickCallback(barSymbol, callback);
bitfinexApiBroker.getQuoteManager().subscribeCandles(barSymbol);
// Wait for 100 bars or 10 seconds. All snapshot ticks are handled in
// a synchronized block, so we receive the full snapshot even if we
// call removeTickCallback.
tickCountdown.await(10, TimeUnit.SECONDS);
bitfinexApiBroker.getQuoteManager().registerCandlestickCallback(barSymbol, callback);
bitfinexApiBroker.getQuoteManager().unsubscribeCandles(barSymbol);
logger.info("Loaded ticks for symbol {} {}", bitfinexString, +timeSeries.get(barSymbol).getEndIndex());
}
return timeSeries;
}
Aggregations