use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class EMABot method addTradeToOpenTradeList.
/**
* Open the trade to the open trades list
* @param currency
* @param trade
*/
private void addTradeToOpenTradeList(final Trade trade) {
final BitfinexCurrencyPair currency = trade.getSymbol();
if (!trades.containsKey(currency)) {
trades.put(currency, new ArrayList<>());
}
trades.get(currency).add(trade);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair 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.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class PortfolioManager method calculatePositionSizes.
/**
* Calculate the position sizes
* @param entries
* @throws APIException
*/
@VisibleForTesting
public void calculatePositionSizes(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException {
final Wallet wallet = getWalletForCurrency("USD");
// Wallet could be empty
if (wallet == null) {
throw new APIException("Unable to find USD wallet");
}
final double capitalAvailable = getAvailablePortfolioValueInUSD() * getInvestmentRate();
double capitalNeeded = 0;
for (final BitfinexCurrencyPair currency : entries.keySet()) {
final CurrencyEntry entry = entries.get(currency);
final double positionSize = calculatePositionSize(entry);
entry.setPositionSize(positionSize);
capitalNeeded = capitalNeeded + (positionSize * entry.getEntryPrice());
}
// Need the n% risk per position more than the available capital
if (capitalNeeded > capitalAvailable) {
final double investmentCorrectionFactor = capitalAvailable / capitalNeeded;
logger.info("Needed capital {}, available capital {} ({})", capitalNeeded, capitalAvailable, investmentCorrectionFactor);
capitalNeeded = 0;
for (final BitfinexCurrencyPair currency : entries.keySet()) {
final CurrencyEntry entry = entries.get(currency);
final double newPositionSize = roundPositionSize(entry.getPositionSize() * investmentCorrectionFactor);
entry.setPositionSize(newPositionSize);
capitalNeeded = capitalNeeded + (entry.getPositionSize() * entry.getEntryPrice());
}
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class PortfolioManager method placeNewEntryOrders.
/**
* Place the new entry orders
* @param entries
* @throws APIException
* @throws InterruptedException
*/
private void placeNewEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
// Check current limits and position sizes
for (final BitfinexCurrencyPair currency : entries.keySet()) {
final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
final CurrencyEntry entry = entries.get(currency);
final double entryPrice = entry.getEntryPrice();
final double positionSize = entry.getPositionSize();
if (positionSize < currency.getMinimumOrderSize()) {
logger.info("Not placing order for {}, position size is too small {}", currency, positionSize);
continue;
}
// Old order present
if (order != null) {
if (hasEntryOrderChanged(order, entryPrice, positionSize)) {
logger.info("Entry order for {}, values changed (amount: {} / {}} (price: {} / {})", currency, order.getAmount(), positionSize, order.getPrice(), entryPrice);
cancelOrder(order);
} else {
logger.info("Not placing a new order for {}, old order still active", currency);
continue;
}
}
final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSize).withPrice(entryPrice).setPostOnly().build();
placeOrder(newOrder);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class BarMegerTest method testBarMergerMinMax.
/**
* Test Bar merger min max
* @throws InterruptedException
* @throws IOException
* @throws ParseException
*/
@Test(timeout = 6000)
public void testBarMergerMinMax() throws InterruptedException, IOException, ParseException {
final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
final CountDownLatch latch = new CountDownLatch(1);
final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
Assert.assertEquals(1.0, t.getMinPrice().doubleValue(), DELTA);
Assert.assertEquals(8.0, t.getMaxPrice().doubleValue(), DELTA);
Assert.assertEquals(3.0, t.getOpenPrice().doubleValue(), DELTA);
Assert.assertEquals(4.5, t.getClosePrice().doubleValue(), DELTA);
latch.countDown();
};
final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, BarConsumer);
BarMerger.addNewPrice(parser.parse("01:01:01").getTime(), 3.0, 5.0);
BarMerger.addNewPrice(parser.parse("01:01:02").getTime(), 2.0, 5.0);
BarMerger.addNewPrice(parser.parse("01:01:03").getTime(), 8.0, 5.0);
BarMerger.addNewPrice(parser.parse("01:01:04").getTime(), 1.5, 5.0);
BarMerger.addNewPrice(parser.parse("01:01:05").getTime(), 2.5, 5.0);
BarMerger.addNewPrice(parser.parse("01:01:06").getTime(), 1.0, 5.0);
BarMerger.addNewPrice(parser.parse("01:01:07").getTime(), 4.5, 5.0);
BarMerger.close();
latch.await();
}
Aggregations