use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class PortfolioManager method cancelRemovedEntryOrders.
/**
* Cancel the removed entry orders
* Position is at the moment not interesting for an entry
*
* @param entries
* @throws APIException
* @throws InterruptedException
*/
private void cancelRemovedEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
final List<ExchangeOrder> entryOrders = getAllOpenEntryOrders();
for (final ExchangeOrder order : entryOrders) {
final String symbol = order.getSymbol();
final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.fromSymbolString(symbol);
if (!entries.containsKey(currencyPair)) {
logger.info("Entry order for {} is not contained, canceling", currencyPair);
cancelOrder(order);
}
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class PortfolioManager method placeNewExitOrders.
/**
* Place the exit orders
* @param exits
* @throws APIException
* @throws InterruptedException
*/
private void placeNewExitOrders(final Map<BitfinexCurrencyPair, Double> exits) throws APIException, InterruptedException {
for (final BitfinexCurrencyPair currency : exits.keySet()) {
final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
final double exitPrice = exits.get(currency);
// Check old orders
if (order != null) {
final double orderPrice = order.getPrice();
if (orderPrice >= exitPrice || MathHelper.almostEquals(orderPrice, exitPrice)) {
logger.info("Old order price for {} is fine (price: order {} model {})", currency, orderPrice, exitPrice);
continue;
}
logger.info("Exit price for {} has moved form {} to {}, canceling old order", currency, orderPrice, exitPrice);
cancelOrder(order);
}
final double positionSize = getOpenPositionSizeForCurrency(currency.getCurrency1());
// * -1.0 for sell order
final double positionSizeSell = positionSize * -1.0;
final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSizeSell).withPrice(exitPrice).setPostOnly().build();
placeOrder(newOrder);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class PortfolioManager method cleanupOldExitOrders.
/**
* Cleanup the old exit orders (remove duplicates, unknown orders)
* @param exits
* @throws APIException
* @throws InterruptedException
*/
private void cleanupOldExitOrders(final Map<BitfinexCurrencyPair, Double> exits) throws APIException, InterruptedException {
final List<ExchangeOrder> oldExitOrders = getAllOpenExitOrders();
// Remove unknown orders
for (final ExchangeOrder order : oldExitOrders) {
final BitfinexCurrencyPair symbol = BitfinexCurrencyPair.fromSymbolString(order.getSymbol());
if (!exits.containsKey(symbol)) {
logger.error("Found old and unknown order {}, canceling", order);
cancelOrder(order);
}
}
// Remove duplicates
final Map<String, List<ExchangeOrder>> oldOrders = oldExitOrders.stream().collect(Collectors.groupingBy(ExchangeOrder::getSymbol));
for (final String symbol : oldOrders.keySet()) {
final List<ExchangeOrder> symbolOrderList = oldOrders.get(symbol);
if (symbolOrderList.size() > 1) {
logger.error("Found duplicates {}", symbolOrderList);
for (final ExchangeOrder order : symbolOrderList) {
cancelOrder(order);
}
}
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class BarMegerTest method testBarAlignment1.
/**
* Test the alignment of the Bars
* @throws InterruptedException
* @throws IOException
* @throws ParseException
*/
@Test(timeout = 10000)
public void testBarAlignment1() throws InterruptedException, IOException, ParseException {
final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
final CountDownLatch latch = new CountDownLatch(3);
final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
Assert.assertTrue(t.getEndTime().getSecond() == 59);
latch.countDown();
};
final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, BarConsumer);
BarMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
BarMerger.addNewPrice(parser.parse("01:02:33").getTime(), 2.0, 5.0);
BarMerger.addNewPrice(parser.parse("02:03:53").getTime(), 2.0, 5.0);
BarMerger.addNewPrice(parser.parse("22:22:53").getTime(), 2.0, 5.0);
BarMerger.close();
latch.await();
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class BarMegerTest method testBarAlignment2.
/**
* Test the alignment of the Bars
* @throws InterruptedException
* @throws IOException
* @throws ParseException
*/
@Test(timeout = 10000)
public void testBarAlignment2() throws InterruptedException, IOException, ParseException {
final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
final CountDownLatch latch = new CountDownLatch(4);
final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
Assert.assertTrue(t.getEndTime().getMinute() == 14 || t.getEndTime().getMinute() == 29 || t.getEndTime().getMinute() == 44 || t.getEndTime().getMinute() == 59);
Assert.assertEquals(59, t.getEndTime().getSecond());
latch.countDown();
};
final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_15, BarConsumer);
BarMerger.addNewPrice(parser.parse("01:01:00").getTime(), 1.0, 5.0);
BarMerger.addNewPrice(parser.parse("02:41:33").getTime(), 2.0, 5.0);
BarMerger.addNewPrice(parser.parse("10:33:11").getTime(), 2.0, 5.0);
BarMerger.addNewPrice(parser.parse("22:22:53").getTime(), 2.0, 5.0);
BarMerger.close();
latch.await();
}
Aggregations