Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """Analysis results:
- Turnover: £3,422,065
- EV: £132,712
- PnL: £147,777
- Commission: £72,974
- Net PnL: £74,802
- RoI: 2.186%
- """
- import csv
- import os
- import pandas as pd
- import random
- def find_stake_value(row):
- pass
- def calculate_backing_stake_fraction(win_probability, odds):
- return ((win_probability * odds) - 1) / (odds - 1)
- def calculate_laying_stake_fraction(win_probability, odds):
- return ((1 - win_probability) - win_probability * (odds - 1)) / (odds - 1)
- def calculate_side(win_probability, odds):
- stake = calculate_backing_stake_fraction(win_probability, odds)
- if stake > 0:
- return "back"
- else:
- return "lay"
- def get_random_side():
- if random.randint(0, 1) == 1:
- return "back"
- else:
- return "lay"
- def print_all_values(turnover, EV, PnL, commission, Net_PnL, RoI):
- print("Turnover: " + str(int(turnover)))
- print("EV: " + str(int(EV)))
- print("PnL: " + str(int(PnL)))
- print("Commission: " + str(int(commission)))
- print("Net_PnL: " + str(int(Net_PnL)))
- print("RoI: " + str(round(RoI, 3)) + "%")
- def value_calculation(racing_data, bank, minimum_stake, model):
- #Define all values to be calculated by iterative addition
- turnover = 0
- EV = 0
- PnL = 0
- commission = 0
- for index, row in racing_data.iterrows():
- if index == 0:
- #Define race number and race PnL at beginning of for loop
- previous_race_number = 0
- race_PnL = 0
- #After each race, we calculate commission and add race PnL to total PnL.
- #We also reset race PnL to prepare for next race. We need to add a
- #condition that checks for the last index, since there is no transition
- #to another race that we can use in that case.
- if (previous_race_number != row["race_number"]) | (index == len(racing_data.index) - 1):
- if race_PnL > 0:
- commission += race_PnL * 0.05
- PnL += race_PnL
- race_PnL = 0
- #Assign data values to variables for easier readability
- win_probability = 1 / row["win_fair_price"]
- odds = row["win_starting_price"]
- #We have two different models to check, one based on predictions and
- #one which is completely random.
- if model == "prediction":
- side = calculate_side(win_probability, odds)
- if side == "back":
- stake = calculate_backing_stake_fraction(win_probability, odds) * bank
- else:
- stake = calculate_laying_stake_fraction(win_probability, odds) * bank
- if stake > minimum_stake:
- turnover += stake
- #we need to consider 4 scenarios. lay + win, lay + lose, back + win
- #and back + lose.
- if side == "lay":
- EV += stake * (1 - win_probability) - stake * (odds - 1) * win_probability
- if row["winner"] == 0:
- race_PnL += stake
- else:
- race_PnL -= stake * (odds - 1)
- else:
- EV += stake * win_probability * (odds - 1) - stake * (1 - win_probability)
- if row["winner"] == 0:
- race_PnL -= stake
- else:
- race_PnL += stake * (odds - 1)
- previous_race_number = row["race_number"]
- elif model == "monte_carlo":
- side = get_random_side()
- if side == "back":
- #Stake is a random number between 2 and our bank value.
- stake = random.randint(2, bank)
- if row["winner"] == 0:
- race_PnL -= stake
- else:
- race_PnL += stake * (odds - 1)
- else:
- #The maximum stake is determined by stake that gives a liability
- #equal to our bank. We then take a random value between 2 amnd
- #this number
- stake = random.randint(2, round(bank / (odds - 1)))
- if row["winner"] == 0:
- race_PnL += stake
- else:
- race_PnL -= stake * (odds -1)
- turnover += stake
- previous_race_number = row["race_number"]
- else:
- #Simple check to find an input error in the model name.
- raise Exception("Input model incorrect. Choose either 'prediction' or 'monte-carlo' as input string")
- return turnover, EV, PnL, commission, PnL - commission, (PnL - commission) / turnover * 100
- def main():
- #Set working directory so that horses.csv can be accessed without full path.
- os.chdir(os.path.dirname(os.path.realpath(__file__)))
- #Read the CSV file
- csv_path = "horses.csv"
- racing_data = pd.read_csv(csv_path)
- #Define starting bank
- bank = 10000
- minimum_stake = 2
- #Extract Values based on data and the set values on bank and minimum stake.
- turnover, EV, PnL, commission, Net_PnL, RoI = value_calculation(racing_data, bank, minimum_stake, "prediction")
- #Print the prediction values
- print("Values from predictions:")
- print_all_values(turnover, EV, PnL, commission, Net_PnL, RoI)
- #To find the edge of this project, we compare it to a completely random
- #punter. We choose to back or lay at random, and then pick a random amount to
- #bet. When laying, the amount maximum will correspond to a £10,000 liability.
- turnover, EV, PnL, commission, Net_PnL, RoI = value_calculation(racing_data, bank, minimum_stake, "monte_carlo")
- #Print monte-carlo values.
- print("Values from monte-carlo")
- print_all_values(turnover, EV, PnL, commission, Net_PnL, RoI)
- if __name__=="__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement