Java BigDecimal Value Check isProfitable(BigDecimal ask, BigDecimal bid, BigDecimal fee, BigDecimal minProfit)

Here you can find the source of isProfitable(BigDecimal ask, BigDecimal bid, BigDecimal fee, BigDecimal minProfit)

Description

Returns if a combined bid/ask operation is profitable for the specified fees and desired profit

License

Open Source License

Parameter

Parameter Description
ask The ask price
bid The bid price
fee The fees charged in the bid and ask
minProfit Minimum desired profit

Declaration

public static boolean isProfitable(BigDecimal ask, BigDecimal bid,
        BigDecimal fee, BigDecimal minProfit) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;

import java.math.RoundingMode;

public class Main {
    /***/* w  w w  .j a  va 2s  . c o m*/
     * Returns if a combined bid/ask operation is profitable for the specified fees and desired profit
     * @param ask The ask price
     * @param bid The bid price
     * @param fee The fees charged in the bid and ask
     * @param minProfit Minimum desired profit
     * @return
     */
    public static boolean isProfitable(BigDecimal ask, BigDecimal bid,
            BigDecimal fee, BigDecimal minProfit) {
        BigDecimal profit = getProfit(ask, bid, fee);
        boolean result = profit.compareTo(minProfit) >= 0;
        return result;
    }

    /**
     * Get the profit of an operation from its ask/bid and fees values
     * @param ask The ask price
     * @param bid The bid price
     * @param fee The fee
     * 
     *  
     * P = (A/B) * (1-F)^2 - 1
     * 
     * Where:
     * 
     * A = Ask 
     * F = Fees
     * B = Bid
     * 
     * @return
     */
    public static BigDecimal getProfit(BigDecimal ask, BigDecimal bid,
            BigDecimal fee) {
        BigDecimal one = new BigDecimal(1);
        BigDecimal oneMinusFee = one.subtract(fee);
        BigDecimal omfPow = oneMinusFee.multiply(oneMinusFee);
        BigDecimal profit = ask.divide(bid, 8, RoundingMode.FLOOR)
                .multiply(omfPow).subtract(one);
        return profit;
    }
}

Related

  1. isNullOrZero(BigDecimal value)
  2. isParseableBigDecimal(String string)
  3. isPositive(BigDecimal number)
  4. isPositive(BigDecimal value)
  5. isPrimeNumber(BigDecimal inBd)
  6. isSameValue(final BigDecimal val1, final BigDecimal val2)
  7. isStrictementNegatif(BigDecimal pNombre)
  8. isUnscaledValueEqualsOriginal(BigDecimal bigDecimal, BigInteger bigInteger)
  9. isValidDecimal(BigDecimal bd)