-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
65 lines (53 loc) · 2.23 KB
/
graph.js
File metadata and controls
65 lines (53 loc) · 2.23 KB
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
class ArbitrageDetector {
constructor(market) {
this.market = market;
this.cycles = [];
this.profitableCycles = 0;
this.totalCycles = 0;
this.detectedPaths = new Set();
}
detectArbitrage() {
this.cycles = [];
const currencies = this.market.currencies;
const newDetectedPaths = new Set();
for (let start of currencies) {
for (let mid1 of currencies) {
if (mid1 === start) continue;
for (let mid2 of currencies) {
if (mid2 === start || mid2 === mid1) continue;
const rate1 = this.market.getRate(start, mid1);
const rate2 = this.market.getRate(mid1, mid2);
const rate3 = this.market.getRate(mid2, start);
if (rate1 === 0 || rate2 === 0 || rate3 === 0) continue;
const product = rate1 * rate2 * rate3;
const threshold = this.market.includeFees ? 1.0001 : 1.0001;
const spread = (product - 1) * 100;
const pathKey = `${start}-${mid1}-${mid2}`;
if (product > threshold) {
this.cycles.push({
path: [start, mid1, mid2, start],
product: product,
spread: spread,
profitable: true,
rates: [rate1, rate2, rate3]
});
if (!this.detectedPaths.has(pathKey)) {
this.profitableCycles++;
newDetectedPaths.add(pathKey);
}
}
this.totalCycles++;
}
}
}
this.detectedPaths = newDetectedPaths;
this.cycles.sort((a, b) => b.spread - a.spread);
return this.cycles.slice(0, 5);
}
getStats() {
return {
total: this.totalCycles,
profitable: this.profitableCycles
};
}
}