Axel Nordh 4 gadi atpakaļ
vecāks
revīzija
8651c0f2eb

+ 27 - 7
OddsJavaFx/src/controllers/TestsController.java

@@ -23,15 +23,17 @@ import javafx.scene.control.ScrollPane;
 import javafx.scene.control.TextField;
 import javafx.scene.layout.AnchorPane;
 import parser.OddsPortal;
+import tests.AddedScoringTest;
 import tests.AwayTeamWinTest;
-import tests.AwayTests;
-import tests.DrawTests;
+import tests.AwayTests2;
+import tests.DrawTests2;
 import tests.HomeDrawAwayTest;
 import tests.HomeTeamWinTest;
-import tests.HomeTests;
+import tests.HomeTests2;
 import tests.LastResultsTest;
 import tests.PrioCountriesAll;
 import tests.TestClass;
+import tests.recalcTest;
 
 public class TestsController implements Initializable {
 
@@ -57,7 +59,8 @@ public class TestsController implements Initializable {
 	@FXML Button homeDrawAwayTest;
 	@FXML Button homeTeamTest;
 	@FXML Button awayTeamTest;
-
+	@FXML Button addedScoreTest;
+	@FXML Button recalcTest;
 
 	ObservableList<String> countries = FXCollections.observableArrayList();
 	ObservableList<String> leagues = FXCollections.observableArrayList();
@@ -267,7 +270,7 @@ public class TestsController implements Initializable {
 
 	@FXML
 	private void homeTestAction() {
-		final HomeTests test = new HomeTests();
+		final HomeTests2 test = new HomeTests2();
 
 		test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()), 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
 
@@ -276,7 +279,7 @@ public class TestsController implements Initializable {
 
 	@FXML
 	private void drawTestAction() {
-		final DrawTests test = new DrawTests();
+		final DrawTests2 test = new DrawTests2();
 
 		test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()), 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
 
@@ -285,7 +288,7 @@ public class TestsController implements Initializable {
 
 	@FXML
 	private void awayTestAction() {
-		final AwayTests test = new AwayTests();
+		final AwayTests2 test = new AwayTests2();
 
 		test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()), 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
 
@@ -318,4 +321,21 @@ public class TestsController implements Initializable {
 
 		test.runTest();
 	}
+
+	@FXML
+	private void addedScoreTestAction() {
+		final AddedScoringTest test = new AddedScoringTest();
+
+		test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()), 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
+
+		test.runTest();
+	}
+	@FXML
+	private void recalcTestAction() {
+		final recalcTest test = new recalcTest();
+
+		test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()), 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
+
+		test.runTest();
+	}
 }

+ 2 - 0
OddsJavaFx/src/fxml/Testing.fxml

@@ -61,6 +61,8 @@
                                     <Button fx:id="homeDrawAwayTest" onAction="#homeDrawAwayTestAction" text="Combined 1X2 Test"/>
                                     <Button fx:id="homeTeamTest" onAction="#homeTeamTestAction" text="Home Team margin test"/>
                                     <Button fx:id="awayTeamTest" onAction="#awayTeamTestAction" text="Away Team margin test"/>
+                                    <Button fx:id="addedScoreTest" onAction="#addedScoreTestAction" text="Added score test"/>
+                                    <Button fx:id="recalcTest" onAction="#recalcTestAction" text="Recalc test"/>
                                  </children>
                               </VBox>
                            </children>

+ 233 - 0
OddsJavaFx/src/tests/AddedScoringTest.java

@@ -0,0 +1,233 @@
+package tests;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import objects.League;
+import objects.SoccerMatch;
+import objects.Team;
+
+public class AddedScoringTest extends TestClass {
+
+	private ArrayList<SoccerMatch> trainMatches;
+
+	@Override
+	public void runTest() {
+		trainMatches = getTrainMatches();
+		final League leagueInfo = getLeagueInfoById();
+
+		final int betMarginHome = leagueInfo.getBetMarginHome();
+		final int betMarginDraw = leagueInfo.getBetMarginDraw();
+		final int betMarginAway = leagueInfo.getBetMarginAway();
+
+		final int lookbackHome = leagueInfo.getLookbackHome();
+		final int lookbackDraw = leagueInfo.getLookbackDraw();
+		final int lookbackAway = leagueInfo.getLookbackAway();
+
+		final int lookback = leagueInfo.getLookback();
+		final int margin = leagueInfo.getBetMargin();
+
+
+		final float betLevel = bettingLevel / 100f;
+		float bank = startingBank;
+		float prevBank = startingBank;
+		float prevBetAmount = startingBank * betLevel;
+
+		int betOnGameCount = 0;
+		int prevBetOnGameCount = 0;
+
+		float betAmount = startingBank * betLevel;
+		int wins = 0;
+		int prevWins = 0;
+
+		final float betMarginDecimalHome = 1 + (leagueInfo.getBetMarginHome() / 100f);
+		final float betMarginDecimalAway = 1 + (leagueInfo.getBetMarginAway() / 100f);
+		final float betMarginDecimalDraw = 1 + (leagueInfo.getBetMarginDraw() / 100f);
+
+		int betOnX = 0;
+		int correctX = 0;
+		float xBank = startingBank;
+
+		for (final SoccerMatch soccerMatch : trainMatches) {
+			final Team homeTeam = soccerMatch.getHomeTeam();
+			final Team awayTeam = soccerMatch.getAwayTeam();
+
+			final List<SoccerMatch> homeMatches = trainMatches.stream().filter(p -> p.getGameDate().isBefore(soccerMatch.getGameDate()) && p.getHomeTeam().getTeamId() == homeTeam.getTeamId()).limit(lookbackHome).collect(Collectors.toList());
+			final List<SoccerMatch> awayMatches = trainMatches.stream().filter(p -> p.getGameDate().isBefore(soccerMatch.getGameDate()) && p.getAwayTeam().getTeamId() == awayTeam.getTeamId()).limit(lookbackAway).collect(Collectors.toList());
+
+			final long hemmaVinster = homeMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
+			final long hemmaLika = homeMatches.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
+			final long hemmaForluster= homeMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
+
+			final long bortaVinster = awayMatches.stream().filter(p -> p.getAwayScore() > p.getHomeScore()).count();
+			final long bortaLika = awayMatches.stream().filter(p -> p.getAwayScore() == p.getHomeScore()).count();
+			final long bortaForluster = awayMatches.stream().filter(p -> p.getAwayScore() < p.getHomeScore()).count();
+
+			int homeGoals = 0;
+			for (final SoccerMatch hm : homeMatches) {
+				homeGoals += hm.getHomeScore() - hm.getAwayScore();
+			}
+
+			int awayGoals = 0;
+			for (final SoccerMatch am : awayMatches) {
+				awayGoals += am.getAwayScore() - am.getHomeScore();
+			}
+
+			final float homeWinPercent = (hemmaVinster + bortaForluster) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+			final float awayWinPercent = (hemmaForluster + bortaVinster) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+			final float drawWinPercent = (hemmaLika + bortaLika) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+
+
+			prevBetAmount = prevBank * betLevel;
+			// check if should bet on home Win before score added
+			if ((100 / homeWinPercent) * betMarginDecimalHome <= soccerMatch.getOdds1()) {
+				// Bet on Home Win
+				prevBetOnGameCount++;
+				prevBank = prevBank - prevBetAmount;
+				if (soccerMatch.getHomeScore() > soccerMatch.getAwayScore()) {
+					prevWins++;
+					prevBank = prevBank + prevBetAmount * soccerMatch.getOdds1();
+				}
+			}
+
+			// check if should bet on away Win
+			if ((100 / awayWinPercent) * betMarginDecimalAway <= soccerMatch.getOdds2()) {
+				// Bet on Away Win
+				prevBetOnGameCount++;
+				prevBank = prevBank - prevBetAmount;
+				if (soccerMatch.getAwayScore() > soccerMatch.getHomeScore()) {
+					prevWins++;
+					prevBank = prevBank + prevBetAmount * soccerMatch.getOdds2();
+				}
+			}
+
+			//			if ((100 / drawWinPercent) * betMarginDecimalDraw <= soccerMatch.getOddsX()) {
+			//				// Bet on Draw
+			//				prevBetOnGameCount++;
+			//				prevBank = prevBank - prevBetAmount;
+			//				if (soccerMatch.getAwayScore() == soccerMatch.getHomeScore()) {
+			//					prevWins++;
+			//					prevBank = prevBank + prevBetAmount * soccerMatch.getOddsX();
+			//				}
+			//			}
+
+
+			final int goalDiff = homeGoals - awayGoals;
+
+			//			if (goalDiff == 0) {
+			//				System.out.println("Ingen goalDiff borde man satsa på lika, faktiskt resultat " + soccerMatch.getHomeScore() + " - " + soccerMatch.getAwayScore());
+			//			}
+			float homeWinPercentGoals = homeWinPercent + ((homeGoals / 2f) * 10);
+			float awayWinPercentGoals = awayWinPercent + ((awayGoals / 2f) * 10);
+
+
+			if (homeWinPercentGoals <= 0) {
+				homeWinPercentGoals = 0.01f;
+			}
+
+			if (awayWinPercentGoals <= 0) {
+				awayWinPercentGoals = 0.01f;
+			}
+
+			final float homeOdds = 100 / homeWinPercentGoals;
+			final float awayOdds = 100 / awayWinPercentGoals;
+
+			betAmount = bank * betLevel;
+
+			// check if should bet on home Win
+			if (homeOdds * betMarginDecimalHome <= soccerMatch.getOdds1()) {
+				// Bet on Home Win
+				betOnGameCount++;
+				bank = bank - betAmount;
+				if (soccerMatch.getHomeScore() > soccerMatch.getAwayScore()) {
+					wins++;
+					bank = bank + betAmount * soccerMatch.getOdds1();
+				}
+			}
+
+			// check if should bet on away Win
+			if (awayOdds * betMarginDecimalAway  <= soccerMatch.getOdds2()) {
+				// Bet on Home Win
+				betOnGameCount++;
+				bank = bank - betAmount;
+				if (soccerMatch.getAwayScore() > soccerMatch.getHomeScore()) {
+					wins++;
+					bank = bank + betAmount * soccerMatch.getOdds2();
+				}
+			}
+
+			if (goalDiff >= -1 && goalDiff <= 1) {
+				System.out.println("Test betting on X odds " + soccerMatch.getOddsX() +
+						" Actual result " + soccerMatch.getHomeScore() + " - " + soccerMatch.getAwayScore());
+				betOnX++;
+				final float xBetAmount = xBank * betLevel;
+				xBank -= xBetAmount;
+				if (soccerMatch.getHomeScore() == soccerMatch.getAwayScore()) {
+					correctX++;
+					xBank += xBetAmount * soccerMatch.getOddsX();
+				}
+
+			}
+		}
+
+		System.out.println("Test ended with a bank of with score added " + bank + " and with no score added it was " + prevBank);
+		System.out.println("Betted on " + betOnGameCount + " won " + wins + " out of " + trainMatches.size() + " prev bets " + prevBetOnGameCount + " and prev wins " + prevWins);
+
+		System.out.println("Test with draw ended with betting on " + betOnX + " matches and " + correctX + " correct " + (correctX / (float)betOnX) + " bank " + xBank);
+	}
+
+
+	private ArrayList<SoccerMatch> getMatches() {
+		final ArrayList<SoccerMatch> result = new ArrayList<>();
+
+		final String sql = "SELECT res.*, "
+				+ "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
+				+ "FROM SoccerResults as res "
+				+ "Join Team as hTeam ON res.homeTeam = hTeam.id "
+				+ "Join Team as aTeam ON res.awayTeam = aTeam.id "
+				+ "WHERE "
+				+ "DATE(gameDate) > DATE_SUB(NOW(), INTERVAL 1 YEAR) AND "
+				+ "res.leagueId = ? "
+				+ "ORDER BY gameDate ASC";
+
+		try {
+			final PreparedStatement stmt = getConnection().prepareStatement(sql);
+			stmt.setInt(1, leagueId);
+
+			result.addAll(super.getMatches(stmt));
+		} catch (final SQLException e) {
+			e.printStackTrace();
+		}
+
+		return result;
+	}
+
+	private ArrayList<SoccerMatch> getTrainMatches() {
+		final ArrayList<SoccerMatch> result = new ArrayList<>();
+
+		final String sql = "SELECT res.*, "
+				+ "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
+				+ "FROM SoccerResults as res "
+				+ "Join Team as hTeam ON res.homeTeam = hTeam.id "
+				+ "Join Team as aTeam ON res.awayTeam = aTeam.id "
+				+ "WHERE "
+				+ "DATE(gameDate) < DATE_SUB(NOW(), INTERVAL 1 YEAR) AND "
+				+ "res.leagueId = ? "
+				+ "ORDER BY gameDate DESC";
+
+		try {
+			final PreparedStatement stmt = getConnection().prepareStatement(sql);
+			stmt.setInt(1, leagueId);
+
+			result.addAll(super.getMatches(stmt));
+		} catch (final SQLException e) {
+			e.printStackTrace();
+		}
+
+		return result;
+	}
+
+}

+ 1 - 5
OddsJavaFx/src/tests/AwayTeamWinTest.java

@@ -67,22 +67,18 @@ public class AwayTeamWinTest extends TestClass {
 
 					// Hemma lagets resultat
 					final List<SoccerMatch> hemmaMatcher = aktuellaMatcher.stream().filter(p -> p.getHomeTeam().getTeamId() == teamSettings.getTeamId()).limit(lookback).collect(Collectors.toList());
-					final long hemmaVinster = hemmaMatcher.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
-					final long lika = hemmaMatcher.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
 					final long hemmaForluster = hemmaMatcher.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
 
 					// borta lagets resultat
 					final List<SoccerMatch> bortaMatcher = aktuellaMatcher.stream().filter(p -> p.getAwayTeam().getTeamId() == soccerMatch.getAwayTeam().getTeamId()).limit(lookback).collect(Collectors.toList());
 					final long bortaVinster = bortaMatcher.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
-					final long likaBortalag = bortaMatcher.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
-					final long bortaForluster = bortaMatcher.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
 
 					final float awayWinPercent = (bortaVinster + hemmaForluster) / Float.valueOf(hemmaMatcher.size() + bortaMatcher.size()) * 100;
 					final float awayOdds = 100 / awayWinPercent;
 
 					betAmount = bank * betLevel;
 
-					// Spela på hemma vins?
+					// Spela på borta vinst?
 					if (awayOdds * betMarginDecimal <= soccerMatch.getOdds2()) {
 						betOnGameCount++;
 						bank = bank - betAmount;

+ 212 - 0
OddsJavaFx/src/tests/AwayTests2.java

@@ -0,0 +1,212 @@
+package tests;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import objects.League;
+import objects.SoccerMatch;
+
+public class AwayTests2 extends TestClass {
+
+	private League leagueInfo;
+
+	ArrayList<results> possitiveResults = new ArrayList<>();
+
+	@Override
+	public void setup(String date, float startingBank, float bettingLevel, Float betMargin, int lookBack, int sportId,
+			Integer countryId, Integer leagueId) {
+		super.setup(date, startingBank, bettingLevel, betMargin, lookBack, sportId, countryId, leagueId);
+
+		leagueInfo = getLeagueInfoById();
+	}
+
+	@Override
+	public void runTest() {
+		final ArrayList<SoccerMatch> matches = getMatches();
+
+		final LocalDateTime startDate = matches.get(0).getGameDate();
+		final LocalDateTime endDate = matches.get(0).getGameDate().plusYears(1);
+
+		List<SoccerMatch> matchesFiltered = matches.stream()
+				.filter(p ->
+				(p.getGameDate().isAfter(matches.get(0).getGameDate()) || p.getGameDate().equals(matches.get(0).getGameDate())) &&
+				p.getGameDate().isBefore(matches.get(0).getGameDate().plusYears(1)))
+				.collect(Collectors.toList());
+
+		final float betLevel = bettingLevel / 100f;
+
+		final LocalDateTime currentDate = matches.get(0).getGameDate();
+		LocalDate localDate = currentDate.toLocalDate();
+		float betAmount = startingBank * betLevel;
+
+		int betOnGameCount = 0;
+		int wins = 0;
+
+		final float bestBankResult = startingBank;
+		final int bestBetMargin = 0;
+		final int bestLookBack = 0;
+
+		System.out.println("Away Test 2");
+		int i = 0;
+		while (matchesFiltered.size() > 0) {
+			i++;
+			for (int lookBack = 4; lookBack < 25; lookBack++) {
+				for (int betMargin = 1; betMargin < 35; betMargin++) {
+					final float betMarginDecimal = 1 + (betMargin / (float)100);
+					float bank = startingBank;
+					betOnGameCount = 0;
+					wins = 0;
+					for (final SoccerMatch match : matchesFiltered) {
+						if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
+							continue;
+						}
+
+						final List<SoccerMatch> homeMatches = matchesFiltered.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getHomeTeam().getTeamId() == match.getHomeTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+						final List<SoccerMatch> awayMatches = matchesFiltered.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getAwayTeam().getTeamId() == match.getAwayTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+
+						final long hemmaVinster = homeMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
+						final long hemmaForluster= homeMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
+
+						final long bortaVinster = awayMatches.stream().filter(p -> p.getAwayScore() > p.getHomeScore()).count();
+						final long bortaForluster = awayMatches.stream().filter(p -> p.getAwayScore() < p.getHomeScore()).count();
+
+						final float awayWinPercent = (bortaVinster + hemmaForluster) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+						final float awayOdds = 100 / awayWinPercent;
+
+						if (localDate.isBefore(match.getGameDate().toLocalDate())) {
+							betAmount = bank * betLevel;
+							localDate = match.getGameDate().toLocalDate();
+						}
+
+						if (awayOdds * betMarginDecimal <= match.getOdds2()) {
+							betOnGameCount++;
+							bank = bank - betAmount;
+							if (match.getAwayScore() > match.getHomeScore()) {
+								wins++;
+								bank = bank + betAmount * match.getOdds2();
+							}
+						}
+
+					}
+					if (bank > startingBank) {
+						final int lookBack2 = lookBack;
+						final int betMargin2 = betMargin;
+						final java.util.Optional<results> found = possitiveResults.stream().filter(r -> r.getLookback() == lookBack2 && r.getMargin() == betMargin2).findFirst();
+						if (found.isPresent()) {
+							final results results = found.get();
+							results.addCount();
+							results.addBank(bank);
+						} else {
+							possitiveResults.add(new results(lookBack, betMargin, bank));
+						}
+					}
+				}
+			}
+			final LocalDateTime startDate2 = matchesFiltered.get(0).getGameDate().plusYears(1);
+			final LocalDateTime endDate2 = startDate2.plusYears(1);
+			matchesFiltered.clear();
+			matchesFiltered = matches.stream()
+					.filter(p ->
+					(p.getGameDate().isAfter(startDate2) || p.getGameDate().equals(startDate2)) &&
+					p.getGameDate().isBefore(endDate2))
+					.collect(Collectors.toList());
+		}
+
+		Collections.sort(possitiveResults);
+		for (final results res : possitiveResults) {
+			System.out.println("Lookback " + res.getLookback() + " and margin " + res.getMargin() + " At count " + res.getCount() + " of " + i + " with bank: " + res.getBank());
+		}
+	}
+
+	public ArrayList<SoccerMatch> getMatches() {
+		final ArrayList<SoccerMatch> result = new ArrayList<>();
+
+		final String sql = "SELECT res.*, "
+				+ "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
+				+ "FROM SoccerResults as res "
+				+ "Join Team as hTeam ON res.homeTeam = hTeam.id "
+				+ "Join Team as aTeam ON res.awayTeam = aTeam.id "
+				+ "WHERE "
+				+ "res.leagueId = ? "
+				+ "ORDER BY gameDate ASC";
+
+		try {
+			final PreparedStatement stat = getConnection().prepareStatement(sql);
+			stat.setInt(1, leagueId);
+
+			result.addAll(getMatches(stat));
+		} catch (final SQLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+		return result;
+	}
+
+	public class results implements Comparable<results> {
+		int lookback;
+		int margin;
+		int count;
+		float bank;
+
+		public results(int lookback, int margin, float bank) {
+			this.lookback = lookback;
+			this.margin = margin;
+			this.bank = bank;
+			count = 1;
+		}
+
+		public float getBank() {
+			return bank;
+		}
+
+		public void addBank(float bank) {
+			this.bank = (this.bank + bank) / 2;
+		}
+
+		public void addCount() {
+			count++;
+		}
+		public int getCount() {
+			return count;
+		}
+
+		public int getLookback() {
+			return lookback;
+		}
+		public int getMargin() {
+			return margin;
+		}
+		public void setLookback(int lookback) {
+			this.lookback = lookback;
+		}
+		public void setMargin(int margin) {
+			this.margin = margin;
+		}
+
+		@Override
+		public int compareTo(results res) {
+			int returnValue = 0;
+			if (this.count > res.getCount()) {
+				returnValue = -1;
+			} else if (this.count == res.getCount()) {
+				if (this.bank > res.getBank()) {
+					returnValue = -1;
+				} else if (this.bank < res.getBank()) {
+					returnValue = 1;
+				} else {
+					returnValue = 0;
+				}
+			} else if (this.count < res.getCount()) {
+				returnValue = 1;
+			}
+			return returnValue;
+		}
+	}
+}

+ 11 - 15
OddsJavaFx/src/tests/DrawTeamTestNOTDONE.java → OddsJavaFx/src/tests/DrawTeamTest.java

@@ -11,7 +11,7 @@ import objects.League;
 import objects.SoccerMatch;
 import objects.TeamSettings;
 
-public class DrawTeamTestNOTDONE extends TestClass {
+public class DrawTeamTest extends TestClass {
 
 	ArrayList<Integer> recalcTeamsList = new ArrayList<Integer>();
 	private League leagueInfo;
@@ -67,28 +67,24 @@ public class DrawTeamTestNOTDONE extends TestClass {
 
 					// Hemma lagets resultat
 					final List<SoccerMatch> hemmaMatcher = aktuellaMatcher.stream().filter(p -> p.getHomeTeam().getTeamId() == teamSettings.getTeamId()).limit(lookback).collect(Collectors.toList());
-					final long hemmaVinster = hemmaMatcher.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
 					final long lika = hemmaMatcher.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
-					final long hemmaForluster = hemmaMatcher.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
 
 					// borta lagets resultat
 					final List<SoccerMatch> bortaMatcher = aktuellaMatcher.stream().filter(p -> p.getAwayTeam().getTeamId() == soccerMatch.getAwayTeam().getTeamId()).limit(lookback).collect(Collectors.toList());
-					final long bortaVinster = bortaMatcher.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
 					final long likaBortalag = bortaMatcher.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
-					final long bortaForluster = bortaMatcher.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
 
-					final float homeWinPercent = (hemmaVinster + bortaForluster) / Float.valueOf(hemmaMatcher.size() + bortaMatcher.size()) * 100;
-					final float homeOdds = 100 / homeWinPercent;
+					final float drawPercent = (lika + likaBortalag) / Float.valueOf(hemmaMatcher.size() + bortaMatcher.size()) * 100;
+					final float drawOdds = 100 / drawPercent;
 
 					betAmount = bank * betLevel;
 
-					// Spela på hemma vins?
-					if (homeOdds * betMarginDecimal <= soccerMatch.getOdds1()) {
+					// Spela på lika?
+					if (drawOdds * betMarginDecimal <= soccerMatch.getOddsX()) {
 						betOnGameCount++;
 						bank = bank - betAmount;
-						if (soccerMatch.getHomeScore() > soccerMatch.getAwayScore()) {
+						if (soccerMatch.getHomeScore() == soccerMatch.getAwayScore()) {
 							wins++;
-							bank = bank + betAmount * soccerMatch.getOdds1();
+							bank = bank + betAmount * soccerMatch.getOddsX();
 						}
 					}
 
@@ -105,10 +101,10 @@ public class DrawTeamTestNOTDONE extends TestClass {
 			}
 		}
 
-		if (bestLookBack > 0 && teamSettings.getLookbackHome() != bestLookBack) {
-			System.out.println("Setting lookbackHome " + bestLookBack + " and margin " + bestBetMargin + " for team " + teamSettings.getTeamName() + " with a bank of " + bestBankResult);
-			teamSettings.setLookbackHome(bestLookBack);
-			teamSettings.setMarginHome(bestBetMargin);
+		if (bestLookBack > 0 && teamSettings.getLookbackDraw() != bestLookBack) {
+			System.out.println("Setting lookbackDraw " + bestLookBack + " and margin " + bestBetMargin + " for team " + teamSettings.getTeamName() + " with a bank of " + bestBankResult);
+			teamSettings.setLookbackDraw(bestLookBack);
+			teamSettings.setMarginDraw(bestBetMargin);
 		}
 
 		return teamSettings;

+ 209 - 0
OddsJavaFx/src/tests/DrawTests2.java

@@ -0,0 +1,209 @@
+package tests;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import objects.League;
+import objects.SoccerMatch;
+
+public class DrawTests2 extends TestClass {
+
+	private League leagueInfo;
+
+	ArrayList<results> possitiveResults = new ArrayList<>();
+
+	@Override
+	public void setup(String date, float startingBank, float bettingLevel, Float betMargin, int lookBack, int sportId,
+			Integer countryId, Integer leagueId) {
+		super.setup(date, startingBank, bettingLevel, betMargin, lookBack, sportId, countryId, leagueId);
+
+		leagueInfo = getLeagueInfoById();
+	}
+
+	@Override
+	public void runTest() {
+		final ArrayList<SoccerMatch> matches = getMatches();
+
+		final LocalDateTime startDate = matches.get(0).getGameDate();
+		final LocalDateTime endDate = matches.get(0).getGameDate().plusYears(1);
+
+		List<SoccerMatch> matchesFiltered = matches.stream()
+				.filter(p ->
+				(p.getGameDate().isAfter(matches.get(0).getGameDate()) || p.getGameDate().equals(matches.get(0).getGameDate())) &&
+				p.getGameDate().isBefore(matches.get(0).getGameDate().plusYears(1)))
+				.collect(Collectors.toList());
+
+		final float betLevel = bettingLevel / 100f;
+
+		final LocalDateTime currentDate = matches.get(0).getGameDate();
+		LocalDate localDate = currentDate.toLocalDate();
+		float betAmount = startingBank * betLevel;
+
+		int betOnGameCount = 0;
+		int wins = 0;
+
+		final float bestBankResult = startingBank;
+		final int bestBetMargin = 0;
+		final int bestLookBack = 0;
+
+		System.out.println("Draw Test 2");
+		int i = 0;
+		while (matchesFiltered.size() > 0) {
+			i++;
+			for (int lookBack = 4; lookBack < 25; lookBack++) {
+				for (int betMargin = 1; betMargin < 35; betMargin++) {
+					final float betMarginDecimal = 1 + (betMargin / (float)100);
+					float bank = startingBank;
+					betOnGameCount = 0;
+					wins = 0;
+					for (final SoccerMatch match : matchesFiltered) {
+						if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
+							continue;
+						}
+
+						final List<SoccerMatch> homeMatches = matchesFiltered.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getHomeTeam().getTeamId() == match.getHomeTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+						final List<SoccerMatch> awayMatches = matchesFiltered.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getAwayTeam().getTeamId() == match.getAwayTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+
+						final long hemmaLika = homeMatches.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
+						final long bortaLika = awayMatches.stream().filter(p -> p.getAwayScore() == p.getHomeScore()).count();
+
+						final float drawWinPercent = (hemmaLika + bortaLika) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+						final float drawOdds = 100 / drawWinPercent;
+
+						if (localDate.isBefore(match.getGameDate().toLocalDate())) {
+							betAmount = bank * betLevel;
+							localDate = match.getGameDate().toLocalDate();
+						}
+
+						if (drawOdds * betMarginDecimal <= match.getOddsX()) {
+							betOnGameCount++;
+							bank = bank - betAmount;
+							if (match.getAwayScore() == match.getHomeScore()) {
+								wins++;
+								bank = bank + betAmount * match.getOddsX();
+							}
+						}
+
+					}
+					if (bank > startingBank) {
+						final int lookBack2 = lookBack;
+						final int betMargin2 = betMargin;
+						final java.util.Optional<results> found = possitiveResults.stream().filter(r -> r.getLookback() == lookBack2 && r.getMargin() == betMargin2).findFirst();
+						if (found.isPresent()) {
+							final results results = found.get();
+							results.addCount();
+							results.addBank(bank);
+						} else {
+							possitiveResults.add(new results(lookBack, betMargin, bank));
+						}
+					}
+				}
+			}
+			final LocalDateTime startDate2 = matchesFiltered.get(0).getGameDate().plusYears(1);
+			final LocalDateTime endDate2 = startDate2.plusYears(1);
+			matchesFiltered.clear();
+			matchesFiltered = matches.stream()
+					.filter(p ->
+					(p.getGameDate().isAfter(startDate2) || p.getGameDate().equals(startDate2)) &&
+					p.getGameDate().isBefore(endDate2))
+					.collect(Collectors.toList());
+		}
+
+		Collections.sort(possitiveResults);
+		for (final results res : possitiveResults) {
+			System.out.println("Lookback " + res.getLookback() + " and margin " + res.getMargin() + " At count " + res.getCount() + " of " + i + " with bank: " + res.getBank());
+		}
+	}
+
+	public ArrayList<SoccerMatch> getMatches() {
+		final ArrayList<SoccerMatch> result = new ArrayList<>();
+
+		final String sql = "SELECT res.*, "
+				+ "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
+				+ "FROM SoccerResults as res "
+				+ "Join Team as hTeam ON res.homeTeam = hTeam.id "
+				+ "Join Team as aTeam ON res.awayTeam = aTeam.id "
+				+ "WHERE "
+				+ "res.leagueId = ? "
+				+ "ORDER BY gameDate ASC";
+
+		try {
+			final PreparedStatement stat = getConnection().prepareStatement(sql);
+			stat.setInt(1, leagueId);
+
+			result.addAll(getMatches(stat));
+		} catch (final SQLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+		return result;
+	}
+
+	public class results implements Comparable<results> {
+		int lookback;
+		int margin;
+		int count;
+		float bank;
+
+		public results(int lookback, int margin, float bank) {
+			this.lookback = lookback;
+			this.margin = margin;
+			this.bank = bank;
+			count = 1;
+		}
+
+		public float getBank() {
+			return bank;
+		}
+
+		public void addBank(float bank) {
+			this.bank = (this.bank + bank) / 2;
+		}
+
+		public void addCount() {
+			count++;
+		}
+		public int getCount() {
+			return count;
+		}
+
+		public int getLookback() {
+			return lookback;
+		}
+		public int getMargin() {
+			return margin;
+		}
+		public void setLookback(int lookback) {
+			this.lookback = lookback;
+		}
+		public void setMargin(int margin) {
+			this.margin = margin;
+		}
+
+		@Override
+		public int compareTo(results res) {
+			int returnValue = 0;
+			if (this.count > res.getCount()) {
+				returnValue = -1;
+			} else if (this.count == res.getCount()) {
+				if (this.bank > res.getBank()) {
+					returnValue = -1;
+				} else if (this.bank < res.getBank()) {
+					returnValue = 1;
+				} else {
+					returnValue = 0;
+				}
+			} else if (this.count < res.getCount()) {
+				returnValue = 1;
+			}
+			return returnValue;
+		}
+	}
+}

+ 0 - 4
OddsJavaFx/src/tests/HomeTeamWinTest.java

@@ -68,13 +68,9 @@ public class HomeTeamWinTest extends TestClass {
 					// Hemma lagets resultat
 					final List<SoccerMatch> hemmaMatcher = aktuellaMatcher.stream().filter(p -> p.getHomeTeam().getTeamId() == teamSettings.getTeamId()).limit(lookback).collect(Collectors.toList());
 					final long hemmaVinster = hemmaMatcher.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
-					final long lika = hemmaMatcher.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
-					final long hemmaForluster = hemmaMatcher.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
 
 					// borta lagets resultat
 					final List<SoccerMatch> bortaMatcher = aktuellaMatcher.stream().filter(p -> p.getAwayTeam().getTeamId() == soccerMatch.getAwayTeam().getTeamId()).limit(lookback).collect(Collectors.toList());
-					final long bortaVinster = bortaMatcher.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
-					final long likaBortalag = bortaMatcher.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
 					final long bortaForluster = bortaMatcher.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
 
 					final float homeWinPercent = (hemmaVinster + bortaForluster) / Float.valueOf(hemmaMatcher.size() + bortaMatcher.size()) * 100;

+ 2 - 0
OddsJavaFx/src/tests/HomeTests.java

@@ -29,6 +29,8 @@ public class HomeTests extends TestClass {
 	public void runTest() {
 		final ArrayList<SoccerMatch> matches = getMatches();
 
+		final LocalDateTime startDate = matches.get(0).getGameDate();
+
 		final float betLevel = bettingLevel / 100f;
 
 		final LocalDateTime currentDate = matches.get(0).getGameDate();

+ 152 - 0
OddsJavaFx/src/tests/HomeTests2.java

@@ -0,0 +1,152 @@
+package tests;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import objects.League;
+import objects.SoccerMatch;
+
+public class HomeTests2 extends TestClass {
+
+	private League leagueInfo;
+
+	ArrayList<results> possitiveResults = new ArrayList<>();
+
+	@Override
+	public void setup(String date, float startingBank, float bettingLevel, Float betMargin, int lookBack, int sportId,
+			Integer countryId, Integer leagueId) {
+		super.setup(date, startingBank, bettingLevel, betMargin, lookBack, sportId, countryId, leagueId);
+
+		leagueInfo = getLeagueInfoById();
+	}
+
+	@Override
+	public void runTest() {
+		final ArrayList<SoccerMatch> matches = getMatches();
+
+		final LocalDateTime startDate = matches.get(0).getGameDate();
+		final LocalDateTime endDate = matches.get(0).getGameDate().plusYears(1);
+
+		List<SoccerMatch> matchesFiltered = matches.stream()
+				.filter(p ->
+				(p.getGameDate().isAfter(matches.get(0).getGameDate()) || p.getGameDate().equals(matches.get(0).getGameDate())) &&
+				p.getGameDate().isBefore(matches.get(0).getGameDate().plusYears(1)))
+				.collect(Collectors.toList());
+
+		final float betLevel = bettingLevel / 100f;
+
+		final LocalDateTime currentDate = matches.get(0).getGameDate();
+		LocalDate localDate = currentDate.toLocalDate();
+		float betAmount = startingBank * betLevel;
+
+		int betOnGameCount = 0;
+		int wins = 0;
+
+		final float bestBankResult = startingBank;
+		final int bestBetMargin = 0;
+		final int bestLookBack = 0;
+
+		System.out.println("Home Test 2");
+
+		int i = 0;
+		while (matchesFiltered.size() > 0) {
+			i++;
+			for (int lookBack = 4; lookBack < 25; lookBack++) {
+				for (int betMargin = 1; betMargin < 35; betMargin++) {
+					final float betMarginDecimal = 1 + (betMargin / (float)100);
+					float bank = startingBank;
+					betOnGameCount = 0;
+					wins = 0;
+					for (final SoccerMatch match : matchesFiltered) {
+						if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
+							continue;
+						}
+
+						final List<SoccerMatch> homeMatches = matchesFiltered.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getHomeTeam().getTeamId() == match.getHomeTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+						final List<SoccerMatch> awayMatches = matchesFiltered.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getAwayTeam().getTeamId() == match.getAwayTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+
+						final long hemmaVinster = homeMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
+						final long hemmaForluster= homeMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
+
+						final long bortaVinster = awayMatches.stream().filter(p -> p.getAwayScore() > p.getHomeScore()).count();
+						final long bortaForluster = awayMatches.stream().filter(p -> p.getAwayScore() < p.getHomeScore()).count();
+
+						final float homeWinPercent = (hemmaVinster + bortaForluster) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+						final float homeOdds = 100 / homeWinPercent;
+
+						if (localDate.isBefore(match.getGameDate().toLocalDate())) {
+							betAmount = bank * betLevel;
+							localDate = match.getGameDate().toLocalDate();
+						}
+
+						if (homeOdds * betMarginDecimal <= match.getOdds1()) {
+							betOnGameCount++;
+							bank = bank - betAmount;
+							if (match.getHomeScore() > match.getAwayScore()) {
+								wins++;
+								bank = bank + betAmount * match.getOdds1();
+							}
+						}
+
+					}
+					if (bank > startingBank) {
+						final int lookBack2 = lookBack;
+						final int betMargin2 = betMargin;
+						final java.util.Optional<results> found = possitiveResults.stream().filter(r -> r.getLookback() == lookBack2 && r.getMargin() == betMargin2).findFirst();
+						if (found.isPresent()) {
+							final results results = found.get();
+							results.addCount();
+							results.addBank(bank);
+						} else {
+							possitiveResults.add(new results(lookBack, betMargin, bank));
+						}
+					}
+				}
+			}
+			final LocalDateTime startDate2 = matchesFiltered.get(0).getGameDate().plusYears(1);
+			final LocalDateTime endDate2 = startDate2.plusYears(1);
+			matchesFiltered.clear();
+			matchesFiltered = matches.stream()
+					.filter(p ->
+					(p.getGameDate().isAfter(startDate2) || p.getGameDate().equals(startDate2)) &&
+					p.getGameDate().isBefore(endDate2))
+					.collect(Collectors.toList());
+		}
+
+		Collections.sort(possitiveResults);
+		for (final results res : possitiveResults) {
+			System.out.println("Lookback " + res.getLookback() + " and margin " + res.getMargin() + " At count " + res.getCount() + " of " + i + " with bank: " + res.getBank());
+		}
+	}
+
+	public ArrayList<SoccerMatch> getMatches() {
+		final ArrayList<SoccerMatch> result = new ArrayList<>();
+
+		final String sql = "SELECT res.*, "
+				+ "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
+				+ "FROM SoccerResults as res "
+				+ "Join Team as hTeam ON res.homeTeam = hTeam.id "
+				+ "Join Team as aTeam ON res.awayTeam = aTeam.id "
+				+ "WHERE "
+				+ "res.leagueId = ? "
+				+ "ORDER BY gameDate ASC";
+
+		try {
+			final PreparedStatement stat = getConnection().prepareStatement(sql);
+			stat.setInt(1, leagueId);
+
+			result.addAll(getMatches(stat));
+		} catch (final SQLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+		return result;
+	}
+}

+ 324 - 0
OddsJavaFx/src/tests/TestClass.java

@@ -4,8 +4,13 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
 
 import com.google.common.collect.Lists;
 
@@ -24,7 +29,17 @@ public abstract class TestClass {
 	int sportId;
 	int countryId;
 	int leagueId;
+	protected int bestAwayLookback;
+	protected int bestAwayMargin;
+	protected int bestDrawMargin;
+	protected int bestDrawLookback;
+	protected int bestHomeMargin;
+	protected int bestHomeLookback;
+	protected float bestDrawBank;
+	protected float bestAwayBank;
+	protected float bestHomeBank;
 
+	ArrayList<results> possitiveResults = new ArrayList<>();
 
 	public abstract void runTest();
 
@@ -91,4 +106,313 @@ public abstract class TestClass {
 	public League getLeagueInfoByName(String leagueName) {
 		return GuiMysql.getInstance().getLeagueInfo(leagueName);
 	}
+
+	public void CalculateLookback(int leagueId, LocalDateTime date, int daysToCheck) {
+		final LocalDateTime startDate = date.minusDays(daysToCheck);
+		final String sql = "SELECT res.*, "
+				+ "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
+				+ "FROM SoccerResults as res "
+				+ "Join Team as hTeam ON res.homeTeam = hTeam.id "
+				+ "Join Team as aTeam ON res.awayTeam = aTeam.id "
+				+ "WHERE res.leagueId = ? AND DATE(gameDate) < ? AND DATE(gameDate) > ? ORDER BY gameDate ASC";
+
+		try {
+			final PreparedStatement stmt = getConnection().prepareStatement(sql);
+			stmt.setInt(1, leagueId);
+			stmt.setString(2, date.format(DateTimeFormatter.ISO_DATE));
+			stmt.setString(3, startDate.format(DateTimeFormatter.ISO_DATE));
+
+			final ArrayList<SoccerMatch> matches = getMatches(stmt);
+			stmt.close();
+
+			if (matches.size() > 0) {
+				this.bestHomeBank = startingBank;
+				this.bestDrawBank = startingBank;
+				this.bestAwayBank = startingBank;
+
+				// TODO Fixa så att man kontrollerar 1 år bakåt i tiden som Filtered Matches,
+				// plocka bort om possitive results är färre än 50%
+				recalcHome(matches);
+				recalcDraw(matches);
+				recalcAway(matches);
+			}
+		} catch (final SQLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+	}
+
+	private void recalcHome(ArrayList<SoccerMatch> matches) {
+		if (this.bestHomeBank < 0) {
+			this.bestHomeBank = startingBank;
+		}
+
+		this.bestHomeLookback = 0;
+		this.bestHomeMargin = 0;
+
+		final float betLevel = bettingLevel / 100f;
+
+		final LocalDateTime currentDate = matches.get(0).getGameDate();
+		LocalDate localDate = currentDate.toLocalDate();
+		float betAmount = startingBank * betLevel;
+
+		possitiveResults.clear();
+
+		for (int lookBack = 4; lookBack < 25; lookBack++) {
+			for (int betMargin = 4; betMargin < 35; betMargin++) {
+				final float betMarginDecimal = 1 + (betMargin / (float)100);
+				float bank = startingBank;
+				for (final SoccerMatch match : matches) {
+					if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
+						continue;
+					}
+
+					final List<SoccerMatch> homeMatches = matches.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getHomeTeam().getTeamId() == match.getHomeTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+					final List<SoccerMatch> awayMatches = matches.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getAwayTeam().getTeamId() == match.getAwayTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+
+					final long hemmaVinster = homeMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
+					final long bortaForluster = awayMatches.stream().filter(p -> p.getAwayScore() < p.getHomeScore()).count();
+
+					final float homeWinPercent = (hemmaVinster + bortaForluster) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+					final float homeOdds = 100 / homeWinPercent;
+
+					if (localDate.isBefore(match.getGameDate().toLocalDate())) {
+						betAmount = bank * betLevel;
+						localDate = match.getGameDate().toLocalDate();
+					}
+
+					if (homeOdds * betMarginDecimal <= match.getOdds1()) {
+						bank = bank - betAmount;
+						if (match.getHomeScore() > match.getAwayScore()) {
+							bank = bank + betAmount * match.getOdds1();
+						}
+					}
+
+				}
+				if (bank > startingBank) {
+					final int lookBack2 = lookBack;
+					final int betMargin2 = betMargin;
+					final java.util.Optional<results> found = possitiveResults.stream().filter(r -> r.getLookback() == lookBack2 && r.getMargin() == betMargin2).findFirst();
+					if (found.isPresent()) {
+						final results results = found.get();
+						results.addCount();
+						results.addBank(bank);
+					} else {
+						possitiveResults.add(new results(lookBack, betMargin, bank));
+					}
+				}
+			}
+		}
+		Collections.sort(possitiveResults);
+		if (possitiveResults.size() > 0) {
+			final results res = possitiveResults.get(0);
+			this.bestHomeLookback = res.getLookback();
+			this.bestHomeMargin = res.getMargin();
+			this.bestHomeBank = res.getBank();
+		}
+	}
+
+	private void recalcDraw(ArrayList<SoccerMatch> matches) {
+		if (this.bestDrawBank < 0) {
+			this.bestDrawBank = startingBank;
+		}
+
+		this.bestDrawLookback = 0;
+		this.bestDrawMargin = 0;
+
+		final float betLevel = bettingLevel / 100f;
+
+		final LocalDateTime currentDate = matches.get(0).getGameDate();
+		LocalDate localDate = currentDate.toLocalDate();
+		float betAmount = startingBank * betLevel;
+
+		possitiveResults.clear();
+
+		for (int lookBack = 4; lookBack < 25; lookBack++) {
+			for (int betMargin = 4; betMargin < 35; betMargin++) {
+				final float betMarginDecimal = 1 + (betMargin / (float)100);
+				float bank = startingBank;
+				for (final SoccerMatch match : matches) {
+					if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
+						continue;
+					}
+
+					final List<SoccerMatch> homeMatches = matches.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getHomeTeam().getTeamId() == match.getHomeTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+					final List<SoccerMatch> awayMatches = matches.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getAwayTeam().getTeamId() == match.getAwayTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+
+					final long hemmaLika = homeMatches.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
+					final long bortaLika = awayMatches.stream().filter(p -> p.getAwayScore() == p.getHomeScore()).count();
+
+					final float drawPercent = (hemmaLika + bortaLika) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+					final float drawOdds = 100 / drawPercent;
+
+					if (localDate.isBefore(match.getGameDate().toLocalDate())) {
+						betAmount = bank * betLevel;
+						localDate = match.getGameDate().toLocalDate();
+					}
+
+					if (drawOdds * betMarginDecimal <= match.getOddsX()) {
+						bank = bank - betAmount;
+						if (match.getHomeScore() == match.getAwayScore()) {
+							bank = bank + betAmount * match.getOddsX();
+						}
+					}
+
+				}
+				if (bank > startingBank) {
+					final int lookBack2 = lookBack;
+					final int betMargin2 = betMargin;
+					final java.util.Optional<results> found = possitiveResults.stream().filter(r -> r.getLookback() == lookBack2 && r.getMargin() == betMargin2).findFirst();
+					if (found.isPresent()) {
+						final results results = found.get();
+						results.addCount();
+						results.addBank(bank);
+					} else {
+						possitiveResults.add(new results(lookBack, betMargin, bank));
+					}
+				}
+			}
+		}
+		Collections.sort(possitiveResults);
+		if (possitiveResults.size() > 0) {
+			final results res = possitiveResults.get(0);
+			this.bestDrawLookback = res.getLookback();
+			this.bestDrawMargin = res.getMargin();
+			this.bestDrawBank = res.getBank();
+		}
+	}
+
+	private void recalcAway(ArrayList<SoccerMatch> matches) {
+		if (this.bestAwayBank < 0) {
+			this.bestAwayBank = startingBank;
+		}
+
+		this.bestAwayLookback = 0;
+		this.bestAwayMargin = 0;
+
+		final float betLevel = bettingLevel / 100f;
+
+		final LocalDateTime currentDate = matches.get(0).getGameDate();
+		LocalDate localDate = currentDate.toLocalDate();
+		float betAmount = startingBank * betLevel;
+
+		possitiveResults.clear();
+
+		for (int lookBack = 4; lookBack < 25; lookBack++) {
+			for (int betMargin = 4; betMargin < 35; betMargin++) {
+				final float betMarginDecimal = 1 + (betMargin / (float)100);
+				float bank = startingBank;
+				for (final SoccerMatch match : matches) {
+					if (match.getHomeScore() < 0 || match.getAwayScore() < 0) {
+						continue;
+					}
+
+					final List<SoccerMatch> homeMatches = matches.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getHomeTeam().getTeamId() == match.getHomeTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+					final List<SoccerMatch> awayMatches = matches.stream().filter(p -> p.getGameDate().isBefore(match.getGameDate()) && p.getAwayTeam().getTeamId() == match.getAwayTeam().getTeamId()).limit(lookBack).collect(Collectors.toList());
+
+					final long hemmaForluster = homeMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
+					final long bortaVinster = awayMatches.stream().filter(p -> p.getAwayScore() > p.getHomeScore()).count();
+
+					final float awayWinPercent = (hemmaForluster + bortaVinster) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100;
+					final float awayOdds = 100 / awayWinPercent;
+
+					if (localDate.isBefore(match.getGameDate().toLocalDate())) {
+						betAmount = bank * betLevel;
+						localDate = match.getGameDate().toLocalDate();
+					}
+
+					if (awayOdds * betMarginDecimal <= match.getOdds2()) {
+						bank = bank - betAmount;
+						if (match.getHomeScore() < match.getAwayScore()) {
+							bank = bank + betAmount * match.getOdds2();
+						}
+					}
+
+				}
+
+				if (bank > startingBank) {
+					final int lookBack2 = lookBack;
+					final int betMargin2 = betMargin;
+					final java.util.Optional<results> found = possitiveResults.stream().filter(r -> r.getLookback() == lookBack2 && r.getMargin() == betMargin2).findFirst();
+					if (found.isPresent()) {
+						final results results = found.get();
+						results.addCount();
+						results.addBank(bank);
+					} else {
+						possitiveResults.add(new results(lookBack, betMargin, bank));
+					}
+				}
+			}
+		}
+
+		Collections.sort(possitiveResults);
+		if (possitiveResults.size() > 0) {
+			final results res = possitiveResults.get(0);
+			this.bestAwayLookback = res.getLookback();
+			this.bestAwayMargin = res.getMargin();
+			this.bestAwayBank = res.getBank();
+		}
+	}
+
+	public class results implements Comparable<results> {
+		int lookback;
+		int margin;
+		int count;
+		float bank;
+
+		public results(int lookback, int margin, float bank) {
+			this.lookback = lookback;
+			this.margin = margin;
+			this.bank = bank;
+			count = 1;
+		}
+
+		public float getBank() {
+			return bank;
+		}
+
+		public void addBank(float bank) {
+			this.bank = (this.bank + bank) / 2;
+		}
+
+		public void addCount() {
+			count++;
+		}
+		public int getCount() {
+			return count;
+		}
+
+		public int getLookback() {
+			return lookback;
+		}
+		public int getMargin() {
+			return margin;
+		}
+		public void setLookback(int lookback) {
+			this.lookback = lookback;
+		}
+		public void setMargin(int margin) {
+			this.margin = margin;
+		}
+
+		@Override
+		public int compareTo(results res) {
+			int returnValue = 0;
+			if (this.count > res.getCount()) {
+				returnValue = -1;
+			} else if (this.count == res.getCount()) {
+				if (this.bank > res.getBank()) {
+					returnValue = -1;
+				} else if (this.bank < res.getBank()) {
+					returnValue = 1;
+				} else {
+					returnValue = 0;
+				}
+			} else if (this.count < res.getCount()) {
+				returnValue = 1;
+			}
+			return returnValue;
+		}
+	}
 }

+ 0 - 10
OddsJavaFx/src/tests/addedScoringTest.java

@@ -1,10 +0,0 @@
-package tests;
-
-public class addedScoringTest extends TestClass {
-
-	@Override
-	public void runTest() {
-
-	}
-
-}

+ 158 - 0
OddsJavaFx/src/tests/recalcTest.java

@@ -0,0 +1,158 @@
+package tests;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import objects.SoccerMatch;
+
+public class recalcTest extends TestClass {
+
+	float prevHomeBank = -1;
+	float prevDrawBank = -1;
+	float prevAwayBank = -1;
+
+	@Override
+	public void runTest() {
+
+		final int daysInPast = 360;
+		final int recalcDays = 14;
+
+		final ArrayList<SoccerMatch> matches = getMatches();
+
+		final LocalDateTime startDate = matches.get(0).getGameDate();
+		LocalDateTime endDate = startDate.plusYears(1);
+
+		CalculateLookback(leagueId, startDate, daysInPast);
+		LocalDateTime recalcDate = startDate.plusDays(recalcDays);
+
+		float bank = startingBank;
+		final float betLevel = this.bettingLevel / 100f;
+		float betAmount = bank * betLevel;
+
+		int betHomeCount = 0;
+		int betDrawCount = 0;
+		int betAwayCount = 0;
+
+		int winHome = 0;
+		int winDraw = 0;
+		int winAway = 0;
+
+
+		for (final SoccerMatch soccerMatch : matches) {
+			if (soccerMatch.getGameDate().isAfter(endDate)) {
+				System.out.println("End date reached " + endDate.format(DateTimeFormatter.ISO_DATE) + " with bank: " + bank +
+						" Betted on: " + betHomeCount + "," + betDrawCount + "," + betAwayCount +
+						" winPercent " + (betHomeCount>0?winHome/Float.valueOf(betHomeCount):0) + "," +
+						(betDrawCount>0?winDraw/Float.valueOf(betDrawCount):0) + "," +
+						(betAwayCount>0?winAway/Float.valueOf(betAwayCount):0));
+				bank = startingBank;
+				endDate = soccerMatch.getGameDate().plusYears(1);
+
+				betHomeCount = 0;
+				betDrawCount = 0;
+				betAwayCount = 0;
+
+				winHome = 0;
+				winDraw = 0;
+				winAway = 0;
+			}
+			if (recalcDate.isBefore(soccerMatch.getGameDate())) {
+				final int prevBestLookbackHome = this.bestHomeLookback;
+				final int prevBestLookbackDraw = this.bestDrawLookback;
+				final int prevBestLookbackAway = this.bestAwayLookback;
+
+				final int prevBestMarginHome = this.bestHomeMargin;
+				final int prevBestMarginDraw = this.bestDrawMargin;
+				final int prevBestMarginAway = this.bestAwayMargin;
+
+				CalculateLookback(leagueId, soccerMatch.getGameDate(), daysInPast);
+
+				recalcDate = soccerMatch.getGameDate().plusDays(recalcDays);
+			}
+
+			final List<SoccerMatch> homeMatches = matches.stream().filter(p -> p.getGameDate().isBefore(soccerMatch.getGameDate()) && p.getHomeTeam().getTeamId() == soccerMatch.getHomeTeam().getTeamId()).limit(bestHomeLookback).collect(Collectors.toList());
+			final List<SoccerMatch> awayMatches = matches.stream().filter(p -> p.getGameDate().isBefore(soccerMatch.getGameDate()) && p.getAwayTeam().getTeamId() == soccerMatch.getAwayTeam().getTeamId()).limit(bestAwayLookback).collect(Collectors.toList());
+
+			final long homeWins = homeMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
+			final long homeLosses = homeMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count(); // borde det vara <=??
+			final long homeDraws = homeMatches.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
+
+			final long awayWins = awayMatches.stream().filter(p -> p.getHomeScore() < p.getAwayScore()).count();
+			final long awayLosses = awayMatches.stream().filter(p -> p.getHomeScore() > p.getAwayScore()).count();
+			final long awayDraws = awayMatches.stream().filter(p -> p.getHomeScore() == p.getAwayScore()).count();
+
+			final float hemmaPercent = (homeWins + awayLosses) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100f;
+			final float bortaPercent = (awayWins + homeLosses) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100f;
+			final float likaPercent = (homeDraws + awayDraws) / Float.valueOf(homeMatches.size() + awayMatches.size()) * 100f;
+
+			final float homeOdds = 100 / hemmaPercent;
+			final float drawOdds = 100 / likaPercent;
+			final float awayOdds = 100 / bortaPercent;
+
+			betAmount = bank * betLevel;
+
+			if (this.bestHomeMargin > 0 && homeOdds * (1 + (this.bestHomeMargin / 100f)) <= soccerMatch.getOdds1()) {
+				bank -= betAmount;
+				betHomeCount++;
+				if (soccerMatch.getHomeScore() > soccerMatch.getAwayScore()) {
+					winHome++;
+					bank += betAmount * soccerMatch.getOdds1();
+				}
+			}
+			if (this.bestDrawMargin > 0 && drawOdds * (1 + (this.bestDrawMargin / 100f)) <= soccerMatch.getOddsX()) {
+				bank -= betAmount;
+				betDrawCount++;
+				if (soccerMatch.getHomeScore() == soccerMatch.getAwayScore()) {
+					winDraw++;
+					bank += betAmount * soccerMatch.getOddsX();
+				}
+			}
+			if (this.bestAwayMargin > 0 && awayOdds * (1 + (this.bestAwayMargin / 100f)) <= soccerMatch.getOdds2()) {
+				bank -= betAmount;
+				betAwayCount++;
+				if (soccerMatch.getHomeScore() < soccerMatch.getAwayScore()) {
+					winAway++;
+					bank += betAmount * soccerMatch.getOdds2();
+				}
+			}
+		}
+
+		System.out.println("End of test reached with bank: " + bank +
+				" Betted on: " + betHomeCount + "," + betDrawCount + "," + betAwayCount +
+				" winPercent " + (betHomeCount>0?winHome/Float.valueOf(betHomeCount):0) + "," +
+				(betDrawCount>0?winDraw/Float.valueOf(betDrawCount):0) + "," +
+				(betAwayCount>0?winAway/Float.valueOf(betAwayCount):0));
+
+		System.out.println("Ending Margins " + this.bestHomeMargin + ", " + this.bestDrawMargin + ", " + this.bestAwayMargin + System.lineSeparator() + "Best Lookback " + this.bestHomeLookback + ", " + this.bestDrawLookback  + ", " + this.bestAwayLookback);
+	}
+
+
+	private ArrayList<SoccerMatch> getMatches() {
+		final ArrayList<SoccerMatch> result = new ArrayList<>();
+
+		final String sql = "SELECT res.*, "
+				+ "hTeam.name as homeTeamName, aTeam.name as awayTeamName "
+				+ "FROM SoccerResults as res "
+				+ "Join Team as hTeam ON res.homeTeam = hTeam.id "
+				+ "Join Team as aTeam ON res.awayTeam = aTeam.id "
+				+ "WHERE "
+				+ "res.leagueId = ? "
+				+ "ORDER BY gameDate ASC";
+
+		try {
+			final PreparedStatement stmt = getConnection().prepareStatement(sql);
+			stmt.setInt(1, leagueId);
+
+			result.addAll(super.getMatches(stmt));
+		} catch (final SQLException e) {
+			e.printStackTrace();
+		}
+
+		return result;
+	}
+}

+ 3 - 1
OddsJavaFx/src/web/index.php

@@ -87,7 +87,7 @@ $leagues = $conn->getLeagues();
         echo "<td>" . $name . "</td>";
         echo "<td>" . $res['wins'] . "</td>";
         echo "<td>" . $res['losses'] . "</td>";
-        echo "<td>" . $res['wins'] / ($res['wins'] + $res['losses']) . "</td>";
+        echo "<td>" . round($res['wins'] / ($res['wins'] + $res['losses']),2) . "</td>";
         echo "<td>" . $res['result'] . "</td>";
         echo "<td>" . $res['pending'] . "</td>";
     }
@@ -110,6 +110,7 @@ $dayStats = $conn->getDayBettingsStats();
 <th>Datum</th>
 <th>Resultat</th>
 <th>Games Played</th>
+<th>Bet amount</th>
 </tr>
 </thead>
 <tbody>
@@ -120,6 +121,7 @@ foreach ($dayStats as $ds) {
     echo "<td>" . $ds['date'] . "</td>";
     echo "<td>" . $ds['sumAmount'] . "</td>";
     echo "<td>" . $ds['numGames'] . "</td>";
+    echo "<td>" . $ds['betAmount'] . "</td>";
     echo "</tr>";
     $sum += $ds['sumAmount'];
 }

+ 5 - 4
OddsJavaFx/src/web/webDbConnection.php

@@ -43,7 +43,7 @@ class WebDbConnection {
     
     public function getLeagues() {
         $mysql = $this->dbCon->getConnection();
-        $sql = "SELECT id, name, countryId FROM League WHERE id IN (SELECT leagueId FROM SoccerResults WHERE DATE(gameDate) >= DATE(NOW())) GROUP BY name ORDER BY prio DESC, name ASC";
+        $sql = "SELECT id, name, countryId FROM League WHERE id IN (SELECT leagueId FROM SoccerResults WHERE DATE(gameDate) >= DATE(NOW())) GROUP BY countryId, name ORDER BY prio DESC, name ASC";
         
 //         $result = $mysql->query( $sql );
 //         if ($result === false) {
@@ -170,12 +170,13 @@ class WebDbConnection {
     public function getDayBettingsStats() {
         $sql = "SELECT DATE(gameDate) as date, ROUND(SUM(
     CASE
-        WHEN betOn = '1' AND sr.homeScore > sr.awayScore THEN amount * odds
-        WHEN betOn = 'X' AND sr.homeScore = sr.awayScore THEN amount * odds
-        WHEN betOn = '2' AND sr.homeScore < sr.awayScore THEN amount * odds
+        WHEN betOn = '1' AND sr.homeScore > sr.awayScore THEN (amount * odds) -amount
+        WHEN betOn = 'X' AND sr.homeScore = sr.awayScore THEN (amount * odds) -amount
+        WHEN betOn = '2' AND sr.homeScore < sr.awayScore THEN (amount * odds) -amount
 		ELSE -amount
 	END
 ),2 ) as sumAmount, 
+SUM(amount) as betAmount, 
 COUNT(gameDate) as numGames
 FROM `Bets` INNER JOIN SoccerResults sr ON matchId = sr.id GROUP BY DATE(sr.gameDate) ORDER BY sr.gameDate ASC";