-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (64 loc) · 2.36 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
const ccxt = require('ccxt/ccxt.js')
const chalk = require('chalk')
const exchangeNames = [
'kraken',
'cex',
'bitfinex'
]
const exchangeInstances = {}
const symbol = 'ETH/USD'
const sleep = (ms) => new Promise (resolve => setTimeout (resolve, ms))
function printTicker (name, ticker) {
console.log(chalk.green(symbol), 'ticker',
ticker['datetime'],
'bid: ' + chalk.green(ticker['bid']),
'ask: ' + chalk.green(ticker['ask']),
'volume: ' + ticker['quoteVolume'],
chalk.green(name)
)
}
exchangeNames.forEach((name) => {
exchangeInstances[name] = {
name: name,
instance: new ccxt[name](),
tickers: {}
}
})
const fetchTicker = async (name) => {
const exchange = exchangeInstances[name].instance
const ticker = await exchange.fetchTicker(symbol)
exchangeInstances[name].tickers[symbol] = ticker
// await sleep(exchange.rateLimit)
printTicker(name, ticker)
return ticker
}
function fetchTickers() {
return Promise.all(exchangeNames.map(value => fetchTicker(value)))
}
fetchTickers().then(() => {
for (let i=0; i < exchangeNames.length; i++) {
const referenceName = exchangeNames[i]
const referenceExchange = exchangeInstances[referenceName].tickers[symbol]
for (let j = i+1; j < exchangeNames.length; j++) {
const comparisonName = exchangeNames[j]
const comparisonExchange = exchangeInstances[comparisonName].tickers[symbol]
if (referenceExchange.ask < comparisonExchange.bid) {
console.log(chalk.green('arbitrage opportunity: '), symbol)
console.log(referenceName, ' BUY ', referenceExchange.ask)
console.log(comparisonName, ' SELL ', comparisonExchange.bid)
console.log('gain $: ', comparisonExchange.bid - referenceExchange.ask)
console.log('gain %: ', comparisonExchange.bid / referenceExchange.ask - 1)
console.log('------')
}
else if (comparisonExchange.ask < referenceExchange.bid) {
console.log(chalk.green('arbitrage opportunity: '), symbol)
console.log(comparisonName, ' BUY ', comparisonExchange.ask)
console.log(referenceName, ' SELL ', referenceExchange.bid)
console.log('gain $: ', referenceExchange.bid - comparisonExchange.ask)
console.log('gain %: ', referenceExchange.bid / comparisonExchange.ask - 1)
console.log('------')
}
}
}
console.log('end')
})