Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public TreeSet<StockStatistic> computeCorrelation(StockTicker correlationForTicker, int period, CorrelationType correlationType) {
- log.debug("Finding most correlated stocks for: " + correlationForTicker);
- if (period != 10 && period != 30 && period != 60 && period != 90) {
- throw new IllegalArgumentException("Wrong period");
- }
- Objects.requireNonNull(correlationForTicker);
- Objects.requireNonNull(correlationType);
- step = 0;
- isComputing = true;
- Correlator correlator = getCorrelatorImpl(correlationType);
- TreeSet<StockStatistic> correlationTreeSet = new TreeSet<StockStatistic>();
- final EnumSet<StockTicker> tickersToScan = complementOf(EnumSet.of(correlationForTicker));
- final double[] sourceClosePrices = getClosePrices(getContent(correlationForTicker, period));
- ExecutorService executor = Executors.newFixedThreadPool(2);
- for (StockTicker ticker : tickersToScan) {
- executor.submit(() -> {
- double[] targetClosePrices = getClosePrices(getContent(ticker, period));
- double correlation = correlator.correlate(sourceClosePrices, targetClosePrices);
- StockStatistic statistic = new StockStatistic(correlation, ticker);
- correlationTreeSet.add(statistic);
- increaseStep();
- });
- }
- executor.shutdown();
- try {
- executor.awaitTermination(1, TimeUnit.MINUTES);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- this.step = 0;
- this.isComputing = false;
- return correlationTreeSet;
- }
Advertisement
Add Comment
Please, Sign In to add comment