Binary Strategies

Binary strategies are strategies that reason about singular binary bets. These bets have some payoff if they win, some loss if they lose, and a transaction cost regardless. Bets are not continuous, they either win or lose.

Base Strategy

class keeks.binary_strategies.base.BaseStrategy(payoff: float, loss: float, transaction_cost: float = 0)[source]

Bases: ABC

Abstract base class for all binary betting strategies.

This class defines the interface that all binary betting strategies must implement. Concrete strategy implementations should inherit from this class and implement the evaluate method.

abstract evaluate(probability: float, current_bankroll: float) float[source]

Evaluate the strategy for a given probability.

Parameters:
  • probability (float) – The probability of winning.

  • current_bankroll (float) – The current bankroll to use for calculations.

Returns:

The proportion of the bankroll to bet.

Return type:

float

get_max_safe_bet(current_bankroll: float) float[source]

Calculate the maximum safe bet size based on current bankroll.

Parameters:

current_bankroll (float) – The current bankroll to use for calculations.

Returns:

The maximum safe bet size as a proportion of bankroll.

Return type:

float

Naive Strategy

class keeks.binary_strategies.simple.NaiveStrategy(payoff, loss, transaction_cost)[source]

Bases: BaseStrategy

A simple betting strategy that bets based on expected value.

This strategy calculates the expected value of a bet and bets a fixed fraction of the bankroll if the expected value is positive.

Parameters:
  • payoff (float) – The amount won per unit bet on a successful outcome.

  • loss (float) – The amount lost per unit bet on an unsuccessful outcome.

  • transaction_cost (float) – The fixed cost per transaction, regardless of outcome.

evaluate(probability, current_bankroll)[source]

Calculate the bet size based on expected value.

The expected value is calculated as: EV = (probability * payoff) - ((1 - probability) * loss) - transaction_cost

If EV is positive, bet a fixed fraction of the bankroll. If EV is negative, do not bet.

Parameters:
  • probability (float) – The probability of a successful outcome, typically between 0 and 1.

  • current_bankroll (float) – The current bankroll amount.

Returns:

The proportion of the bankroll to bet.

Return type:

float

Fixed Fraction Strategy

class keeks.binary_strategies.simple.FixedFractionStrategy(fraction, payoff, loss, transaction_cost=0, min_probability=0.5)[source]

Bases: BaseStrategy

A simple strategy that bets a fixed percentage of the bankroll.

This strategy ignores probabilities and odds completely, and simply wagers a fixed fraction of the bankroll. It can be used as a baseline for comparison or as a simple approach for risk management.

Parameters:
  • fraction (float) – The fixed fraction of the bankroll to bet (between 0 and 1).

  • payoff (float) – The amount won per unit bet on a successful outcome.

  • loss (float) – The amount lost per unit bet on an unsuccessful outcome.

  • transaction_cost (float, default=0) – The fixed cost per transaction, regardless of outcome.

  • min_probability (float, default=0.5) – The minimum probability required to place a bet.

evaluate(probability, current_bankroll)[source]

Return the fixed fraction if the probability meets the minimum threshold.

Parameters:
  • probability (float) – The probability of a successful outcome, typically between 0 and 1.

  • current_bankroll (float) – The current bankroll amount.

Returns:

The fixed fraction if probability >= min_probability, otherwise 0.

Return type:

float

CPPI Strategy

class keeks.binary_strategies.simple.CPPIStrategy(floor_fraction, multiplier, initial_bankroll, payoff, loss, transaction_cost=0, min_probability=0.5)[source]

Bases: BaseStrategy

Implementation of Constant Proportion Portfolio Insurance (CPPI) for binary betting.

This strategy maintains a dynamic floor below which the bankroll should not fall, and invests a multiple of the cushion (amount above floor) in each bet.

Parameters:
  • floor_fraction (float) – The fraction of initial bankroll to maintain as a floor.

  • multiplier (float) – The multiplier to apply to the cushion for determining exposure.

  • initial_bankroll (float) – The initial bankroll amount.

  • payoff (float) – The amount won per unit bet on a successful outcome.

  • loss (float) – The amount lost per unit bet on an unsuccessful outcome.

  • transaction_cost (float) – The fixed cost per transaction, regardless of outcome.

  • min_probability (float, default=0.5) – The minimum probability required to place a bet.

evaluate(probability, current_bankroll)[source]

Calculate the CPPI bet size based on the cushion above the floor.

Parameters:
  • probability (float) – The probability of a successful outcome, typically between 0 and 1.

  • current_bankroll (float) – The current bankroll amount.

Returns:

The proportion of the current bankroll to bet, or 0 if below the minimum probability threshold.

Return type:

float

update_bankroll(new_bankroll)[source]

Update the current bankroll value and adjust floor based on peak value.

Parameters:

new_bankroll (float) – The current value of the bankroll.

Dynamic Bankroll Management

class keeks.binary_strategies.simple.DynamicBankrollManagement(base_fraction, payoff, loss, transaction_cost, window_size=10, max_fraction=0.2, min_fraction=0.05)[source]

Bases: BaseStrategy

A dynamic bankroll management strategy that adjusts bet sizes based on performance.

This strategy adjusts the base bet fraction based on: 1. Recent performance (win/loss streak) 2. Volatility of returns 3. Current drawdown level 4. Probability of success

Parameters:
  • base_fraction (float) – The base fraction of bankroll to bet.

  • payoff (float) – The amount won per unit bet on a successful outcome.

  • loss (float) – The amount lost per unit bet on an unsuccessful outcome.

  • transaction_cost (float) – The fixed cost per transaction, regardless of outcome.

  • window_size (int, default=10) – The number of recent results to consider for adjustments.

  • max_fraction (float, default=0.2) – The maximum fraction of bankroll that can be bet.

  • min_fraction (float, default=0.05) – The minimum fraction of bankroll to bet.

evaluate(probability, current_bankroll)[source]

Calculate the bet size based on all adjustment factors.

Parameters:
  • probability (float) – The probability of a successful outcome.

  • current_bankroll (float) – The current bankroll amount.

Returns:

The proportion of the current bankroll to bet.

Return type:

float

get_drawdown_factor()[source]

Calculate the adjustment factor based on current drawdown.

get_probability_factor(probability)[source]

Calculate the adjustment factor based on probability.

get_streak_factor()[source]

Calculate the adjustment factor based on recent performance.

get_volatility_factor()[source]

Calculate the adjustment factor based on return volatility.

record_result(won, return_pct=None)[source]

Record the result of a bet.

Parameters:
  • won (bool) – Whether the bet was won.

  • return_pct (float, optional) – The return percentage of the bet. If not provided, calculated from won/loss.

Kelly Criterion

class keeks.binary_strategies.kelly.KellyCriterion(payoff, loss, transaction_cost, min_probability=0.5)[source]

Bases: BaseStrategy

Implementation of the Kelly Criterion for binary betting.

The Kelly Criterion is a mathematical formula that determines the optimal size of a series of bets to maximize long-term growth rate.

Parameters:
  • payoff (float) – The amount won per unit bet on a successful outcome.

  • loss (float) – The amount lost per unit bet on an unsuccessful outcome.

  • transaction_cost (float) – The fixed cost per transaction, regardless of outcome.

  • min_probability (float, default=0.5) – The minimum probability required to place a bet.

evaluate(probability, current_bankroll)[source]

Calculate the optimal Kelly bet size.

The Kelly Criterion formula is: f* = (bp - q) / b

where: f* = fraction of current bankroll to bet b = net odds received on the bet (payoff/loss) p = probability of winning q = probability of losing (1 - p)

Parameters:
  • probability (float) – The probability of a successful outcome, typically between 0 and 1.

  • current_bankroll (float) – The current bankroll amount.

Returns:

The optimal proportion of the bankroll to bet.

Return type:

float

Fractional Kelly Criterion

class keeks.binary_strategies.kelly.FractionalKellyCriterion(payoff, loss, transaction_cost, fraction)[source]

Bases: BaseStrategy

Implementation of the Fractional Kelly Criterion for binary betting.

The Fractional Kelly Criterion applies a fraction to the full Kelly bet size, which can reduce volatility at the expense of long-term growth rate.

Parameters:
  • payoff (float) – The amount won per unit bet on a successful outcome.

  • loss (float) – The amount lost per unit bet on an unsuccessful outcome.

  • transaction_cost (float) – The fixed cost per transaction, regardless of outcome.

  • fraction (float) – The fraction of the full Kelly bet to use (typically between 0 and 1).

evaluate(probability, current_bankroll)[source]

Calculate the fractional Kelly bet size.

Parameters:
  • probability (float) – The probability of a successful outcome, typically between 0 and 1.

  • current_bankroll (float) – The current bankroll amount.

Returns:

The optimal proportion of the bankroll to bet, multiplied by the fraction parameter.

Return type:

float

Drawdown-Adjusted Kelly

class keeks.binary_strategies.kelly.DrawdownAdjustedKelly(payoff, loss, transaction_cost, max_acceptable_drawdown=0.2)[source]

Bases: BaseStrategy

Implementation of the Drawdown-Adjusted Kelly Criterion for binary betting.

This strategy adjusts the Kelly bet size based on a maximum acceptable drawdown. It provides a more conservative approach by reducing the bet size to minimize the risk of large drawdowns.

Parameters:
  • payoff (float) – The amount won per unit bet on a successful outcome.

  • loss (float) – The amount lost per unit bet on an unsuccessful outcome.

  • transaction_cost (float) – The fixed cost per transaction, regardless of outcome.

  • max_acceptable_drawdown (float) – The maximum acceptable drawdown as a fraction of the bankroll (0 to 1).

evaluate(probability, current_bankroll)[source]

Calculate the drawdown-adjusted Kelly bet size.

The adjustment is an approximation based on the relationship between bet size and expected drawdown in repeated betting scenarios.

Parameters:
  • probability (float) – The probability of a successful outcome, typically between 0 and 1.

  • current_bankroll (float) – The current bankroll amount.

Returns:

The drawdown-adjusted proportion of the bankroll to bet.

Return type:

float

OptimalF

class keeks.binary_strategies.simple.OptimalF(payoff, loss, transaction_cost, win_rate, max_risk_fraction=0.2)[source]

Bases: BaseStrategy

Implementation of the Optimal f strategy for binary betting.

This strategy calculates the optimal fraction of bankroll to bet based on the win rate and payoff ratio. It’s similar to Kelly but uses a different approach to risk management.

Parameters:
  • payoff (float) – The amount won per unit bet on a successful outcome.

  • loss (float) – The amount lost per unit bet on an unsuccessful outcome.

  • transaction_cost (float) – The fixed cost per transaction, regardless of outcome.

  • win_rate (float) – The historical or expected win rate (between 0 and 1).

  • max_risk_fraction (float, default=0.2) – The maximum fraction of bankroll that can be risked on a single bet.

evaluate(probability, current_bankroll)[source]

Calculate the optimal f bet size based on win rate and payoff ratio.

This implementation follows Ralph Vince’s approach to Optimal f, which is based on maximizing the terminal wealth relative (TWR) for a given risk-to-reward profile.

Parameters:
  • probability (float) – The probability of a successful outcome, typically between 0 and 1.

  • current_bankroll (float) – The current bankroll amount.

Returns:

The optimal proportion of the bankroll to bet.

Return type:

float