Skip to content

Commit

Permalink
calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
pretrehr committed Aug 11, 2021
1 parent e8947ee commit 4aaef4d
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 4 deletions.
95 changes: 92 additions & 3 deletions interface_pysimplegui.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
find_values_interface, odds_match_values_interface,
open_bookmaker_odds, find_perf_players, display_middle_info, search_perf,
display_surebet_info, best_match_miles_interface, sort_middle_gap, sort_middle_trj,
sort_middle_proba, get_best_conversion_rates_freebet, compute_odds)
sort_middle_proba, get_best_conversion_rates_freebet, compute_odds, calculator_interface)

PATH_DATA = os.path.dirname(sb.__file__) + "/resources/data.json"
PATH_SITES = os.path.dirname(sb.__file__) + "/resources/sites.json"
Expand All @@ -63,7 +63,7 @@
HEIGHT_FIELD_SIMPLE = 10
HEIGHT_FIELD_GAGNANT = 12
HEIGHT_FIELD_COMBINE = 18
LENGTH_FIELD = 120
LENGTH_FIELD = 160

sb.DB_MANAGEMENT = "--db" in sys.argv
nb_bookmakers = len(sb.BOOKMAKERS)
Expand Down Expand Up @@ -610,6 +610,46 @@
font="Consolas 10", visible=False)]])],
]

visible_calc=2
calculator_layout = [
[sg.Button("Ajouter cote", key="ADD_CALC"), sg.Button("Retirer cote", key="REMOVE_CALC")],
[
sg.Col([
[sg.Text("Site")],
*([[sg.InputText(size=(12, 1), key="SITE_CALC_" + str(i), visible=i<visible_calc, enable_events=True)] for i in range(9)])
]),
sg.Col([
[sg.Text("Back")],
*([[sg.Radio('', "BACK_LAY_CALC_" + str(i), key="BACK_BACK_LAY_CALC_" + str(i), visible=i<visible_calc, default=True, enable_events=True)] for i in range(9)])
]),
sg.Col([
[sg.Text("Lay")],
*([[sg.Radio('', "BACK_LAY_CALC_" + str(i), key="LAY_BACK_LAY_CALC_" + str(i), visible=i<visible_calc, enable_events=True)] for i in range(9)])
]),
sg.Col([
[sg.Text("Cote")],
*([[sg.InputText(size=(7, 1), key="ODD_CALC_" + str(i), visible=i<visible_calc, enable_events=True)] for i in range(9)])
]),
sg.Col([
[sg.Text("Commission (%)")],
*([[sg.InputText(size=(7, 1), key="COMMISSION_CALC_" + str(i), visible=i<visible_calc, enable_events=True)] for i in range(9)])
]),
sg.Col([
[sg.Text("Intitulé")],
*([[sg.InputText(size=(60, 1), key="NAME_CALC_" + str(i), visible=i<visible_calc, enable_events=True)] for i in range(9)])
]),
sg.Col([
[sg.Text("Mise")],
*([[sg.InputText(size=(7, 1), key="STAKE_CALC_" + str(i), visible=i<visible_calc, enable_events=True, disabled=i!=0)] for i in range(9)])
]),
sg.Col([
[sg.Text("Référence")],
*([[sg.Radio('', "REFERENCE_STAKE_CALC", key="REFERENCE_STAKE_CALC_" + str(i), visible=i<2, default=i==0, enable_events=True)] for i in range(9)])
])
],
[sg.MLine(size=(LENGTH_FIELD, HEIGHT_FIELD_COMBINE), key="RESULT_CALC", font="Consolas 10", visible=False)]
]


layout = [[sg.TabGroup([[sg.Tab('Récupération des cotes', parsing_layout),
sg.Tab('Cotes', odds_layout),
Expand All @@ -625,7 +665,8 @@
sg.Tab('Surebets', surebets_layout),
sg.Tab('Values', values_layout, visible=sb.BETA),
sg.Tab('Perf players', perf_players_layout, visible=sb.BETA),
sg.Tab('Miles', miles_layout, visible=sb.BETA)
sg.Tab('Miles', miles_layout, visible=sb.BETA),
sg.Tab('Calculateur', calculator_layout, visible=sb.BETA),
]])],
[sg.Button('Quitter', button_color=("white", "red"))]]

Expand Down Expand Up @@ -1041,6 +1082,54 @@ def perf_thread():
del sb.ODDS[sport_miles[0]][match_miles]
elif event == "RELOAD_ODDS_MILES":
sb.ODDS = load_odds(PATH_DATA)
elif event == "ADD_CALC":
if visible_calc < 9:
window["SITE_CALC_" + str(visible_calc)].update(visible=True)
window["BACK_BACK_LAY_CALC_" + str(visible_calc)].update(visible=True)
window["LAY_BACK_LAY_CALC_" + str(visible_calc)].update(visible=True)
window["ODD_CALC_" + str(visible_calc)].update(visible=True)
window["COMMISSION_CALC_" + str(visible_calc)].update(visible=True)
window["NAME_CALC_" + str(visible_calc)].update(visible=True)
window["STAKE_CALC_" + str(visible_calc)].update(visible=True)
window["REFERENCE_STAKE_CALC_" + str(visible_calc)].update(visible=True)
visible_calc += 1
elif event == "REMOVE_CALC":
if visible_calc > 2:
visible_calc -= 1
window["SITE_CALC_" + str(visible_calc)].update(visible=False)
window["BACK_BACK_LAY_CALC_" + str(visible_calc)].update(visible=False)
window["LAY_BACK_LAY_CALC_" + str(visible_calc)].update(visible=False)
window["ODD_CALC_" + str(visible_calc)].update(visible=False)
window["COMMISSION_CALC_" + str(visible_calc)].update(visible=False)
window["NAME_CALC_" + str(visible_calc)].update(visible=False)
window["STAKE_CALC_" + str(visible_calc)].update(visible=False)
window["REFERENCE_STAKE_CALC_" + str(visible_calc)].update(visible=False)
elif event.startswith("REFERENCE_STAKE_CALC_"):
ref_calc = int(event.split("_")[-1])
for i in range(9):
window["STAKE_CALC_" + str(i)].update(disabled=i!=ref_calc)
calculator_interface(window, values, visible_calc)
elif "LAY_BACK_LAY_CALC_" in event:
lay_calc = int(event.split("_")[-1])
window["COMMISSION_CALC_" + str(lay_calc)].update(3)
window["SITE_CALC_" + str(lay_calc)].update("OrbitX")
window["NAME_CALC_" + str(lay_calc)].update("Lay")
values["COMMISSION_CALC_" + str(lay_calc)] = 3
values["SITE_CALC_" + str(lay_calc)] = "OrbitX"
values["NAME_CALC_" + str(lay_calc)] = "Lay"
calculator_interface(window, values, visible_calc)
elif event.startswith("SITE_CALC_"):
site_calc = int(event.split("_")[-1])
window["BACK_BACK_LAY_CALC_" + str(site_calc)].update(True)
window["COMMISSION_CALC_" + str(site_calc)].update(0)
values["BACK_BACK_LAY_CALC_" + str(site_calc)] = True
values["COMMISSION_CALC_" + str(site_calc)] = 0
if values["SITE_CALC_" + str(site_calc)].lower() == "orbitx":
window["COMMISSION_CALC_" + str(site_calc)].update(3)
values["COMMISSION_CALC_" + str(site_calc)] = 3
calculator_interface(window, values, visible_calc)
elif "_CALC_" in event:
calculator_interface(window, values, visible_calc)
else:
pass
sb.INTERFACE = False
Expand Down
34 changes: 34 additions & 0 deletions sportsbetting/auxiliary_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,37 @@ def save_odds(odds, path):
with open(path, "w") as file:
json.dump(saved_odds, file, indent=2)


def get_real_odd(odd, commission, is_lay):
if is_lay:
return round(1 + (1 - commission) / (odd - 1), 3)
return round(1 + (1 - commission) * (odd - 1), 3)


def calculator(odds, lay, commissions, stake, reference, outcomes, sites):
real_odds = []
back_odds = []
lay_odds = []
stakes = []
backers_stakes = []
trj = 0
profit = 0
try:
back_odds = ["" if lay else odd for odd, lay in zip(odds, lay)]
lay_odds = [odd if lay else "" for odd, lay in zip(odds, lay)]
real_odds = [get_real_odd(odd, commission, is_lay) for odd, commission, is_lay in zip(odds, commissions, lay)]
stakes = list(map(lambda x :round(x, 2), mises2(real_odds, stake, reference)))
backers_stakes = [round(stake_i / (odd_i - 1), 2) if lay_i else "" for stake_i, lay_i, odd_i in zip(stakes, lay, odds)]
trj = gain(real_odds)
profit = stake*real_odds[reference]-sum(stakes)
except ZeroDivisionError:
pass
table = {"Site":sites, "Issue": outcomes, "Lay":lay_odds, "Cote": real_odds, "Mise": stakes, "Stake" : backers_stakes, "Total": [round(odd_i*stake_i, 2) for odd_i, stake_i in zip(real_odds, stakes)], "Infos":["TRJ : {}%".format(round(100*trj, 3)), "Plus-value : {}".format(round(profit, 2))]}
if not any(lay):
del table["Lay"]
del table["Stake"]
text = tabulate.tabulate(table, headers='keys', tablefmt='fancy_grid')
print(text)
if sys.platform.startswith("win"):
copy_to_clipboard(text)

33 changes: 32 additions & 1 deletion sportsbetting/interface_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import webbrowser

import sportsbetting as sb
from sportsbetting.auxiliary_functions import get_nb_outcomes, copy_to_clipboard
from sportsbetting.auxiliary_functions import get_nb_outcomes, copy_to_clipboard, calculator
from sportsbetting.database_functions import get_all_current_competitions, get_all_competitions
from sportsbetting.user_functions import (best_match_under_conditions, best_match_under_conditions2,
best_match_freebet, best_stakes_match,
Expand Down Expand Up @@ -1068,5 +1068,36 @@ def get_best_conversion_rates_freebet(window):
window["CONVERT_RATES_FREEBET"].update(table)
visible = len(high_conversion_rates) > 0
window["HIGH_FREEBET_PARSING"].update("Taux de conversion freebet haut ({})".format(", ".join(high_conversion_rates)), visible=visible, text_color="orange")

def calculator_interface(window, values, visible_calc):
odds = []
lay = []
commissions = []
stake = 0
reference = 0
outcomes = []
sites = []
for i in range(visible_calc):
odd = 0
try:
odd = float(values["ODD_CALC_" + str(i)])
except ValueError:
pass
odds.append(odd)
lay.append(bool(values["LAY_BACK_LAY_CALC_" + str(i)]))
commission = values["COMMISSION_CALC_" + str(i)]
commissions.append(float(commission)/100 if commission else 0)
outcomes.append(values["NAME_CALC_" + str(i)])
sites.append(values["SITE_CALC_" + str(i)])
if values["REFERENCE_STAKE_CALC_" + str(i)]:
reference = i
stake = values["STAKE_CALC_" + str(i)]
stake = float(stake) if stake else 0
old_stdout = sys.stdout # Memorize the default stdout stream
sys.stdout = buffer = io.StringIO()
calculator(odds, lay, commissions, stake, reference, outcomes, sites)
sys.stdout = old_stdout # Put the old stream back in place
what_was_printed = buffer.getvalue() # contains the entire contents of the buffer.
window["RESULT_CALC"].update(what_was_printed, visible=True)


0 comments on commit 4aaef4d

Please sign in to comment.