HomeTests2.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package tests;
  2. import java.sql.PreparedStatement;
  3. import java.sql.SQLException;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.List;
  9. import java.util.stream.Collectors;
  10. import objects.League;
  11. import objects.SoccerMatch;
  12. public class HomeTests2 extends TestClass {
  13. private League leagueInfo;
  14. ArrayList<results> possitiveResults = new ArrayList<>();
  15. @Override public void setup(String date, float startingBank, float bettingLevel, Float betMargin, int lookBack, int sportId, Integer countryId,
  16. Integer leagueId) {
  17. super.setup(date, startingBank, bettingLevel, betMargin, lookBack, sportId, countryId, leagueId);
  18. leagueInfo = getLeagueInfoById();
  19. }
  20. @Override public void runTest() {
  21. final ArrayList<SoccerMatch> matches = getMatches();
  22. final LocalDateTime startDate = matches.get(0).getGameDate();
  23. final LocalDateTime endDate = matches.get(0).getGameDate().plusYears(1);
  24. List<SoccerMatch> matchesFiltered = matches.stream()
  25. .filter(p -> (p.getGameDate().isAfter(matches.get(0).getGameDate()) || p.getGameDate().equals(matches.get(0).getGameDate()))
  26. && p.getGameDate().isBefore(matches.get(0).getGameDate().plusYears(1)))
  27. .collect(Collectors.toList());
  28. final float betLevel = bettingLevel / 100f;
  29. final LocalDateTime currentDate = matches.get(0).getGameDate();
  30. LocalDate localDate = currentDate.toLocalDate();
  31. float betAmount = startingBank * betLevel;
  32. int betOnGameCount = 0;
  33. int wins = 0;
  34. final float bestBankResult = startingBank;
  35. final int bestBetMargin = 0;
  36. final int bestLookBack = 0;
  37. System.out.println("Home Test 2");
  38. int i = 0;
  39. while (matchesFiltered.size() > 0) {
  40. i++;
  41. for (int lookBack = 4; lookBack < 25; lookBack++) {
  42. for (int betMargin = 1; betMargin < 35; betMargin++) {
  43. final float betMarginDecimal = 1 + (betMargin / (float) 100);
  44. float bank = startingBank;
  45. betOnGameCount = 0;
  46. wins = 0;
  47. for (final SoccerMatch match : matchesFiltered) {
  48. if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
  49. continue;
  50. }
  51. final List<SoccerMatch> homeMatches = matchesFiltered.stream().filter(
  52. p -> p.getGameDate().isBefore(match.getGameDate()) && p.getHomeTeam().getTeamId() == match.getHomeTeam().getTeamId())
  53. .limit(lookBack).collect(Collectors.toList());
  54. final List<SoccerMatch> awayMatches = matchesFiltered.stream().filter(
  55. p -> p.getGameDate().isBefore(match.getGameDate()) && p.getAwayTeam().getTeamId() == match.getAwayTeam().getTeamId())
  56. .limit(lookBack).collect(Collectors.toList());
  57. final long hemmaVinster = homeMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
  58. final long hemmaForluster = homeMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
  59. final long bortaVinster = awayMatches.stream().filter(p -> p.getAwayScore() > p.getHomeScore()).count();
  60. final long bortaForluster = awayMatches.stream().filter(p -> p.getAwayScore() < p.getHomeScore()).count();
  61. final float homeWinPercent = (hemmaVinster + bortaForluster) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
  62. final float homeOdds = 100 / homeWinPercent;
  63. if (localDate.isBefore(match.getGameDate().toLocalDate())) {
  64. betAmount = bank * betLevel;
  65. localDate = match.getGameDate().toLocalDate();
  66. }
  67. if (homeOdds * betMarginDecimal <= match.getOdds1()) {
  68. betOnGameCount++;
  69. bank = bank - betAmount;
  70. if (match.getHomeScore() > match.getAwayScore()) {
  71. wins++;
  72. bank = bank + betAmount * match.getOdds1();
  73. }
  74. }
  75. }
  76. if (bank > startingBank) {
  77. final int lookBack2 = lookBack;
  78. final int betMargin2 = betMargin;
  79. final java.util.Optional<results> found = possitiveResults.stream()
  80. .filter(r -> r.getLookback() == lookBack2 && r.getMargin() == betMargin2).findFirst();
  81. if (found.isPresent()) {
  82. final results results = found.get();
  83. results.addCount();
  84. results.addBank(bank);
  85. } else {
  86. possitiveResults.add(new results(lookBack, betMargin, bank));
  87. }
  88. }
  89. }
  90. }
  91. final LocalDateTime startDate2 = matchesFiltered.get(0).getGameDate().plusYears(1);
  92. final LocalDateTime endDate2 = startDate2.plusYears(1);
  93. matchesFiltered.clear();
  94. matchesFiltered = matches.stream()
  95. .filter(p -> (p.getGameDate().isAfter(startDate2) || p.getGameDate().equals(startDate2)) && p.getGameDate().isBefore(endDate2))
  96. .collect(Collectors.toList());
  97. }
  98. Collections.sort(possitiveResults);
  99. for (final results res : possitiveResults) {
  100. System.out.println("Lookback " + res.getLookback() + " and margin " + res.getMargin() + " At count " + res.getCount() + " of " + i
  101. + " with bank: " + res.getBank());
  102. }
  103. }
  104. public ArrayList<SoccerMatch> getMatches() {
  105. final ArrayList<SoccerMatch> result = new ArrayList<>();
  106. final String sql = "SELECT res.*, " + "hTeam.name as homeTeamName, aTeam.name as awayTeamName " + "FROM SoccerResults as res "
  107. + "Join Team as hTeam ON res.homeTeam = hTeam.id " + "Join Team as aTeam ON res.awayTeam = aTeam.id " + "WHERE " + "res.leagueId = ? "
  108. + "ORDER BY gameDate ASC";
  109. try {
  110. final PreparedStatement stat = getConnection().prepareStatement(sql);
  111. stat.setInt(1, leagueId);
  112. result.addAll(getMatches(stat));
  113. } catch (final SQLException e) {
  114. e.printStackTrace();
  115. }
  116. return result;
  117. }
  118. }