AwayTests.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.time.format.DateTimeFormatter;
  7. import java.util.ArrayList;
  8. import data.GuiMysql;
  9. import objects.League;
  10. import objects.SoccerMatch;
  11. import objects.TeamResults;
  12. public class AwayTests extends TestClass {
  13. private League leagueInfo;
  14. @Override
  15. public void setup(String date, float startingBank, float bettingLevel, Float betMargin, int lookBack, int sportId,
  16. Integer countryId, Integer leagueId) {
  17. super.setup(date, startingBank, bettingLevel, betMargin, lookBack, sportId, countryId, leagueId);
  18. leagueInfo = getLeagueInfoById();
  19. }
  20. @Override
  21. public void runTest() {
  22. final ArrayList<SoccerMatch> matches = getMatches();
  23. final float betLevel = bettingLevel / 100f;
  24. final LocalDateTime currentDate = matches.get(0).getGameDate();
  25. LocalDate localDate = currentDate.toLocalDate();
  26. float betAmount = startingBank * betLevel;
  27. int betOnGameCount = 0;
  28. int wins = 0;
  29. float bestBankResult = startingBank;
  30. int bestBetMargin = 0;
  31. int bestLookBack = 0;
  32. for (int lookBack = 4; lookBack < 25; lookBack++) {
  33. for (int betMargin = 1; betMargin < 35; betMargin++) {
  34. final float betMarginDecimal = 1 + (betMargin / (float)100);
  35. float bank = startingBank;
  36. betOnGameCount = 0;
  37. wins = 0;
  38. for (final SoccerMatch match : matches) {
  39. if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
  40. continue;
  41. }
  42. final TeamResults homeTeamResults = GuiMysql.getInstance().getTeamResultsTest(match.getHomeTeam().getTeamId(), lookBack, true, match.getGameDate().format(DateTimeFormatter.ISO_DATE));
  43. final TeamResults awayTeamResults = GuiMysql.getInstance().getTeamResultsTest(match.getAwayTeam().getTeamId(), lookBack, false, match.getGameDate().format(DateTimeFormatter.ISO_DATE));
  44. final float awayWinPercent = (homeTeamResults.getLosses() + awayTeamResults.getWins()) / Float.valueOf(homeTeamResults.getCount() + awayTeamResults.getCount()) * 100;
  45. final float awayOdds = 100 / awayWinPercent;
  46. if (localDate.isBefore(match.getGameDate().toLocalDate())) {
  47. betAmount = bank * betLevel;
  48. localDate = match.getGameDate().toLocalDate();
  49. }
  50. if (awayOdds * betMarginDecimal <= match.getOdds2()) {
  51. betOnGameCount++;
  52. bank = bank - betAmount;
  53. if (match.getAwayScore() > match.getHomeScore()) {
  54. wins++;
  55. bank = bank + betAmount * match.getOdds2();
  56. }
  57. }
  58. }
  59. if (bestBankResult < bank) {
  60. System.out.println("New best bank " + bank + " with lookback " + lookBack + " and betMargin " + betMargin +
  61. " Bet on " + betOnGameCount + " of " + matches.size() + "(" + betOnGameCount / (float)matches.size() + ") wins " + wins + "(" + wins/Float.valueOf(betOnGameCount) + ")" +
  62. " Win / game " + (bank - startingBank) / Float.valueOf(wins) + " kr");
  63. bestBetMargin = betMargin;
  64. bestLookBack = lookBack;
  65. bestBankResult = bank;
  66. } else {
  67. System.out.println("NOT BEST bank " + bank + " with lookback " + lookBack + " and betMargin " + betMargin +
  68. " Bet on " + betOnGameCount + " of " + matches.size() + "(" + betOnGameCount / (float)matches.size() + ") wins " + wins + "(" + wins/Float.valueOf(betOnGameCount) + ")" +
  69. " Win / game " + (bank - startingBank) / Float.valueOf(wins) + " kr");
  70. }
  71. }
  72. }
  73. }
  74. public ArrayList<SoccerMatch> getMatches() {
  75. final ArrayList<SoccerMatch> result = new ArrayList<>();
  76. final String sql = "SELECT res.*, "
  77. + "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
  78. + "FROM SoccerResults as res "
  79. + "Join Team as hTeam ON res.homeTeam = hTeam.id "
  80. + "Join Team as aTeam ON res.awayTeam = aTeam.id "
  81. + "WHERE "
  82. + "res.leagueId = ? "
  83. + "ORDER BY gameDate ASC";
  84. try {
  85. final PreparedStatement stat = getConnection().prepareStatement(sql);
  86. stat.setInt(1, leagueId);
  87. result.addAll(getMatches(stat));
  88. } catch (final SQLException e) {
  89. // TODO Auto-generated catch block
  90. e.printStackTrace();
  91. }
  92. return result;
  93. }
  94. }