Backtest#

Backtest module

Used to backtest different betting strategies.

class penaltyblog.backtest.Account(bankroll: float)[source]#

Bases: object

Used to make and track bets made during the backtest

place_bet(odds, stake, outcome)[source]#

Places a virtual bet

place_bet(odds: float, stake: float, outcome: int)[source]#
Parameters:
  • odds (float) – The odds for the bet in European decimal format

  • stake (float) – The number of units to be staked on the bet

  • outcome (int) – The outcome of the bet, 1 for successful and 0 for failed

class penaltyblog.backtest.Backtest(data: DataFrame, start_date: str, end_date: str, stop_at_negative: bool = False)[source]#

Bases: object

Used to backtest different betting strategies.

start(bankroll, logic, trainer)[source]#

Runs the backtest using the logic function (and optionally the trainer function)

results()[source]#

Calculates how well the backtest has performed

results() dict[source]#

Calculates the results of the backtest and returns them as a dict

Return type:

Dictionary containing metrics about the backtest

start(bankroll: float, logic: Callable, trainer: Callable | None = None)[source]#
Parameters:
  • bankroll (float) – The initial starting value for the bankroll

  • logic (callable) – The function to apply to each individual fixture. The function should have one argument called ctx, which contains the the information required to run the strategy. See the example notebooks for more examples of the logic function and how to use the ctx object. ctx will contain an instance of the Account class, which contains functions for placing virtual bets, lookback which contains all the fuxtures prior to the date of the current fixture, fixture which is the current fixture being processed, and optionally model if a trainer function is used.

  • trainer (callable) – The function used to train a model, which is then added to the ctx object passed to the logic function. This function should have one argument called ctx, which contains the the information required to train the model and should return the trained model. See the example notebooks for more examples of the trainer function and how to use the ctx object. The trainer function gets called once per unique date and then is made available to all fixtures for that date.

Return type:

None

class penaltyblog.backtest.Context(account: Account, lookback: DataFrame, fixture: Series | None, model=None)[source]#

Bases: object

Object passed into the logic and trainer functions. Contains the account object, lookback data, fixture being processed and optionally a trained model