Hedging Calculator in Python: Lock Profit Across 350+ Books

Hedging Calculator in Python - OddsPapi API Blog
How To Guides July 7, 2026

You backed a team early at a price you loved. Now the game is hours away, the line has moved your way, and you would rather lock in a guaranteed return than sweat the result. That is hedging: placing a second bet on the opposite outcome so you profit (or cap your loss) no matter what happens. The catch is that the price you get on the hedge leg decides whether you lock a profit or a loss, and that price is different at every book.

This guide builds a Python hedging calculator, then shows why pulling the hedge price from 350+ bookmakers instead of one turns a break-even hedge into a profitable one. Every number below came from a live MLB market.

Hedging Is Not Arbitrage

People conflate the two. They are different jobs:

  • Arbitrage backs every outcome at the same moment for a guaranteed profit, with no prior position. See our arbitrage bot guide.
  • Hedging starts from a bet you already placed. You add one bet on the other side to lock in a result, usually because the line moved or you want to bank a sure thing.

The math overlaps, but the decision is different. With a hedge you are not hunting an edge from scratch, you are protecting a position you already hold. And the only lever you control is which book you take the hedge at.

The Core Formula

To lock in an equal return regardless of outcome, the hedge stake is your original return divided by the hedge odds:

def hedge(original_stake, original_odds, hedge_odds):
    """Lock an equal return on both sides. Returns (stake, profit, roi%)."""
    target_return = original_stake * original_odds
    hedge_stake = target_return / hedge_odds
    total_staked = original_stake + hedge_stake
    profit = target_return - total_staked          # identical either way
    roi = profit / total_staked * 100
    return round(hedge_stake, 2), round(profit, 2), round(roi, 2)


# You took the Red Sox at 2.20 with a 100 unit stake.
print(hedge(100, 2.20, 1.852))   # hedge the other side at 1.852
# -> (118.79, 1.21, 0.55)

Whichever team wins, you get back 220 for 218.79 staked: a locked 1.21 unit profit. That works because the combined implied probability of the two prices, 1/2.20 + 1/1.852 = 0.9945, is below 1. Anything under 1 means a guaranteed profit is on the table.

Why the Hedge Book Decides Everything

Here is the part single-book bettors miss. We pulled a live moneyline (Seattle Mariners vs Boston Red Sox, 15 books) and looked at every price available to hedge the Mariners side after backing the Red Sox at 2.20:

Hedge price on Mariners Book Hedge stake Locked result
1.852 (best available) Polymarket 118.79 +1.21 (+0.55%)
1.847 Pinnacle 119.11 +0.89 (+0.41%)
1.714 (worst active) HardRock Bet 128.35 -8.35 (-3.66%)

Same original bet, same opponent, same moment. Hedging at the best of 15 books locks a profit. Hedging at the worst locks a 3.66 percent loss. The only variable is where you place the hedge, and the gap is nearly 10 units on a 100 unit position. If you only check the book you originally bet at, you are gambling on it happening to have the best hedge price, which it usually does not.

Pull Every Hedge Price in One Call

This is exactly what an aggregator is for. One call to the OddsPapi feed returns every book’s price for the outcome you want to hedge, already in decimal. Auth is the apiKey query parameter, never a header.

import requests

API_KEY = "YOUR_API_KEY"
BASE = "https://api.oddspapi.io/v4"

# Mariners vs Red Sox moneyline. Market 131; outcome 131 home, 132 away.
odds = requests.get(f"{BASE}/odds",
                    params={"apiKey": API_KEY,
                            "fixtureId": "id1300010963303711"}).json()

def best_price(odds, market_id, outcome_id):
    """Highest active price for an outcome across every book."""
    best_book, best = None, 0.0
    for slug, book in odds["bookmakerOdds"].items():
        try:
            o = book["markets"][str(market_id)]["outcomes"][str(outcome_id)]
            px = o["players"]["0"]
        except KeyError:
            continue
        if px["active"] and px["price"] > best:
            best_book, best = slug, px["price"]
    return best_book, best


book, price = best_price(odds, 131, 131)      # best Mariners hedge price
print(book, price)                            # -> polymarket 1.852

stake, profit, roi = hedge(100, 2.20, price)
print(f"Hedge {stake} on Mariners at {book} ({price}) -> lock {profit}")
# -> Hedge 118.79 on Mariners at polymarket (1.852) -> lock 1.21

The active check matters: a suspended outcome can still carry a stale price, and hedging into a number that no longer trades is how a “guaranteed” lock evaporates. Always filter on active before you size a hedge.

Partial Hedges: Guarantee No Loss, Keep Upside

A full hedge returns the same either way. Often you would rather guarantee you cannot lose while keeping upside if your original pick wins. Hedge a smaller amount so the hedge side just breaks even:

def partial_hedge(original_stake, original_odds, hedge_odds):
    """Hedge just enough that the hedge side breaks even; keep the upside."""
    # hedge side: hs*hedge_odds - (original_stake + hs) = 0  ->  solve hs
    hs = original_stake / (hedge_odds - 1)
    if_original_wins = original_stake * original_odds - (original_stake + hs)
    return round(hs, 2), round(if_original_wins, 2)


# Hedge the Mariners just enough to risk nothing
hs, upside = partial_hedge(100, 2.20, 1.852)
print(hs, upside)   # -> 117.37, 2.63  (can't lose; keep +2.63 if Red Sox win)

This is the realistic middle ground for futures and live betting: you de-risk a long-held ticket to a no-lose floor without surrendering all the value you built. For tracking when those windows open, the dynamic odds guide shows how to watch a price move in real time.

Where the Free API Earns Its Keep

A hedge is only as good as the price you find for it. That is a coverage problem, and coverage is the whole pitch:

  • 350+ bookmakers in one response, so the best hedge price (which was Polymarket here, not a traditional sportsbook) is always in view.
  • Free historical odds to replay how a hedge window opened and closed, so you learn when to pull the trigger.
  • Prediction markets and exchanges included, which frequently post the sharpest number on one side of a two-way market.

Pair this with line shopping to always source the best hedge leg, the middling guide for the related “win both sides” structure, and the vig calculator to understand why some books are cheaper to hedge at than others.

Frequently Asked Questions

What is hedging in sports betting?

Hedging is placing a bet on the opposite outcome of a bet you already hold, so you lock in a guaranteed return or limit your loss regardless of the result. It differs from arbitrage, which backs every outcome at once with no prior position.

How do I calculate a hedge bet stake?

For a full hedge that returns the same either way, divide your original potential return by the hedge odds. If you backed 100 at 2.20 (a 220 return) and hedge at 1.852, the hedge stake is 220 / 1.852 = 118.79. The profit is identical whichever side wins.

When does hedging guarantee a profit?

When the combined implied probability of your original price and the hedge price is below 1 (that is, 1/original_odds + 1/hedge_odds < 1). In the example, 1/2.20 + 1/1.852 = 0.9945, so the hedge locks a small profit. Above 1, the hedge locks a loss.

Why does the bookmaker I hedge at matter so much?

The hedge price sets the result. On the same live market, hedging at the best of 15 books locked a 0.55 percent profit while hedging at the worst locked a 3.66 percent loss. Pulling every book’s price and taking the highest is the difference between a good hedge and a bad one.

Can I hedge a bet across different bookmakers?

Yes, and you usually should. Your original bet sits at one book; the best price to hedge against it is often at a different book or a prediction market entirely. An odds API that aggregates 350+ books lets you find that best hedge price in a single call.

Lock It In

A hedge is one formula and one good price away. Get a free OddsPapi API key, pull every book’s price on the outcome you need, and size your hedge against the best number in the market.