AddedScoringTest.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package tests;
  2. import java.sql.PreparedStatement;
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import objects.League;
  7. import objects.SoccerMatch;
  8. import objects.Team;
  9. public class AddedScoringTest extends TestClass {
  10. private List<SoccerMatch> trainMatches;
  11. @Override public void runTest() {
  12. trainMatches = getTrainMatches();
  13. final League leagueInfo = getLeagueInfoById();
  14. final int betMarginHome = leagueInfo.getBetMarginHome();
  15. final int betMarginDraw = leagueInfo.getBetMarginDraw();
  16. final int betMarginAway = leagueInfo.getBetMarginAway();
  17. final int lookbackHome = leagueInfo.getLookbackHome();
  18. final int lookbackDraw = leagueInfo.getLookbackDraw();
  19. final int lookbackAway = leagueInfo.getLookbackAway();
  20. final int lookback = leagueInfo.getLookback();
  21. final int margin = leagueInfo.getBetMargin();
  22. final float betLevel = bettingLevel / 100f;
  23. float bank = startingBank;
  24. float prevBank = startingBank;
  25. float prevBetAmount = startingBank * betLevel;
  26. int betOnGameCount = 0;
  27. int prevBetOnGameCount = 0;
  28. float betAmount = startingBank * betLevel;
  29. int wins = 0;
  30. int prevWins = 0;
  31. final float betMarginDecimalHome = 1 + (leagueInfo.getBetMarginHome() / 100f);
  32. final float betMarginDecimalAway = 1 + (leagueInfo.getBetMarginAway() / 100f);
  33. final float betMarginDecimalDraw = 1 + (leagueInfo.getBetMarginDraw() / 100f);
  34. int betOnX = 0;
  35. int correctX = 0;
  36. float xBank = startingBank;
  37. for (final SoccerMatch soccerMatch : trainMatches) {
  38. final Team homeTeam = soccerMatch.getHomeTeam();
  39. final Team awayTeam = soccerMatch.getAwayTeam();
  40. final List<SoccerMatch> homeMatches = trainMatches.stream()
  41. .filter(p -> p.getGameDate().isBefore(soccerMatch.getGameDate())
  42. && p.getHomeTeam().getTeamId() == homeTeam.getTeamId())
  43. .limit(lookbackHome).toList();
  44. final List<SoccerMatch> awayMatches = trainMatches.stream()
  45. .filter(p -> p.getGameDate().isBefore(soccerMatch.getGameDate())
  46. && p.getAwayTeam().getTeamId() == awayTeam.getTeamId())
  47. .limit(lookbackAway).toList();
  48. final long hemmaVinster = homeMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
  49. final long hemmaLika = homeMatches.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
  50. final long hemmaForluster = homeMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
  51. final long bortaVinster = awayMatches.stream().filter(p -> p.getAwayScore() > p.getHomeScore()).count();
  52. final long bortaLika = awayMatches.stream().filter(p -> p.getAwayScore() == p.getHomeScore()).count();
  53. final long bortaForluster = awayMatches.stream().filter(p -> p.getAwayScore() < p.getHomeScore()).count();
  54. int homeGoals = 0;
  55. for (final SoccerMatch hm : homeMatches) {
  56. homeGoals += hm.getHomeScore() - hm.getAwayScore();
  57. }
  58. int awayGoals = 0;
  59. for (final SoccerMatch am : awayMatches) {
  60. awayGoals += am.getAwayScore() - am.getHomeScore();
  61. }
  62. final float homeWinPercent = (hemmaVinster + bortaForluster)
  63. / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
  64. final float awayWinPercent = (hemmaForluster + bortaVinster)
  65. / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
  66. final float drawWinPercent = (hemmaLika + bortaLika)
  67. / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
  68. prevBetAmount = prevBank * betLevel;
  69. // check if should bet on home Win before score added
  70. if ((100 / homeWinPercent) * betMarginDecimalHome <= soccerMatch.getOdds1()) {
  71. // Bet on Home Win
  72. prevBetOnGameCount++;
  73. prevBank = prevBank - prevBetAmount;
  74. if (soccerMatch.getHomeScore() > soccerMatch.getAwayScore()) {
  75. prevWins++;
  76. prevBank = prevBank + prevBetAmount * soccerMatch.getOdds1();
  77. }
  78. }
  79. // check if should bet on away Win
  80. if ((100 / awayWinPercent) * betMarginDecimalAway <= soccerMatch.getOdds2()) {
  81. // Bet on Away Win
  82. prevBetOnGameCount++;
  83. prevBank = prevBank - prevBetAmount;
  84. if (soccerMatch.getAwayScore() > soccerMatch.getHomeScore()) {
  85. prevWins++;
  86. prevBank = prevBank + prevBetAmount * soccerMatch.getOdds2();
  87. }
  88. }
  89. final int goalDiff = homeGoals - awayGoals;
  90. float homeWinPercentGoals = homeWinPercent + ((homeGoals / 2f) * 10);
  91. float awayWinPercentGoals = awayWinPercent + ((awayGoals / 2f) * 10);
  92. if (homeWinPercentGoals <= 0) {
  93. homeWinPercentGoals = 0.01f;
  94. }
  95. if (awayWinPercentGoals <= 0) {
  96. awayWinPercentGoals = 0.01f;
  97. }
  98. final float homeOdds = 100 / homeWinPercentGoals;
  99. final float awayOdds = 100 / awayWinPercentGoals;
  100. betAmount = bank * betLevel;
  101. // check if should bet on home Win
  102. if (homeOdds * betMarginDecimalHome <= soccerMatch.getOdds1()) {
  103. // Bet on Home Win
  104. betOnGameCount++;
  105. bank = bank - betAmount;
  106. if (soccerMatch.getHomeScore() > soccerMatch.getAwayScore()) {
  107. wins++;
  108. bank = bank + betAmount * soccerMatch.getOdds1();
  109. }
  110. }
  111. // check if should bet on away Win
  112. if (awayOdds * betMarginDecimalAway <= soccerMatch.getOdds2()) {
  113. // Bet on Home Win
  114. betOnGameCount++;
  115. bank = bank - betAmount;
  116. if (soccerMatch.getAwayScore() > soccerMatch.getHomeScore()) {
  117. wins++;
  118. bank = bank + betAmount * soccerMatch.getOdds2();
  119. }
  120. }
  121. if (goalDiff >= -1 && goalDiff <= 1) {
  122. System.out.println("Test betting on X odds " + soccerMatch.getOddsX() + " Actual result "
  123. + soccerMatch.getHomeScore() + " - " + soccerMatch.getAwayScore());
  124. betOnX++;
  125. final float xBetAmount = xBank * betLevel;
  126. xBank -= xBetAmount;
  127. if (soccerMatch.getHomeScore() == soccerMatch.getAwayScore()) {
  128. correctX++;
  129. xBank += xBetAmount * soccerMatch.getOddsX();
  130. }
  131. }
  132. }
  133. System.out.println(
  134. "Test ended with a bank of with score added " + bank + " and with no score added it was " + prevBank);
  135. System.out.println("Betted on " + betOnGameCount + " won " + wins + " out of " + trainMatches.size()
  136. + " prev bets " + prevBetOnGameCount + " and prev wins " + prevWins);
  137. System.out.println("Test with draw ended with betting on " + betOnX + " matches and " + correctX + " correct "
  138. + (correctX / (float) betOnX) + " bank " + xBank);
  139. }
  140. private ArrayList<SoccerMatch> getMatches() {
  141. final ArrayList<SoccerMatch> result = new ArrayList<>();
  142. final String sql = "SELECT res.*, " + "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
  143. + "FROM SoccerResults as res " + "Join Team as hTeam ON res.homeTeam = hTeam.id "
  144. + "Join Team as aTeam ON res.awayTeam = aTeam.id " + "WHERE "
  145. + "DATE(gameDate) > DATE_SUB(NOW(), INTERVAL 1 YEAR) AND " + "res.leagueId = ? "
  146. + "ORDER BY gameDate ASC";
  147. try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
  148. stat.setInt(1, leagueId);
  149. result.addAll(super.getMatches(stat));
  150. } catch (final SQLException e) {
  151. e.printStackTrace();
  152. }
  153. return result;
  154. }
  155. private List<SoccerMatch> getTrainMatches() {
  156. final ArrayList<SoccerMatch> result = new ArrayList<>();
  157. final String sql = "SELECT res.*, " + "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
  158. + "FROM SoccerResults as res " + "Join Team as hTeam ON res.homeTeam = hTeam.id "
  159. + "Join Team as aTeam ON res.awayTeam = aTeam.id " + "WHERE "
  160. + "DATE(gameDate) < DATE_SUB(NOW(), INTERVAL 1 YEAR) AND " + "res.leagueId = ? "
  161. + "ORDER BY gameDate DESC";
  162. try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
  163. stat.setInt(1, leagueId);
  164. result.addAll(super.getMatches(stat));
  165. } catch (final SQLException e) {
  166. e.printStackTrace();
  167. }
  168. return result;
  169. }
  170. }