-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1x2.arber.ts
269 lines (246 loc) · 6.45 KB
/
1x2.arber.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import {
biggestArb,
intersect3,
matchesToArbs,
postulateArb,
} from '@algorithms';
import {
ArbGroup,
BookieBet,
BookieEvents,
BookieName,
BookieRetrieverTuple,
} from '@models';
import { Money } from '@money/types';
import { arbLogger } from '@utils';
import { combineLatest, from, interval, Observable } from 'rxjs';
import { exhaustMap, filter, map, tap } from 'rxjs/operators';
import { EventAccomulatorArber } from '../base/accomulator.arber';
export class _1X2Arber extends EventAccomulatorArber {
// Banned matches in current run
protected banned: {
bookie: BookieName;
bet: string;
}[] = [
// {
// bookie: BookieName.Caliente,
// bet: 'Daria',
// },
];
// Monitor function
private monitorFn: () => Observable<BookieEvents>[];
// Intersect mode (3 way)
private intersectFn = intersect3;
constructor(retrievers: BookieRetrieverTuple[], investment: Money) {
super(retrievers, investment, 5);
}
/**
* Main function.
* Fires arbing
*/
public async start() {
// Start arb proccess
if (this.retrievers.every(({ bookie }) => bookie.authenticated)) {
this.place().subscribe();
} else {
console.log('Arber', this.id);
console.log('Some bookies are not authenticated');
console.log(
'Will only reach selection chain (excluding postulation and placing)',
);
this.selection(true).subscribe();
}
}
/**
* Initializes variables needed before start arbing proccess.
*
* Monitor function generation
*/
private initialize() {
// Initialize monitor fn
this.monitorFn = () =>
this.retrievers.map((r) => {
return from(r.retriever()).pipe(
map((events) => {
return { bookie: r.bookie, events };
}),
);
});
}
/**
* 1st Step. Data creation chain
*
* Starts monitoring bookies. Creates BookieEvents
* @param each Ticking interval in ms
* @returns
*/
public monitor(each = 250) {
// Monitor is always the first step. So initialize here
this.initialize();
if (!this.monitorFn) {
throw 'Monitor function needs to be defined';
}
// Will emit events tuples each ms interval
const monitor = interval(each).pipe(
filter(() => !this.blocked),
exhaustMap(() => combineLatest(this.monitorFn())),
filter(() => !this.blocked),
);
return monitor;
}
/**
* 2nd Step. Arb lookup chain
*
* Quickly intersects the events
* and looks for the best arb opportunity
*/
private selection(log = false) {
// Intersect bets to create possible matches
const selection = this.monitor().pipe(
map((bookieEvents) => {
// Intersect and filter banned matches
const groups = this.intersectFn(bookieEvents).filter((group) =>
this.hasBannedMatch(group),
);
// Create arb opportunities
const arbs = matchesToArbs(
groups,
{ ...this.investment },
{
roundTo: 10,
margin: {
min: 1,
max: 5,
sharpMax: 5,
},
},
);
return arbs;
}),
tap((arbs) => this.accomulate(arbs)),
map((arbs) => arbs.filter((arb) => this.isConstantArb(arb))),
filter((arbs) => !!arbs.length),
map((arbs) => biggestArb(arbs)),
filter(() => !this.blocked),
tap((arb) => {
// Before postulation event
// Notify postulating
this.notifyPostulating(arb.map(({ bookie }) => bookie));
// If logging indicated
log && arbLogger(arb);
}),
);
return selection;
}
/**
* 3rd Step. Postulation chain
*
* Postulates given arb opportunity
*/
private postulate() {
const postulate = this.doPostulate(this.selection()).pipe(
// After postulation event
tap(async (results) => {
const anyInvalid = results.some((result) => !result.postulation.valid);
if (anyInvalid) {
// If any of the postulation is invalid, clean both betslips
await Promise.all(results.map((r) => r.arb.bet.clean()));
this.resetStatus();
this.resume();
}
}),
filter((results) => results.every((result) => result.postulation.valid)),
tap((results) => {
// Before place event
// Notify placing
this.notifyPlacing(results.map((r) => r.arb.bookie));
}),
);
return postulate;
}
/**
* 4th Step. Placing chain
*
* Places given postulations
* @returns
*/
private place() {
const place = this.postulate().pipe(
exhaustMap((results) => {
return Promise.all(
results.map(({ arb }) =>
arb.bet.place().then((placed) => {
return { placed, arb };
}),
),
);
}),
);
return place;
}
/**
* Tries to postulate the arb match
* @param selection
* @returns
*/
private doPostulate(selection: Observable<ArbGroup>) {
return selection.pipe(
filter(() => !this.blocked),
tap(() => this.pause()),
tap((best) => arbLogger(best)),
tap(() => (this.accomulator = [])),
exhaustMap((arbGroup) =>
postulateArb(arbGroup).pipe(
// map(postulations => {
// if (postulations.some(postulation => 'maxStake' in postulation)) {
// // Needs to recalculate
// }
// }),
map((postulationResults) => {
return postulationResults.map((result, index) => {
return {
postulation: result,
arb: arbGroup[index],
};
});
}),
),
),
// Log postulation results
tap((result) => console.log(result.map((r) => r.postulation))),
);
}
/**
* Checks if any bookie bet is banned atm
* @param group
* @returns
*/
private hasBannedMatch(group: [BookieBet, BookieBet, BookieBet]) {
const hasBannedMatch = group.some((bb) => {
return this.banned.includes({
bookie: bb.bookie.name,
bet: bb.bet.title,
});
});
return !hasBannedMatch;
}
/**
* Bans matches from current run
* @param toBan
*/
private banMatches(
toBan: {
bet: string;
bookie: BookieName;
}[],
) {
this.banned.push(...toBan);
}
/**
* When terminating
* Close all the bookie instances
*/
protected async onClose() {
console.log('Banned matches:', this.banned);
}
}