CopyTwitterStrategy implements TweetHarvester<T> { @SuppressWarnings("unchecked") TradingDecision<T> analyseHistoricClosedTradesForInstrument(Collection<CloseFXTradeTweet<T>> closedTrades, NewFXTradeTweet<T> newTrade) { int lossCtr = 0; int profitCtr = 0; for (CloseFXTradeTweet<T> closedTrade : closedTrades) { if (closedTrade.getProfit() <= 0) { lossCtr++; } else { profitCtr++; } } TradingSignal signal = TradingSignal.NONE; if ((lossCtr != 0 || profitCtr != 0) && closedTrades.size() >= MIN_HISTORIC_TWEETS) { double profitAccuracy = profitCtr / ((profitCtr + lossCtr) * 1.0); double lossAccuracy = 1.0 - profitAccuracy; if (profitAccuracy >= ACCURACY_DESIRED) { signal = newTrade.getAction(); return new TradingDecision<T>(newTrade.getInstrument(), signal, newTrade.getTakeProfit(), newTrade.getStopLoss(), newTrade.getPrice(), TradingDecision.SRCDECISION.SOCIAL_MEDIA); } else if (lossAccuracy >= ACCURACY_DESIRED) { signal = newTrade.getAction().flip(); double price = newTrade.getPrice(); if (price == 0.0) { Map<TradeableInstrument<T>, Price<T>> priceMap = this.currentPriceInfoProvider .getCurrentPricesForInstruments(Lists.newArrayList(newTrade.getInstrument())); Price<T> instrumentPrice = priceMap.get(newTrade.getInstrument()); price = signal == TradingSignal.LONG ? instrumentPrice.getAskPrice() : instrumentPrice.getBidPrice(); } final double takeProfit = newTrade.getTakeProfit() != 0 ? price + (price - newTrade.getTakeProfit()) : newTrade.getTakeProfit(); final double stopLoss = newTrade.getStopLoss() != 0.0 ? price + (price - newTrade.getStopLoss()) : newTrade.getStopLoss(); return new TradingDecision<T>(newTrade.getInstrument(), signal, takeProfit, stopLoss, price, TradingDecision.SRCDECISION.SOCIAL_MEDIA); } } return new TradingDecision<T>(newTrade.getInstrument(), signal); }  @PostConstruct void init(); synchronized void harvestAndTrade(); @Override Collection<NewFXTradeTweet<T>> harvestNewTradeTweets(String userId); @Override Collection<CloseFXTradeTweet<T>> harvestHistoricTradeTweets(String userId,
			TradeableInstrument<T> instrument);  }
@Test public void analyseHistoricTweetsTest() { CopyTwitterStrategy<String> copyTwitterStrategy = new CopyTwitterStrategy<String>(); double[] profits = { 13.45, 11.54, -8.9, 12.09, 21.67, 2.76, -3.5, 11.67, 9.21 }; Collection<CloseFXTradeTweet<String>> closedTrades = createClosedTrades(profits); double takeProfit = 0.9812; double stopLoss = 0.949; double price = 0.9735; NewFXTradeTweet<String> newTrade = mock(NewFXTradeTweet.class); TradeableInstrument<String> usdchf = new TradeableInstrument<String>("USD_CHF"); when(newTrade.getAction()).thenReturn(TradingSignal.LONG); when(newTrade.getInstrument()).thenReturn(usdchf); when(newTrade.getTakeProfit()).thenReturn(takeProfit); when(newTrade.getStopLoss()).thenReturn(stopLoss); when(newTrade.getPrice()).thenReturn(price); TradingDecision<String> tradingDecision = copyTwitterStrategy.analyseHistoricClosedTradesForInstrument( closedTrades, newTrade); assertEquals(TradingSignal.LONG, tradingDecision.getSignal()); assertEquals(TradingDecision.SRCDECISION.SOCIAL_MEDIA, tradingDecision.getTradeSource()); assertEquals(usdchf, tradingDecision.getInstrument()); assertEquals(takeProfit, tradingDecision.getTakeProfitPrice(), ZERO); assertEquals(stopLoss, tradingDecision.getStopLossPrice(), ZERO); assertEquals(price, tradingDecision.getLimitPrice(), ZERO); double profits2[] = { -23.9, -10, -56.0, -12.9, -11.5, 8.5 }; closedTrades = createClosedTrades(profits2); tradingDecision = copyTwitterStrategy.analyseHistoricClosedTradesForInstrument(closedTrades, newTrade); assertEquals(TradingSignal.SHORT, tradingDecision.getSignal()); assertEquals(TradingDecision.SRCDECISION.SOCIAL_MEDIA, tradingDecision.getTradeSource()); assertEquals(usdchf, tradingDecision.getInstrument()); assertEquals(0.9658, tradingDecision.getTakeProfitPrice(), TradingAppTestConstants.precision); assertEquals(0.998, tradingDecision.getStopLossPrice(), TradingAppTestConstants.precision); assertEquals(price, tradingDecision.getLimitPrice(), ZERO); stopLoss = 1.1125; takeProfit = 1.0705; price = 1.0895; TradeableInstrument<String> eurchf = new TradeableInstrument<String>("EUR_CHF"); NewFXTradeTweet<String> newTrade2 = mock(NewFXTradeTweet.class); when(newTrade2.getAction()).thenReturn(TradingSignal.SHORT); when(newTrade2.getInstrument()).thenReturn(eurchf); when(newTrade2.getTakeProfit()).thenReturn(takeProfit); when(newTrade2.getStopLoss()).thenReturn(stopLoss); when(newTrade2.getPrice()).thenReturn(price); tradingDecision = copyTwitterStrategy.analyseHistoricClosedTradesForInstrument(closedTrades, newTrade2); assertEquals(TradingSignal.LONG, tradingDecision.getSignal()); assertEquals(TradingDecision.SRCDECISION.SOCIAL_MEDIA, tradingDecision.getTradeSource()); assertEquals(eurchf, tradingDecision.getInstrument()); assertEquals(1.1085, tradingDecision.getTakeProfitPrice(), TradingAppTestConstants.precision); assertEquals(1.0665, tradingDecision.getStopLossPrice(), TradingAppTestConstants.precision); assertEquals(price, tradingDecision.getLimitPrice(), ZERO); stopLoss = 15.0545; takeProfit = 15.0985; price = 0.0; TradeableInstrument<String> usdzar = new TradeableInstrument<>("USD_ZAR"); NewFXTradeTweet<String> newTrade3 = mock(NewFXTradeTweet.class); when(newTrade3.getAction()).thenReturn(TradingSignal.LONG); when(newTrade3.getInstrument()).thenReturn(usdzar); when(newTrade3.getTakeProfit()).thenReturn(takeProfit); when(newTrade3.getStopLoss()).thenReturn(stopLoss); when(newTrade3.getPrice()).thenReturn(price); closedTrades = createClosedTrades(profits2); CurrentPriceInfoProvider<String> currPriceInfoProvider = mock(CurrentPriceInfoProvider.class); copyTwitterStrategy.currentPriceInfoProvider = currPriceInfoProvider; Map<TradeableInstrument<String>, Price<String>> priceMap = Maps.newHashMap(); Price<String> usdzarPrice = new Price<>(usdzar, 15.0715, 15.0727, DateTime.now()); priceMap.put(usdzar, usdzarPrice); when(currPriceInfoProvider.getCurrentPricesForInstruments(eq(Lists.newArrayList(usdzar)))).thenReturn(priceMap); tradingDecision = copyTwitterStrategy.analyseHistoricClosedTradesForInstrument(closedTrades, newTrade3); assertEquals(TradingSignal.SHORT, tradingDecision.getSignal()); assertEquals(TradingDecision.SRCDECISION.SOCIAL_MEDIA, tradingDecision.getTradeSource()); assertEquals(usdzar, tradingDecision.getInstrument()); assertEquals(15.0445, tradingDecision.getTakeProfitPrice(), TradingAppTestConstants.precision); assertEquals(15.0885, tradingDecision.getStopLossPrice(), TradingAppTestConstants.precision); assertEquals(15.0715, tradingDecision.getLimitPrice(), ZERO); }