Search in sources :

Example 6 with Wallet

use of com.github.jnidzwetzki.bitfinex.v2.entity.Wallet 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());
        }
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) CurrencyEntry(com.github.jnidzwetzki.cryptobot.CurrencyEntry) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with Wallet

use of com.github.jnidzwetzki.bitfinex.v2.entity.Wallet in project crypto-bot by jnidzwetzki.

the class TestCapitalAllocation method buildPortfolioManager.

/**
 * Build the portfolio manager
 * @return
 * @throws APIException
 */
private PortfolioManager buildPortfolioManager() throws APIException {
    final Collection<Wallet> wallets = new ArrayList<>();
    wallets.add(new Wallet(Wallet.WALLET_TYPE_EXCHANGE, "USD", 1000, 0, 1000));
    final BitfinexApiBroker apiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(walletManager.getWallets()).thenReturn(wallets);
    Mockito.when(apiBroker.getWalletManager()).thenReturn(walletManager);
    return new BasePortfolioManager(apiBroker, 0.05);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) BasePortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) ArrayList(java.util.ArrayList)

Example 8 with Wallet

use of com.github.jnidzwetzki.bitfinex.v2.entity.Wallet in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletHandlerTest method testWalletSnapshot.

/**
 * Test the wallet parsing
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 20000)
public void testWalletSnapshot() throws APIException, InterruptedException {
    final String callbackValue = "[0,\"ws\",[[\"exchange\",\"ETH\",9,0,null],[\"exchange\",\"USD\",1826.56468323,0,null],[\"margin\",\"USD\",0,0,null],[\"exchange\",\"XRP\",0,0,null],[\"exchange\",\"EOS\",0,0,null],[\"exchange\",\"NEO\",0,0,null],[\"exchange\",\"LTC\",0,0,null],[\"exchange\",\"IOT\",0,0,null],[\"exchange\",\"BTC\",0,0,null]]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final CountDownLatch walletLatch = new CountDownLatch(1);
    final Table<String, String, Wallet> walletTable = HashBasedTable.create();
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(bitfinexApiBroker.getWalletManager()).thenReturn(walletManager);
    Mockito.when(walletManager.getWalletTable()).thenReturn(walletTable);
    Mockito.when(bitfinexApiBroker.getConnectionReadyLatch()).thenReturn(walletLatch);
    Assert.assertTrue(walletTable.isEmpty());
    final WalletHandler walletHandler = new WalletHandler();
    walletHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    walletLatch.await();
    Assert.assertEquals(9, walletTable.size());
    Assert.assertEquals(9, walletTable.get("exchange", "ETH").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertEquals(1826.56468323, walletTable.get("exchange", "USD").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("margin", "USD").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("margin", "USD").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("margin", "USD").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertTrue(walletTable.get("margin", "USD").toString().length() > 0);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) JSONArray(org.json.JSONArray) WalletHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with Wallet

use of com.github.jnidzwetzki.bitfinex.v2.entity.Wallet in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletHandlerTest method testWalletUpdate.

/**
 * Test the wallet parsing
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 20000)
public void testWalletUpdate() throws APIException, InterruptedException {
    final String callbackValue = "[0,\"ws\",[\"exchange\",\"ETH\",9,0,null]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final CountDownLatch walletLatch = new CountDownLatch(1);
    final Table<String, String, Wallet> walletTable = HashBasedTable.create();
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(bitfinexApiBroker.getWalletManager()).thenReturn(walletManager);
    Mockito.when(walletManager.getWalletTable()).thenReturn(walletTable);
    Mockito.when(bitfinexApiBroker.getConnectionReadyLatch()).thenReturn(walletLatch);
    Assert.assertTrue(walletTable.isEmpty());
    final WalletHandler walletHandler = new WalletHandler();
    walletHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    walletLatch.await();
    Assert.assertEquals(1, walletTable.size());
    Assert.assertEquals(9, walletTable.get("exchange", "ETH").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) JSONArray(org.json.JSONArray) WalletHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with Wallet

use of com.github.jnidzwetzki.bitfinex.v2.entity.Wallet in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletManager method calculateWalletMarginBalance.

/**
 * Calculate the wallet margin balance for the given currency (e.g., BTC)
 *
 * @param symbol
 * @throws APIException
 */
public void calculateWalletMarginBalance(final String symbol) throws APIException {
    throwExceptionIfUnauthenticated();
    bitfinexApiBroker.sendCommand(new CalculateCommand("wallet_margin_" + symbol));
}
Also used : CalculateCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CalculateCommand)

Aggregations

Wallet (com.github.jnidzwetzki.bitfinex.v2.entity.Wallet)9 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)4 WalletManager (com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager)4 WalletHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler)3 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CalculateCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CalculateCommand)2 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)2 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)2 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)2 BasePortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager)2 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 Test (org.junit.Test)2 DoNothingHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.DoNothingHandler)1 HeartbeatHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.HeartbeatHandler)1 NotificationHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.NotificationHandler)1 OrderHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.OrderHandler)1 PositionHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.PositionHandler)1 TradeHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.TradeHandler)1