diff --git a/Flask/app.py b/Flask/app.py index 663360703..775949f78 100644 --- a/Flask/app.py +++ b/Flask/app.py @@ -1,3 +1,4 @@ +from datetime import date from flask import Flask, render_template from functools import lru_cache import subprocess @@ -23,8 +24,9 @@ def fetch_betmgm(ttl_hash=None): def fetch_game_data(sportsbook="fanduel"): cmd = ["python", "main.py", "-xgb", f"-odds={sportsbook}"] stdout = subprocess.check_output(cmd, cwd="../").decode() - data_re = re.compile(r'\n(?P[\w ]+)(\((?P[\d+\.]+)%\))? vs (?P[\w ]+)(\((?P[\d+\.]+)%\))?: (?POVER|UNDER) (?P[\d+\.]+) (\((?P[\d+\.]+)%\))?', re.MULTILINE) + data_re = re.compile(r'\n(?P[\w ]+)(\((?P[\d+\.]+)%\))? vs (?P[\w ]+)(\((?P[\d+\.]+)%\))?: (?POVER|UNDER) (?P[\d+\.]+) (\((?P[\d+\.]+)%\))?', re.MULTILINE) ev_re = re.compile(r'(?P[\w ]+) EV: (?P[-\d+\.]+)', re.MULTILINE) + odds_re = re.compile(r'(?P[\w ]+) \((?P-?\d+)\) @ (?P[\w ]+) \((?P-?\d+)\)', re.MULTILINE) games = [] for match in data_re.finditer(stdout): game_dict = {'away_team': match.group('away_team').strip(), @@ -35,11 +37,16 @@ def fetch_game_data(sportsbook="fanduel"): 'ou_value': match.group('ou_value'), 'ou_confidence': match.group('ou_confidence')} for ev_match in ev_re.finditer(stdout): - print(ev_match.group('team'), game_dict['home_team']) if ev_match.group('team') == game_dict['away_team']: game_dict['away_team_ev'] = ev_match.group('ev') if ev_match.group('team') == game_dict['home_team']: game_dict['home_team_ev'] = ev_match.group('ev') + for odds_match in odds_re.finditer(stdout): + if odds_match.group('away_team') == game_dict['away_team']: + game_dict['away_team_odds'] = odds_match.group('away_team_odds') + if odds_match.group('home_team') == game_dict['home_team']: + game_dict['home_team_odds'] = odds_match.group('home_team_odds') + print(game_dict) games.append(game_dict) return games @@ -51,10 +58,13 @@ def get_ttl_hash(seconds=600): app = Flask(__name__) +app.jinja_env.add_extension('jinja2.ext.loopcontrols') + @app.route("/") def index(): fanduel = fetch_fanduel(ttl_hash=get_ttl_hash()) draftkings = fetch_draftkings(ttl_hash=get_ttl_hash()) betmgm = fetch_betmgm(ttl_hash=get_ttl_hash()) - return render_template('index.html', data={"fanduel": fanduel, "draftkings": draftkings, "betmgm": betmgm}) \ No newline at end of file + + return render_template('index.html', today=date.today(), data={"fanduel": fanduel, "draftkings": draftkings, "betmgm": betmgm}) \ No newline at end of file diff --git a/Flask/templates/index.html b/Flask/templates/index.html index e925c9f64..002fc2722 100644 --- a/Flask/templates/index.html +++ b/Flask/templates/index.html @@ -8,34 +8,67 @@
-

NBA Machine Learning Picks

-
- {% for sportsbook in ["fanduel", "draftkings", "betmgm"] %} -
-

{{sportsbook}}

- - {% for game in data.get(sportsbook) %} +

NBA Machine Learning Picks ({{today}})

+
+ + + + + + + + + {% for game in data.get('fanduel') %} + {% set game_idx = loop.index0 %} + + + {% for sportsbook in ['fanduel', 'draftkings', 'betmgm'] %} + {% set sbgame = data.get(sportsbook)[game_idx] %} + {% if not sbgame or not sbgame.away_team or not sbgame.home_team %}{% continue %}{%endif%} + + {% endfor %} + + {%endfor%} + +
TeamsFanduelDraftkingsBetmgm
+ + - - - + + + + - {% endfor %} +
- {% if game.away_confidence %}{{ game.away_team }} ({{game.away_confidence}}%){% else %}{{ game.away_team }}{% endif %} -
- {% if game.home_confidence %}{{ game.home_team }} ({{game.home_confidence}}%){% else %}{{ game.home_team }}{% endif %} -
- {{ game.away_team_ev }}
- {{ game.home_team_ev }} -
{{ game.ou_pick }} {{ game.ou_value }} ({{game.ou_confidence}}%){{ game.away_team }}
{{ game.home_team }}
- - {% endfor %} - +
+ + + + + + + + + + + + + +
{% if sbgame.away_team_odds|int > 0 %}+{%endif%}{{ sbgame.away_team_odds }}{% if sbgame.away_confidence %} ({{sbgame.away_confidence}}%){% endif %} + {{ sbgame.away_team_ev }} + {% if sbgame.ou_pick == 'OVER' %}O{%else%}U{%endif%} {{ sbgame.ou_value }}
{% if sbgame.home_team_odds|int > 0 %}+{%endif%}{{ sbgame.home_team_odds }}{% if sbgame.home_confidence %} ({{sbgame.home_confidence}}%){% endif %} + {{ sbgame.home_team_ev }} + {{sbgame.ou_confidence}}%
+
diff --git a/main.py b/main.py index 6bf03e068..3fafd05da 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import tensorflow as tf from src.Predict import NN_Runner, XGBoost_Runner from src.Utils.Dictionaries import team_index_current -from src.Utils.tools import get_json_data, to_data_frame, get_todays_games_json, create_todays_games +from src.Utils.tools import create_todays_games_from_odds, get_json_data, to_data_frame, get_todays_games_json, create_todays_games from src.DataProviders.SbrOddsProvider import SbrOddsProvider @@ -58,12 +58,9 @@ def createTodaysGames(games, df, odds): def main(): odds = None - data = get_todays_games_json(todays_games_url) - games = create_todays_games(data) - data = get_json_data(data_url) - df = to_data_frame(data) if args.odds: odds = SbrOddsProvider(sportsbook=args.odds).get_odds() + games = create_todays_games_from_odds(odds) if((games[0][0]+':'+games[0][1]) not in list(odds.keys())): print(games[0][0]+':'+games[0][1]) print(Fore.RED, "--------------Games list not up to date for todays games!!! Scraping disabled until list is updated.--------------") @@ -74,6 +71,11 @@ def main(): for g in odds.keys(): home_team, away_team = g.split(":") print(f"{away_team} ({odds[g][away_team]['money_line_odds']}) @ {home_team} ({odds[g][home_team]['money_line_odds']})") + else: + data = get_todays_games_json(todays_games_url) + games = create_todays_games(data) + data = get_json_data(data_url) + df = to_data_frame(data) data, todays_games_uo, frame_ml, home_team_odds, away_team_odds = createTodaysGames(games, df, odds) if args.nn: print("------------Neural Network Model Predictions-----------") diff --git a/src/DataProviders/SbrOddsProvider.py b/src/DataProviders/SbrOddsProvider.py index 66da8a969..52b206e2a 100644 --- a/src/DataProviders/SbrOddsProvider.py +++ b/src/DataProviders/SbrOddsProvider.py @@ -26,12 +26,17 @@ def get_odds(self): home_team_name = game['home_team'].replace("Los Angeles Clippers", "LA Clippers") away_team_name = game['away_team'].replace("Los Angeles Clippers", "LA Clippers") + money_line_home_value = money_line_away_value = totals_value = None + # Get money line bet values - money_line_home_value = game['home_ml'][self.sportsbook] - money_line_away_value = game['away_ml'][self.sportsbook] + if self.sportsbook in game['home_ml']: + money_line_home_value = game['home_ml'][self.sportsbook] + if self.sportsbook in game['away_ml']: + money_line_away_value = game['away_ml'][self.sportsbook] # Get totals bet value - totals_value = game['total'][self.sportsbook] + if self.sportsbook in game['total']: + totals_value = game['total'][self.sportsbook] dict_res[home_team_name + ':' + away_team_name] = { 'under_over_odds': totals_value, diff --git a/src/Predict/XGBoost_Runner.py b/src/Predict/XGBoost_Runner.py index 665103301..965028579 100644 --- a/src/Predict/XGBoost_Runner.py +++ b/src/Predict/XGBoost_Runner.py @@ -76,8 +76,10 @@ def xgb_runner(data, todays_games_uo, frame_ml, games, home_team_odds, away_team for game in games: home_team = game[0] away_team = game[1] - ev_home = float(Expected_Value.expected_value(ml_predictions_array[count][0][1], int(home_team_odds[count]))) - ev_away = float(Expected_Value.expected_value(ml_predictions_array[count][0][0], int(away_team_odds[count]))) + ev_home = ev_away = 0 + if home_team_odds[count] and away_team_odds[count]: + ev_home = float(Expected_Value.expected_value(ml_predictions_array[count][0][1], int(home_team_odds[count]))) + ev_away = float(Expected_Value.expected_value(ml_predictions_array[count][0][0], int(away_team_odds[count]))) if ev_home > 0: print(home_team + ' EV: ' + Fore.GREEN + str(ev_home) + Style.RESET_ALL) else: diff --git a/src/Utils/tools.py b/src/Utils/tools.py index a8d768175..345f056a3 100644 --- a/src/Utils/tools.py +++ b/src/Utils/tools.py @@ -50,3 +50,11 @@ def create_todays_games(input_list): away_team = away.get('tc') + ' ' + away.get('tn') games.append([home_team, away_team]) return games + + +def create_todays_games_from_odds(input_dict): + games = [] + for game in input_dict.keys(): + home_team, away_team = game.split(":") + games.append([home_team, away_team]) + return games