Line shopping and devigging in R

This vignette compares prices and removes the bookmaker margin (the “vig”) using parlayapiR and keyless synthetic sandbox endpoints. It demonstrates calculations, not a tested profitable strategy. Keep market definitions and outcomes aligned before comparing real quotes.

library(parlayapiR)

1. Pull a board of odds

Sandbox mode serves synthetic but structurally identical data from /v1/sandbox/ endpoints. Prices come back in American format.

events <- pa_odds("basketball_nba", markets = "h2h", sandbox = TRUE)
events[, c("home_team", "away_team", "commence_time")]
#>               home_team       away_team        commence_time
#> 1    Los Angeles Lakers  Boston Celtics 2026-12-03T04:51:14Z
#> 2 Golden State Warriors      Miami Heat 2026-12-02T21:22:15Z
#> 3        Denver Nuggets Milwaukee Bucks 2026-12-03T09:01:38Z

Each event carries a bookmakers list-column in the same shape used by The Odds API v4: bookmakers, then markets, then outcomes.

2. Flatten to one row per quote

quotes <- do.call(rbind, lapply(seq_len(nrow(events)), function(i) {
  ev <- events[i, ]
  books <- ev$bookmakers[[1]]
  do.call(rbind, lapply(seq_len(nrow(books)), function(b) {
    mkt <- books$markets[[b]][1, ]
    outs <- mkt$outcomes[[1]]
    data.frame(
      event = paste(ev$away_team, "at", ev$home_team),
      book = books$key[b],
      outcome = outs$name,
      american = outs$price
    )
  }))
}))
quotes$decimal <- pa_american_to_decimal(quotes$american)
head(quotes)
#>                                  event       book            outcome american
#> 1 Boston Celtics at Los Angeles Lakers draftkings Los Angeles Lakers      145
#> 2 Boston Celtics at Los Angeles Lakers draftkings     Boston Celtics     -100
#> 3 Boston Celtics at Los Angeles Lakers    fanduel Los Angeles Lakers     -110
#> 4 Boston Celtics at Los Angeles Lakers    fanduel     Boston Celtics      110
#> 5 Boston Celtics at Los Angeles Lakers     betmgm Los Angeles Lakers      100
#> 6 Boston Celtics at Los Angeles Lakers     betmgm     Boston Celtics      130
#>    decimal
#> 1 2.450000
#> 2 2.000000
#> 3 1.909091
#> 4 2.100000
#> 5 2.000000
#> 6 2.300000

3. Line shopping: the best price per outcome

For this single-market sandbox example, find the highest decimal price for each outcome. With real data, first check exact event, market, period and line identity; matching team names alone is insufficient:

best <- aggregate(decimal ~ event + outcome, data = quotes, FUN = max)
names(best)[3] <- "best_decimal"
best <- merge(
  best,
  quotes,
  by.x = c("event", "outcome", "best_decimal"),
  by.y = c("event", "outcome", "decimal")
)
best[, c("event", "outcome", "book", "best_decimal")]
#>                                   event               outcome       book
#> 1  Boston Celtics at Los Angeles Lakers        Boston Celtics     betmgm
#> 2  Boston Celtics at Los Angeles Lakers    Los Angeles Lakers     kalshi
#> 3  Boston Celtics at Los Angeles Lakers    Los Angeles Lakers draftkings
#> 4   Miami Heat at Golden State Warriors Golden State Warriors    fanduel
#> 5   Miami Heat at Golden State Warriors Golden State Warriors     betmgm
#> 6   Miami Heat at Golden State Warriors            Miami Heat     bovada
#> 7   Miami Heat at Golden State Warriors            Miami Heat polymarket
#> 8   Miami Heat at Golden State Warriors            Miami Heat     kalshi
#> 9     Milwaukee Bucks at Denver Nuggets        Denver Nuggets polymarket
#> 10    Milwaukee Bucks at Denver Nuggets       Milwaukee Bucks draftkings
#>    best_decimal
#> 1          2.30
#> 2          2.45
#> 3          2.45
#> 4          2.45
#> 5          2.45
#> 6          2.30
#> 7          2.30
#> 8          2.30
#> 9          2.20
#> 10         2.10

4. Devig a reference book to get fair probabilities

A quoted -110 / -110 market implies 52.4% for each side, which sums to 104.8%: the extra 4.8% is the margin. pa_devig() removes it. Here we devig one book’s two-way market three ways:

ev1 <- quotes[quotes$event == quotes$event[1], ]
ref <- ev1[ev1$book == ev1$book[1], ]
ref
#>                                  event       book            outcome american
#> 1 Boston Celtics at Los Angeles Lakers draftkings Los Angeles Lakers      145
#> 2 Boston Celtics at Los Angeles Lakers draftkings     Boston Celtics     -100
#>   decimal
#> 1    2.45
#> 2    2.00

pa_devig(ref$american, odds_format = "american")
#>     implied fair_prob fair_decimal
#> 1 0.4081633 0.4494382     2.225000
#> 2 0.5000000 0.5505618     1.816327
pa_devig(ref$american, odds_format = "american", method = "additive")
#>     implied fair_prob fair_decimal
#> 1 0.4081633 0.4540816     2.202247
#> 2 0.5000000 0.5459184     1.831776
pa_devig(ref$american, odds_format = "american", method = "power")
#>     implied fair_prob fair_decimal
#> 1 0.4081633 0.4556081     2.194869
#> 2 0.5000000 0.5443919     1.836912

The multiplicative method scales everything by the same factor. The power method shrinks long shots harder, which better matches the favorite-longshot bias in real markets. For two-way markets near even money the three methods agree closely.

5. Edge: fair probability versus the best available price

An edge exists when the best shopped price implies a lower probability than your fair estimate:

fair <- pa_devig(ref$american, odds_format = "american", method = "power")
comp <- data.frame(
  outcome = ref$outcome,
  fair_prob = fair$fair_prob,
  best_decimal = best$best_decimal[match(
    paste(ev1$event[1], ref$outcome),
    paste(best$event, best$outcome)
  )]
)
comp$edge <- comp$fair_prob * comp$best_decimal - 1
comp
#>              outcome fair_prob best_decimal      edge
#> 1 Los Angeles Lakers 0.4556081         2.45 0.1162398
#> 2     Boston Celtics 0.5443919         2.30 0.2521014

A positive edge means the price exceeds the threshold implied by this particular probability estimate. It does not establish that the estimate is accurate or that a wager has positive expected value.

6. Size the bet with the Kelly criterion

plays <- comp[comp$edge > 0, ]
if (nrow(plays)) {
  plays$stake <- pa_kelly(
    prob = plays$fair_prob,
    odds = plays$best_decimal,
    fraction = 0.25,
    bankroll = 1000
  )
}
plays
#>              outcome fair_prob best_decimal      edge    stake
#> 1 Los Angeles Lakers 0.4556081         2.45 0.1162398 20.04135
#> 2     Boston Celtics 0.5443919         2.30 0.2521014 48.48104

The fraction here is an illustrative input. Kelly sizing is sensitive to probability error; this example is not a staking recommendation.

Going live

Everything above works identically against live data. Get a free key (1,000 credits per month, no card required) at parlay-api.com, then:

pa_auth("your-api-key")
events <- pa_odds("basketball_nba", markets = "h2h,spreads,totals")
props <- pa_props("basketball_nba", markets = "player_points")
closes <- pa_closing_odds("basketball_nba", season = "2024")

Historical odds reach back to 2005, and closing lines include more than 30 million player prop closes since 2022, which makes pa_closing_odds() the natural calibration target for any model you backtest in R.