LastResultsTest.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package tests;
  2. import java.time.LocalDate;
  3. import java.time.LocalDateTime;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.ArrayList;
  6. import data.GuiMysql;
  7. import objects.SoccerMatch;
  8. import objects.TeamResults;
  9. public class LastResultsTest extends TestClass {
  10. float bank;
  11. float betLevel;
  12. float betMarginDecimal;
  13. @Override
  14. public void runTest() {
  15. bank = startingBank;
  16. betLevel = bettingLevel / 100;
  17. betMarginDecimal = 1 + (betMargin / 100);
  18. final ArrayList<SoccerMatch> matches = getMatches(leagueId);
  19. final int totalMatchCount = matches.size();
  20. String season = matches.get(0).getSeason();
  21. int betOnGameCount = 0;
  22. final LocalDateTime currentDate = matches.get(0).getGameDate();
  23. LocalDate localDate = currentDate.toLocalDate();
  24. float betAmount = bank * betLevel;
  25. for (final SoccerMatch soccerMatch : matches) {
  26. if (soccerMatch.getHomeScore() < 0 || soccerMatch.getAwayScore() < 0) {
  27. continue;
  28. }
  29. if (!season.equals(soccerMatch.getSeason())) { // new season reset bank, report result
  30. System.out.println("season " + season + " ended with " + bank);
  31. bank = startingBank;
  32. season = soccerMatch.getSeason();
  33. }
  34. final TeamResults homeTeamResults = GuiMysql.getInstance().getTeamResults(soccerMatch.getHomeTeam().getTeamId(), lookBack, true);
  35. final TeamResults awayTeamResults = GuiMysql.getInstance().getTeamResults(soccerMatch.getAwayTeam().getTeamId(), lookBack, false);
  36. final float homeWinPercent = (homeTeamResults.getWins() + awayTeamResults.getLosses()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount())* 100;
  37. final float drawPercent = (homeTeamResults.getDraws() + awayTeamResults.getDraws()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount()) * 100;
  38. final float awayWinPercent = (homeTeamResults.getLosses() + awayTeamResults.getWins()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount()) * 100;
  39. final float homeOdds = 100 / homeWinPercent;
  40. final float drawOdds = 100 / drawPercent;
  41. final float awayOdds = 100 / awayWinPercent;
  42. if (localDate.isBefore(soccerMatch.getGameDate().toLocalDate())) {
  43. betAmount = bank * betLevel;
  44. localDate = soccerMatch.getGameDate().toLocalDate();
  45. }
  46. if (homeOdds * betMarginDecimal <= soccerMatch.getOdds1()) {
  47. betOnGameCount++;
  48. // System.out.print(soccerMatch.getGameDate() + " Bet " + betAmount + " on home team " + soccerMatch.getHomeTeam().getTeamName() + " - " + soccerMatch.getAwayTeam().getTeamName() + " at odds " + soccerMatch.getOdds1()
  49. // + " Result " + soccerMatch.getHomeScore() + " - " + soccerMatch.getAwayScore());
  50. bank = bank - betAmount;
  51. if (soccerMatch.getHomeScore() > soccerMatch.getAwayScore()) {
  52. bank = bank + betAmount * soccerMatch.getOdds1();
  53. // System.out.println(" Win, new Bank " + bank);
  54. } else {
  55. // System.out.println(" Loss new Bank " + bank);
  56. }
  57. }
  58. if (awayOdds * betMarginDecimal <= soccerMatch.getOdds2()) {
  59. betOnGameCount++;
  60. // System.out.print(soccerMatch.getGameDate() + " Bet " + betAmount + " on away team " + soccerMatch.getHomeTeam().getTeamName() + " - " + soccerMatch.getAwayTeam().getTeamName() + " at odds " + soccerMatch.getOdds2()
  61. // + " Result " + soccerMatch.getHomeScore() + " - " + soccerMatch.getAwayScore());
  62. bank = bank - betAmount;
  63. if (soccerMatch.getAwayScore() > soccerMatch.getHomeScore()) {
  64. bank = bank + betAmount * soccerMatch.getOdds2();
  65. // System.out.println(" Win, new Bank " + bank);
  66. } else {
  67. // System.out.println(" Loss new Bank " + bank);
  68. }
  69. }
  70. }
  71. System.out.println("season " + season + " ended with " + bank);
  72. System.out.println("Match count " + matches.size() + " leagueId " + leagueId + " countryId " + countryId + " bet on " + betOnGameCount + "(" + betOnGameCount / (float)totalMatchCount + ")");
  73. }
  74. public void calcBestResults( int sportId, Integer countryId, Integer leagueId) {
  75. final float startingBank = 1000f;
  76. float bank = startingBank;
  77. final float betLevel = 1 / 100f;
  78. int bestBetMargin = 1;
  79. int bestLookBack = 1;
  80. float bestBankResult = 1000f;
  81. final ArrayList<SoccerMatch> matches = GuiMysql.getInstance().getMatches(sportId, countryId, leagueId, LocalDate.now().atStartOfDay().format(DateTimeFormatter.ISO_DATE), "ASC");
  82. final LocalDateTime currentDate = matches.get(0).getGameDate();
  83. LocalDate localDate = currentDate.toLocalDate();
  84. float betAmount = bank * betLevel;
  85. for (int lookBack = 1; lookBack < 25; lookBack++) {
  86. for (int betMargin = 1; betMargin < 35; betMargin++) {
  87. int betOnGameCount = 0;
  88. int wins = 0;
  89. final float betMarginDecimal = 1 + (betMargin / (float)100);
  90. bank = startingBank;
  91. for (final SoccerMatch soccerMatch : matches) {
  92. if (soccerMatch.getHomeScore() < 0 || soccerMatch.getAwayScore() < 0) {
  93. continue;
  94. }
  95. final TeamResults homeTeamResults = GuiMysql.getInstance().getTeamResultsTest(soccerMatch.getHomeTeam().getTeamId(), lookBack, true, soccerMatch.getGameDate().format(DateTimeFormatter.ISO_DATE));
  96. final TeamResults awayTeamResults = GuiMysql.getInstance().getTeamResultsTest(soccerMatch.getAwayTeam().getTeamId(), lookBack, false, soccerMatch.getGameDate().format(DateTimeFormatter.ISO_DATE));
  97. final float homeWinPercent = (homeTeamResults.getWins() + awayTeamResults.getLosses()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount()) * 100;
  98. final float awayWinPercent = (homeTeamResults.getLosses() + awayTeamResults.getWins()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount()) * 100;
  99. final float homeOdds = 100 / homeWinPercent;
  100. final float awayOdds = 100 / awayWinPercent;
  101. if (localDate.isBefore(soccerMatch.getGameDate().toLocalDate())) {
  102. betAmount = bank * betLevel;
  103. localDate = soccerMatch.getGameDate().toLocalDate();
  104. }
  105. if (homeOdds * betMarginDecimal <= soccerMatch.getOdds1()) {
  106. betOnGameCount++;
  107. bank = bank - betAmount;
  108. if (soccerMatch.getHomeScore() > soccerMatch.getAwayScore()) {
  109. wins++;
  110. bank = bank + betAmount * soccerMatch.getOdds1();
  111. }
  112. }
  113. if (awayOdds * betMarginDecimal <= soccerMatch.getOdds2()) {
  114. betOnGameCount++;
  115. bank = bank - betAmount;
  116. if (soccerMatch.getAwayScore() > soccerMatch.getHomeScore()) {
  117. wins++;
  118. bank = bank + betAmount * soccerMatch.getOdds2();
  119. }
  120. }
  121. }
  122. if (bestBankResult < bank) {
  123. System.out.println("New best bank " + bank + " with lookback " + lookBack + " and betMargin " + betMargin +
  124. " Bet on " + betOnGameCount + " of " + matches.size() + "(" + betOnGameCount / (float)matches.size() + ") wins " + wins + "(" + wins/Float.valueOf(betOnGameCount) + ")" +
  125. " Win / game " + (bank - startingBank) / Float.valueOf(wins) + " kr");
  126. bestBetMargin = betMargin;
  127. bestLookBack = lookBack;
  128. bestBankResult = bank;
  129. } else {
  130. System.out.println("NOT BEST bank " + bank + " with lookback " + lookBack + " and betMargin " + betMargin +
  131. " Bet on " + betOnGameCount + " of " + matches.size() + "(" + betOnGameCount / (float)matches.size() + ") wins " + wins + "(" + wins/Float.valueOf(betOnGameCount) + ")" +
  132. " Win / game " + (bank - startingBank) / Float.valueOf(wins) + " kr");
  133. }
  134. }
  135. }
  136. }
  137. public ArrayList<SoccerMatch> getMatches(int leagueId) {
  138. return GuiMysql.getInstance().getMatches(sportId, countryId, leagueId, date, "ASC");
  139. }
  140. }