Skip to content

Instantly share code, notes, and snippets.

@agran
Created November 8, 2015 00:32
Show Gist options
  • Save agran/8f49b5f8e9c0b3c58021 to your computer and use it in GitHub Desktop.
Save agran/8f49b5f8e9c0b3c58021 to your computer and use it in GitHub Desktop.
private Pair<BigDecimal, BigDecimal> bruteforceAmount(BigDecimal price, BigDecimal amountLeft, BigDecimal increment)
{
BigDecimal add = BigDecimal.ZERO.setScale(8);
BigDecimal satoshi = new BigDecimal("0.00000001").setScale(8);
BigDecimal amount = price.multiply(amountLeft).setScale(8, RoundingMode.DOWN);
amount = amount.subtract(amount.remainder(increment));
price = BigDecimal.ONE.setScale(8).divide(
price,
8,
RoundingMode.DOWN );
boolean nonstop = true;
while ( nonstop )
{
amount = amount.add(add);
BigDecimal multiply = price.multiply(amount).setScale(8, RoundingMode.DOWN);
if(multiply.compareTo(amountLeft) == 1)
{
amount = amount.subtract(add);
nonstop = false;
}
if(multiply.compareTo(amountLeft) == 0)
{
return new Pair<BigDecimal, BigDecimal> (price, amount);
}
add = increment;
}
nonstop = true;
amount = amount.subtract(increment);
if(amount.compareTo(BigDecimal.ZERO)<=0)
{
amount = increment;
}
price = price.multiply(amount).setScale(8, RoundingMode.DOWN);
/*
while ( nonstop )
{
price = price.add(satoshi);
BigDecimal multiply = price.multiply(amount).setScale(8, RoundingMode.DOWN);
if(multiply.compareTo(amountLeft) == 1)
{
price = price.subtract(satoshi);
nonstop = false;
}
if(multiply.compareTo(amountLeft) == 0)
{
nonstop = false;
}
add = increment;
}
*/
return new Pair<BigDecimal, BigDecimal> (price, amount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment