use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class BasePortfolioManager method getAvailablePortfolioValueInUSD.
@Override
protected double getAvailablePortfolioValueInUSD() throws APIException {
final List<Wallet> wallets = getAllWallets();
for (final Wallet wallet : wallets) {
if (wallet.getCurreny().equals("USD")) {
return wallet.getBalance();
}
}
logger.error("Unable to find USD wallet");
return 0;
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class MarginPortfolioManager method getTotalPortfolioValueInUSD.
@Override
protected double getTotalPortfolioValueInUSD() throws APIException {
final List<Wallet> wallets = getAllWallets();
for (final Wallet wallet : wallets) {
if (wallet.getCurreny().equals("USD")) {
return wallet.getBalance();
}
}
logger.error("Unable to find USD wallet");
return 0;
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException 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.APIException 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.APIException 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);
}
}
}
}
Aggregations