Example usage for org.apache.commons.math.util MathUtils sign

List of usage examples for org.apache.commons.math.util MathUtils sign

Introduction

In this page you can find the example usage for org.apache.commons.math.util MathUtils sign.

Prototype

public static short sign(final short x) 

Source Link

Document

Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a> for short value <code>x</code>.

Usage

From source file:ch.algotrader.accounting.PositionTrackerImpl.java

/**
 * processes a transaction on an existing position
 *
 * @return a pair containing the profit and profit percentage
 *//*w w w.j  a  v a 2 s  . c o m*/
@Override
public TradePerformanceVO processTransaction(Position position, Transaction transaction) {

    // get old values
    long oldQty = position.getQuantity();
    double oldCost = position.getCost().doubleValue();
    double oldRealizedPL = position.getRealizedPL().doubleValue();
    double oldAvgPrice = position.getAveragePriceDouble();

    // get transaction values
    long qty = transaction.getQuantity();
    double price = transaction.getPrice().doubleValue();
    double totalCharges = transaction.getTotalChargesDouble();
    double contractSize = transaction.getSecurity().getSecurityFamily().getContractSize();

    // calculate opening and closing quantity
    long closingQty = MathUtils.sign(oldQty) != MathUtils.sign(qty)
            ? Math.min(Math.abs(oldQty), Math.abs(qty)) * MathUtils.sign(qty)
            : 0;
    long openingQty = MathUtils.sign(oldQty) == MathUtils.sign(qty) ? qty : qty - closingQty;

    // calculate new values
    long newQty = oldQty + qty;
    double newCost = oldCost + openingQty * contractSize * price;
    double newRealizedPL = oldRealizedPL;

    // handle a previously non-closed position (aldAvgPrice is Double.NaN for a previously closed position)
    if (closingQty != 0) {
        newCost += closingQty * contractSize * oldAvgPrice;
        newRealizedPL += closingQty * contractSize * (oldAvgPrice - price);
    }

    // handle commissions
    if (openingQty != 0) {
        newCost += totalCharges * openingQty / qty;
    }

    if (closingQty != 0) {
        newRealizedPL -= totalCharges * closingQty / qty;
    }

    // calculate profit and profitPct
    TradePerformanceVO tradePerformance = null;
    if (Long.signum(position.getQuantity()) * Long.signum(transaction.getQuantity()) == -1) {

        double cost, value;
        if (Math.abs(transaction.getQuantity()) <= Math.abs(position.getQuantity())) {
            cost = position.getCost().doubleValue()
                    * Math.abs((double) transaction.getQuantity() / (double) position.getQuantity());
            value = transaction.getNetValueDouble();
        } else {
            cost = position.getCost().doubleValue();
            value = transaction.getNetValueDouble()
                    * Math.abs((double) position.getQuantity() / (double) transaction.getQuantity());
        }

        double profit = value - cost;
        double profitPct = Direction.LONG.equals(position.getDirection()) ? ((value - cost) / cost)
                : ((cost - value) / cost);

        tradePerformance = new TradePerformanceVO(profit, profitPct, profit > 0);
    }

    // set values
    position.setQuantity(newQty);
    position.setCost(RoundUtil.getBigDecimal(newCost));
    position.setRealizedPL(RoundUtil.getBigDecimal(newRealizedPL));

    return tradePerformance;
}