diff options
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/Main.java | 37 | ||||
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java | 13 | ||||
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java (renamed from src/main/java/edu/brown/cs/student/term/ProfitCalculation.java) | 66 | ||||
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/profit/StockHolding.java | 16 |
4 files changed, 101 insertions, 31 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 afbc79b..3b0a258 100644 --- a/src/main/java/edu/brown/cs/student/term/Main.java +++ b/src/main/java/edu/brown/cs/student/term/Main.java @@ -2,8 +2,8 @@ package edu.brown.cs.student.term; import com.google.common.collect.ImmutableMap; import edu.brown.cs.student.term.hub.Holder; -import edu.brown.cs.student.term.hub.HubSearch; -import edu.brown.cs.student.term.hub.LinkMapper; +import edu.brown.cs.student.term.profit.ProfitCalculation; +import edu.brown.cs.student.term.profit.StockHolding; import edu.brown.cs.student.term.hub.SuspicionRanker; import edu.brown.cs.student.term.repl.Command; import edu.brown.cs.student.term.repl.REPL; @@ -26,6 +26,7 @@ import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -123,7 +124,7 @@ public final class Main { } /** - * Gets the list of holders with id, name, and suspicion rank + * Gets the list of holders with id, name, and suspicion rank. */ private static class SuspicionRankHandler implements Route { @Override @@ -159,18 +160,36 @@ public final class Main { public Object handle(Request request, Response response) throws Exception { JSONObject req = new JSONObject(request.body()); String person = req.getString("person"); - Date startPeriod = new Date(req.getLong("startDate")); - Date endPeriod = new Date(req.getLong("endDate")); - + Date startPeriod = new Date(req.getLong("startTime")); + Date endPeriod = new Date(req.getLong("endTime")); + + List<StockHolding> holdings = new LinkedList<>(); + ProfitCalculation profit; + double gains = 0.0; + double sp500PercentGain = 0.0; + double sp500Gain = 0.0; try { DatabaseQuerier db = new DatabaseQuerier("data/trades.sqlite3"); - new ProfitCalculation(DatabaseQuerier.getConn(), "person", startPeriod, endPeriod); + profit = + new ProfitCalculation(DatabaseQuerier.getConn(), person, startPeriod, endPeriod); + holdings = profit.getHoldingsList(); + gains = profit.calculateGains(); + sp500PercentGain = profit.compareToSP500(); } catch (Exception e) { System.out.println("DBQuerier Test, couldn't connect to db???"); return "Error"; } - //TODO: Clark this is just to fix a no return error - return "Temporary"; + + Map<String, Object> res = new HashMap(); + res.put("person", person); + res.put("moneyIn", profit.getMoneyInput()); + res.put("moneyOut", profit.getMoneyInput() + gains); + res.put("holdings", holdings); + res.put("percentGain", 100 * gains / profit.getMoneyInput()); + res.put("SP500", (1 + sp500PercentGain) * profit.getMoneyInput()); + res.put("percentSP500", 100 * sp500PercentGain); + return GSON.toJson(res); + } } 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 dd959f0..9f5f9c1 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 @@ -1,7 +1,7 @@ package edu.brown.cs.student.term.hub; import edu.brown.cs.student.term.DatabaseQuerier; -import edu.brown.cs.student.term.ProfitCalculation; +import edu.brown.cs.student.term.profit.ProfitCalculation; import java.time.Instant; import java.sql.Date; @@ -10,7 +10,8 @@ import java.util.*; public class SuspicionRanker { DatabaseQuerier querier; - public SuspicionRanker(DatabaseQuerier db){ + + public SuspicionRanker(DatabaseQuerier db) { this.querier = db; } @@ -21,7 +22,7 @@ public class SuspicionRanker { return maxEntry.getValue(); } - public List<Holder> getSuspicionScoreList(Instant start, Instant end){ + public List<Holder> getSuspicionScoreList(Instant start, Instant end) { List<Holder> suspicionList = new ArrayList<>(); try { LinkMapper lm = new LinkMapper(querier); @@ -29,8 +30,8 @@ public class SuspicionRanker { Map<Holder, Double> holderToHubScore = hub.runHubSearch(start, end); ProfitCalculation pc = new ProfitCalculation(DatabaseQuerier.getConn(), "", - new Date(start.toEpochMilli()), - new Date(end.toEpochMilli())); + new Date(start.toEpochMilli()), + new Date(end.toEpochMilli())); Map<Integer, Double> profitMap = pc.getProfitMap(); @@ -38,7 +39,7 @@ public class SuspicionRanker { double hubMax = getMaxOfMap(holderToHubScore); - for(Holder guy: holderToHubScore.keySet()){ + for (Holder guy : holderToHubScore.keySet()) { double normalizedProfitScore = profitMap.get(guy.getId()) / profitMax; double normalizedHubScore = holderToHubScore.get(guy) / hubMax; double suspicionScore = normalizedHubScore * 0.6 + normalizedProfitScore * 0.4; diff --git a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java b/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java index 30d7f36..85b2a9a 100644 --- a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java +++ b/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java @@ -1,6 +1,7 @@ -package edu.brown.cs.student.term; +package edu.brown.cs.student.term.profit; +import edu.brown.cs.student.term.profit.StockHolding; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -20,6 +21,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.LinkedList; import java.util.HashMap; +import java.util.List; import java.util.Map; public class ProfitCalculation { @@ -27,6 +29,7 @@ public class ProfitCalculation { private String person; private Date startTime; private Date endTime; + private boolean tablesFilled; private String BASE_URL = "https://data.alpaca.markets/v1"; @@ -68,6 +71,7 @@ public class ProfitCalculation { realizedGainsMap = new HashMap<>(); unrealizedGainsMap = new HashMap<>(); currentStockPrices = new HashMap<>(); + tablesFilled = false; } /** @@ -257,9 +261,12 @@ public class ProfitCalculation { } public double calculateGains() { - organizeOrders(); - getRealizedGains(); - getUnrealizedGains(); + if (!tablesFilled) { + organizeOrders(); + getRealizedGains(); + getUnrealizedGains(); + tablesFilled = true; + } double realizedGains = 0; double unrealizedGains = 0; @@ -271,19 +278,41 @@ public class ProfitCalculation { unrealizedGains += value; } return unrealizedGains + realizedGains; + } + + public List<StockHolding> getHoldingsList() { + if (!tablesFilled) { + organizeOrders(); + getRealizedGains(); + getUnrealizedGains(); + tablesFilled = true; + } -// System.out.println("Money In: " + moneyInput); -// System.out.println("Money Out: " + (moneyInput + totalGains)); -// System.out.println("NASDAQ on money In: " + (moneyInput * compareToSP500())); -// System.out.println( -// "Total: " + totalGains + "| unrealized: " + unrealizedGains + " | realized: " + -// realizedGains); + List<StockHolding> holdings = new LinkedList<>(); + + for (String key : buyHistoryMap.keySet()) { + double realizedGains = 0; + double unrealizedGains = 0; + if (unrealizedGainsMap.containsKey(key)) { + unrealizedGains = unrealizedGainsMap.get(key); + } + if (realizedGainsMap.containsKey(key)) { + realizedGains = realizedGainsMap.get(key); + } + + int shares = 0; + for (OrderTuple order : buyHistoryMap.get(key)) { + shares += order.getShares(); + } + holdings.add(new StockHolding(key, realizedGains, unrealizedGains, shares)); + } + return holdings; } /** * return percent change in SPY (SP 500) over the time period. */ - private double compareToSP500() { + public double compareToSP500() { String url = "https://data.alpaca.markets/v1/bars/" + "day?" + "symbols=SPY" @@ -313,7 +342,7 @@ public class ProfitCalculation { double endPrice = object.getJSONObject(object.length() - 1).getDouble("c"); //get percent change //end - start /start - return 1 + ((endPrice - startPrice) / startPrice); + return ((endPrice - startPrice) / startPrice); } @@ -332,11 +361,12 @@ public class ProfitCalculation { while (rs.next()) { int id = rs.getInt("holder_id"); this.person = rs.getString("holder_name"); - //TODO: Temporary fix for the moneyinput divide by 0 error - if(moneyInput == 0){ - moneyInput = 1; + if (moneyInput == 0) { + profitMap.put(id, 0.0); + } else { + profitMap.put(id, this.calculateGains() / moneyInput); } - profitMap.put(id, this.calculateGains() / moneyInput); + } } catch (SQLException throwables) { System.out.println("ERROR: SQl error in profit calculation"); @@ -345,6 +375,10 @@ public class ProfitCalculation { return profitMap; } + public double getMoneyInput() { + return this.moneyInput; + } + public void setConnection(String filename) throws SQLException, ClassNotFoundException { // Initialize the database connection, turn foreign keys on diff --git a/src/main/java/edu/brown/cs/student/term/profit/StockHolding.java b/src/main/java/edu/brown/cs/student/term/profit/StockHolding.java new file mode 100644 index 0000000..e2b174c --- /dev/null +++ b/src/main/java/edu/brown/cs/student/term/profit/StockHolding.java @@ -0,0 +1,16 @@ +package edu.brown.cs.student.term.profit; + +public class StockHolding { + private String ticker; + private Double realizedGain; + private Double unrealizedGain; + private int shares; + + public StockHolding(String ticker, Double realizedGain, Double unrealizedGain, int shares) { + this.ticker = ticker; + this.realizedGain = realizedGain; + this.unrealizedGain = unrealizedGain; + this.shares = shares; + } + +}
\ No newline at end of file |