-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
90 lines (82 loc) · 2.49 KB
/
app.js
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
var request = require('request');
var creds = require('./credentials.json');
var currencies = ["USD", "EUR", "JPY", "GBP", "AUD", "CHF", "CAD", "MXN", "CNY"];
var getConversionArray = function(currencies) {
var arr = [];
for (var i = 0; i < currencies.length; i++) {
for (var j = 0; j < currencies.length; j++) {
if (i != j) {
arr.push(currencies[i] + currencies[j]);
}
}
}
return arr;
}
var sanitizeConversionArray = function(arr) {
var str = JSON.stringify(arr);
str = str.substring(1, str.length - 1);
str = "(" + str + ")";
return str;
}
var arbitrage = function(data) {
for (var i = 0; i < data.length; i++) {
var mySymbol = data[i].Name.substring(0, 3);
var myTarget = data[i].Name.substring(4, 7);
for (var j = 0; j < data.length; j++) {
var leg1Symbol = data[j].Name.substring(0, 3);
var leg1Target = data[j].Name.substring(4, 7);
if (leg1Symbol == myTarget && leg1Target != mySymbol) {
for (var k = 0; k < data.length; k++) {
if (data[k].Name === leg1Target + "_" + mySymbol) {
var res = data[i].Rate * data[j].Rate * data[k].Rate;
if (res > 1)
console.log(res + ": " + data[i].Name + " " + data[i].Rate + " -> "
+ data[j].Name + " " + data[j].Rate + " -> " + data[k].Name + " " + data[k].Rate);
}
}
}
}
}
}
function buildHeader(myURL) {
return {
url: myURL,
headers: {
'Authorization': 'Bearer ' + creds.API_Key
}
};
}
function priceCallback(error, response, body) {
if (!error && response.statusCode == 200) {
var ultimate = [];
var obj = JSON.parse(body).prices;
for (var i = 0; i < obj.length; i++) {
if (obj[i].bids) {
var symb = obj[i].instrument;
ultimate.push({
Name: symb,
Rate: obj[i].bids[0].price,
Order: "sell " + symb
});
ultimate.push({
Name: symb.substring(4, 7) + "_" + symb.substring(0, 3),
Rate: 1 / (obj[i].asks[0].price),
Order: "buy " + symb
});
}
}
arbitrage(ultimate);
}
}
function pairCallback(error, response, body) {
if (!error && response.statusCode == 200) {
var obj = JSON.parse(body).instruments;
var pairs = "";
for (var i = 0; i < obj.length; i++) {
pairs = pairs + "," + obj[i].name;
}
pairs = pairs.substring(0, pairs.length - 1);
request(buildHeader("https://api-" + creds.env + ".oanda.com/v3/accounts/" + creds.accountID + "/pricing?instruments=" + pairs), priceCallback);
}
}
request(buildHeader("https://api-" + creds.env + ".oanda.com/v3/accounts/" + creds.accountID + "/instruments"), pairCallback);