-
Notifications
You must be signed in to change notification settings - Fork 0
/
OddsComparer.py
185 lines (135 loc) · 5.99 KB
/
OddsComparer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# from bs4 import BeautifulSoup
import re, time, sys, datetime, math, signal
from config import CONFIG as config
import HTML_parser
import Emailer
import BettingDB
import random
import json
def print_json(json_object):
print json.dumps(json_object, indent=4, sort_keys=True)
print "\n"
class OddsComparer():
def __init__(self):
self.parser = HTML_parser.HTML_parser()
self.bets_DB = BettingDB.BettingDB()
self.emailer = Emailer.Emailer()
self.games = {}
self.date = datetime.datetime.now()
random.seed(int(time.time()))
def run(self):
# get the money lines for each website
while True:
self.games = self.parser.get_moneylines()
results = []
# print_json(self.games)
# for sport in self.games:
for group in self.games:
# check to see if game id exsists in
site = group["site"]
# print site
for game in group["moneylines"]:
"""
your code for what to do with the lines goes here.
standard format for lines is a dictionary like so:
game = {
"home_team": home team's short name,
"away_team": away team's short name,
"game_time": game time (miiiiight not be standardized),
"home_line": home team's line,
"away_line": away team's line,
"sport": name of the sport
}
right now the below code just stores and compares lines
using mysql
"""
game["poll_time"] = int(time.time())
game["site"] = site
game_id = self.bets_DB.get_game_id(game)
self.bets_DB.add_moneyline(game,game_id)
betting_result = self.compare_moneylines(game_id,game)
if betting_result:
results.append(betting_result)
"""
end of area where you put your code
"""
# if results:
# self.emailer.send_email(results)
time_to_sleep = int(self.get_poisson_arrival_time(1/float(20*60)))
print "sleeping {} seconds".format(time_to_sleep)
self.countdown_sleep(time_to_sleep)
if interrupted:
try:
self.bets_DB.shutdown()
self.parser.shutdown()
except:
pass
break
def countdown_sleep(self, time_to_sleep):
for i in range(time_to_sleep):
sys.stdout.write('\r')
sys.stdout.write(str(time_to_sleep - i))
sys.stdout.flush()
time.sleep(1)
if interrupted:
break
def compare_moneylines(self, game_id, game):
# Look at current line
# determine if the underdog is the home team or the away team, same with favourite
# find lines with winning differences
# store line details
# return the best difference
result = None
if game["home_line"] > game["away_line"]:
# the home team is the underdog and the away team is the favourite
favourite = "home"
underdog = "away"
else:
underdog = "away"
favourite = "home"
# betting on a team with +320 for 100 bucks and a team with -250 for 280
# then if the + team wins then you get +320 -280 = 40 and if - team wins you get 112 - 100 = 12
money_lines_to_compare = self.bets_DB.get_moneylines(game)
betting_max = {"now_team": None, "before_team": None, "date": None, "diff": 0}
for money_line in money_lines_to_compare:
gametime_string = self.convert_timestamp_to_time_string(money_line["poll_time"])
if money_line[favourite + "_line"] + game[underdog + "_line"] > betting_max["diff"]:
betting_max["diff"] = money_line[favourite + "_line"] + game[underdog + "_line"]
betting_max["now_team"] = game[underdog+"_team"]
betting_max["before_team"] = game[favourite+"_team"]
betting_max["date"] = gametime_string
elif money_line[underdog + "_line"] + game[favourite + "_line"] > betting_max["diff"]:
betting_max["diff"] = money_line[underdog + "_line"] + game[favourite + "_line"]
betting_max["now_team"] = game[favourite+"_team"]
betting_max["before_team"] = game[underdog+"_team"]
betting_max["date"] = gametime_string
if betting_max["diff"] > 0:
result = "We should have bet on {0} at {1} and {2} now for a difference of {3} points".format(
betting_max["before_team"],betting_max["date"], betting_max["now_team"],betting_max["diff"])
return result
def flush_games(self):
self.games = {"live": [], "upcoming": []}
def convert_game_time_to_timestamp(self, time):
pass
def convert_timestamp_to_time_string(self,timestamp):
game_datetime = datetime.datetime.fromtimestamp(timestamp)
game_string = str(game_datetime.day) + ":" + str(game_datetime.hour) + \
":" + str(game_datetime.minute) + " (Day:Hour:Minute)"
return game_string
def get_poisson_arrival_time(self, lambda_val):
return -1*math.log(max(0.0001,random.random()))/float(lambda_val)
if __name__ == "__main__":
odds = OddsComparer()
def signal_handler(signal, frame):
try:
odd.bets_DB.shutdown()
odds.parser.shutdown()
except:
pass
global interrupted
interrupted = True
signal.signal(signal.SIGINT, signal_handler)
interrupted = False
odds.run()
# odds.add_moneylines_to_database()
# odds.flush_games()