Created
January 24, 2020 14:37
-
-
Save joelanders/fc96a463e7394696a2373a0f09d2705a to your computer and use it in GitHub Desktop.
Revisions
-
joelanders created this gist
Jan 24, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,90 @@ 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 with open("table-trump.html", "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): # 23 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[23]/*/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 fig = go.Figure( data=[ go.Candlestick( x=all_date_strings_with_data, open=all_opens, high=all_highs, low=all_lows, close=all_closes, ), ] ) fig.show()