diff options
| author | clarkohw <66530369+clarkohw@users.noreply.github.com> | 2021-04-19 03:02:50 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-19 03:02:50 -0400 |
| commit | 534d0cc5070287b221fa77f5dd564dd4544b5780 (patch) | |
| tree | 7a6c356cca51f4fa4b549c693e168f6fc61302b3 /src | |
| parent | 0466db8b9051cb6300f274f0bba480d1020c63cf (diff) | |
| parent | ab4621b0e245043ef60db13e509fdb0c5b4cfc64 (diff) | |
Merge pull request #13 from cs0320-2021/profit-testing
Profit testing
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java | 104 | ||||
| -rw-r--r-- | src/test/java/edu/brown/cs/student/ProfitCalculationTest.java | 25 |
2 files changed, 69 insertions, 60 deletions
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 15f31cc..0a19bcb 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 @@ -125,13 +125,12 @@ public class ProfitCalculation { } } else { //ignore sell orders for which we do not have buys for - if (buyHistoryMap.containsKey(ticker)) { - if (sellHistoryMap.containsKey(ticker)) { - sellHistoryMap.get(ticker).addLast(order); - } else { - sellHistoryMap.put(ticker, oneElement); - } + if (sellHistoryMap.containsKey(ticker)) { + sellHistoryMap.get(ticker).addLast(order); + } else { + sellHistoryMap.put(ticker, oneElement); } + } } @@ -150,39 +149,41 @@ public class ProfitCalculation { 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; + } - //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())) { - 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); + 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 { - sharesToSell -= buyBundle.getShares(); - sharesAtBundlePrice = buyBundle.getShares(); + break; } - realizedGain += sharesAtBundlePrice * (sell.getCost() - buyBundle.getCost()); - } else { - break; - } + } } } @@ -242,12 +243,15 @@ public class ProfitCalculation { if (currentStockPrices.containsKey(ticker)) { return currentStockPrices.get(ticker); } else { - String PRICE_URL = BASE_URL + "/last/stocks/" + ticker; - System.out.println("LOG: Making call to " + PRICE_URL + " in " + getClass()); + String url = "https://data.alpaca.markets/v1/bars/" + + "day?" + + "symbols=" + ticker + + "&start=" + startTime + + "&end=" + endTime; HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(PRICE_URL)).setHeader("APCA-API-KEY-ID", API_KEY) + .uri(URI.create(url)).setHeader("APCA-API-KEY-ID", API_KEY) .setHeader("APCA-API-SECRET-KEY", SECRET_KEY) .build(); @@ -255,18 +259,15 @@ public class ProfitCalculation { try { response = client.send(request, HttpResponse.BodyHandlers.ofString()); - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); + } catch (Exception e) { + System.out.println("ERROR: error getting price for profit calculation"); } - - JSONObject object = new JSONObject(response.body()); + JSONArray object = new JSONObject(response.body()).getJSONArray(ticker); try { - double price = object.getJSONObject("last").getDouble("price"); - currentStockPrices.put(ticker, price); - return price; + double endPrice = object.getJSONObject(object.length() - 1).getDouble("c"); + currentStockPrices.put(ticker, endPrice); + return endPrice; } catch (JSONException e) { currentStockPrices.put(ticker, -1.0); return -1.0; @@ -280,20 +281,15 @@ public class ProfitCalculation { if (!tablesFilled) { organizeOrders(); getRealizedGains(); - getUnrealizedGains(); tablesFilled = true; } double realizedGains = 0; - double unrealizedGains = 0; for (double value : realizedGainsMap.values()) { realizedGains += value; } - for (double value : unrealizedGainsMap.values()) { - unrealizedGains += value; - } - return unrealizedGains + realizedGains; + return realizedGains; } public List<StockHolding> getHoldingsList() { @@ -332,8 +328,8 @@ public class ProfitCalculation { String url = "https://data.alpaca.markets/v1/bars/" + "day?" + "symbols=SPY" - + "&start=" + startTime - + "&end=" + endTime; + + "&start=" + startTime.toString() + "T09:30:00-04:00" + + "&end=" + endTime.toString() + "T09:30:00-04:00"; HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() diff --git a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java index 256afed..a4baa53 100644 --- a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java +++ b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java @@ -12,6 +12,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.time.LocalDate; import java.util.List; import java.sql.Date; import java.time.Instant; @@ -35,7 +36,7 @@ public class ProfitCalculationTest { @Before public void setUp() { try { - db = new DatabaseQuerier("data/testing/test_trades.sqlite3"); + db = new DatabaseQuerier("data/lil_mock.sqlite3"); } catch (Exception e) { System.out.println("DBQuerier Test, couldn't connect to db???"); } @@ -47,16 +48,28 @@ public class ProfitCalculationTest { } @Test - public void testEmptyDB() { + public void testBasicTrades() { setUp(); ProfitCalculation profitCalculation = - new ProfitCalculation(DatabaseQuerier.getConn(), "CAKEBREAD STEVEN", new Date(1518010558000l), - new Date(1718010556000l)); + new ProfitCalculation(DatabaseQuerier.getConn(), "Don", new Date(1518010558000l), + new Date(1618698807000l)); + //price of GME at end time is 154.69 List<StockHolding> trade = profitCalculation.getHoldingsList(); - double gain = trade.get(0).getUnrealizedGain(); - assertEquals(294800.0, gain, .01); + //buy with no sell + assertEquals(trade.get(0).getUnrealizedGain(), 3842.25, .25); + assertEquals(trade.get(0).getRealizedGain(), 0, .01); + } + public void checkAPICalls() { + ProfitCalculation profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "Don", new Date(1618234200000l), + new Date(1618814264000l)); + + //check sp500 calculation. 411.28 to 417.30 + assertEquals(profitCalculation.compareToSP500(), .01464, .001); + + } }
\ No newline at end of file |
