Created
February 5, 2020 10:39
-
-
Save joelanders/a58643a5ac6dc61cdbdb260923c5ed7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from lxml import html | |
string = "" | |
# https://www.oddschecker.com/politics/us-politics/us-presidential-election-2020/winner/bet-history/bernie-sanders#all-history | |
# with open("table-bernie.html", "r") as f: | |
# string = f.read() | |
# https://www.oddschecker.com/politics/us-politics/us-presidential-election-2020/winner/bet-history/joe-biden#all-history | |
# with open("table-biden.html", "r") as f: | |
# string = f.read() | |
# https://www.oddschecker.com/politics/us-politics/us-presidential-election-2020/winner/bet-history/elizabeth-warren#all-history | |
# with open("table-warren.html", "r") as f: | |
# string = f.read() | |
# https://www.oddschecker.com/politics/us-politics/us-presidential-election-2020/winner/bet-history/donald-trump#all-history | |
LIST_OF_NAMES = ["bernie-sanders", "pete-buttigieg", "elizabeth-warren", "joe-biden", "donald-trump"] | |
name_to_data = {} | |
name_to_inverse_data = {} | |
for name in LIST_OF_NAMES: | |
with open("./5-feb/%s" % name, "r") as f: | |
string = f.read() | |
tree = html.fromstring(string) | |
num_rows = len(tree.xpath("/html/body/div[1]/div/div/div/div/div/section/div/div/div/div[3]/div[2]/table/tbody/*")) | |
all_days_of_betfair_odds = [] | |
all_date_strings = [] | |
for i in range(1, num_rows): | |
# 22 is hardcoded for Betfair's column | |
one_day_of_betfair_odds = tree.xpath("/html/body/div[1]/div/div/div/div/div/section/div/div/div/div[3]/div[2]/table/tbody/tr[%d]/td[22]/*/text()" % i) | |
all_days_of_betfair_odds.append(one_day_of_betfair_odds) | |
date_string = tree.xpath("/html/body/div[1]/div/div/div/div/div/section/div/div/div/div[3]/div[2]/table/tbody/tr[%d]/*/text()" % i)[0] | |
all_date_strings.append(date_string) | |
assert len(all_days_of_betfair_odds) == len(all_date_strings) | |
all_days_of_betfair_odds_decimals = [] | |
for one_day_of_betfair_odds in all_days_of_betfair_odds: | |
one_day_of_betfair_odds_decimals = [] | |
for one_odds in one_day_of_betfair_odds: | |
try: | |
numerator, denominator = one_odds.split("/") | |
except ValueError: | |
numerator = int(one_odds) | |
denominator = 1 | |
decimal = int(numerator) / int(denominator) | |
one_day_of_betfair_odds_decimals.append(decimal) | |
all_days_of_betfair_odds_decimals.append(one_day_of_betfair_odds_decimals) | |
all_date_strings_with_data = [] | |
all_opens = [] | |
all_highs = [] | |
all_lows = [] | |
all_closes = [] | |
for i in range(0, len(all_days_of_betfair_odds_decimals)): | |
try: | |
one_date_string_with_data = all_date_strings[i] | |
one_open = all_days_of_betfair_odds_decimals[i][-1] | |
one_high = max(all_days_of_betfair_odds_decimals[i]) | |
one_low = min(all_days_of_betfair_odds_decimals[i]) | |
one_close = all_days_of_betfair_odds_decimals[i][0] | |
all_date_strings_with_data.append(one_date_string_with_data) | |
all_opens.append(one_open) | |
all_highs.append(one_high) | |
all_lows.append(one_low) | |
all_closes.append(one_close) | |
except IndexError: | |
print("incomplete data for %s" % all_date_strings[i]) | |
assert len(all_date_strings_with_data) == \ | |
len(all_opens) == len(all_highs) == len(all_closes) | |
import plotly.graph_objects as go | |
import pandas as pd | |
from datetime import datetime | |
# XXX is this right? 1/1 odds -> 50/50% yeah? | |
all_inverse_opens = list(map(lambda x: 1.0 / (1 + x), all_opens)) | |
all_inverse_highs = list(map(lambda x: 1.0 / (1 + x), all_highs)) | |
all_inverse_lows = list(map(lambda x: 1.0 / (1 + x), all_lows)) | |
all_inverse_closes = list(map(lambda x: 1.0 / (1 + x), all_closes)) | |
# fig1 = go.Figure( | |
# data=[ | |
# go.Candlestick( | |
# x=all_date_strings_with_data, | |
# open=all_inverse_opens, | |
# high=all_inverse_highs, | |
# low=all_inverse_lows, | |
# close=all_inverse_closes, | |
# ), | |
# ] | |
# ) | |
# fig1.update_layout(title=name) | |
# fig1.update_layout( | |
# xaxis=go.layout.XAxis( | |
# fixedrange=True, | |
# range=["2019-10-01 00:00:00.0000", "2020-02-06 00:00:00.0000"], | |
# rangeslider=dict(visible=False), | |
# type="date" | |
# ), | |
# yaxis=go.layout.YAxis( | |
# autorange=True | |
# ) | |
# ) | |
# fig1.show() | |
name_to_inverse_data[name] = {} | |
name_to_inverse_data[name]["dates"] = all_date_strings_with_data | |
name_to_inverse_data[name]["opens"] = all_inverse_opens | |
name_to_inverse_data[name]["highs"] = all_inverse_highs | |
name_to_inverse_data[name]["lows"] = all_inverse_lows | |
name_to_inverse_data[name]["closes"] = all_inverse_closes | |
import pandas as pd | |
from plotly.subplots import make_subplots | |
from datetime import datetime | |
import plotly.express as px | |
# fig = make_subplots( | |
# rows=len(LIST_OF_NAMES), cols=1, | |
# subplot_titles=tuple(LIST_OF_NAMES), | |
# shared_xaxes=True, | |
# vertical_spacing=0.1) | |
fig = go.Figure() | |
row_num = 0 | |
for name in LIST_OF_NAMES: | |
color1 = px.colors.qualitative.Dark2[row_num] | |
# color2 = px.colors.qualitative.Set2[row_num] | |
fig.add_trace( | |
go.Candlestick(x=name_to_inverse_data[name]['dates'], | |
open=name_to_inverse_data[name]["opens"], | |
high=name_to_inverse_data[name]["highs"], | |
low=name_to_inverse_data[name]["lows"], | |
close=name_to_inverse_data[name]["closes"], | |
increasing_line_color=color1, | |
decreasing_line_color=color1, | |
name=name, | |
), | |
) | |
row_num = row_num + 1 | |
fig.update_layout( | |
xaxis=go.layout.XAxis( | |
fixedrange=True, | |
range=["2019-09-01 00:00:00.0000", "2020-02-06 00:00:00.0000"], | |
rangeslider=dict(visible=False), | |
type="date" | |
) | |
) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment