Skip to content

Commit a23464e

Browse files
fix: various fixes and patches
1 parent d129eee commit a23464e

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.swofty</groupId>
88
<artifactId>stockmarkettester</artifactId>
9-
<version>1.1.1</version>
9+
<version>1.1.6</version>
1010

1111
<properties>
1212
<maven.compiler.source>21</maven.compiler.source>

src/main/java/net/swofty/stockmarkettester/Portfolio.java

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Portfolio {
1717
private final Map<String, List<Option>> options;
1818
private final Map<String, List<StopOrder>> stopOrders;
1919
private final Map<String, ShortOrder> shortPositions;
20+
private final Map<String, Double> cachedValues;
2021
private double cash;
2122
private double marginAvailable;
2223
private static final double MARGIN_REQUIREMENT = 0.5; // 50% margin requirement
@@ -27,6 +28,7 @@ public Portfolio(double initialCash) {
2728
this.options = new ConcurrentHashMap<>();
2829
this.stopOrders = new ConcurrentHashMap<>();
2930
this.shortPositions = new ConcurrentHashMap<>();
31+
this.cachedValues = new ConcurrentHashMap<>();
3032
this.cash = initialCash;
3133
this.marginAvailable = initialCash * 2; // 2x leverage
3234
}
@@ -159,16 +161,24 @@ public double getRealizedPnL(String ticker) {
159161
}
160162

161163
public double getTotalValue(Map<String, MarketDataPoint> currentPrices) {
164+
positions.forEach((k, v) -> {
165+
if (currentPrices.containsKey(k)) {
166+
cachedValues.put(k, currentPrices.get(k).close());
167+
}
168+
});
169+
162170
double longValue = positions.entrySet().stream()
163171
.mapToDouble(entry -> {
164172
MarketDataPoint price = currentPrices.get(entry.getKey());
173+
if (price == null) return cachedValues.get(entry.getKey());
165174
return entry.getValue().quantity() * price.close();
166175
})
167176
.sum();
168177

169178
double shortValue = shortPositions.entrySet().stream()
170179
.mapToDouble(entry -> {
171180
MarketDataPoint price = currentPrices.get(entry.getKey());
181+
if (price == null) return cachedValues.get(entry.getKey());
172182
return -entry.getValue().quantity() * price.close();
173183
})
174184
.sum();

src/main/java/net/swofty/stockmarkettester/data/HistoricalMarketService.java

+38-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public class HistoricalMarketService implements AutoCloseable {
1919
private final Map<String, HistoricalData> historicalCache;
2020
@Getter
2121
private final MarketDataProvider provider;
22+
2223
private final ExecutorService requestExecutor;
24+
private final ExecutorService fetchExecutor;
25+
2326
private final int maxRetries;
2427
private volatile boolean isInitialized = false;
2528
private final Object initLock = new Object();
@@ -30,6 +33,7 @@ public HistoricalMarketService(MarketDataProvider provider, int maxRetries, Path
3033
this.provider = provider;
3134
this.historicalCache = new ConcurrentHashMap<>();
3235
this.requestExecutor = Executors.newSingleThreadExecutor();
36+
this.fetchExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
3337
this.maxRetries = maxRetries;
3438
this.cacheDirectory = Optional.ofNullable(cacheDirectory);
3539

@@ -166,28 +170,39 @@ public CompletableFuture<Map<String, List<MarketDataPoint>>> fetchHistoricalData
166170
LocalDateTime start,
167171
LocalDateTime end) {
168172

169-
return CompletableFuture.supplyAsync(() -> {
170-
if (!isInitialized) {
171-
throw new IllegalStateException("HistoricalMarketService not initialized");
172-
}
173-
174-
Map<String, List<MarketDataPoint>> result = new ConcurrentHashMap<>();
173+
if (!isInitialized) {
174+
throw new IllegalStateException("HistoricalMarketService not initialized");
175+
}
175176

176-
for (String ticker : tickers) {
177-
HistoricalData cachedData = historicalCache.get(ticker);
178-
if (cachedData == null && cacheDirectory.isPresent()) {
179-
// Try loading from file cache as fallback if caching is enabled
180-
cachedData = loadFromCache(ticker, start, end);
181-
if (cachedData == null) {
182-
throw new IllegalStateException("No cached data for ticker: " + ticker);
177+
Map<String, List<MarketDataPoint>> result = new ConcurrentHashMap<>();
178+
List<CompletableFuture<Void>> futures = new ArrayList<>();
179+
180+
for (String ticker : tickers) {
181+
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
182+
try {
183+
HistoricalData cachedData = historicalCache.get(ticker);
184+
if (cachedData == null && cacheDirectory.isPresent()) {
185+
cachedData = loadFromCache(ticker, start, end);
186+
if (cachedData == null) {
187+
throw new IllegalStateException("No cached data for ticker: " + ticker);
188+
}
189+
historicalCache.put(ticker, cachedData);
183190
}
184-
historicalCache.put(ticker, cachedData);
191+
if (cachedData != null) {
192+
result.put(ticker, cachedData.getDataPoints(start, end));
193+
} else {
194+
throw new IllegalStateException("No data available for ticker: " + ticker);
195+
}
196+
} catch (Exception e) {
197+
throw new CompletionException(e);
185198
}
186-
result.put(ticker, cachedData.getDataPoints(start, end));
187-
}
199+
}, fetchExecutor);
188200

189-
return result;
190-
});
201+
futures.add(future);
202+
}
203+
204+
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
205+
.thenApply(v -> result);
191206
}
192207

193208
public void clearCache() {
@@ -211,12 +226,17 @@ public void clearCache() {
211226
@Override
212227
public void close() {
213228
requestExecutor.shutdown();
229+
fetchExecutor.shutdown();
214230
try {
215231
if (!requestExecutor.awaitTermination(30, TimeUnit.SECONDS)) {
216232
requestExecutor.shutdownNow();
217233
}
234+
if (!fetchExecutor.awaitTermination(30, TimeUnit.SECONDS)) {
235+
fetchExecutor.shutdownNow();
236+
}
218237
} catch (InterruptedException e) {
219238
requestExecutor.shutdownNow();
239+
fetchExecutor.shutdownNow();
220240
Thread.currentThread().interrupt();
221241
}
222242
}

0 commit comments

Comments
 (0)