diff options
author | clarkohw <clark_oh-willeke@brown.edu> | 2021-04-03 19:43:18 -0400 |
---|---|---|
committer | clarkohw <clark_oh-willeke@brown.edu> | 2021-04-03 19:43:18 -0400 |
commit | d2cb17744211b73080fa5ae476843e5acd52946b (patch) | |
tree | a42cee9b99f730819f9d7eebd1b2057d049ea01e | |
parent | 2402ca839b33d4c16c59e43575a5791e1c6e7630 (diff) |
before restructuring
-rw-r--r-- | data/mock_tradeTesting.sqlite3 | bin | 0 -> 45056 bytes | |||
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/Main.java | 9 | ||||
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/ProfitCalculation.java | 115 |
3 files changed, 95 insertions, 29 deletions
diff --git a/data/mock_tradeTesting.sqlite3 b/data/mock_tradeTesting.sqlite3 Binary files differnew file mode 100644 index 0000000..9521fef --- /dev/null +++ b/data/mock_tradeTesting.sqlite3 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 e6584f1..7838dd3 100644 --- a/src/main/java/edu/brown/cs/student/term/Main.java +++ b/src/main/java/edu/brown/cs/student/term/Main.java @@ -48,8 +48,13 @@ public final class Main { } - ProfitCalculation person = new ProfitCalculation(null, "Vincent", new Date(1615629591000L), new Date(1615507898000L)); - person.setConnection("./data/mock_tradeClarks.sqlite3"); + ProfitCalculation person = new ProfitCalculation(null, "Sophie", new Date(1615629591000L), new Date(1615507898000L)); + try { + person.setConnection("./data/mock_tradeClarks.sqlite3"); + } catch(Exception e) { + e.printStackTrace(); + } + person.calculateGains(); // HashMap<String, Command> commandHashMap = new HashMap<>(); 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 7c275d2..3975cef 100644 --- a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java +++ b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java @@ -10,12 +10,25 @@ import java.sql.Statement; import java.util.LinkedList; import java.util.List; import java.util.HashMap; +import java.util.Map; public class ProfitCalculation { private Connection conn; private String person; - Date startTime; - Date endTime; + private Date startTime; + private Date endTime; + + //map of stock to list of buy orders, first element in list is oldest + private Map<String, LinkedList<OrderTuple>> buyHistoryMap; + + //map of stock to list of buy orders, first element in list is oldest + private Map<String, LinkedList<OrderTuple>> sellHistoryMap; + + //map of stock to gains from sell orders + private Map<String, Double> realizedGainsMap; + + //map of stock to gains from increases in value of holdings + private Map<String, Double> unrealizedGainsMap; /** * constructor for ProfitCalculation. @@ -38,38 +51,66 @@ public class ProfitCalculation { * @return */ public double calculateGains() { - HashMap<String, LinkedList<BuyTuple>> buyHistory = new HashMap<String, LinkedList<BuyTuple>>(); + HashMap<String, LinkedList<OrderTuple>> buyHistory = new HashMap<String, LinkedList<OrderTuple>>(); HashMap<String, Double> perStockGain = new HashMap<String, Double>(); - double totalGain = 0; - //query for List<Trades> per person + double realizedGain = 0; + double netWorthGain = 0; + //get a list of trades for a person to consider try { PreparedStatement prep; - prep = conn.prepareStatement("SELECT * FROM \'users\' WHERE user_id= ?;"); - //prep.setString(1, id); + // TODO: add start and end time + prep = + conn.prepareStatement("SELECT * FROM \'trades\' WHERE holder_name= ? " + + "order by trade_timestamp asc;"); + prep.setString(1, this.person); + //prep.setString(2, "NVDA"); ResultSet rs = prep.executeQuery(); + while (rs.next()) { - boolean buy = true; - String ticker = "STOCK"; + String ticker = rs.getString("stock_name"); + //for buy orders, build up buy history - if (buy) { - int sharesBought = 99; - double cost = 420.0; + if (rs.getInt("is_buy") != 0) { + int sharesBought = rs.getInt("number_of_shares"); + double cost = rs.getDouble("trade_amount") / sharesBought; //add a buy to the end of a history for a given stock if (buyHistory.containsKey(ticker)) { - buyHistory.get(ticker).addLast(new BuyTuple(sharesBought, cost)); + buyHistory.get(ticker).addLast(new OrderTuple(sharesBought, cost)); } else { - LinkedList<BuyTuple> tickerHistory = new LinkedList<>(); - tickerHistory.add(new BuyTuple(sharesBought, cost)); + LinkedList<OrderTuple> tickerHistory = new LinkedList<>(); + tickerHistory.add(new OrderTuple(sharesBought, cost)); buyHistory.put(ticker, tickerHistory); } //for sell orders, calculate realized gains } else { -// int sharesSold = 99; -// if (buyHistory.containsKey(ticker)) { -// LinkedList<BuyTuple> = -// } + int sharesSold = rs.getInt("number_of_shares"); + double shareSellPrice = rs.getDouble("trade_amount") / sharesSold; + if (buyHistory.containsKey(ticker)) { + LinkedList<OrderTuple> stockHistory = buyHistory.get(ticker); + + //use FIFO sell off to realize gains + while (sharesSold > 0 && !stockHistory.isEmpty()) { + OrderTuple buyBundle = stockHistory.removeFirst(); + int sharesAtBundlePrice; + if (buyBundle.getShares() > sharesSold) { + sharesAtBundlePrice = sharesSold; + sharesSold = 0; + //add back the holdings that were not sold + stockHistory + .addFirst( + new OrderTuple(buyBundle.getShares() - sharesAtBundlePrice, + buyBundle.getCost())); + } else { + sharesSold -= buyBundle.getShares(); + sharesAtBundlePrice = buyBundle.getShares(); + } + realizedGain += sharesAtBundlePrice * (shareSellPrice - buyBundle.getCost()); + + } + + } } @@ -80,19 +121,23 @@ public class ProfitCalculation { } - //FOR EACH RESULT in ResultsSet... - //if buy: add to hashmap tuple entry (shares, cost) to hashmap - // - get cost of stock at time of purchase - //if sell: check if we hold this stock. If so use FIFO to sell - - return totalGain; + //calculate change in value of holdings + for (String ticker : buyHistory.keySet()) { + double currentPrice = getCurrentPrice(ticker); + LinkedList<OrderTuple> stockHistory = buyHistory.get(ticker); + for (OrderTuple order : stockHistory) { + netWorthGain += order.getShares() * (currentPrice - order.getCost()); + } + } + System.out.println(realizedGain + netWorthGain); + return realizedGain + netWorthGain; } - private class BuyTuple { + private class OrderTuple { private int shares; private double cost; - private BuyTuple(int shares, double cost) { + private OrderTuple(int shares, double cost) { this.shares = shares; this.cost = cost; } @@ -106,6 +151,15 @@ public class ProfitCalculation { } } + public double getCurrentPrice(String ticker) { + return 100.0; + } + + public double getPriceAtTime(String ticker, Date sellTime) { + return 100.0; + //shuold be able ot use amount + } + public void setConnection(String filename) throws SQLException, ClassNotFoundException { // Initialize the database connection, turn foreign keys on @@ -118,4 +172,11 @@ public class ProfitCalculation { } + + //TODO: HELPER METHODS + //organizeOrders() + //getRealizedGains() + //getUnrealizedGains() + + } |