How do I backtest NBA closing lines in Python?
A day of games with every sportsbook's opening and closing moneyline, spread and total, next to whether the home side covered and the total went over.
Code verified November 18, 2026. One call to get_odds, 5 credits.
The call
date is a US Eastern day. Loop it over a range to build a season.
import requests
import pandas as pd
API_KEY = "YOUR_API_KEY"
resp = requests.post(
"https://mcp.dunkapi.com/v1/get_odds",
headers={"x-api-key": API_KEY},
json={
"date": "2026-01-15",
},
timeout=60,
)
resp.raise_for_status()
data = resp.json()["data"]
rows = []
for g in data["games"]:
for b in g["books"]:
margin = g["home"]["score"] - g["away"]["score"]
rows.append({
"game": g["away"]["abbrev"] + " @ " + g["home"]["abbrev"],
"book": b["provider"],
"spread_close": b["home_spread_close"],
"margin": margin,
"home_covered": g["result"]["home_covered"],
"total_close": b["total_close"],
"points": g["home"]["score"] + g["away"]["score"],
"over": g["result"]["over"],
})
df = pd.DataFrame(rows)
df["cover_margin"] = df["margin"] + df["spread_close"]
print(df.to_string(index=False))
print(df.groupby("book")[["home_covered", "over"]].mean().round(2))
Replace YOUR_API_KEY with a key from your dashboard. Python needs requests and pandas (pip install requests pandas). JavaScript runs on Node 18 or newer, or in the browser.
Or use the official client: pip install dunkapi
What comes back
The data object of the response, trimmed to the first rows. Every field is real output from the run above.
{
"games": [
{
"game_id": "401810433",
"date": "2026-01-15",
"status": "final",
"home": {
"team_id": "19",
"abbrev": "ORL",
"name": "Orlando Magic",
"score": 118
},
"away": {
"team_id": "29",
"abbrev": "MEM",
"name": "Memphis Grizzlies",
"score": 111
},
"books": [
{
"provider": "BetMGM",
"home_ml_open": -210,
"home_ml_close": -220,
"away_ml_open": 170,
"away_ml_close": 180,
"home_close_prob": 0.688,
"away_close_prob": 0.357,
"home_spread_open": -5.5,
"home_spread_close": -5.5,
"spread_home_odds": -110,
"spread_away_odds": -110,
"total_open": 231.5,
"total_close": 227.5,
"over_odds": -110,
"under_odds": -110,
"captured_at": "2026-01-15T18:50:38.000Z"
}
],
"result": {
"median_home_spread": -5.5,
"median_total": 227.5,
"home_covered": true,
"over": true
}
},
"...8 more"
],
"count": 72
}What to do next
- get_line_movement (10 credits). Snapshots of each line before tip-off, to test a rule on the open instead of the close.
- get_ratings (2 credits). Our team Elo ratings, to compare a model's win chance with the implied probability.
The data this uses
Every row above comes from NBA odds history and NBA player props by sportsbook, from 2023. Each sportsbook's moneyline, spread and total for NBA games from 2013-14, with results against the line. Built for backtests. REST or MCP.
More guides
The same guide for other sports
- PuckAPI: How do I get NHL opening and closing odds by sportsbook in Python?
- EndzoneAPI: How do I backtest NFL closing lines in Python?
- FighterAPI: How do I compare UFC closing odds with fight results in Python?
- StrikerAPI: How do I get Premier League odds from every sportsbook with the results?
- AcesAPI: How do I compare ATP closing odds with the result and Elo in Python?
- BirdieAPI: How do I turn golf major winner odds into probabilities in Python?
- FastballAPI: How do I backtest MLB closing moneylines and run totals in Python?
- YorkerAPI: How do I get historical cricket odds in Python?
The latest lines from every book are on the odds board. Rather ask Claude? 16 free Claude skills call these endpoints for you.
Start with 500 free credits every month
No card. REST or MCP.