AnalysisBettTester.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package tests;
  2. import java.math.BigDecimal;
  3. import java.time.LocalDate;
  4. import java.util.ArrayList;
  5. import java.util.LinkedHashMap;
  6. import java.util.List;
  7. import java.util.Map.Entry;
  8. import data.TestDatabase;
  9. import objects.League;
  10. import objects.SoccerMatch;
  11. import objects.SoccerMatchAnalysis;
  12. import objects.bets.AsianHandicap;
  13. import objects.bets.Bet;
  14. import tests.objects.LeagueDTO;
  15. public class AnalysisBettTester extends TestClass {
  16. TestDatabase database;
  17. List<SoccerMatch> betsToPlace = new ArrayList<>();
  18. float betAmount;
  19. List<DifferenceBetDTO> bets = new ArrayList<>();
  20. int wins = 0;
  21. int losses = 0;
  22. float bank;
  23. private List<DifferenceBetDTO> betSeries = new ArrayList<>();
  24. private League leagueInfo;
  25. @Override
  26. public void runTest() {
  27. bank = startingBank;
  28. database = TestDatabase.getInstance();
  29. leagueInfo = database.getLeagueInfo(leagueId);
  30. LeagueDTO leagueDTO = database.getLeagueDTO(leagueId);
  31. LinkedHashMap<String, List<SoccerMatch>> matches = getMatchesOrderedBySeason(leagueDTO);
  32. this.betAmount = startingBank * (bettingLevel / 100.0f);
  33. boolean skipFirst = true;
  34. for (Entry<String, List<SoccerMatch>> seasonMatches : matches.entrySet()) {
  35. if (skipFirst) {
  36. skipFirst = false;
  37. continue;
  38. }
  39. analyze(seasonMatches.getValue());
  40. System.out
  41. .println(String.format("Wins %s Losses %s %s total bank: %s", wins, losses,
  42. wins / (float) (wins + losses) + "%", bank));
  43. break;
  44. }
  45. }
  46. private void analyze(List<SoccerMatch> seasonMatches) {
  47. LocalDate currentMatchDate = seasonMatches.get(0).getGameDate().toLocalDate();
  48. for (SoccerMatch match : seasonMatches) {
  49. if (currentMatchDate.isBefore(match.getGameDate().toLocalDate())) {
  50. placeBets();
  51. checkBetResults();
  52. betAmount = bank * (bettingLevel / 100.0f);
  53. }
  54. /**
  55. * Matchen har en differens mellan hemma lagets mål och borta laget på minst X
  56. * (starta att testa med 1)
  57. */
  58. SoccerMatchAnalysis analysis = new SoccerMatchAnalysis(match);
  59. int homeWinsCount = analysis.winLossRatio(leagueInfo.getWinLossRatio(), true);
  60. int awayWinsCount = analysis.winLossRatio(leagueInfo.getWinLossRatio(), false);
  61. int homeWinLossRatioCount = analysis.winLossRationHomeAndAway(true,
  62. leagueInfo.getWinLossRatioHomeAndAway());
  63. int awayWinLossRatioCount = analysis.winLossRationHomeAndAway(false,
  64. leagueInfo.getWinLossRatioHomeAndAway());
  65. int homeScoringTotal = analysis.scoringTotal(leagueInfo.getScoringTotal(), true);
  66. int awayScoringTotal = analysis.scoringTotal(leagueInfo.getScoringTotal(), false);
  67. int scoringDiffLastGames = analysis.getScoringDiffLastGames(leagueInfo.getScoringDiffLastGame());
  68. int winsCountDiff = homeWinsCount - awayWinsCount;
  69. int winLossRatioDiff = homeWinLossRatioCount - awayWinLossRatioCount;
  70. int scoringTotalDiff = homeScoringTotal - awayScoringTotal;
  71. int testValue = 0;
  72. if (scoringDiffLastGames < 0 && winsCountDiff < 0 && winLossRatioDiff < 0 && scoringTotalDiff < 0) {
  73. addBet(match, scoringDiffLastGames, winsCountDiff, winLossRatioDiff, scoringTotalDiff);
  74. } else if (scoringDiffLastGames > 0 && winsCountDiff > 0 && winLossRatioDiff > 0 && scoringTotalDiff > 0) {
  75. addBet(match, scoringDiffLastGames, winsCountDiff, winLossRatioDiff, scoringTotalDiff);
  76. } else {
  77. // match.put("analysis", "NoBet");
  78. }
  79. currentMatchDate = match.getGameDate().toLocalDate();
  80. }
  81. }
  82. private void addBet(SoccerMatch match, int scoringDiffLastGames, int winsCountDiff, int winLossRatioDiff,
  83. int scoringTotalDiff) {
  84. int testValue;
  85. testValue = (scoringDiffLastGames + winsCountDiff + winLossRatioDiff + scoringTotalDiff) / 4;
  86. match.setGoalsDiff(BigDecimal.valueOf(testValue));// Temp use GoldsDiff for value
  87. betsToPlace.add(match);
  88. }
  89. private void placeBets() {
  90. for (SoccerMatch soccerMatch : betsToPlace) {
  91. float currentBetAmount = 0;
  92. // betSeries.sort((b1, b2) -> Float.compare(b2.getBetAmount(), b1.getBetAmount()));
  93. if (!betSeries.isEmpty() && !betsToPlace.isEmpty()) {
  94. currentBetAmount += betSeries.get(0).getBetAmount();
  95. betSeries.remove(0);
  96. }
  97. if (soccerMatch.getGoalsDiff().floatValue() > 0f) {
  98. if (currentBetAmount > 0) {
  99. currentBetAmount = (float) (currentBetAmount / (soccerMatch.getOdds1() - 1.0));
  100. }
  101. bets.add(new DifferenceBetDTO(soccerMatch, currentBetAmount + betAmount, true));
  102. } else if (soccerMatch.getGoalsDiff().floatValue() < 0f) {
  103. if (currentBetAmount > 0) {
  104. currentBetAmount = (float) (currentBetAmount / (soccerMatch.getOdds2() - 1.0));
  105. }
  106. bets.add(new DifferenceBetDTO(soccerMatch, currentBetAmount + betAmount, false));
  107. }
  108. bank -= currentBetAmount + betAmount;
  109. }
  110. betsToPlace.clear();
  111. }
  112. private void checkBetResults() {
  113. List<DifferenceBetDTO> activeBets = bets.stream().filter(p -> !p.resolved).toList();
  114. for (DifferenceBetDTO bet : activeBets) {
  115. if (bet.getResult() > 0) {
  116. bank += bet.getResult(); // bet.getBetAmount() * bet.getBetOdds();
  117. String winText = bet.correctBet() ? bet.isBetOnHomeTeam() ? "HOME WIN " : "AWAY WIN" : "HALF WIN";
  118. System.out.println(String.format(winText + "\t" + "%s \t\tnew bank %s", bet, bank));
  119. wins++;
  120. } else {
  121. // LOSS
  122. System.out.println(String.format("LOSS\t\t%s \t\t bank: %s", bet, bank));
  123. losses++;
  124. betSeries.add(bet);
  125. }
  126. bet.resolved = true;
  127. }
  128. if (!betSeries.isEmpty()) {
  129. System.out.println("Outstanding bets:");
  130. betSeries.forEach(b -> System.out.println(b));
  131. }
  132. }
  133. public class DifferenceBetDTO extends Bet {
  134. boolean resolved = false;
  135. public DifferenceBetDTO(SoccerMatch match, float betAmount, boolean betOnHomeTeam) {
  136. super(0, match, betOnHomeTeam ? "1" : "2", betAmount, betOnHomeTeam ? match.getOdds1() : match.getOdds2());
  137. }
  138. @Override
  139. public String toString() {
  140. return String.format("%s \t%s - %s \t\tbet amount %s \todds %s \twinner %s \t result %s-%s \tdiff %s",
  141. getMatch().getGameDate().toLocalDate(),
  142. getMatch().getHomeTeamName(),
  143. getMatch().getAwayTeamName(), getBetAmount(), super.getBetOdds(),
  144. super.isBetOnHomeTeam() ? "1" : "2", getMatch().getHomeScore(),
  145. getMatch().getAwayScore(), getMatch().getGoalsDiff());
  146. }
  147. }
  148. public class DifferenceAsianBetDTO extends AsianHandicap {
  149. boolean resolved = false;
  150. public DifferenceAsianBetDTO(SoccerMatch match, float betAmount, boolean betOnHomeTeam, float handicap) {
  151. super(match, betOnHomeTeam ? "1" : "2", betAmount, betOnHomeTeam ? match.getOdds1() : match.getOdds2(),
  152. handicap);
  153. }
  154. @Override
  155. public String toString() {
  156. return String.format(
  157. "%s \t%s - %s \t\tbet amount %s \todds %s \twinner %s \t result %s-%s \tdiff %s with results %s",
  158. getMatch().getGameDate().toLocalDate(),
  159. getMatch().getHomeTeamName(),
  160. getMatch().getAwayTeamName(), getBetAmount(), super.getBetOdds(),
  161. super.isBetOnHomeTeam() ? "1" : "2", getMatch().getHomeScore(),
  162. getMatch().getAwayScore(), getMatch().getGoalsDiff(), getResult());
  163. }
  164. }
  165. }