package tests; import java.sql.PreparedStatement; import java.sql.SQLException; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import objects.League; import objects.SoccerMatch; public class HomeTests2 extends TestClass { private League leagueInfo; ArrayList possitiveResults = new ArrayList<>(); @Override public void setup(String date, float startingBank, float bettingLevel, Float betMargin, int lookBack, int sportId, Integer countryId, Integer leagueId) { super.setup(date, startingBank, bettingLevel, betMargin, lookBack, sportId, countryId, leagueId); leagueInfo = getLeagueInfoById(); } @Override public void runTest() { final ArrayList matches = getMatches(); final LocalDateTime startDate = matches.get(0).getGameDate(); final LocalDateTime endDate = matches.get(0).getGameDate().plusYears(1); List matchesFiltered = matches.stream() .filter(p -> (p.getGameDate().isAfter(matches.get(0).getGameDate()) || p.getGameDate().equals(matches.get(0).getGameDate())) && p.getGameDate().isBefore(matches.get(0).getGameDate().plusYears(1))) .collect(Collectors.toList()); final float betLevel = bettingLevel / 100f; final LocalDateTime currentDate = matches.get(0).getGameDate(); LocalDate localDate = currentDate.toLocalDate(); float betAmount = startingBank * betLevel; int betOnGameCount = 0; int wins = 0; final float bestBankResult = startingBank; final int bestBetMargin = 0; final int bestLookBack = 0; System.out.println("Home Test 2"); int i = 0; while (matchesFiltered.size() > 0) { i++; for (int lookBack = 4; lookBack < 25; lookBack++) { for (int betMargin = 1; betMargin < 35; betMargin++) { final float betMarginDecimal = 1 + (betMargin / (float) 100); float bank = startingBank; betOnGameCount = 0; wins = 0; for (final SoccerMatch match : matchesFiltered) { if (match.getHomeScore() < 0 || match.getAwayScore() < 0) { continue; } final List homeMatches = matchesFiltered.stream().filter( p -> p.getGameDate().isBefore(match.getGameDate()) && p.getHomeTeam().getTeamId() == match.getHomeTeam().getTeamId()) .limit(lookBack).collect(Collectors.toList()); final List awayMatches = matchesFiltered.stream().filter( p -> p.getGameDate().isBefore(match.getGameDate()) && p.getAwayTeam().getTeamId() == match.getAwayTeam().getTeamId()) .limit(lookBack).collect(Collectors.toList()); final long hemmaVinster = homeMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count(); final long hemmaForluster = homeMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count(); final long bortaVinster = awayMatches.stream().filter(p -> p.getAwayScore() > p.getHomeScore()).count(); final long bortaForluster = awayMatches.stream().filter(p -> p.getAwayScore() < p.getHomeScore()).count(); final float homeWinPercent = (hemmaVinster + bortaForluster) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100; final float homeOdds = 100 / homeWinPercent; if (localDate.isBefore(match.getGameDate().toLocalDate())) { betAmount = bank * betLevel; localDate = match.getGameDate().toLocalDate(); } if (homeOdds * betMarginDecimal <= match.getOdds1()) { betOnGameCount++; bank = bank - betAmount; if (match.getHomeScore() > match.getAwayScore()) { wins++; bank = bank + betAmount * match.getOdds1(); } } } if (bank > startingBank) { final int lookBack2 = lookBack; final int betMargin2 = betMargin; final java.util.Optional found = possitiveResults.stream() .filter(r -> r.getLookback() == lookBack2 && r.getMargin() == betMargin2).findFirst(); if (found.isPresent()) { final results results = found.get(); results.addCount(); results.addBank(bank); } else { possitiveResults.add(new results(lookBack, betMargin, bank)); } } } } final LocalDateTime startDate2 = matchesFiltered.get(0).getGameDate().plusYears(1); final LocalDateTime endDate2 = startDate2.plusYears(1); matchesFiltered.clear(); matchesFiltered = matches.stream() .filter(p -> (p.getGameDate().isAfter(startDate2) || p.getGameDate().equals(startDate2)) && p.getGameDate().isBefore(endDate2)) .collect(Collectors.toList()); } Collections.sort(possitiveResults); for (final results res : possitiveResults) { System.out.println("Lookback " + res.getLookback() + " and margin " + res.getMargin() + " At count " + res.getCount() + " of " + i + " with bank: " + res.getBank()); } } public ArrayList getMatches() { final ArrayList result = new ArrayList<>(); final String sql = "SELECT res.*, " + "hTeam.name as homeTeamName, aTeam.name as awayTeamName " + "FROM SoccerResults as res " + "Join Team as hTeam ON res.homeTeam = hTeam.id " + "Join Team as aTeam ON res.awayTeam = aTeam.id " + "WHERE " + "res.leagueId = ? " + "ORDER BY gameDate ASC"; try { final PreparedStatement stat = getConnection().prepareStatement(sql); stat.setInt(1, leagueId); result.addAll(getMatches(stat)); } catch (final SQLException e) { e.printStackTrace(); } return result; } }