-
Notifications
You must be signed in to change notification settings - Fork 1
/
viewer.py
243 lines (218 loc) · 8.42 KB
/
viewer.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import json
import webbrowser
import os
from http.server import HTTPServer, SimpleHTTPRequestHandler
import threading
import signal
import sys
from datetime import datetime, timezone
def format_date(date_string):
date = datetime.fromisoformat(date_string.replace('Z', '+00:00'))
return date.strftime('%Y-%m-%d %I:%M %p %Z')
def calculate_profit_and_payout(arb, wager):
total_implied_prob = sum(1/odd for odd in arb['best_odds'].values())
profit_margin = (1 / total_implied_prob - 1)
profit = wager * profit_margin
payout = wager + profit
return profit, payout
def generate_html(data):
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Arbitrage Opportunities Viewer</title>
<style>
body {{
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}}
h1 {{
color: #2c3e50;
text-align: center;
}}
.summary {{
background-color: #ecf0f1;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}}
.opportunity {{
background-color: #f9f9f9;
border: 1px solid #ddd;
padding: 15px;
margin-bottom: 15px;
border-radius: 5px;
}}
.opportunity h2 {{
color: #2980b9;
margin-top: 0;
}}
.odds {{
display: flex;
justify-content: space-between;
}}
.odds div {{
flex: 1;
padding: 10px;
background-color: #e8f6fe;
margin: 5px;
border-radius: 3px;
}}
#wager-input {{
margin-bottom: 20px;
padding: 10px;
background-color: #e8f6fe;
border-radius: 5px;
}}
#wager-input input {{
margin-left: 10px;
padding: 5px;
}}
#wager-input button {{
margin-left: 10px;
padding: 5px 10px;
background-color: #2980b9;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}}
.profit-payout {{
margin-top: 10px;
font-weight: bold;
}}
</style>
<script>
function updateProfits() {{
const wager = parseFloat(document.getElementById('wager').value);
if (isNaN(wager) || wager <= 0) {{
alert('Please enter a valid wager amount.');
return;
}}
const opportunities = document.getElementsByClassName('opportunity');
for (let opp of opportunities) {{
const profitMargin = parseFloat(opp.getAttribute('data-profit-margin')) / 100;
const profit = wager * profitMargin;
const payout = wager + profit;
opp.querySelector('.profit').textContent = profit.toFixed(2);
opp.querySelector('.payout').textContent = payout.toFixed(2);
const bets = opp.querySelectorAll('.bet-amount');
const totalImpliedProb = parseFloat(opp.getAttribute('data-total-implied-prob'));
bets.forEach(bet => {{
const impliedProb = parseFloat(bet.getAttribute('data-implied-prob'));
const betAmount = (wager * impliedProb / totalImpliedProb).toFixed(2);
bet.textContent = betAmount;
}});
}}
}}
// Initial update
updateProfits();
</script>
</head>
<body>
<h1>Arbitrage Opportunities Viewer</h1>
<div id="wager-input">
<label for="wager">Enter wager amount: $</label>
<input type="number" id="wager" min="0" step="0.01" value="100">
<button onclick="updateProfits()">Update Profits</button>
</div>
<div class="summary">
<h2>Summary</h2>
<p>Total events analyzed: {total_events}</p>
<p>Total arbitrage opportunities found: {total_arbs}</p>
</div>
<div id="opportunities">
{opportunities}
</div>
</body>
</html>
"""
# Sort arbitrage opportunities by profit margin in descending order
sorted_arbs = sorted(data['arbitrage_opportunities'], key=lambda x: x['profit_margin'], reverse=True)
opportunities_html = ""
for arb in sorted_arbs:
# Filter out 'spread' key for implied probability calculation
odds_for_calc = {k: v for k, v in arb['best_odds'].items() if k != 'spread'}
total_implied_prob = sum(1/odd for odd in odds_for_calc.values())
opportunities_html += f"""
<div class="opportunity" data-profit-margin="{arb['profit_margin']}" data-total-implied-prob="{total_implied_prob}">
<h2>{arb['event']}</h2>
<p>Profit Margin: {arb['profit_margin']:.2f}%</p>
<p>Date: {format_date(arb['commence_time'])}</p>
<p>Market: {arb.get('market', 'N/A')}</p>
"""
if arb.get('market') == 'spreads':
opportunities_html += f"<p>Points Spread: {arb.get('points', 'N/A')}</p>"
elif arb.get('market') == 'totals':
opportunities_html += f"<p>Total Points: {arb.get('points', 'N/A')}</p>"
opportunities_html += '<div class="odds">'
for outcome, odd in arb['best_odds'].items():
if outcome != 'spread': # Skip the spread key when displaying odds
bookmaker = arb['bookmakers'][outcome]
implied_prob = 1 / odd
if arb.get('market') == 'spreads':
spread = f"+{arb['points']}" if outcome == 'Underdog' else f"-{arb['points']}"
label = f"{outcome} ({spread})"
else:
label = outcome
opportunities_html += f"""
<div>
<h3>{label}</h3>
<p>Odds: {odd:.2f}</p>
<p>Bookmaker: {bookmaker}</p>
<p>Bet Amount: $<span class="bet-amount" data-implied-prob="{implied_prob}">0.00</span></p>
</div>
"""
opportunities_html += """
</div>
<div class="profit-payout">
<p>Profit: $<span class="profit">0.00</span></p>
<p>Payout: $<span class="payout">0.00</span></p>
</div>
</div>
"""
return html_template.format(
total_events=data['total_events'],
total_arbs=data['total_arbitrage_opportunities'],
opportunities=opportunities_html
)
def run_server(port=8000):
server_address = ('', port)
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
print(f"Server running on http://localhost:{port}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down the server...")
httpd.shutdown()
def signal_handler(sig, frame):
print("\nExiting the viewer. Goodbye!")
sys.exit(0)
def main():
signal.signal(signal.SIGINT, signal_handler)
with open('arbitrage_results.json', 'r') as f:
data = json.load(f)
html_content = generate_html(data)
with open('arbitrage_viewer.html', 'w') as f:
f.write(html_content)
# Start the server in a separate thread
server_thread = threading.Thread(target=run_server)
server_thread.daemon = True
server_thread.start()
# Open the default web browser
webbrowser.open('http://localhost:8000/arbitrage_viewer.html')
print("Press Ctrl+C to stop the server and exit.")
try:
# Keep the main thread alive
while True:
signal.pause()
except KeyboardInterrupt:
print("\nExiting the viewer. Goodbye!")
if __name__ == "__main__":
main()