Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Augur Sportsdataio spread direction #11

Open
wants to merge 2 commits into
base: augur/testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions packages/composites/augur/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
}
3 changes: 3 additions & 0 deletions packages/composites/augur/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@types/chai": "^4.2.21",
"@types/chai-spies": "^1",
"@types/express": "*",
"@types/jest": "^27.0.1",
"@types/luxon": "^1.27.1",
"@types/mocha": "^9.0.0",
"@types/node": "^14.14.35",
Expand All @@ -70,6 +71,8 @@
"hardhat-deploy": "0.8.11",
"hardhat-docgen": "^1.1.0",
"hardhat-typechain": "^0.3.5",
"jest": "^27.0.6",
"ts-jest": "^27.0.5",
"typechain": "^4.0.3",
"typescript": "^4.2.3"
}
Expand Down
29 changes: 27 additions & 2 deletions packages/composites/augur/src/dataProviders/sportsdataio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface NFLEvent {
OverUnder: number | null
}

interface TeamSchedule {
export interface TeamSchedule {
Date: string
GameID: number
AwayTeamName: string
Expand Down Expand Up @@ -346,7 +346,7 @@ export const createTeam: Execute = async (input, context) => {
awayTeamName: event.AwayTeamName,
awayTeamId: event.AwayTeamID,
startTime,
homeSpread: event.PointSpread || 0,
homeSpread: calcHomeSpread(event),
totalScore: event.OverUnder || 0,
createSpread: event.PointSpread !== null,
createTotalScore: event.OverUnder !== null,
Expand All @@ -364,6 +364,31 @@ export const createTeam: Execute = async (input, context) => {
})
}

export function calcHomeSpread(event: TeamSchedule): number {
const { HomeTeamMoneyLine, AwayTeamMoneyLine, PointSpread } = event
if (HomeTeamMoneyLine === null) return 0
if (AwayTeamMoneyLine === null) return 0
if (PointSpread === null) return 0

// If one is negative then it's favored.
// If both are negative then the most-negative is favored.
// Ergo, the smallest is favored.
const homeFavored = HomeTeamMoneyLine < AwayTeamMoneyLine

// Here we canculate home spread specifically, which should be positive is home is favored.
return homeFavored ? ensurePositive(PointSpread) : ensureNegative(PointSpread)
}

export function ensureNegative(n: number): number {
if (n > 0) n = -n
return n
}

export function ensurePositive(n: number): number {
if (n < 0) n = -n
return n
}

interface FightSchedule {
Active: boolean
DateTime: string
Expand Down
67 changes: 67 additions & 0 deletions packages/composites/augur/test/unit/calculations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
calcHomeSpread,
ensureNegative,
ensurePositive,
TeamSchedule,
} from '../../src/dataProviders/sportsdataio'

describe('calculations', () => {
describe('calcHomeSpread', () => {
enum r {
zero,
positive,
negative,
}
const testcases = [
// favor the most negative line
{ spread: 1.5, home: -1, away: +1, result: 1.5 },
{ spread: 1.5, home: -2, away: -1, result: 1.5 },
{ spread: 1.5, home: +1, away: -1, result: -1.5 },
{ spread: 1.5, home: -1, away: -2, result: -1.5 },

// input sign shouldn't matter
{ spread: -1.5, home: -1, away: +1, result: 1.5 },
{ spread: -1.5, home: -2, away: -1, result: 1.5 },
{ spread: -1.5, home: +1, away: -1, result: -1.5 },
{ spread: -1.5, home: -1, away: -2, result: -1.5 },

// if lines are the same then spread should be zero anyway
{ spread: 0, home: -1, away: -1, result: 0 },

// if a field is missing then return zero
{ spread: null, home: -1, away: -1, result: 0 },
{ spread: 1.5, home: null, away: -1, result: 0 },
{ spread: 1.5, home: -1, away: null, result: 0 },
]

for (const testcase of testcases) {
const { spread, home, away, result } = testcase
it(`calc with lines ${home}:${away} :: ${spread} -> ${result}`, () => {
const event = makeFakeEvent({
PointSpread: spread,
HomeTeamMoneyLine: home,
AwayTeamMoneyLine: away,
})
const calc = calcHomeSpread(event)
expect(calc).toEqual(result)
})
}
})
})

function makeFakeEvent(override: Partial<TeamSchedule>): TeamSchedule {
return {
Date: '',
GameID: 0,
AwayTeamName: '',
AwayTeamID: 0,
HomeTeamName: '',
HomeTeamID: 0,
Status: '',
PointSpread: null,
AwayTeamMoneyLine: null,
HomeTeamMoneyLine: null,
OverUnder: null,
...override,
}
}
Loading