-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (131 loc) · 4.58 KB
/
index.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
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
let https = require('https');
var CronJob = require('cron').CronJob;
let PushBullet = require('pushbullet');
let pusher = new PushBullet(''); // API key of the account
let channelTagObject = {channel_tag: 'arbitrage'} // exchange for own tag, or simply use device ID as string (not inside an object)
var calc = {
priceHigh: null,
priceLow: null,
priceTemp0: null,
priceTemp1: null,
profit: null,
profitPercentage: null,
withdrawFeeCrypto: 0.001,
withdrawFeeFiat: 0.15,
tradeVolumeFiat: 500,
exchangeFeePercentage: 0.26,
tradePair: null
}
var job = new CronJob('00 */5 8-24 * * *', async() => {
calculateProfits()
}, () => {
},
false
);
calculateProfits()
job.start()
console.log('Started Cronjob!')
async function calculateProfits() {
try {
var msg = ''
let profitThreshold = 10
let enabledPairs = ['LTCEUR', 'ETHEUR']
for (let pair of enabledPairs) {
var error = await getPrices(pair)
if(error) {
console.log(getDateString() + JSON.stringify(error))
return
}
calculate()
if(calc.profit > profitThreshold) msg += constructMessage(calc) + '\n'
}
if(!isEmpty(msg)) {
msg = msg.trim()
sendPush(msg)
console.log(getDateString() + 'Message sent: ' + msg)
} else {
console.log(getDateString() + 'No currency arbitrage would have given a profit of at least ' + formatEur(profitThreshold))
}
} catch(err) {
console.error(err)
}
}
function sendPush(message) {
pusher.note(channelTagObject, 'Arbitrage', message, function(error, response) {
if(error) {
console.log(getDateString() + JSON.stringify(error))
}
});
}
function getDateString () {
let d = new Date();
return datestring = ("0" + d.getDate()).slice(-2) + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" +
d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2) + ' ';
}
function constructMessage (calc) {
return 'Gewinn ' + calc.tradePair + ': ' + formatEur(calc.profit) + ' — ' + formatPercentage(calc.profitPercentage) + " Kauf " + (gdaxPricierThanKraken() ? "Kraken" : "GDAX")
}
function calculate () {
if (calc.priceTemp0 > calc.priceTemp1) {
calc.priceHigh = calc.priceTemp0
calc.priceLow = calc.priceTemp1
} else {
calc.priceHigh = calc.priceTemp1
calc.priceLow = calc.priceTemp0
}
var amountCrypto = calc.tradeVolumeFiat / calc.priceLow
amountCrypto = amountCrypto * (100 - calc.exchangeFeePercentage) / 100
amountCrypto = amountCrypto - calc.withdrawFeeCrypto
let fiatAmount = amountCrypto * calc.priceHigh
calc.profit = fiatAmount - calc.tradeVolumeFiat - calc.withdrawFeeFiat
calc.profitPercentage = calc.profit / calc.tradeVolumeFiat * 100
}
function gdaxPricierThanKraken () {
return calc.priceTemp0 > calc.priceTemp1
}
function getPrices (tradePair) {
return new Promise(resolve => {
const corsPrefix = 'https://proxy-arbitrage.chagemann.de/'
https.get(corsPrefix + 'markets/gdax/' + tradePair + '/price', (res1) => {
let body1 = [];
let body2 = [];
res1.on('data', (chunk1) => {
body1.push(chunk1)
}).on('end', () => {
body1 = Buffer.concat(body1).toString();
try {
calc.priceTemp0 = JSON.parse(body1).result.price
} catch (e) {
resolve(e)
}
https.get(corsPrefix + 'markets/kraken/' + tradePair + '/price', (res2) => {
res2.on('data', (chunk2) => {
body2.push(chunk2)
}).on('end', () => {
body2 = Buffer.concat(body2).toString();
try {
calc.priceTemp1 = JSON.parse(body2).result.price
} catch (e) {
resolve(e)
}
calc.tradePair = tradePair
resolve(null)
});
})
}).on('error', (e) => {
resolve(e)
});
}).on('error', (e) => {
resolve(e)
});
})
}
function formatEur (number) {
return number.toFixed(2) + '€'
}
function formatPercentage (number) {
return number.toFixed(2) + '%'
}
function isEmpty(str) {
return (!str || 0 === str.length);
}