Guest User

Untitled

a guest
Aug 22nd, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1.     public TreeSet<StockStatistic> computeCorrelation(StockTicker correlationForTicker, int period, CorrelationType correlationType) {
  2.         log.debug("Finding most correlated stocks for: " + correlationForTicker);
  3.         if (period != 10 && period != 30 && period != 60 && period != 90) {
  4.             throw new IllegalArgumentException("Wrong period");
  5.         }
  6.         Objects.requireNonNull(correlationForTicker);
  7.         Objects.requireNonNull(correlationType);
  8.        
  9.         step = 0;
  10.         isComputing = true;
  11.  
  12.         Correlator correlator = getCorrelatorImpl(correlationType);
  13.         TreeSet<StockStatistic> correlationTreeSet = new TreeSet<StockStatistic>();
  14.         final EnumSet<StockTicker> tickersToScan = complementOf(EnumSet.of(correlationForTicker));
  15.         final double[] sourceClosePrices = getClosePrices(getContent(correlationForTicker, period));
  16.  
  17.         ExecutorService executor = Executors.newFixedThreadPool(2);
  18.  
  19.         for (StockTicker ticker : tickersToScan) {
  20.             executor.submit(() -> {
  21.                 double[] targetClosePrices = getClosePrices(getContent(ticker, period));
  22.                 double correlation = correlator.correlate(sourceClosePrices, targetClosePrices);
  23.                 StockStatistic statistic = new StockStatistic(correlation, ticker);
  24.                 correlationTreeSet.add(statistic);
  25.                 increaseStep();
  26.             });
  27.         }
  28.  
  29.         executor.shutdown();
  30.  
  31.         try {
  32.             executor.awaitTermination(1, TimeUnit.MINUTES);
  33.         } catch (InterruptedException e) {
  34.             e.printStackTrace();
  35.         }
  36.  
  37.         this.step = 0;
  38.         this.isComputing = false;
  39.         return correlationTreeSet;
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment