use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class PortfolioOrderManager method closeTrade.
/**
* Close a trade
* @param trade
*/
public void closeTrade(final Trade trade) {
if (!bitfinexApiBroker.isAuthenticated()) {
logger.error("Unable to execute trade {} on marketplace, conecction is not authenticated", trade);
return;
}
final double amount = trade.getAmount() * -1.0;
final BitfinexOrder order = BitfinexOrderBuilder.create(trade.getSymbol(), BitfinexOrderType.EXCHANGE_MARKET, amount).build();
try {
trade.setTradeState(TradeState.CLOSING);
trade.addCloseOrder(order);
bitfinexApiBroker.getOrderManager().placeOrder(order);
trade.setTradeState(TradeState.CLOSED);
} catch (APIException e) {
logger.error("Got an exception while closing trade {}", trade);
trade.setTradeState(TradeState.ERROR);
} finally {
persistTrade(trade);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class PortfolioOrderManager method openTrade.
/**
* Open a new trade
* @param trade
*/
public void openTrade(final Trade trade) {
if (!bitfinexApiBroker.isAuthenticated()) {
logger.error("Unable to execute trade {} on marketplace, conecction is not authenticated", trade);
return;
}
final double amount = trade.getAmount();
final BitfinexOrder order = BitfinexOrderBuilder.create(trade.getSymbol(), BitfinexOrderType.EXCHANGE_MARKET, amount).build();
try {
trade.setTradeState(TradeState.OPENING);
trade.addOpenOrder(order);
bitfinexApiBroker.getOrderManager().placeOrder(order);
trade.setTradeState(TradeState.OPEN);
} catch (APIException e) {
logger.error("Got an exception while opening trade {}", trade);
trade.setTradeState(TradeState.ERROR);
} finally {
persistTrade(trade);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException 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.APIException in project crypto-bot by jnidzwetzki.
the class MarginPortfolioManager method getOpenPositionSizeForCurrency.
/**
* Get the position size for the symbol
* @param symbol
* @return
* @throws APIException
*/
@Override
protected double getOpenPositionSizeForCurrency(final String currency) throws APIException {
final List<Position> positions = bitfinexApiBroker.getPositionManager().getPositions();
final Position position = positions.stream().filter(p -> p.getCurreny().getCurrency1().equals(currency)).findAny().orElse(null);
if (position == null) {
return 0;
}
return position.getAmount();
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class MarginPortfolioManager 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.getBalanceAvailable();
}
}
logger.error("Unable to find USD wallet");
return 0;
}
Aggregations