def backtest_algo2(self, number, dates, algo): #level number corresponds to an index wins = [0] * 11 losses = [0] * 11 #creates processes num_processes = 16 start = int(number * (len(dates) / num_processes)) end = int((number + 1) * (len(dates) / num_processes)) print() print(number) print(start) print(end) print() #this is actually a do-while loop for x in range(start, end): print("At " + str(dates[x]) + " END: " + str(dates[end - 1])) games = self.universal.get_games(dates[x]) for y in range(0, len(games)): #team might not exist anymore, so don't count that game if len(games[y]['team1']) != 0 and len(games[y]['team2']) != 0: data1 = self.universal.load_data(games[y]['team1'], games[y]['date']) data2 = self.universal.load_data(games[y]['team2'], games[y]['date']) returned1 = self.odds_calculator.analyze2( games[y]['team1'], games[y]['team2'], data1, "away") returned2 = self.odds_calculator.analyze2( games[y]['team2'], games[y]['team1'], data2, "home") algorithm = Algo(self.league) #sets algo to backtest algorithm.algorithm[self.league] = algo if self.algo_version == "Algo_V1": algo_data = algorithm.calculate( games[y]['date'], returned1, returned2) elif self.algo_version == "Algo_V2": algo_data = algorithm.calculate_V2( games[y]['date'], returned1, returned2) total = algo_data['total'] if self.algo_version == "Algo_V1": #categorizes odds levels = [] levels.append([0, 3]) levels.append([3, 6]) levels.append([6, 9]) levels.append([9, 12]) levels.append([12, 15]) levels.append([15, 18]) levels.append([18, 21]) levels.append([21, 24]) levels.append([24, 27]) levels.append([27, 100]) elif self.algo_version == "Algo_V2": #categorizes odds levels = [] levels.append([50, 55]) levels.append([55, 60]) levels.append([60, 65]) levels.append([65, 70]) levels.append([70, 75]) levels.append([75, 80]) levels.append([80, 85]) levels.append([85, 90]) levels.append([90, 95]) levels.append([95, 100]) level = 0 for z in range(0, len(levels)): if (total >= levels[z][0] and total < levels[z][1] ) or (total * -1 >= levels[z][0] and total * -1 < levels[z][1]): level = z + 1 # #0 is team1, and 1 is team2 # projected_team=0 # if self.league=="nba": # #if team1 is projected to win # if total>0: # projected_team=0 # #go with home team # elif total<=0: # projected_team=1 # else: # #if team1 is projected to win # if total>0: # projected_team=0 # #go with home team # elif total<=0: # projected_team=1 #0 is team1, and 1 is team2 #if team1 is projected to win if total > 0: projected_team = 0 #go with home team elif total <= 0: projected_team = 1 #0 is team1, and 1 is team2 winning_team = 0 if games[y]['game_scores'][0] > games[y]['game_scores'][1]: winning_team = 0 else: winning_team = 1 #if algo was right if projected_team == winning_team: wins[level] += 1 else: losses[level] += 1 temp = [] for x in range(0, len(wins)): temp.append([losses[x], wins[x]]) algo_list = str(algo).replace("[", "").replace("]", "").replace(" ", "") path = "./" + str(self.league) + "/analyze/backtests/" + str( algo_list) + "_temp" + str(number) + ".csv" self.universal.save_to_csv(path, temp)
def backtest_odds(self, start_date, end_date): #breaks up date temp = start_date.split("-") month = int(temp[0]) day = int(temp[1]) year = int(temp[2]) cur_date = start_date content = [] #this is actually a do-while loop while True: games = self.universal.get_games(cur_date) print("date: " + cur_date) for game in games: print(" Game: " + str(game)) # - Strategy 0.0: Bet on algo's projected winner, no matter the odds. # - Strategy 0.1: Bet on oddsmaker's projected winner, no matter the odds. # All below strategies incorporate placing a bet if the algorithm projects a team to win more often than the oddsmaker projects # - Strategy 1: Default strategy. # - Strategy 2: Placing a bet if that team is also the algo's favorite. # - Strategy 3: Placing a bet if that team is the algo's favorite, and the oddsmaker's underdog. # - Strategy 4: Placing a bet if the difference between the algorithm's projected odds and the oddsmaker's odds is also >= 45 to_save = [] strat00 = {"total_bet": 0, "total_win": 0} strat01 = {"total_bet": 0, "total_win": 0} strat1 = {"total_bet": 0, "total_win": 0} strat2 = {"total_bet": 0, "total_win": 0} strat3 = {"total_bet": 0, "total_win": 0} strat4 = {"total_bet": 0, "total_win": 0} for x in range(0, len(games)): #team might not exist anymore, so don't count that game if len(games[x]['team1']) != 0 and len(games[x]['team2']) != 0: data1 = self.universal.load_data(games[x]['team1'], games[x]['date']) data2 = self.universal.load_data(games[x]['team2'], games[x]['date']) returned1 = self.odds_calculator.analyze2( games[x]['team1'], games[x]['team2'], data1, "away") returned2 = self.odds_calculator.analyze2( games[x]['team2'], games[x]['team1'], data2, "home") # print(returned1) # print() # print(returned2) # print() algo = Algo(self.league) algo_data = algo.calculate_V2(games[x]['date'], returned1, returned2) total = algo_data['total'] # to_return={} # to_return['record_points']= odds['records'] # to_return['home_away_points']= odds['home_away'] # to_return['home_away_10_games_points']= odds['home_away_10_games'] # to_return['last_10_games_points']= odds['last_10_games'] # to_return['avg_points']= odds['avg_points'] # to_return['avg_points_10_games']= odds['avg_points_10_games'] # # to_return['win_streak']= win_streak # to_return['win_streak_home_away']= odds['win_streak_home_away'] # to_return['total']= self.universal.convert_number(average) odds = abs(total) favorable_odds = round((100 / (100 - abs(odds)) - 1) * 100) underdog_odds = round((100 / (100 - abs(odds)) - 1) * 100) # print(str(year)+" | "+str(games[x]['team1'])+" | "+str(games[x]['team2'])+" | "+str(games[x]['game_scores'])) oddsportal_odds = self.universal.get_odds_game( year, games[x]['team1'], games[x]['team2'], games[x]['game_scores']) if oddsportal_odds[0] != 0: to_add = [] #date to_add.append(cur_date) #away to_add.append(games[x]['team1'][1]) #home to_add.append(games[x]['team2'][1]) #Algo Proj to_add.append(str(total) + "%") #Away proj away_proj = 0 if total < 0: away_proj = favorable_odds else: away_proj = underdog_odds * -1 to_add.append(away_proj) #Home proj home_proj = 0 if total > 0: home_proj = favorable_odds else: home_proj = underdog_odds * -1 to_add.append(home_proj) #Away odds to_add.append(oddsportal_odds[0]) #Home odds to_add.append(oddsportal_odds[1]) #Diff Away away_diff = 0 if abs(away_proj - oddsportal_odds[0]) > 200: away_diff = abs(away_proj - oddsportal_odds[0]) - 200 else: away_diff = abs(away_proj - oddsportal_odds[0]) to_add.append(away_diff) #Diff Home home_diff = 0 if abs(home_proj - oddsportal_odds[1]) > 200: home_diff = abs(home_proj - oddsportal_odds[1]) - 200 else: home_diff = abs(home_proj - oddsportal_odds[1]) to_add.append(home_diff) ## Strategy 0.0 ## if away_proj < 0: #Bet to_add.append("$100") strat00['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[0] > 0): to_win = 100 * (oddsportal_odds[0] / 100) else: to_win = 100 / (oddsportal_odds[0] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][0] > games[x][ 'game_scores'][1]: to_add.append("$" + str(100 + to_win)) strat00['total_win'] += (100 + to_win) else: to_add.append("$0") else: #Bet to_add.append("$100") strat00['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[1] > 0): to_win = 100 * (oddsportal_odds[1] / 100) else: to_win = 100 / (oddsportal_odds[1] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][1] > games[x][ 'game_scores'][0]: to_add.append("$" + str(100 + to_win)) strat00['total_win'] += (100 + to_win) else: to_add.append("$0") ## Strategy 0.1 ## if oddsportal_odds[0] < 0 and oddsportal_odds[ 0] < oddsportal_odds[1]: #Bet to_add.append("$100") strat01['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[0] > 0): to_win = 100 * (oddsportal_odds[0] / 100) else: to_win = 100 / (oddsportal_odds[0] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][0] > games[x][ 'game_scores'][1]: to_add.append("$" + str(100 + to_win)) strat01['total_win'] += (100 + to_win) else: to_add.append("$0") else: #Bet to_add.append("$100") strat01['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[1] > 0): to_win = 100 * (oddsportal_odds[1] / 100) else: to_win = 100 / (oddsportal_odds[1] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][1] > games[x][ 'game_scores'][0]: to_add.append("$" + str(100 + to_win)) strat01['total_win'] += (100 + to_win) else: to_add.append("$0") ## Strategy 1 ## if oddsportal_odds[0] > away_proj: #Bet to_add.append("$100") strat1['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[0] > 0): to_win = 100 * (oddsportal_odds[0] / 100) else: to_win = 100 / (oddsportal_odds[0] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][0] > games[x][ 'game_scores'][1]: to_add.append("$" + str(100 + to_win)) strat1['total_win'] += (100 + to_win) else: to_add.append("$0") elif oddsportal_odds[1] > home_proj: #Bet to_add.append("$100") strat1['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[1] > 0): to_win = 100 * (oddsportal_odds[1] / 100) else: to_win = 100 / (oddsportal_odds[1] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][1] > games[x][ 'game_scores'][0]: to_add.append("$" + str(100 + to_win)) strat1['total_win'] += (100 + to_win) else: to_add.append("$0") else: to_add.append("") to_add.append("") to_add.append("") ## Strategy 2 ## if oddsportal_odds[0] > away_proj and away_proj < 0: #Bet to_add.append("$100") strat2['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[0] > 0): to_win = 100 * (oddsportal_odds[0] / 100) else: to_win = 100 / (oddsportal_odds[0] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][0] > games[x][ 'game_scores'][1]: to_add.append("$" + str(100 + to_win)) strat2['total_win'] += (100 + to_win) else: to_add.append("$0") elif oddsportal_odds[1] > home_proj and home_proj < 0: #Bet to_add.append("$100") strat2['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[1] > 0): to_win = 100 * (oddsportal_odds[1] / 100) else: to_win = 100 / (oddsportal_odds[1] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][1] > games[x][ 'game_scores'][0]: to_add.append("$" + str(100 + to_win)) strat2['total_win'] += (100 + to_win) else: to_add.append("$0") else: to_add.append("") to_add.append("") to_add.append("") ## Strategy 3 ## if oddsportal_odds[ 0] > away_proj and away_proj < 0 and oddsportal_odds[ 0] > 0: #Bet to_add.append("$100") strat3['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[0] > 0): to_win = 100 * (oddsportal_odds[0] / 100) else: to_win = 100 / (oddsportal_odds[0] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][0] > games[x][ 'game_scores'][1]: to_add.append("$" + str(100 + to_win)) strat3['total_win'] += (100 + to_win) else: to_add.append("$0") elif oddsportal_odds[ 1] > home_proj and home_proj < 0 and oddsportal_odds[ 1] > 0: #Bet to_add.append("$100") strat3['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[1] > 0): to_win = 100 * (oddsportal_odds[1] / 100) else: to_win = 100 / (oddsportal_odds[1] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][1] > games[x][ 'game_scores'][0]: to_add.append("$" + str(100 + to_win)) strat3['total_win'] += (100 + to_win) else: to_add.append("$0") else: to_add.append("") to_add.append("") to_add.append("") ## Strategy 4 ## if self.league == "mlb": diff_amount = 45 elif self.league == "nba": diff_amount = 100 if oddsportal_odds[ 0] > away_proj and away_diff >= diff_amount: #Bet to_add.append("$100") strat4['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[0] > 0): to_win = 100 * (oddsportal_odds[0] / 100) else: to_win = 100 / (oddsportal_odds[0] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][0] > games[x][ 'game_scores'][1]: to_add.append("$" + str(100 + to_win)) strat4['total_win'] += (100 + to_win) else: to_add.append("$0") elif oddsportal_odds[ 1] > home_proj and home_diff >= diff_amount: #Bet to_add.append("$100") strat4['total_bet'] += 100 #To Win to_win = 0 if (oddsportal_odds[1] > 0): to_win = 100 * (oddsportal_odds[1] / 100) else: to_win = 100 / (oddsportal_odds[1] * -1 / 100) to_add.append("$" + str(to_win)) #Won if games[x]['game_scores'][1] > games[x][ 'game_scores'][0]: to_add.append("$" + str(100 + to_win)) strat4['total_win'] += (100 + to_win) else: to_add.append("$0") else: to_add.append("") to_add.append("") to_add.append("") else: to_add = [] # #appends winning team # if games[x]['game_scores'][0]>games[x]['game_scores'][1]: # to_add.append(games[x]['team1'][0]) # else: # to_add.append(games[x]['team2'][0]) # #appends score # score=str(games[x]['game_scores'][0])+"-"+str(games[x]['game_scores'][1]) # to_add.append(score) if len(to_add) != 0: to_save.append(to_add) # to_save.append(["Date", "Away", "Home", "Algo proj", "Away proj", "Home proj", "Away odds", "Home odds", "Diff away", "Diff home", "Bet", "To win", "Won"]) #only saves day's games if there were actually games on that day if len(to_save) > 2: #summary strat00_profit = strat00['total_win'] - strat00['total_bet'] strat00_perc = strat00_profit / strat00['total_bet'] * 100 strat01_profit = strat01['total_win'] - strat01['total_bet'] strat01_perc = strat01_profit / strat01['total_bet'] * 100 strat1_profit = strat1['total_win'] - strat1['total_bet'] strat1_perc = strat1_profit / strat1['total_bet'] * 100 strat2_profit = strat2['total_win'] - strat2['total_bet'] if (strat2['total_bet'] > 0): strat2_perc = strat2_profit / strat2['total_bet'] * 100 else: strat2_perc = 0 strat3_profit = strat3['total_win'] - strat3['total_bet'] if (strat3['total_bet'] > 0): strat3_perc = strat3_profit / strat3['total_bet'] * 100 else: strat3_perc = 0 strat4_profit = strat4['total_win'] - strat4['total_bet'] if (strat4['total_bet'] > 0): strat4_perc = strat4_profit / strat4['total_bet'] * 100 else: strat4_perc = 0 #initializes with buffer columns summary = ["", "", "", "", "", "", "", "", "", ""] summary.append("$" + str(strat00['total_bet'])) summary.append("$" + str(strat00_profit)) summary.append(str(strat00_perc) + "%") summary.append("$" + str(strat01['total_bet'])) summary.append("$" + str(strat01_profit)) summary.append(str(strat01_perc) + "%") summary.append("$" + str(strat1['total_bet'])) summary.append("$" + str(strat1_profit)) summary.append(str(strat1_perc) + "%") summary.append("$" + str(strat2['total_bet'])) summary.append("$" + str(strat2_profit)) summary.append(str(strat2_perc) + "%") summary.append("$" + str(strat3['total_bet'])) summary.append("$" + str(strat3_profit)) summary.append(str(strat3_perc) + "%") summary.append("$" + str(strat4['total_bet'])) summary.append("$" + str(strat4_profit)) summary.append(str(strat4_perc) + "%") to_save.append(summary) #space between data to_save.append([ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" ]) content.append(to_save) #breaks loop to act like do-while if cur_date == end_date: break day += 1 if day > 31: month += 1 day = 1 #doesn't increment year since the season's year doesn't change if month > 12: month = 1 day = 1 #increments season at the end of the season to sometime in the middle if self.league == "nba": # if "4-1-" in cur_date: # year+=1 # month=2 # day=1 if "4-1-" in cur_date: year += 1 month = 1 day = 15 elif self.league == "nhl": if "4-1-" in cur_date: year += 1 month = 2 day = 1 elif self.league == "mlb": if "10-1-" in cur_date: year += 1 month = 7 day = 1 cur_date = str(month) + "-" + str(day) + "-" + str(year) # #has most recent games first # content.reverse() to_save = [] to_save.append([ "Date", "Away", "Home", "Algo proj", "Away proj", "Home proj", "Away odds", "Home odds", "Diff away", "Diff home", "Bet", "To win", "Won", "Bet", "To win", "Won", "Bet", "To win", "Won", "Bet", "To win", "Won" ]) for x in range(0, len(content)): for y in range(0, len(content[x])): to_save.append(content[x][y]) if start_date != end_date: self.universal.save_to_csv( "./" + str(self.league) + "/analyze/" + str(self.league) + "_Algo_V2_" + str(start_date) + "_" + str(end_date) + "_backtest_odds.csv", to_save) else: self.universal.save_to_csv( "./" + str(self.league) + "/analyze/" + str(self.league) + "_Algo_V2_" + str(end_date) + "_backtest_odds.csv", to_save)
def backtest_csv_output(self, start_date, end_date): #breaks up date temp = start_date.split("-") month = int(temp[0]) day = int(temp[1]) year = int(temp[2]) cur_date = start_date content = [] #this is actually a do-while loop while True: games = self.universal.get_games(cur_date) print("date: " + cur_date) for game in games: print(" Game: " + str(game)) to_save = [] to_save.append([ cur_date, "Away", "Home", "Algo points", "Proj winner", "Winner", "", "", "Score" ]) for x in range(0, len(games)): #team might not exist anymore, so don't count that game if len(games[x]['team1']) != 0 and len(games[x]['team2']) != 0: data1 = self.universal.load_data(games[x]['team1'], games[x]['date']) data2 = self.universal.load_data(games[x]['team2'], games[x]['date']) # print("Teams: "+str(games[x]['team1'])+" | "+str(games[x]['team2'])) # print("date: "+str(games[x]['date'])) # print(data1) # print(data1[0]['dates'][0]) # print(data1[0]['dates'][-1]) returned1 = self.odds_calculator.analyze2( games[x]['team1'], games[x]['team2'], data1, "away") returned2 = self.odds_calculator.analyze2( games[x]['team2'], games[x]['team1'], data2, "home") # print(returned1) # print() # print(returned2) # print() algo = Algo(self.league) if self.algo_version == "Algo_V1": algo_data = algo.calculate(games[x]['date'], returned1, returned2) elif self.algo_version == "Algo_V2": algo_data = algo.calculate_V2(games[x]['date'], returned1, returned2) total = algo_data['total'] to_add = [] to_add.append("") to_add.append(games[x]['team1'][0]) to_add.append(games[x]['team2'][0]) to_add.append(total) if self.algo_version == "Algo_V1": #categorizes odds (points) levels = [] levels.append([0, 3]) levels.append([3, 6]) levels.append([6, 9]) levels.append([9, 12]) levels.append([12, 15]) levels.append([15, 18]) levels.append([18, 21]) levels.append([21, 24]) levels.append([24, 27]) levels.append([27, 100]) elif self.algo_version == "Algo_V2": #categorizes odds (percentage) levels = [] levels.append([50, 55]) levels.append([55, 60]) levels.append([60, 65]) levels.append([65, 70]) levels.append([70, 75]) levels.append([75, 80]) levels.append([80, 85]) levels.append([85, 90]) levels.append([90, 95]) levels.append([95, 100]) level = 0 for y in range(0, len(levels)): if (total >= levels[y][0] and total < levels[y][1] ) or (total * -1 >= levels[y][0] and total * -1 < levels[y][1]): level = y + 1 #appends projected team if total > 0: to_add.append(games[x]['team1'][0]) elif total <= 0: to_add.append(games[x]['team2'][0]) #appends winning team if games[x]['game_scores'][0] > games[x]['game_scores'][1]: to_add.append(games[x]['team1'][0]) else: to_add.append(games[x]['team2'][0]) #appends score score = str(games[x]['game_scores'][0]) + "-" + str( games[x]['game_scores'][1]) to_add.append(score) # #appends algo data # if to_add[-2]==to_add[-3]: # to_add.append("") # if self.algo_version=="Algo_V1": # to_add.append(str(level)) # elif self.algo_version=="Algo_V2": # to_add.append(total) # elif to_add[-3]!="": # if self.algo_version=="Algo_V1": # to_add.append(str(level)) # elif self.algo_version=="Algo_V2": # to_add.append(total) # to_add.append("") # else: # to_add.append("") # to_add.append("") #appends algo data if to_add[-2] == to_add[-3]: to_add.append("") to_add.append(str(level)) elif to_add[-3] != "": to_add.append(str(level)) to_add.append("") else: to_add.append("") to_add.append("") #appends betting odds if self.algo_version == "Algo_V1": odds_calculator = Odds_Calculator(self.league) odds = odds_calculator.get_odds(total) elif self.algo_version == "Algo_V2": odds = abs(total) favorable_odds = (100 / (100 - abs(odds)) - 1) * 100 underdog_odds = (100 / (100 - abs(odds)) - 1) * 100 if total > 0: to_add.append("-" + str(favorable_odds)) to_add.append("+" + str(underdog_odds)) else: to_add.append("+" + str(underdog_odds)) to_add.append("-" + str(favorable_odds)) to_save.append(to_add) #space between data to_save.append(["", "", "", "", "", "", "", "", ""]) #only saves day's games if there were actually games on that day if len(to_save) > 2: content.append(to_save) #breaks loop to act like do-while if cur_date == end_date: break day += 1 if day > 31: month += 1 day = 1 #doesn't increment year since the season's year doesn't change if month > 12: month = 1 day = 1 #increments season at the end of the season to sometime in the middle if self.league == "nba": if "4-1-" in cur_date: year += 1 month = 2 day = 1 elif self.league == "nhl": if "4-1-" in cur_date: year += 1 month = 2 day = 1 elif self.league == "mlb": if "10-1-" in cur_date: year += 1 month = 7 day = 1 cur_date = str(month) + "-" + str(day) + "-" + str(year) #has most recent games first content.reverse() to_save = [] for x in range(0, len(content)): for y in range(0, len(content[x])): to_save.append(content[x][y]) if start_date != end_date: self.universal.save_to_csv( "./" + str(self.league) + "/analyze/" + str(self.league) + "_" + str(self.algo_version) + "_" + str(start_date) + "_" + str(end_date) + "_analysis.csv", to_save) else: self.universal.save_to_csv( "./" + str(self.league) + "/analyze/" + str(self.league) + "_" + str(self.algo_version) + "_" + str(end_date) + "_analysis.csv", to_save)
class Odds_Calculator: opener = None scraper = None algo = None universal = None user_agents = [] #can be nba, nhl, nfl, mlb league = "nba" num_periods = {'nba': 4, 'nhl': 3, 'nfl': 4, 'mlb': 9} def __init__(self, league): self.league = league.lower() self.universal = Universal_Functions(self.league) self.espn_scraper = ESPN_Scraper(self.league) #analyzes a single team def single_team_analysis(self, team): cur_year = input("Current season year: ") self.espn_scraper.update_data(team, cur_year) data = self.universal.load_data(team, "", cur_year) self.analyze(team, data, cur_year) #analyzes 2 teams and compares to determine which has best chance of winning def team_comparison(self, algo_version, team1, team2, date, cur_year): self.algo = Algo(self.league) self.espn_scraper.update_data(team1, cur_year) self.espn_scraper.update_data(team2, cur_year) data1 = self.universal.load_data(team1, date, cur_year) data2 = self.universal.load_data(team2, date, cur_year) returned1 = self.analyze2(team1, team2, data1, "away") returned2 = self.analyze2(team2, team1, data2, "home") # print(str(team1)+" | "+str(team2)) # print(returned1) # print(returned2) # print() if algo_version == "Algo_V1": algo_data = self.algo.calculate(date, returned1, returned2) elif algo_version == "Algo_V2": algo_data = self.algo.calculate_V2(date, returned1, returned2) record_points = algo_data['record_points'] home_away_points = algo_data['home_away_points'] home_away_10_games_points = algo_data['home_away_10_games_points'] last_10_games_points = algo_data['last_10_games_points'] avg_points = algo_data['avg_points'] avg_points_10_games = algo_data['avg_points_10_games'] # win_streak_10_games= algo_data['win_streak_10_games'] if self.league == "nhl": win_streak_home_away = algo_data['win_streak_home_away'] total = algo_data['total'] to_output = [] to_output.append("") to_output.append("Date: " + str(date)) to_output.append("Away: " + str(team1[1]) + " | Home: " + str(team2[1])) if algo_version == "Algo_V1": win_streak = algo_data['win_streak'] win_streak_home_away = algo_data['win_streak_home_away'] if self.league == "nba": to_output.append("Seasonal Record: " + str(record_points * 10) + "/10 = " + str(record_points)) to_output.append("Home Away: " + str(home_away_points * 10) + "/10 = " + str(home_away_points)) to_output.append("Home away 10: " + str(home_away_10_games_points * 5) + "/5 = " + str(home_away_10_games_points)) to_output.append("Last 10 games: " + str(last_10_games_points * 5) + "/5 = " + str(last_10_games_points)) to_output.append("Avg points: " + str(avg_points * 8) + "/8 = " + str(avg_points)) to_output.append("Avg points 10: " + str(avg_points_10_games * 8) + "/8 = " + str(avg_points_10_games)) to_output.append("Win streak: " + str(win_streak * 3) + "/3 = " + str(win_streak)) to_output.append("Win streak home away: " + str(win_streak_home_away * 3) + "/3 = " + str(win_streak_home_away)) else: to_output.append("Seasonal Record: " + str(record_points * 5) + "/5 = " + str(record_points)) to_output.append("Home Away: " + str(home_away_points * 5) + "/5 = " + str(home_away_points)) to_output.append("Home away 10: " + str(home_away_10_games_points * 5) + "/5 = " + str(home_away_10_games_points)) to_output.append("Last 10 games: " + str(last_10_games_points * 5) + "/5 = " + str(last_10_games_points)) to_output.append("Avg points: " + str(avg_points / 2) + "*2 = " + str(avg_points)) to_output.append("Avg points 10: " + str(avg_points_10_games / 2) + "*2 = " + str(avg_points_10_games)) to_output.append("Win streak: " + str(win_streak * 3) + "/3 = " + str(win_streak)) to_output.append("Win streak home away: " + str(win_streak_home_away * 3) + "/3 = " + str(win_streak_home_away)) to_output.append("--------") to_output.append("Total: " + str(total)) to_output.append("--------") elif algo_version == "Algo_V2": to_output.append("Seasonal Record: " + str(record_points) + "%") to_output.append("Home Away: " + str(home_away_points) + "%") to_output.append("Home away 10: " + str(home_away_10_games_points) + "%") to_output.append("Last 10 games: " + str(last_10_games_points) + "%") to_output.append("Avg points: " + str(avg_points) + "%") to_output.append("Avg points 10: " + str(avg_points_10_games) + "%") # to_output.append("Win streak: "+str(win_streak)+"%") if self.league == "nhl": to_output.append("Win streak home away: " + str(win_streak_home_away) + "%") to_output.append("--------") to_output.append("Total: " + str(total) + "%") to_output.append("--------") #chance of favorable team winning if algo_version == "Algo_V1": winning_odds = self.get_odds(total) elif algo_version == "Algo_V2": winning_odds = abs(total) to_output.append("Perc chance to win: " + str(winning_odds) + "%") favorable_odds = (100 / (100 - winning_odds) - 1) * 100 underdog_odds = (100 / (100 - winning_odds) - 1) * 100 to_output.append("Favorable team odds: -" + str(favorable_odds)) to_output.append("Underdog team odds: +" + str(underdog_odds)) return to_output #gets odds of winning for algo_V1 def get_odds(self, total_points): #puts total points at a max of 27 max_points = 27 if abs(total_points) > max_points: total_points = max_points x = abs(total_points) / max_points * 10 #2D polynomial that follows the percentage chance of winning per level of ranking 1-10 if self.league == "nba": y = -0.23 * (x**2) + 7.25 * x + 47.9 else: y = -0.23 * (x**2) + 7.25 * x + 47.9 if y < 50: y = 50 return y #analyzes current team def analyze(self, team, data, end_year): if os.path.isdir("./" + str(self.league) + "/analyze/single_analysis/" + str(team[1])) == False: os.mkdir("./" + str(self.league) + "/analyze/single_analysis/" + str(team[1])) home_away = input("Are they home or away: ").lower() other_team = input("Playing against (letter abbreviation): ") returned = self.analyze2(team, other_team, data, home_away) self.save_analysis(team, data, returned, home_away) returned['output'] = self.get_output_analysis("", team, returned, home_away) more_output = self.analyze_wins_ranked_teams(team, data, end_year) # more_output=[] for line in more_output: returned['output'].append(line) self.universal.save_to_txt( "./" + str(self.league) + "/analyze/single_analysis/" + str(team[1]) + "/" + str(team[1]) + "_analysis.txt", returned['output']) #analyzes whatever team needed for self.analyze() def analyze2(self, team, other_team, data, home_away): print("Analyzing " + str(team)) to_return = {} season_record = self.get_seasonal_records(data) # print("Season record: "+str(season_record)) # input("waiting...") #seasonal win-loss ratio to_return['seasonal_records'] = self.get_seasonal_records(data) #average point stats to_return['avg_game_points'] = self.get_avg_points(data) #stats in home vs away games to_return['home_away_record'] = self.get_home_away_record(data) #seasonal win-loss ratio to_return['current_win_ratio'] = self.get_current_win_ratio(data) #last 10 games win ratio to_return['10_game_win_ratio'] = self.analyze_10_games_win_ratio(data) #winning or losing streaks against specified team #definition only accepts "lal" and not ["lal", "los-angeles-lakers"], so check if isinstance(other_team, list): to_return[ 'win_loss_streaks_against'] = self.get_win_streaks_against( other_team[0], data) else: to_return[ 'win_loss_streaks_against'] = self.get_win_streaks_against( other_team, data) return to_return def save_analysis(self, team, data, returned, home_away): #seasonal win-loss ratio records = returned['seasonal_records'] to_save = [] for x in range(0, len(records)): to_save.append( ["1-1-" + str(data[x]['year']), records[x][0] - records[x][1]]) path = "./" + str(self.league) + "/analyze/single_analysis/" + str( team[1]) + "/" + str(team[1]) + "_seasonal_records.csv" self.universal.save_to_csv(path, to_save) print("Saved to " + str(path)) #average point stats avg_points = returned['avg_game_points'] to_save = [] for x in range(0, len(avg_points['avg_game_points'])): to_add = [] to_add.append("1-1-" + str(data[x]['year'])) to_add.append(avg_points['avg_game_points'][x]) to_add.append(avg_points['avg_other_game_points'][x]) to_add.append(avg_points['avg_game_points'][x] + avg_points['avg_other_game_points'][x]) for y in range(0, len(avg_points['avg_quarter_points'][x])): to_add.append(avg_points['avg_quarter_points'][x][y]) to_save.append(to_add) path = "./" + str(self.league) + "/analyze/single_analysis/" + str( team[1]) + "/" + str(team[1]) + "_avg_game_points.csv" self.universal.save_to_csv(path, to_save) print("Saved to " + str(path)) #stats in home vs away games home_away_records = returned['home_away_record'] to_save = [] for x in range(0, len(home_away_records['home_record'])): to_add = [] to_add.append("1-1-" + str(data[x]['year'])) to_add.append(home_away_records['home_record'][x][0]) to_add.append(home_away_records['home_record'][x][1]) to_save.append(to_add) to_save.append(["", "", ""]) to_save.append(["", "", ""]) to_save.append(["", "", ""]) for x in range(0, len(home_away_records['away_record'])): to_add = [] to_add.append("1-1-" + str(data[x]['year'])) to_add.append(home_away_records['away_record'][x][0]) to_add.append(home_away_records['away_record'][x][1]) to_save.append(to_add) path = "./" + str(self.league) + "/analyze/single_analysis/" + str( team[1]) + "/" + str(team[1]) + "_home_away_record.csv" self.universal.save_to_csv(path, to_save) print("Saved to " + str(path)) #seasonal win-loss ratio win_loss = returned['current_win_ratio'] path = "./" + str(self.league) + "/analyze/single_analysis/" + str( team[1]) + "/" + str(team[1]) + "_current_win_ratio.csv" self.universal.save_to_csv(path, win_loss) print(path) #last 10 games win ratio last_10_games = returned['10_game_win_ratio'] to_save = [] to_save.append(["Year", "win-loss", "num wins", "num games"]) for x in range(0, len(last_10_games)): for y in range(-10, 11, 2): to_add = [] #only has year at beginning of listing if y == -10: to_add.append(data[x]['year']) else: to_add.append("") # to_add.append(str(y)) temp = { '-10': '"0-10"', '-8': '"1-9"', '-6': '"2-8"', '-4': '"3-7"', '-2': '"4-6"', '0': '"5-5"', '2': '"6-4"', '4': '"7-3"', '6': '"8-2"', '8': '"9-1"', '10': '"10-0"' } #turns -4 into "3-7" to_add.append(temp[str(y)]) to_add.append(last_10_games[x][str(y)][0]) to_add.append(last_10_games[x][str(y)][1]) #gets win percentage if last_10_games[x][str(y)][1] != 0: to_add.append("=C" + str(len(to_save) + 1) + "/D" + str(len(to_save) + 1) + "*100") else: to_add.append(0) to_save.append(to_add) to_save.append(["", "", "", ""]) path = "./" + str(self.league) + "/analyze/single_analysis/" + str( team[1]) + "/" + str(team[1]) + "_10_game_win_ratio.csv" self.universal.save_to_csv(path, to_save) print(path) #winning or losing streaks against specified team to_save = [] wins_against = returned['win_loss_streaks_against'] to_save.append(["Losing streak", wins_against['games_since_last_win']]) to_save.append( ["Winning streak", wins_against['games_since_last_loss']]) if home_away == "away": to_save.append([ "Losing streak away", wins_against['games_since_last_win_away'] ]) to_save.append([ "Winning streak away", wins_against['games_since_last_loss_away'] ]) elif home_away == "home": to_save.append([ "Losing streak home", wins_against['games_since_last_win_home'] ]) to_save.append([ "Winning streak home", wins_against['games_since_last_loss_home'] ]) path = "./" + str(self.league) + "/analyze/single_analysis/" + str( team[1]) + "/" + str(team[1]) + "_win_loss_streaks_against.csv" self.universal.save_to_csv(path, to_save) print(path) def get_output_analysis(self, indent, team, returned, home_away): records = returned['seasonal_records'] avg_points = returned['avg_game_points'] home_away_records = returned['home_away_record'] win_loss = returned['current_win_ratio'] last_10_games = returned['10_game_win_ratio'] wins_against = returned['win_loss_streaks_against'] #### output #### to_output = [] to_output.append("") to_output.append("") to_output.append(indent + team[1]) if (records[-1][0] - records[-1][1]) > (records[-2][0] - records[-2][1]): temp = "uptrend" else: temp = "downtrend" to_output.append(indent + "Season: " + str(records[-1][0] - records[-1][1]) + " on " + str(temp)) if home_away == "away": to_output.append(indent + "Home-Away: " + str(home_away_records['away_record'][-1][0]) + "-" + str(home_away_records['away_record'][-1][1]) + " away") to_output.append(indent + " Last 10 away games: " + str(home_away_records['away_10_games'][-1][0]) + "-" + str(home_away_records['away_10_games'][-1][1])) elif home_away == "home": to_output.append(indent + "Home-Away: " + str(home_away_records['home_record'][-1][0]) + "-" + str(home_away_records['home_record'][-1][1]) + " home") to_output.append(indent + " Last 10 home games: " + str(home_away_records['home_10_games'][-1][0]) + "-" + str(home_away_records['home_10_games'][-1][1])) win_10_games = 0 for x in range(len(win_loss) - 1, len(win_loss) - 11, -1): win_10_games += win_loss[x][2] temp = { '-10': '0-10', '-8': '1-9', '-6': '2-8', '-4': '3-7', '-2': '4-6', '0': '5-5', '2': '6-4', '4': '7-3', '6': '8-2', '8': '9-1', '10': '10-0' } to_output.append(indent + "10 Games: " + temp[str(win_10_games)]) won = last_10_games[-1][str(win_10_games)][0] num_games = last_10_games[-1][str(win_10_games)][1] if num_games != 0: to_output.append(indent + " " + str(won) + " won out of " + str(num_games) + " games | " + str(won / num_games * 100) + "%") else: to_output.append(indent + " " + str(won) + " won out of " + str(num_games) + " games | N/A%") to_output.append(indent + "Avg points: " + str(avg_points['avg_game_points'][-1]) + " - " + str(avg_points['avg_other_game_points'][-1])) to_output.append(indent + " Last 10 games: " + str(avg_points['avg_10_games'][-1]) + " - " + str(avg_points['avg_other_10_games'][-1])) #on winning streak if wins_against['games_since_last_loss'] > 0: to_output.append(indent + "Winning streak against " + str(wins_against['other_team']) + ": " + str(wins_against['games_since_last_loss'])) to_output.append(indent + " Winning streak " + home_away + ": " + str(wins_against['games_since_last_loss_' + str(home_away)])) elif wins_against['games_since_last_win'] > 0: to_output.append(indent + "Losing streak against " + str(wins_against['other_team']) + ": " + str(wins_against['games_since_last_win'])) to_output.append(indent + " Losing streak " + home_away + ": " + str(wins_against['games_since_last_win_' + str(home_away)])) return to_output #analyzes number of wins against teams of certain rankings. Like # wins against even teams (23-25 to 27-25) or against good teams (30-15) or bad teams (15-30)... etc def analyze_wins_ranked_teams(self, team, data, end_year): total_output = [] for x in range( len(data[-1]['other_team']) - 1, len(data[-1]['other_team']) - 11, -1): other_team = [] other_team.append(data[-1]['other_team'][x]) other_team.append("") date = data[-1]['dates'][x] # print("Date: "+str(date)) home_away = data[-1]['home_away'][x] if home_away == "home": other_home_away = "away" elif home_away == "away": other_home_away = "home" temp = [] temp.append(date) temp.append(other_team) # temp.append() league_teams = self.universal.load_league_teams() #gets "los-angeles-lakers" if given "lal" for y in range(0, len(league_teams)): name = league_teams[y] if name[0] == other_team[0]: other_team[1] = name[1] indent = " " cur_data = self.universal.load_data(team, date, end_year) print(cur_data[-1]['other_team'][-1]) returned = self.analyze2(team, other_team[0], cur_data, data[-1]['home_away'][x]) output = self.get_output_analysis(indent, team, returned, data[-1]['home_away'][x]) for line in output: total_output.append(line) other_data = self.universal.load_data(other_team, date, end_year) print( str(other_data[-1]['other_team'][-1]) + " | " + str(date) + " | " + str(other_data[-1]['dates'][-5])) returned = self.analyze2(other_team, team[0], other_data, other_home_away) output = self.get_output_analysis(indent, other_team, returned, other_home_away) print() for line in output: print(line) total_output.append(line) total_output.append("") #adds winner and scores cur_team_score = data[-1]['game_scores'][x][0] other_team_score = data[-1]['game_scores'][x][1] if cur_team_score > other_team_score: total_output.append(indent + "Winner: " + team[1] + " | " + str(cur_team_score) + "-" + str(other_team_score)) else: total_output.append(indent + "Winner: " + other_team[1] + " | " + str(other_team_score) + "-" + str(cur_team_score)) total_output.append(indent + "----------------------------------------") print() return total_output #returns wins/loss streaks against other_team def get_win_streaks_against(self, other_team, original_data): to_return = {} to_return['other_team'] = other_team to_return['games_since_last_win'] = 0 to_return['games_since_last_loss'] = 0 to_return['games_since_last_win_away'] = 0 to_return['games_since_last_win_home'] = 0 to_return['games_since_last_loss_away'] = 0 to_return['games_since_last_loss_home'] = 0 for x in range(0, len(original_data)): data = original_data[x] year = data['year'] for y in range(0, len(data['other_team'])): if data['other_team'][y] == other_team: # if x==len(original_data)-1: # print(str(year)+" | "+str(other_team)+" | "+str(data['game_scores'][y][0])+"-"+str(data['game_scores'][y][1])) #if won if data['game_scores'][y][0] > data['game_scores'][y][1]: to_return['games_since_last_win'] = 0 to_return['games_since_last_loss'] += 1 if data['home_away'][y] == "away": to_return['games_since_last_win_away'] = 0 to_return['games_since_last_loss_away'] += 1 else: to_return['games_since_last_win_home'] = 0 to_return['games_since_last_loss_home'] += 1 #if lost else: to_return['games_since_last_win'] += 1 to_return['games_since_last_loss'] = 0 if data['home_away'][y] == "away": to_return['games_since_last_win_away'] += 1 to_return['games_since_last_loss_away'] = 0 else: to_return['games_since_last_win_home'] += 1 to_return['games_since_last_loss_home'] = 0 return to_return # #gets percentage of games won if ahead after 1st quarter, 2nd quarter, etc. # def get_perc_win_quarters_ahead(self, data): # #gets total goals for and goals against # def get_goals_for_against(self, data): #determines whether teams win or lose more often if they have a good or bad last 10 games def analyze_10_games_win_ratio(self, original_data): to_return = [] for x in range(0, len(original_data)): data = original_data[x] year = data['year'] #win_data['4'] will hold data for last 10 games with ratio 7-3 #increments by 2 since subtracting losses from wins of last 10 games will never have odd number win_data = {} for y in range(-10, 11, 2): win_data[str(y)] = [0, 0] last_10_record = [] for y in range(0, len(data['other_team'])): #only gets win ratio if 10 records present if len(last_10_record) == 10: temp = sum(last_10_record) #adding 1 or -1 is same as subtracting num losses from num wins if data['game_scores'][y][0] > data['game_scores'][y][1]: #only counts this win if 10 records already present if len(last_10_record) == 10: win_data[str(sum(last_10_record))][0] += 1 win_data[str(sum(last_10_record))][1] += 1 last_10_record.append(1) else: if len(last_10_record) == 10: win_data[str(sum(last_10_record))][1] += 1 last_10_record.append(-1) if len(last_10_record) > 10: last_10_record.pop(0) to_return.append(win_data) return to_return #gets win-loss ratio during each game during the current season def get_current_win_ratio(self, original_data): data = original_data[-1] to_return = [] cur_score = 0 for x in range(0, len(data['game_scores'])): to_add = [] to_add.append(data['game_scores'][x][0]) to_add.append(data['game_scores'][x][1]) # print(data['other_team'][x]+" | "+str(to_add)) if data['game_scores'][x][0] > data['game_scores'][x][1]: temp = 1 else: temp = -1 to_add.append(temp) cur_score += temp to_add.append(cur_score) to_return.append(to_add) return to_return #gets wins-losses while at home or away def get_home_away_record(self, original_data): to_return = {} to_return['home_record'] = [] to_return['away_record'] = [] to_return['home_10_games'] = [] to_return['away_10_games'] = [] for x in range(0, len(original_data)): data = original_data[x] home_away = data['home_away'] game_scores = data['game_scores'] home_record = [] away_record = [] for y in range(0, len(home_away)): if home_away[y] == "home": if game_scores[y][0] > game_scores[y][1]: home_record.append(1) else: home_record.append(-1) elif home_away[y] == "away": if game_scores[y][0] > game_scores[y][1]: away_record.append(1) else: away_record.append(-1) to_return['home_record'].append( [home_record.count(1), home_record.count(-1)]) to_return['away_record'].append( [away_record.count(1), away_record.count(-1)]) #gets stats on last 10 games home_10_games = [ home_record[-10:].count(1), home_record[-10:].count(-1) ] away_10_games = [ away_record[-10:].count(1), away_record[-10:].count(-1) ] to_return['home_10_games'].append(home_10_games) to_return['away_10_games'].append(away_10_games) return to_return #calculates a bunch of average points stats def get_avg_points(self, original_data): to_return = {} avg_game_points = [] avg_other_game_points = [] avg_10_games = [] avg_other_10_games = [] avg_quarters = [] for x in range(0, len(original_data)): data = original_data[x] if len(data['other_team']) != 0: # print("Year: "+str(original_data[x]['year'])) #gets avg_game_points total_points = 0 other_total_points = 0 for y in range(0, len(data['other_team'])): total_points += data['game_scores'][y][0] other_total_points += data['game_scores'][y][1] average = total_points / len(data['other_team']) average_other = other_total_points / len(data['other_team']) avg_game_points.append(self.universal.convert_number(average)) avg_other_game_points.append( self.universal.convert_number(average_other)) #gets average points for last 10 games total_points = 0 other_total_points = 0 for y in range( len(data['other_team']) - 1, len(data['other_team']) - 11, -1): total_points += data['game_scores'][y][0] other_total_points += data['game_scores'][y][1] average = total_points / 10 avg_10_games.append(self.universal.convert_number(average)) average = other_total_points / 10 avg_other_10_games.append( self.universal.convert_number(average)) #gets avg_game_points num_periods = self.num_periods[self.league] total_quarters = [0] * num_periods * 2 for y in range(0, len(data['other_team'])): # print(data['period_scores'][y]) # print("Num periods: "+str(num_periods)) #adds current team's 4 quarters try: for z in range(0, num_periods): total_quarters[z] += int( data['period_scores'][y][0][z]) except Exception as error: pass #adds other team's 4 quarters try: for z in range(0, len(data['period_scores'][y][1])): total_quarters[z + num_periods] += int( data['period_scores'][y][1][z]) except Exception as error: pass #gets average quarter scores for y in range(0, len(total_quarters)): total_quarters[y] = total_quarters[y] / len( data['other_team']) avg_quarters.append(total_quarters) to_return['avg_game_points'] = avg_game_points to_return['avg_other_game_points'] = avg_other_game_points to_return['avg_10_games'] = avg_10_games to_return['avg_other_10_games'] = avg_other_10_games to_return['avg_quarter_points'] = avg_quarters return to_return #gets records like 2016: 49-20 for all seasons def get_seasonal_records(self, original_data): records = [] for x in range(0, len(original_data)): data = original_data[x] num_wins = 0 for y in range(0, len(data['other_team'])): if data['game_scores'][y][0] > data['game_scores'][y][1]: num_wins += 1 # record=num_wins-len(data['game_scores'])-num_wins record = [num_wins, len(data['game_scores']) - num_wins] records.append(record) return records