aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorclarkohw <clark_oh-willeke@brown.edu>2021-04-19 02:59:57 -0400
committerclarkohw <clark_oh-willeke@brown.edu>2021-04-19 02:59:57 -0400
commit8e76bf15c53c540a72a273ec560a624dfb7e11df (patch)
tree41c2679481ba3ff1e7bafda71f702e0c09fe69d8 /src
parent26827e5631cc7c0d5d24fa5459a6abbe9e4c60a5 (diff)
efficiency for profit calc + tests
Diffstat (limited to 'src')
-rw-r--r--src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java33
-rw-r--r--src/test/java/edu/brown/cs/student/ProfitCalculationTest.java10
2 files changed, 23 insertions, 20 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 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<StockHolding> 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<StockHolding> 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);
}