From 61ad5896d35c1d49a4a5d9c1d8d87f53907f7da7 Mon Sep 17 00:00:00 2001 From: clarkohw Date: Sat, 17 Apr 2021 15:11:05 -0400 Subject: fixed case were date of buy/sell is equal --- .../cs/student/term/profit/ProfitCalculation.java | 69 +++++++++++----------- 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'src/main/java') 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 0fe35d9..b4bf708 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 @@ -110,13 +110,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); } + } } @@ -135,39 +134,41 @@ public class ProfitCalculation { LinkedList sells = sellHistoryMap.get(ticker); LinkedList 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; - } + } } } -- cgit v1.2.3-70-g09d2 From 26827e5631cc7c0d5d24fa5459a6abbe9e4c60a5 Mon Sep 17 00:00:00 2001 From: clarkohw Date: Mon, 19 Apr 2021 02:20:36 -0400 Subject: simple test --- .../brown/cs/student/term/profit/ProfitCalculation.java | 2 +- .../java/edu/brown/cs/student/ProfitCalculationTest.java | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'src/main/java') 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 b4bf708..0b86172 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 @@ -246,7 +246,7 @@ public class ProfitCalculation { e.printStackTrace(); } - + System.out.println(response.body()); JSONObject object = new JSONObject(response.body()); try { double price = object.getJSONObject("last").getDouble("price"); diff --git a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java index 974fff0..d1fc9be 100644 --- a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java +++ b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java @@ -35,7 +35,7 @@ public class ProfitCalculationTest { @Before public void setUp() { try { - db = new DatabaseQuerier("data/trades.sqlite3"); + db = new DatabaseQuerier("data/lil_mock.sqlite3"); } catch (Exception e) { System.out.println("DBQuerier Test, couldn't connect to db???"); } @@ -47,17 +47,17 @@ 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 trade = profitCalculation.getHoldingsList(); - double gain = trade.get(0).getUnrealizedGain(); - assertEquals(294800.0, gain, .01); + //buy with no sell + assertEquals(3842.25, trade.get(0).getUnrealizedGain(),6); } - } \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 8e76bf15c53c540a72a273ec560a624dfb7e11df Mon Sep 17 00:00:00 2001 From: clarkohw Date: Mon, 19 Apr 2021 02:59:57 -0400 Subject: efficiency for profit calc + tests --- .../cs/student/term/profit/ProfitCalculation.java | 33 ++++++-------- .../brown/cs/student/ProfitCalculationTest.java | 10 ++++- tatus | 52 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 tatus (limited to 'src/main/java') 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 0b86172..fe12612 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 @@ -228,11 +228,15 @@ public class ProfitCalculation { if (currentStockPrices.containsKey(ticker)) { return currentStockPrices.get(ticker); } else { - String PRICE_URL = BASE_URL + "/last/stocks/" + ticker; + 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(); @@ -240,18 +244,14 @@ 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"); } - System.out.println(response.body()); - 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"); + return endPrice; } catch (JSONException e) { currentStockPrices.put(ticker, -1.0); return -1.0; @@ -265,20 +265,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 getHoldingsList() { @@ -317,8 +312,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 d1fc9be..f435c7c 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; @@ -55,7 +56,14 @@ public class ProfitCalculationTest { //price of GME at end time is 154.69 List trade = profitCalculation.getHoldingsList(); //buy with no sell - assertEquals(3842.25, trade.get(0).getUnrealizedGain(),6); + assertEquals(trade.get(0).getUnrealizedGain(), 3842.25,.25); + assertEquals(trade.get(0).getRealizedGain(), 0, .01); + + + profitCalculation = new ProfitCalculation(DatabaseQuerier.getConn(), "Don", new Date(1618234200000l), + new Date(1618814264000l)); + + assertEquals(profitCalculation.compareToSP500(), .01464, .001); } diff --git a/tatus b/tatus new file mode 100644 index 0000000..29e3798 --- /dev/null +++ b/tatus @@ -0,0 +1,52 @@ +diff --git a/data/mock_tradeClarks.sqlite3 b/data/mock_tradeClarks.sqlite3 +index 980c539..e816ee8 100644 +Binary files a/data/mock_tradeClarks.sqlite3 and b/data/mock_tradeClarks.sqlite3 differ +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 31bf7a3..c4a7814 100644 +--- a/src/main/java/edu/brown/cs/student/term/Main.java ++++ b/src/main/java/edu/brown/cs/student/term/Main.java +@@ -47,12 +47,21 @@ public final class Main { + //do a gui type thing + //runSparkServer((int) options.valueOf("port")); + } +-  +- HashMap commandHashMap = new HashMap<>(); +- commandHashMap.put("setup", new SetupCommand()); +- /** add commands to map here! */ +- REPL repl = new REPL(commandHashMap); +- repl.runREPL(); ++ ++ ProfitCalculation person = new ProfitCalculation(null, "Vincent", new Date(1515629591000L), new Date(1715507898000L)); ++ try { ++ person.setConnection("./data/mock_tradeClarks.sqlite3"); ++ } catch(Exception e) { ++ e.printStackTrace(); ++ } ++ ++ person.calculateGains(); ++ ++// HashMap commandHashMap = new HashMap<>(); ++// commandHashMap.put("setup", new SetupCommand()); ++// /** add commands to map here! */ ++// REPL repl = new REPL(commandHashMap); ++// repl.runREPL(); + } +  +  +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 aa1bc09..94f87f7 100644 +--- a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java ++++ b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java +@@ -265,10 +265,10 @@ public class ProfitCalculation { + double totalGains = unrealizedGains + realizedGains; +  + System.out.println("Money In: " + moneyInput); ++ System.out.println("SP500 on money In: " + (moneyInput * compareToSP500())); + System.out.println("Money Out: " + (moneyInput + totalGains)); +- System.out.println("NASDAQ on money In: " + (moneyInput * compareToSP500())); + System.out.println( +- "Total: " + totalGains + "| unrealized: " + unrealizedGains + " | realized: " + ++ "Total gain: " + totalGains + "| unrealized: " + unrealizedGains + " | realized: " + + realizedGains); + } +  -- cgit v1.2.3-70-g09d2 From 0508b076ac948a11bde14cfa9f5261796d890ef2 Mon Sep 17 00:00:00 2001 From: clarkohw Date: Mon, 19 Apr 2021 03:01:48 -0400 Subject: caching of prices profit --- .../edu/brown/cs/student/term/profit/ProfitCalculation.java | 1 + .../java/edu/brown/cs/student/ProfitCalculationTest.java | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'src/main/java') 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 fe12612..77c1c3a 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 @@ -251,6 +251,7 @@ public class ProfitCalculation { JSONArray object = new JSONObject(response.body()).getJSONArray(ticker); try { double endPrice = object.getJSONObject(object.length() - 1).getDouble("c"); + currentStockPrices.put(ticker, endPrice); return endPrice; } catch (JSONException e) { currentStockPrices.put(ticker, -1.0); diff --git a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java index f435c7c..a4baa53 100644 --- a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java +++ b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java @@ -56,13 +56,17 @@ public class ProfitCalculationTest { //price of GME at end time is 154.69 List trade = profitCalculation.getHoldingsList(); //buy with no sell - assertEquals(trade.get(0).getUnrealizedGain(), 3842.25,.25); + 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)); - 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); } -- cgit v1.2.3-70-g09d2