overtime-live-trading-utils 1.1.36 → 1.1.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,153 +1,153 @@
1
- import * as oddslib from 'oddslib';
2
-
3
- export const getBookmakersArray = (bookmakersData, sportId, backupLiveOddsProviders) => {
4
- const sportBookmakersData = bookmakersData.find((data) => Number(data.sportId) === Number(sportId));
5
- if (sportBookmakersData) {
6
- if (sportBookmakersData.primaryBookmaker == '') {
7
- return backupLiveOddsProviders;
8
- }
9
- const bookmakersArray: string[] = [];
10
-
11
- sportBookmakersData.primaryBookmaker ? bookmakersArray.push(sportBookmakersData.primaryBookmaker) : '';
12
- sportBookmakersData.secondaryBookmaker ? bookmakersArray.push(sportBookmakersData.secondaryBookmaker) : '';
13
- sportBookmakersData.tertiaryBookmaker ? bookmakersArray.push(sportBookmakersData.tertiaryBookmaker) : '';
14
-
15
- return bookmakersArray;
16
- }
17
- return backupLiveOddsProviders;
18
- };
19
-
20
- export const checkOddsFromBookmakers = (
21
- oddsMap: Map<string, any>,
22
- arrayOfBookmakers: string[],
23
- isTwoPositionalSport: boolean,
24
- maxImpliedPercentageDifference: number,
25
- minOddsForDiffChecking: number
26
- ) => {
27
- // Main bookmaker odds
28
- const firstBookmakerOdds = oddsMap.get(arrayOfBookmakers[0]);
29
-
30
- if (!firstBookmakerOdds) {
31
- // If no matching bookmakers are found, return zero odds
32
- return {
33
- homeOdds: 0,
34
- awayOdds: 0,
35
- drawOdds: 0,
36
- errorMessage: 'Returning zero odds cause no matching bookmakers have been found',
37
- };
38
- }
39
-
40
- const homeOdd = firstBookmakerOdds.homeOdds;
41
- const awayOdd = firstBookmakerOdds.awayOdds;
42
- const drawOdd = isTwoPositionalSport ? 0 : firstBookmakerOdds.drawOdds;
43
-
44
- // Check if any bookmaker has odds of 0 or 0.0001
45
- const hasZeroOrOne = arrayOfBookmakers.some((bookmakerId) => {
46
- const line = oddsMap.get(bookmakerId);
47
- if (line) {
48
- return (
49
- line.homeOdds === 0 ||
50
- line.awayOdds === 0 ||
51
- (!isTwoPositionalSport && line.drawOdds === 0) ||
52
- line.homeOdds === 1 ||
53
- line.awayOdds === 1 ||
54
- (!isTwoPositionalSport && line.drawOdds === 1)
55
- );
56
- }
57
- return false; // fix for es-lint
58
- });
59
-
60
- if (hasZeroOrOne) {
61
- // If any bookmaker has zero odds, return zero odds
62
- return {
63
- homeOdds: 0,
64
- awayOdds: 0,
65
- drawOdds: 0,
66
- errorMessage: `Returning zero odds cause bookmakers have 0 or 1 odds`,
67
- // TODO: Return sportsbook name with zero odds
68
- };
69
- }
70
-
71
- if (arrayOfBookmakers.length == 1) {
72
- return {
73
- homeOdds: homeOdd,
74
- awayOdds: awayOdd,
75
- drawOdds: isTwoPositionalSport ? 0 : drawOdd,
76
- };
77
- }
78
-
79
- // If none of the bookmakers have zero odds, check implied odds percentage difference
80
- const hasLargeImpliedPercentageDifference = arrayOfBookmakers.slice(1).some((bookmakerId) => {
81
- const line = oddsMap.get(bookmakerId);
82
- if (line) {
83
- const otherHomeOdd = line.homeOdds;
84
- const otherAwayOdd = line.awayOdds;
85
- const otherDrawOdd = line.drawOdds;
86
-
87
- const homeOddsImplied = oddslib.from('decimal', homeOdd).to('impliedProbability');
88
-
89
- const awayOddsImplied = oddslib.from('decimal', awayOdd).to('impliedProbability');
90
-
91
- // Calculate implied odds for the "draw" if it's not a two-positions sport
92
- const drawOddsImplied = isTwoPositionalSport
93
- ? 0
94
- : oddslib.from('decimal', drawOdd).to('impliedProbability');
95
-
96
- const otherHomeOddImplied = oddslib.from('decimal', otherHomeOdd).to('impliedProbability');
97
-
98
- const otherAwayOddImplied = oddslib.from('decimal', otherAwayOdd).to('impliedProbability');
99
-
100
- // Calculate implied odds for the "draw" if it's not a two-positions sport
101
- const otherDrawOddImplied = isTwoPositionalSport
102
- ? 0
103
- : oddslib.from('decimal', otherDrawOdd).to('impliedProbability');
104
-
105
- // Calculate the percentage difference for implied odds
106
- const homeOddsDifference = calculateImpliedOddsDifference(homeOddsImplied, otherHomeOddImplied);
107
-
108
- const awayOddsDifference = calculateImpliedOddsDifference(awayOddsImplied, otherAwayOddImplied);
109
-
110
- // Check implied odds difference for the "draw" only if it's not a two-positions sport
111
- const drawOddsDifference = isTwoPositionalSport
112
- ? 0
113
- : calculateImpliedOddsDifference(drawOddsImplied, otherDrawOddImplied);
114
-
115
- // Check if the percentage difference exceeds the threshold
116
- if (
117
- (homeOddsDifference > maxImpliedPercentageDifference &&
118
- homeOddsImplied > minOddsForDiffChecking &&
119
- otherHomeOddImplied > minOddsForDiffChecking) ||
120
- (awayOddsDifference > maxImpliedPercentageDifference &&
121
- awayOddsImplied > minOddsForDiffChecking &&
122
- otherAwayOddImplied > minOddsForDiffChecking) ||
123
- (!isTwoPositionalSport &&
124
- drawOddsDifference > maxImpliedPercentageDifference &&
125
- drawOddsImplied > minOddsForDiffChecking &&
126
- otherDrawOddImplied > minOddsForDiffChecking)
127
- ) {
128
- return true;
129
- }
130
- }
131
- return false; // fix for es-lint
132
- });
133
-
134
- if (hasLargeImpliedPercentageDifference) {
135
- return {
136
- homeOdds: 0,
137
- awayOdds: 0,
138
- drawOdds: 0,
139
- errorMessage: 'Returning zero odds due to percentage difference between bookmakers',
140
- };
141
- }
142
-
143
- return {
144
- homeOdds: homeOdd,
145
- awayOdds: awayOdd,
146
- drawOdds: isTwoPositionalSport ? 0 : drawOdd,
147
- };
148
- };
149
-
150
- export const calculateImpliedOddsDifference = (impliedOddsA: number, impliedOddsB: number): number => {
151
- const percentageDifference = (Math.abs(impliedOddsA - impliedOddsB) / impliedOddsA) * 100;
152
- return percentageDifference;
153
- };
1
+ import * as oddslib from 'oddslib';
2
+
3
+ export const getBookmakersArray = (bookmakersData, sportId, backupLiveOddsProviders) => {
4
+ const sportBookmakersData = bookmakersData.find((data) => Number(data.sportId) === Number(sportId));
5
+ if (sportBookmakersData) {
6
+ if (sportBookmakersData.primaryBookmaker == '') {
7
+ return backupLiveOddsProviders;
8
+ }
9
+ const bookmakersArray: string[] = [];
10
+
11
+ sportBookmakersData.primaryBookmaker ? bookmakersArray.push(sportBookmakersData.primaryBookmaker) : '';
12
+ sportBookmakersData.secondaryBookmaker ? bookmakersArray.push(sportBookmakersData.secondaryBookmaker) : '';
13
+ sportBookmakersData.tertiaryBookmaker ? bookmakersArray.push(sportBookmakersData.tertiaryBookmaker) : '';
14
+
15
+ return bookmakersArray;
16
+ }
17
+ return backupLiveOddsProviders;
18
+ };
19
+
20
+ export const checkOddsFromBookmakers = (
21
+ oddsMap: Map<string, any>,
22
+ arrayOfBookmakers: string[],
23
+ isTwoPositionalSport: boolean,
24
+ maxImpliedPercentageDifference: number,
25
+ minOddsForDiffChecking: number
26
+ ) => {
27
+ // Main bookmaker odds
28
+ const firstBookmakerOdds = oddsMap.get(arrayOfBookmakers[0]);
29
+
30
+ if (!firstBookmakerOdds) {
31
+ // If no matching bookmakers are found, return zero odds
32
+ return {
33
+ homeOdds: 0,
34
+ awayOdds: 0,
35
+ drawOdds: 0,
36
+ errorMessage: 'Returning zero odds cause no matching bookmakers have been found',
37
+ };
38
+ }
39
+
40
+ const homeOdd = firstBookmakerOdds.homeOdds;
41
+ const awayOdd = firstBookmakerOdds.awayOdds;
42
+ const drawOdd = isTwoPositionalSport ? 0 : firstBookmakerOdds.drawOdds;
43
+
44
+ // Check if any bookmaker has odds of 0 or 0.0001
45
+ const hasZeroOrOne = arrayOfBookmakers.some((bookmakerId) => {
46
+ const line = oddsMap.get(bookmakerId);
47
+ if (line) {
48
+ return (
49
+ line.homeOdds === 0 ||
50
+ line.awayOdds === 0 ||
51
+ (!isTwoPositionalSport && line.drawOdds === 0) ||
52
+ line.homeOdds === 1 ||
53
+ line.awayOdds === 1 ||
54
+ (!isTwoPositionalSport && line.drawOdds === 1)
55
+ );
56
+ }
57
+ return false; // fix for es-lint
58
+ });
59
+
60
+ if (hasZeroOrOne) {
61
+ // If any bookmaker has zero odds, return zero odds
62
+ return {
63
+ homeOdds: 0,
64
+ awayOdds: 0,
65
+ drawOdds: 0,
66
+ errorMessage: `Returning zero odds cause bookmakers have 0 or 1 odds`,
67
+ // TODO: Return sportsbook name with zero odds
68
+ };
69
+ }
70
+
71
+ if (arrayOfBookmakers.length == 1) {
72
+ return {
73
+ homeOdds: homeOdd,
74
+ awayOdds: awayOdd,
75
+ drawOdds: isTwoPositionalSport ? 0 : drawOdd,
76
+ };
77
+ }
78
+
79
+ // If none of the bookmakers have zero odds, check implied odds percentage difference
80
+ const hasLargeImpliedPercentageDifference = arrayOfBookmakers.slice(1).some((bookmakerId) => {
81
+ const line = oddsMap.get(bookmakerId);
82
+ if (line) {
83
+ const otherHomeOdd = line.homeOdds;
84
+ const otherAwayOdd = line.awayOdds;
85
+ const otherDrawOdd = line.drawOdds;
86
+
87
+ const homeOddsImplied = oddslib.from('decimal', homeOdd).to('impliedProbability');
88
+
89
+ const awayOddsImplied = oddslib.from('decimal', awayOdd).to('impliedProbability');
90
+
91
+ // Calculate implied odds for the "draw" if it's not a two-positions sport
92
+ const drawOddsImplied = isTwoPositionalSport
93
+ ? 0
94
+ : oddslib.from('decimal', drawOdd).to('impliedProbability');
95
+
96
+ const otherHomeOddImplied = oddslib.from('decimal', otherHomeOdd).to('impliedProbability');
97
+
98
+ const otherAwayOddImplied = oddslib.from('decimal', otherAwayOdd).to('impliedProbability');
99
+
100
+ // Calculate implied odds for the "draw" if it's not a two-positions sport
101
+ const otherDrawOddImplied = isTwoPositionalSport
102
+ ? 0
103
+ : oddslib.from('decimal', otherDrawOdd).to('impliedProbability');
104
+
105
+ // Calculate the percentage difference for implied odds
106
+ const homeOddsDifference = calculateImpliedOddsDifference(homeOddsImplied, otherHomeOddImplied);
107
+
108
+ const awayOddsDifference = calculateImpliedOddsDifference(awayOddsImplied, otherAwayOddImplied);
109
+
110
+ // Check implied odds difference for the "draw" only if it's not a two-positions sport
111
+ const drawOddsDifference = isTwoPositionalSport
112
+ ? 0
113
+ : calculateImpliedOddsDifference(drawOddsImplied, otherDrawOddImplied);
114
+
115
+ // Check if the percentage difference exceeds the threshold
116
+ if (
117
+ (homeOddsDifference > maxImpliedPercentageDifference &&
118
+ homeOddsImplied > minOddsForDiffChecking &&
119
+ otherHomeOddImplied > minOddsForDiffChecking) ||
120
+ (awayOddsDifference > maxImpliedPercentageDifference &&
121
+ awayOddsImplied > minOddsForDiffChecking &&
122
+ otherAwayOddImplied > minOddsForDiffChecking) ||
123
+ (!isTwoPositionalSport &&
124
+ drawOddsDifference > maxImpliedPercentageDifference &&
125
+ drawOddsImplied > minOddsForDiffChecking &&
126
+ otherDrawOddImplied > minOddsForDiffChecking)
127
+ ) {
128
+ return true;
129
+ }
130
+ }
131
+ return false; // fix for es-lint
132
+ });
133
+
134
+ if (hasLargeImpliedPercentageDifference) {
135
+ return {
136
+ homeOdds: 0,
137
+ awayOdds: 0,
138
+ drawOdds: 0,
139
+ errorMessage: 'Returning zero odds due to percentage difference between bookmakers',
140
+ };
141
+ }
142
+
143
+ return {
144
+ homeOdds: homeOdd,
145
+ awayOdds: awayOdd,
146
+ drawOdds: isTwoPositionalSport ? 0 : drawOdd,
147
+ };
148
+ };
149
+
150
+ export const calculateImpliedOddsDifference = (impliedOddsA: number, impliedOddsB: number): number => {
151
+ const percentageDifference = (Math.abs(impliedOddsA - impliedOddsB) / impliedOddsA) * 100;
152
+ return percentageDifference;
153
+ };