@Override protected Object[] toConvertedColumns(BigMoney value) { return new Object[] { value.getCurrencyUnit(), value.getAmount() }; }
@Override protected BigMoney fromConvertedColumns(Object[] convertedColumns) { CurrencyUnit currencyUnitPart = (CurrencyUnit) convertedColumns[0]; BigDecimal amountPart = (BigDecimal) convertedColumns[1]; BigMoney money = BigMoney.of(currencyUnitPart, amountPart); return money; }
@Override protected Value<String> toDatastore(final BigMoney value) { return StringValue.of(value.toString()); } }
@Override protected Object[] toConvertedColumns(BigMoney value) { return new Object[] { value.getCurrencyUnit(), value.getAmountMajorLong() }; }
@Override protected Object[] toConvertedColumns(BigMoney value) { return new Object[] { value.getCurrencyUnit(), value.getAmountMinorLong() }; }
log.debug("{} BTC Balance: {}",exchangeName,balanceBTC.toString()); }else { log.error("ERROR: {} BTC Balance is null.",exchangeName); log.debug("Max. BTC: {}",maxBTC.toString()); }else { log.error("ERROR: Max. BTC is null"); log.debug("Min. BTC: {}",minBTC.toString()); }else { log.error("ERROR: Min. BTC is null"); if(!balanceBTC.isZero()) { BigMoney qtyToSell; BigDecimal bigWeight = new BigDecimal(weight / Math.pow(2, asksInARow.get(localCurrency))); if(riskAlgorithm.equals(1)) { qtyToSell = balanceBTC.multipliedBy(bigWeight); }else { if(balanceBTC.compareTo(maxBTC) >= 0) { qtyToSell = maxBTC.multipliedBy(bigWeight); }else { qtyToSell = balanceBTC.multipliedBy(bigWeight); if(qtyToSell.isGreaterThan(maxBTC)) { log.info("{} was more than the configured limit of {}",qtyToSell.withScale(8,RoundingMode.HALF_EVEN).toString(),maxBTC.toString()); log.info("Reducing {} order size to {}",exchangeName,maxBTC.toString()); qtyToSell = maxBTC; if(qtyToSell.isLessThan(minBTC)) {
public void calcProfitLoss() { BigMoney equivBTCStartBal = BigMoney.zero(CurrencyUnit.of("BTC")); BigMoney equivBTCEndBal = BigMoney.zero(CurrencyUnit.of("BTC")); BigMoney startBal = BigMoney.zero(CurrencyUnit.of("BTC")); BigMoney endBal = BigMoney.zero(CurrencyUnit.of("BTC")); BigDecimal startRate = BigDecimal.ZERO; BigDecimal endRate = BigDecimal.ZERO; equivBTCStartBal = equivBTCStartBal.plus(startBal); equivBTCEndBal = equivBTCEndBal.plus(endBal); } else { if (startBal.isPositive() || endBal.isPositive()) { if (rates.containsKey(currency)) { if ((rates.get(currency).size() >= 2)) { startRate=rates.get(currency).get(0).getAmount(); endRate=rates.get(currency).get(rates.get(currency).size() - 1).getAmount(); equivBTCStartBal = equivBTCStartBal.plus(startBal.convertedTo(CurrencyUnit.of("BTC"),BigDecimal.ONE.divide(startRate,16,RoundingMode.HALF_EVEN))); equivBTCEndBal = equivBTCEndBal.plus(endBal.convertedTo(CurrencyUnit.of("BTC"),BigDecimal.ONE.divide(endRate,16,RoundingMode.HALF_EVEN))); } else { log.info("Not enough "+currency.getCode()+" ticker data collected yet to calculate profit/loss"); profitBTC = equivBTCEndBal.minus(equivBTCStartBal); profitPercent = profitBTC.getAmount().divide(equivBTCStartBal.getAmount(),16,RoundingMode.HALF_EVEN); String profitToDisplay = percentFormat.format(profitPercent); log.info("Equivalent BTC Start Balance: "+equivBTCStartBal.withScale(8,RoundingMode.HALF_EVEN).toString()+" Equivalent BTC Current Balance: "+equivBTCEndBal.withScale(8,RoundingMode.HALF_EVEN).toString()); log.info("BTC Profit/Loss: "+profitBTC.withScale(8,RoundingMode.HALF_EVEN).toString()+" Percentage Profit/Loss: "+profitToDisplay);
BigMoney lastTickAskFrom = lastTickMap.get(from.getCurrencyUnit()).getAsk(); BigMoney lastTickBidTo = lastTickMap.get(to.getCurrencyUnit()).getBid(); BigDecimal oneDivFrom = BigDecimal.ONE.divide(lastTickAskFrom.getAmount(),16,RoundingMode.HALF_EVEN); BigDecimal oneDivTo = BigDecimal.ONE.divide(lastTickBidTo.getAmount(),16,RoundingMode.HALF_EVEN); log.debug("Last ticker Ask price was "+lastTickAskFrom.toString()); log.debug("BTC/"+from.getCurrencyUnit().toString()+" is "+oneDivFrom.toString()); log.debug("Last ticker Bid price was "+lastTickBidTo.toString()); log.debug("BTC/"+to.getCurrencyUnit().toString()+" is "+oneDivTo.toString()); BigMoney qtyFrom = AccountManager.getInstance(exchangeName).getBalance(from.getCurrencyUnit()); BigMoney qtyFromBTC = qtyFrom.convertedTo(CurrencyUnit.of("BTC"),oneDivFrom); BigMoney qtyTo = qtyFromBTC.convertedTo(to.getCurrencyUnit(),lastTickBidTo.getAmount()); BigMoney qtyToBTC = qtyTo.convertedTo(CurrencyUnit.of("BTC"),oneDivTo); if (!qtyFrom.isZero()){ MarketOrder buyOrder = new MarketOrder(OrderType.BID,qtyFromBTC.getAmount(),"BTC",from.getCurrencyUnit().toString()); MarketOrder sellOrder = new MarketOrder(OrderType.ASK,qtyToBTC.getAmount(),"BTC",to.getCurrencyUnit().toString()); log.debug(exchangeName+" Arbitrage buy order is buy "+qtyFromBTC.toString()+" for "+qtyFrom.toString()); log.debug(exchangeName+" Arbitrage sell order is sell "+qtyToBTC.toString()+" for "+qtyTo.toString()); log.info("Arbitrage sold "+qtyFrom.withScale(8,RoundingMode.HALF_EVEN).toString() +" for "+ qtyFromBTC.withScale(8,RoundingMode.HALF_EVEN).toString()+" on "+exchangeName); if(!Application.getInstance().getSimMode()){ marketsellOrderReturnValue = tradeService.placeMarketOrder(sellOrder); log.info("Arbitrage bought "+qtyTo.withScale(8,RoundingMode.HALF_EVEN).toString() +" for "+ qtyToBTC.withScale(8,RoundingMode.HALF_EVEN).toString()+" on "+exchangeName); log.info("Arbitrage successfully traded "+qtyFrom.toString()+" for "+qtyTo.toString()+" on "+exchangeName); log.info(AccountManager.getInstance(exchangeName).getAccountInfo().toString());
public synchronized BigMoney getLowestAsk() throws WalletNotFoundException { BigMoney lowFactor = BigMoney.of(baseCurrency,100); synchronized (lastTickMap) { BigMoney basePrice = lastTickMap.get(baseCurrency).getLast(); for(CurrencyUnit currency : lastTickMap.keySet()) { BigMoney testPrice = lastTickMap.get(currency).getAsk(); BigMoney factor = basePrice.isSameCurrency(testPrice) ? basePrice.dividedBy(testPrice.getAmount(),RoundingMode.HALF_EVEN) : basePrice.convertedTo(currency,BigDecimal.ONE.divide(testPrice.getAmount(),16,RoundingMode.HALF_EVEN)); if(factor.getAmount().compareTo(lowFactor.getAmount()) < 0 ) { lowFactor = factor; } } } return lowFactor; }
str.append(localCurrency.getCode()); str.append(" "); str.append(numberFormat.format(emaLong.getAmount())); str.append(" | "); str.append("Short EMA: "); str.append(localCurrency.getCode()); str.append(" "); str.append(numberFormat.format(emaShort.getAmount())); log.debug(str.toString()); str.append(localCurrency.getCode()); str.append(" market is trending"); if(emaShort.isGreaterThan(emaLong)) { }else if(emaShort.isLessThan(emaLong)) { str.append(exchangeName + ":- "); str.append("Long SMA: "); str.append(smaLong.toString()); str.append(" | "); str.append("Short SMA: "); str.append(smaShort.toString()); log.debug(str.toString()); str.append(localCurrency.getCode()); str.append(" market is trending"); if(smaShort.isGreaterThan(smaLong)) { }else if(smaShort.isLessThan(smaLong)) {
@Override public BigDecimal toNonNullValue(BigMoney value) { if (!currencyUnit.equals(value.getCurrencyUnit())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrencyUnit()); } return BigMoney.of(value).getAmount(); }
bidArrow = 0; askArrow = 0; shortSMA = BigMoney.zero(localCurrency); longSMA = BigMoney.zero(localCurrency); shortEMA = BigMoney.zero(localCurrency); longEMA = BigMoney.zero(localCurrency); shortMACD = BigMoney.zero(localCurrency); longMACD = BigMoney.zero(localCurrency); sigLineMACD = BigMoney.zero(localCurrency); Double expLongMACD = Double.valueOf(0); Double expSigLineMACD = Double.valueOf(0); BigMoney sumShortSMA = BigMoney.zero(localCurrency); BigMoney sumLongSMA = BigMoney.zero(localCurrency); BigMoney newBid = null, oldBid = BigMoney.zero(localCurrency); BigMoney newAsk = null, oldAsk = BigMoney.zero(localCurrency); BigMoney newPrice = null, oldPrice = BigMoney.zero(localCurrency); BigDecimal newVolume = null, oldVolume = BigDecimal.ZERO; BigDecimal totalVolume = BigDecimal.ZERO, absVolume = null, changedVolume = null; vwap = BigMoney.zero(localCurrency); sigLineMACD = shortMACD.minus(longMACD); if(newPrice.isGreaterThan(high.getLast())){ high = tick; }else if(newPrice.isLessThan(low.getLast())){ low = tick;
double profit = highestBid.getAmount().subtract(lowestAsk.getAmount()).doubleValue(); double profitAfterFee = profit - (fee * 2); +" on currency pair "+lowestAsk.getCurrencyUnit().toString()+"/"+highestBid.getCurrencyUnit().toString()+" on "+exchangeName); log.info(exchangeName+" Conversion Factors:- \tHighest Bid: "+highestBid.toString()+"\t Lowest Ask: "+lowestAsk.toString());
/** * Gets the currency. * * @return the currency, never null */ public CurrencyUnit getCurrencyUnit() { return money.getCurrencyUnit(); }
private void writeBigMoney(ObjectOutput out, BigMoney obj) throws IOException { writeCurrency(out, obj.getCurrencyUnit()); byte[] bytes = obj.getAmount().unscaledValue().toByteArray(); out.writeInt(bytes.length); out.write(bytes); out.writeInt(obj.getScale()); }
/** * Validates that the currency of this money and the specified money match. * * @param moneyProvider the money to check, not null * @throws CurrencyMismatchException if the currencies differ */ private BigMoney checkCurrencyEqual(BigMoneyProvider moneyProvider) { BigMoney money = of(moneyProvider); if (isSameCurrency(money) == false) { throw new CurrencyMismatchException(getCurrencyUnit(), money.getCurrencyUnit()); } return money; }
/** * Checks if this instance and the specified instance have the same currency. * * @param money the money to check, not null * @return true if they have the same currency */ public boolean isSameCurrency(BigMoneyProvider money) { return (currency.equals(of(money).getCurrencyUnit())); }
/** * Returns a copy of this monetary value with the amount subtracted. * <p> * This subtracts the specified amount from this monetary amount, returning a new object. * The amount subtracted must be in the same currency. * <p> * No precision is lost in the result. * The scale of the result will be the maximum of the two scales. * For example,'USD 25.95' minus 'USD 3.021' gives 'USD 22.929'. * <p> * This instance is immutable and unaffected by this method. * * @param moneyToSubtract the monetary value to subtract, not null * @return the new instance with the input amount subtracted, never null * @throws CurrencyMismatchException if the currencies differ */ public BigMoney minus(BigMoneyProvider moneyToSubtract) { BigMoney toSubtract = checkCurrencyEqual(moneyToSubtract); return minus(toSubtract.getAmount()); }
public String addOrder(Order.OrderType orderType, BigMoney price, BigDecimal amount) throws UnsupportedEncodingException, JsonProcessingException { String typeStr = Order.OrderType.ASK == orderType ? "ask" : "bid"; String priceStr = MtGoxUtils.getPriceString(price); String amountStr = MtGoxUtils.getAmountString(amount); HashMap<String, String> params = new HashMap<String, String>(3); params.put("type", typeStr); params.put("amount_int", amountStr); // if price <= 0, it's a market order if (price.isGreaterThan(BigMoney.zero(price.getCurrencyUnit()))) { params.put("price_int", priceStr); } String reqId = String.format("order_add:%s:%s:%s", typeStr, priceStr, amountStr); return signedCall("order/add", params, reqId); }
/** * Returns a copy of this monetary value with the amount added. * <p> * This adds the specified amount to this monetary amount, returning a new object. * The amount added must be in the same currency. * <p> * No precision is lost in the result. * The scale of the result will be the maximum of the two scales. * For example, 'USD 25.95' plus 'USD 3.021' gives 'USD 28.971'. * <p> * This instance is immutable and unaffected by this method. * * @param moneyToAdd the monetary value to add, not null * @return the new instance with the input amount added, never null * @throws CurrencyMismatchException if the currencies differ */ public BigMoney plus(BigMoneyProvider moneyToAdd) { BigMoney toAdd = checkCurrencyEqual(moneyToAdd); return plus(toAdd.getAmount()); }