diff options
3 files changed, 85 insertions, 15 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 55efbce..0a70b91 100644 --- a/src/main/java/edu/brown/cs/student/term/Main.java +++ b/src/main/java/edu/brown/cs/student/term/Main.java @@ -3,6 +3,7 @@ package edu.brown.cs.student.term; 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.StockHolding; import edu.brown.cs.student.term.repl.Command; import edu.brown.cs.student.term.repl.REPL; import edu.brown.cs.student.term.repl.commands.LoadCommand; @@ -193,16 +194,35 @@ 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"; } + + 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/ProfitCalculation.java b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java index 19b439e..28f7c79 100644 --- a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java +++ b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java @@ -1,6 +1,8 @@ package edu.brown.cs.student.term; +import edu.brown.cs.student.term.profit.StockHolding; +import org.eclipse.jetty.util.DecoratedObjectFactory; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -20,6 +22,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 +30,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 +72,7 @@ public class ProfitCalculation { realizedGainsMap = new HashMap<>(); unrealizedGainsMap = new HashMap<>(); currentStockPrices = new HashMap<>(); + tablesFilled = false; } /** @@ -257,9 +262,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 +279,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 +343,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); } @@ -341,6 +371,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 |