Search code examples
javapercentagetrading

Calculate total percent from a suite of trading orders


I try to sum up percent from a suite of simple trading orders in Java.

Let say that an "order" is composed of a position (BUY/SELL), a price and a quantity. If I have:

TIME | POSITION | QTY |PRICE

DAY1: BUY 1 at 10$

DAY2: SELL 1 at 12$

I have made 20% and it's quite easy to program it. But... taking another example, I'm not smart enough to find the right way to get the total percent:

TIME | POSITION | QTY | MARKET PRICE

DAY1: BUY 1 at 10$

DAY2: SELL 1 at 12$ -> (20%)

DAY3: BUY 2 at 10$ -> (0%)

DAY4: SELL 1 at 13$ -> (30%)

DAY5: SELL 1 at 14$ -> (40%)

So a total of 90%.

First question, can these percentages be sum up ? (is it mathematically correct ?)

Second, how you will do that in java ?

Here is a sample method which is just working for first example:

public static double getTotalPercent(List<MarketOrder> orders) {

    double percent = 0;
    MarketOrder previousOrder = orders.get(0);
    for (int i = 1; i < orders.size(); i++) {
        MarketOrder order = orders.get(i);
        percent += getPercent(previousOrder, order);
        previousOrder = order;
    }
    return percent;
}

Solution

  • Assuming the following:

    You have some type MarketOrder that looks like this,

    class MarketOrder {
        public static enum OrderType {
            BUY, 
            SELL, 
        }
    
        private final MarketOrder.OrderType type; 
        private final int amount; 
        private final int money; 
    
        public MarketOrder(MarketOrder.OrderType type, int amount, int money) { 
            if (amount < 1 || money < 1) { 
                throw new IllegalArgumentException(); 
            }
    
            this.type = type; 
            this.amount = amount; 
            this.money = money; 
        }
    }
    

    and you have some Collection<MarketOrder> that represents transactions that you want to calculate a profit for, then declare a static method on MarketOrder that calculates the profit or loss of a Collection<MarketOrder>,

    public static double calculateProfit(Collection<MarketOrder> orders) {
        if (orders == null) {
            throw new NullPointerException("orders is null");
        }
    
        int buyTotal = 0, sellTotal = 0, buyMoney = 0, sellMoney = 0; 
        for (MarketOrder order : orders) { 
            if (order.type == MarketOrder.OrderType.BUY) { // accumulate the units bought and money
                buyTotal += order.amount; 
                buyMoney += order.money * order.amount; 
            } else {
                sellTotal += order.amount; 
                sellMoney += order.money * order.amount;
            }
        }
    
        if (buyTotal != sellTotal) { 
            throw new IllegalArgumentException("buyTotal != sellTotal");
        }
    
        return ((double)sellMoney/(double)buyMoney) - 1; 
    }
    

    Obviously, the exact code you'll need to work out depends upon your MarketOrder type, but the key bits (accumlating the amounts of the transactions, doing the math) is the same.