public static double CalculateHorseRaceEV(
double backOdds, // Decimal odds for the horse to win
double top2Odds, // Decimal odds for the horse to finish in the top 2
double stake, // The stake amount
double layOdds, // Decimal odds for laying the horse
double commissionRate, // The commission rate for winning lay bets
double underlayFactor // Underlay factor (1 for normal laying, < 1 for underlaying)
)
{
// Calculate the probabilities from the odds
double probabilityWin = 1 / backOdds;
double probabilityTop2 = 1 / top3Odds;
double probability2nd = probabilityTop2 - probabilityWin;
double probabilityLose = 1 - probabilityTop2;
// Calculate the potential returns
double payoutWin = stake * (backOdds - 1);
double bonus = stake * 0.8;
double loss = -stake;
// Calculate the lay stake, adjust for underlaying
double layStake = (underlayFactor * stake * (backOdds - 1)) / (layOdds - commissionRate);
// Calculate the returns for lay bet
double layWinProfit = layStake * (1 - commissionRate);
double layLoss = -layStake * (layOdds - 1);
// Calculate the EV considering the lay and back bets
double ev = (probabilityWin * (payoutWin - layLoss)) +
(probability2nd * (bonus - layLoss)) +
(probabilityLose * (loss + layWinProfit));
return ev;
}