def cross_check_teams(table: webdriver, bets_db: list) -> (int, tuple): preds_list = table.find_elements_by_xpath('.//tr[@class="ng-scope"]') teams_web = [] preds_details = [] for pred in preds_list: match = pred.find_element_by_xpath('.//td[6]').text team1, team2 = match.strip().split(' - ') quote = float(pred.find_element_by_xpath('.//td[10]').text) result = pred.find_element_by_xpath('.//td[11]').text label_element = pred.find_element_by_xpath( './/div[contains(@class,"ng-scope")]') label = label_element.get_attribute('ng-switch-when') teams_web.append(team1) teams_web.append(team2) preds_details.append((team1, team2, quote, result, label)) teams_web.sort() for bet_db_id, _ in bets_db: teams_db = dbf.db_select(table='predictions', columns=['team1', 'team2'], where=f'bet_id = {bet_db_id}') teams_db = [t for i in teams_db for t in i] teams_db.sort() if teams_web == teams_db: return bet_db_id, preds_details else: continue return 0, []
def system_simulation(combs_to_play: dict): bet_ids = dbf.db_select(table='bets', columns=['id'], where='result != "Unknown" AND euros > 0') all_bets = [dbf.db_select(table='predictions', columns=['quote', 'label'], where=f'bet_id = {b_id}') for b_id in bet_ids] all_bets = [[(q, 1) if lb == 'WINNING' else (q, 0) for q, lb in bet] for bet in all_bets] bets_as_system = [] for i, bet in enumerate(all_bets, 1): size = len(bet) win = 0 played = 0 for j in range(1, size+1): if j not in combs_to_play:# and j != size: continue euros_per_bet = combs_to_play[j] comb = list(combinations(bet, j)) for c in comb: win += np.prod(np.array(c).flatten())*euros_per_bet played += euros_per_bet bets_as_system.append(win - played) _, ax = plt.subplots(figsize=(25, 6)) bal = [sum(bets_as_system[:i]) for i in range(1, len(bets_as_system)+1)] plt.plot(bal) # Money played and money won for real, day by day real_mn_bet, real_mn_won = get_money_and_prizes() # Day by day balance real_trend = get_trend(euros_played=real_mn_bet, euros_won=real_mn_won) plt.plot(real_trend) wins = np.array([np.prod(i) for i in all_bets]) wins = np.argwhere(wins > 0) for w in wins: plt.axvline(x=w, c='r', alpha=.3) return
def get_data(use_combo: bool) -> pd.DataFrame: cols = ['date', 'team1', 'team2', 'league', 'bet_alias', 'quote', 'label'] data = dbf.db_select(table='simulations', columns=cols, where='') df = pd.DataFrame(data, columns=cols) if not use_combo: df = df[~df['bet_alias'].str.contains('+', regex=False)].copy() df.replace({'WINNING': 1, 'LOSING': 0}, inplace=True) return df
def get_money_and_prizes(): money_bet = dbf.db_select( table='bets', columns=['euros', 'prize', 'result'], where='status = "Placed" AND result != "Unknown"') euros, prizes, labels = zip(*money_bet) euros = np.array(euros) labels = [True if i == 'WINNING' else False for i in labels] prizes = np.multiply(np.array(prizes), np.array(labels)) return euros[euros > 0], prizes[euros > 0]
def get_bets_as_df() -> pd.DataFrame: cols = ['date', 'euros', 'prize', 'result'] bets = dbf.db_select(table='bets', columns=cols, where='result != "Unknown"') bets = pd.DataFrame(bets, columns=cols) bets.index += 1 bets['date'] = pd.to_datetime(bets['date'], infer_datetime_format=True) return bets
def scrape_all_quotes() -> None: """ Download all the quotes from the website and save them in the database. """ browser = None leagues = dbf.db_select(table='leagues', columns=['name'], where='') for league in leagues: start = time.time() browser = scrape_league_quotes(brow=browser, league_name=league) m, s = utl.time_needed(start) cfg.LOGGER.info(f'FILL DB WITH QUOTES - {league} aggiornata: {m}:{s}') browser.quit() utl.remove_matches_without_quotes()
def get_preds_as_df() -> pd.DataFrame: cols = [ 'id', 'bet_id', 'user', 'date', 'team1', 'team2', 'league', 'bet_alias', 'quote', 'result', 'label' ] preds = dbf.db_select(table='predictions', columns=cols, where='result != "NULL"') preds = pd.DataFrame(preds, columns=cols) preds.set_index('id', drop=True, inplace=True) preds.index.name = None preds['date'] = pd.to_datetime(preds['date'], infer_datetime_format=True) return preds
def all_fields_and_bets(panel: webdriver) -> [(str, webdriver)]: # Select all fields we want to scrape fields_in_db = dbf.db_select(table='fields', columns=['name'], where='') fields_names = [f for i in fields_in_db for f, _ in (i.split('_'), )] fields_names = set(fields_names) all_fields_path = './/div[@class="market-info"]/div' all_bets_path = './/div[@class="market-selections"]' fields = panel.find_elements_by_xpath(all_fields_path) bets = panel.find_elements_by_xpath(all_bets_path) field_bets = [] for field, bet_group in zip(fields, bets): field_name = field.get_attribute('innerText').upper().strip() if field_name in fields_names: field_bets.append((field_name, bet_group)) return field_bets
def score(which) -> None: if which == 'general': year1, year2 = 2017, 2030 else: year1, year2 = which.split('-') from_ = datetime.strptime(f'{year1}-08-01 00:00:00', '%Y-%m-%d %H:%M:%S') to_ = datetime.strptime(f'{year2}-07-31 00:00:00', '%Y-%m-%d %H:%M:%S') tmp = preds[(from_ <= preds['date']) & (preds['date'] <= to_)] names = [name for name in players] indices = np.array([compute_index(tmp, name) for name in names]) data = sorted(zip(names, indices), key=lambda x: x[1], reverse=True) names, indices = zip(*data) indices = normalize_indices(raw_indices=indices) ratio = [get_user_ratio(tmp, name) for name in names] perc = [get_user_perc(tmp, name) for name in names] quotes = [get_user_avg_quotes(tmp, name) for name in names] avg_quote, avg_quoteW = zip(*quotes) colors = [get_user_color(name) for name in names] fig, ax = plt.subplots(figsize=(9, 7)) bars = plt.bar(range(5), indices, 0.5, color=colors, edgecolor='black', linewidth=.8, clip_on=False) plt.xticks(range(5), names, fontsize=16) plt.ylim(0, 1.35) plt.box(on=None) plt.tick_params(axis='x', which='both', bottom=False, labelbottom=True) plt.tick_params(axis='y', which='both', left=False, labelleft=False) plt.title(which, fontsize=16, fontweight='bold', style='italic') last_update = dbf.db_select(table='last_results_update', columns=['message'], where='')[0] plt.text(0.81, 0.98, str(last_update), transform=ax.transAxes, fontsize=8) for i, bar in enumerate(bars): text = f'{ratio[i]}\n{perc[i]}%\n{avg_quote[i]}\n{avg_quoteW[i]}' plt.text(bar.get_x() + bar.get_width() / 2, indices[i] + 0.03, text, ha='center', va='bottom', fontsize=12, style='italic') for i, bar in enumerate(bars): text = f'{indices[i]}' plt.text(bar.get_x() + bar.get_width() / 2, indices[i] + 0.23, text, ha='center', va='bottom', fontsize=14, fontweight='bold') for bar in bars: if not bar.get_height(): bar.set_linewidth(0) plt.text(.8, .9, 'Score', horizontalalignment='center', transform=ax.transAxes, fontweight='bold', fontsize=14) expl = 'Win/Total\n%\nAvg Quote\nAvg Quote WIN' plt.text(.8, .75, expl, horizontalalignment='center', transform=ax.transAxes, style='italic', fontsize=12) plt.savefig(f'score_{which}.png', dpi=120, bbox_inches='tight') plt.gcf().clear()
def get_user_color(nickname: str) -> str: color = dbf.db_select(table='people', columns=['color'], where=f'nick = "{nickname}"')[0] return color
def get_people() -> list: return dbf.db_select(table='people', columns=['nick'], where='')