diff options
author | clarkohw <66530369+clarkohw@users.noreply.github.com> | 2021-04-19 20:39:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-19 20:39:13 -0400 |
commit | 87fe6542c4c39a8b79f8b86a2b965e19d4b674c3 (patch) | |
tree | 6fa47afa0b624eaea0d9ffa24531cad8be35f9d3 /src | |
parent | 01e50d7a0bbab78ffbbf858b03c6b365d2886397 (diff) | |
parent | c1a59efe3ac070bdd6866666bbee5f5b57786777 (diff) |
Merge pull request #15 from cs0320-2021/profit-optimization
Profit optimization
Diffstat (limited to 'src')
4 files changed, 207 insertions, 36 deletions
diff --git a/src/main/java/edu/brown/cs/student/term/Main.java b/src/main/java/edu/brown/cs/student/term/Main.java index df95ec0..dd304c5 100644 --- a/src/main/java/edu/brown/cs/student/term/Main.java +++ b/src/main/java/edu/brown/cs/student/term/Main.java @@ -185,7 +185,7 @@ public final class Main { ProfitCalculation profit = new ProfitCalculation(DatabaseQuerier.getConn(), person, startPeriod, endPeriod); List<StockHolding> holdings = profit.getHoldingsList(); - double gains = profit.calculateGains(); + double gains = profit.calculateGainsSingle(); double sp500PercentGain = profit.compareToSP500(); Map<String, Object> res = new HashMap<>(); diff --git a/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java b/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java index 0ca7258..d37910e 100644 --- a/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java +++ b/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java @@ -16,8 +16,9 @@ public class SuspicionRanker { } private <K, V extends Comparable<V>> V getMaxOfMap(Map<K, V> map) { - Map.Entry<K, V> maxEntry = Collections.max(map.entrySet(), Map.Entry.comparingByValue()); - return maxEntry.getValue(); + //Map.Entry<K, V> maxEntry = Collections.max(map.entrySet(), Map.Entry.comparingByValue()); + Collection<V> values = map.values(); + return Collections.max(map.values()); } private <K, V extends Comparable<V>> V getMinOfMap(Map<K, V> map) { @@ -65,13 +66,14 @@ public class SuspicionRanker { } double profitMax = getMaxOfMap(profitMap); + //if all of our values are negative, we need to flip sides so that the //biggest loser doesn't end up being the most suspicious person*/ - if(profitMax <= 0) { profitMax = Math.abs(getMinOfMap(profitMap)); } + //if both the min we found and max we found are 0, then we have //the special case where all the values are 0, in which case we //need to avoid dividing by 0 @@ -81,17 +83,17 @@ public class SuspicionRanker { } - double hubMax = getMaxOfMap(holderToHubScore); for (Holder guy : holderToHubScore.keySet()) { - if(!profitMap.containsKey(guy.getId())){ - continue; + double normalizedProfitScore = 0; + if (profitMap.containsKey(guy.getId())) { + normalizedProfitScore = profitMap.get(guy.getId()) / profitMax; } - double normalizedProfitScore = profitMap.get(guy.getId()) / profitMax; double normalizedHubScore = holderToHubScore.get(guy) / hubMax; - double suspicionScore = normalizedHubScore* 0.6 + normalizedProfitScore * 0.4; + double suspicionScore = normalizedHubScore * 0.6 + normalizedProfitScore * 0.4; + guy.setSuspicionScore(suspicionScore); orderedSuspicion.add(guy); } diff --git a/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java b/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java index 0ef87c3..d0df8a8 100644 --- a/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java +++ b/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java @@ -83,10 +83,10 @@ public class ProfitCalculation { private String validateTicker(String ticker) { //this is cleaning some improperly formatted tickers ticker = ticker.replaceAll("[^a-zA-Z0-9]", "").toUpperCase(); - if(ticker.contains("[0-9]") || - ticker.length() > 5 || - ticker.length() < 2 || - ticker.contains("NONE")) { + if (ticker.contains("[0-9]") || + ticker.length() > 5 || + ticker.length() < 2 || + ticker.contains("NONE")) { return ""; } @@ -99,7 +99,7 @@ public class ProfitCalculation { PreparedStatement prep; prep = conn.prepareStatement("SELECT * FROM \'trades\' WHERE holder_name= ? " - + " AND trade_timestamp BETWEEN ? AND ?" + + " AND trade_timestamp BETWEEN ? AND ? " + "order by trade_timestamp asc;"); prep.setString(1, this.person); prep.setDate(2, startTime); @@ -109,7 +109,7 @@ public class ProfitCalculation { while (rs.next()) { String ticker = rs.getString("stock_name"); ticker = validateTicker(ticker); - if(ticker.equals("")){ + if (ticker.equals("")) { continue; } int shares = rs.getInt("number_of_shares"); @@ -283,12 +283,15 @@ public class ProfitCalculation { } - public double calculateGains() { + public double calculateGainsSingle() { + if (!tablesFilled) { organizeOrders(); getRealizedGains(); tablesFilled = true; } + + double realizedGains = 0; for (double value : realizedGainsMap.values()) { @@ -383,27 +386,193 @@ public class ProfitCalculation { } try { PreparedStatement prep; + long START = System.currentTimeMillis(); prep = - conn.prepareStatement("SELECT * from trades group by holder_name;"); + conn.prepareStatement( + "SELECT * From trades GROUP BY holder_id having max(is_buy) = 1;"); ResultSet rs = prep.executeQuery(); + + long QUERY = System.currentTimeMillis(); + //System.out.println((QUERY - START) + " query time"); + + //set of all people who have made both buy and sell orders + Set<Integer> people = new HashSet<>(); + while (rs.next()) { - int id = rs.getInt("holder_id"); - this.person = rs.getString("holder_name"); - resetClass(); - double gain = this.calculateGains(); - if (moneyInput == 0) { - profitMap.put(id, 0.0); - } else { - profitMap.put(id, gain / moneyInput); - } +// int id = rs.getInt("holder_id"); +// this.person = rs.getString("holder_name"); +// resetClass(); +// +// +// +// double gain = this.calculateGains(); +// if (moneyInput == 0) { +// profitMap.put(id, 0.0); +// } else { +// profitMap.put(id, gain / moneyInput); +// } + people.add(rs.getInt("holder_id")); } + + profitMap = calculateGains(people); + + long LOOP = System.currentTimeMillis(); + //System.out.println((LOOP - QUERY) + " loop"); + } catch (SQLException throwables) { System.out.println("ERROR: SQl error in profit calculation"); } return profitMap; } + private Map<Integer, Double> calculateGains(Set<Integer> people) { + Map<Integer, Double> gainsMap = new HashMap<>(); + + //map of stock to list of buy orders, first element in list is oldest + Map<Integer, Map<String, LinkedList<OrderTuple>>> sellMap = new HashMap<>(); + //map of stock to list of buy orders, first element in list is oldest + Map<Integer, Map<String, LinkedList<OrderTuple>>> buyMap = new HashMap<>(); + //money input + Map<Integer, Double> moneyInMap = new HashMap<>(); + + + try { + PreparedStatement prep; + prep = + conn.prepareStatement("SELECT * FROM \'trades\'" + + " WHERE NOT number_of_shares = 0 AND trade_timestamp BETWEEN ? AND ? " + + "order by trade_timestamp asc;"); + prep.setDate(1, startTime); + prep.setDate(2, endTime); + ResultSet rs = prep.executeQuery(); + while (rs.next()) { + if (people.contains(rs.getInt("holder_id"))) { + String ticker = rs.getString("stock_name"); + ticker = validateTicker(ticker); + if (ticker.equals("")) { + continue; + } + int shares = rs.getInt("number_of_shares"); + double price = rs.getDouble("share_price"); + int holder_id = rs.getInt("holder_id"); + if (!buyMap.containsKey(holder_id)) { + buyMap.put(holder_id, new HashMap<>()); + } + if (!sellMap.containsKey(holder_id)) { + sellMap.put(holder_id, new HashMap<>()); + } + + + OrderTuple order = new OrderTuple(shares, price, rs.getDate("trade_timestamp")); + + //one element list for first time ticker is seen. + LinkedList<OrderTuple> oneElement = new LinkedList<OrderTuple>(); + oneElement.addLast(order); + + //for buy orders, build up buy history + if (rs.getInt("is_buy") != 0) { + + if (moneyInMap.containsKey(holder_id)) { + moneyInMap.put(holder_id, moneyInMap.get(holder_id) + shares * price); + } else { + moneyInMap.put(holder_id, shares * price); + } + + + if (buyMap.get(holder_id).containsKey(ticker)) { + buyMap.get(holder_id).get(ticker).addLast(order); + } else { + buyMap.get(holder_id).put(ticker, oneElement); + } + } else { + //ignore sell orders for which we do not have buys for + if (sellMap.get(holder_id).containsKey(ticker)) { + sellMap.get(holder_id).get(ticker).addLast(order); + } else { + sellMap.get(holder_id).put(ticker, oneElement); + } + + } + } + } + } catch (SQLException e) { + System.out.println("ERROR: sql error getting trades"); + } + + + //part 2 doing math... + for (Integer person : people) { + this.buyHistoryMap = buyMap.get(person); + this.sellHistoryMap = sellMap.get(person); + if (sellHistoryMap == null) { + continue; + } + + for (String ticker : sellHistoryMap.keySet()) { + //use FIFO selling + LinkedList<OrderTuple> sells = sellHistoryMap.get(ticker); + LinkedList<OrderTuple> buys = buyHistoryMap.get(ticker); + double realizedGain = 0; + if (sells != null && buys != null) { + //process each sell order (unless all buy orders are "drained" + for (OrderTuple sell : sells) { + //stop if buys are empty, stop if buy happened after sell + if (buys.isEmpty()) { + break; + } + + int sharesToSell = sell.getShares(); + + //sell off through list of buys + while (sharesToSell > 0 && !buys.isEmpty()) { + //dont sell from buys which didn't exist at the time. + if (sell.getDate().after(buys.getFirst().getDate()) + || sell.getDate().equals(buys.getFirst().getDate())) { + OrderTuple buyBundle = buys.removeFirst(); + int sharesAtBundlePrice; + //the buy has more shares than we want to sell + if (buyBundle.getShares() > sharesToSell) { + sharesAtBundlePrice = sharesToSell; + sharesToSell = 0; + //add back the holdings that were not sold + buyBundle.setShares(buyBundle.getShares() - sharesAtBundlePrice); + buys.addFirst(buyBundle); + } else { + sharesToSell -= buyBundle.getShares(); + sharesAtBundlePrice = buyBundle.getShares(); + } + realizedGain += sharesAtBundlePrice * (sell.getCost() - buyBundle.getCost()); + } else { + break; + } + + + } + } + } + + if (gainsMap.containsKey(person)) { + gainsMap.put(person, gainsMap.get(person) + realizedGain); + } else { + gainsMap.put(person, realizedGain); + } + + } + + //percent gain + if (gainsMap.containsKey(person) && moneyInMap.containsKey(person)) { + gainsMap.put(person, gainsMap.get(person) / moneyInMap.get(person)); + } else { + gainsMap.put(person, 0.0); + } + + + } + + return gainsMap; + } + public double getMoneyInput() { return this.moneyInput; } diff --git a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java index 0721052..0d22109 100644 --- a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java +++ b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java @@ -67,7 +67,7 @@ public class ProfitCalculationTest { new Date(1618698807000l)); trade = profitCalculation.getHoldingsList(); assertTrue(trade.isEmpty()); - assertEquals(profitCalculation.calculateGains(), 0, 0.001); + assertEquals(profitCalculation.calculateGainsSingle(), 0, 0.001); tearDown(); } @@ -80,16 +80,18 @@ public class ProfitCalculationTest { new ProfitCalculation(DatabaseQuerier.getConn(), "concurrentBS", new Date(1518010558000l), new Date(1715629591000l)); - assertEquals(profitCalculation.getProfitMap().get(100), 1, .01); + Map<Integer, Double> map = profitCalculation.getProfitMap(); + + assertEquals(map.get(100), 1, .01); //buys at multiple prices profitCalculation = new ProfitCalculation(DatabaseQuerier.getConn(), "mulitpleBuyPrices", new Date(1518010558000l), new Date(1715629591000l)); - assertEquals(profitCalculation.getProfitMap().get(101), 1, .01); - assertEquals(profitCalculation.getMoneyInput(), 3750, .01); + assertEquals(profitCalculation.getHoldingsList().get(0).getRealizedGain(), 3750, 0.01); + assertEquals(profitCalculation.getMoneyInput(), 3750, .01); //left over holdings profitCalculation = @@ -133,12 +135,10 @@ public class ProfitCalculationTest { //invalid stock ticker - //profitCalculation = - //new ProfitCalculation(DatabaseQuerier.getConn(), "invalidTicker", new Date(1518010558000l), - ///new Date(1618698807000l)); - //assertEquals(profitCalculation.getHoldingsList().get(0).getRealizedGain(), 0, .01); - //assertEquals(profitCalculation.getHoldingsList().get(0).getUnrealizedGain(), 0, .01); + profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "invalidTicker", new Date(1518010558000l), + new Date(1618698807000l)); + assertTrue(profitCalculation.getHoldingsList().isEmpty()); } - }
\ No newline at end of file |