-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
161 lines (122 loc) · 6.34 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
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
const balance = require('./markets/Balance');
const exchanges = [require('./markets/BfxTrade'), require('./markets/PoloTrade')];
const bfxPairs = ['ETHBTC', 'XRPBTC', 'LTCBTC', 'XMRBTC', 'EOSBTC', 'BCHBTC', 'NEOBTC', 'OMGBTC', 'DSHBTC', 'ETCBTC'];
const poloPairs = ['BTC_ETH', 'BTC_XRP', 'BTC_LTC', 'BTC_XMR', 'BTC_EOS', 'BTC_BCH', 'BTC_NEO', 'BTC_OMG', 'BTC_DSH', 'BTC_ETC'];
var bids = [], asks = [];
var maxBid = 0, minAsk = 0;
var bidExchange = -1, askExchange = -1;
var minOrderLimits = {};
exchanges[0].initPairs(bfxPairs);
exchanges[1].initPairs(poloPairs);
exchanges[0].getMinOrderSize((data) => {
minOrderLimits = data;
console.log('min order Limits =', minOrderLimits);
});
for (exchange of exchanges) {
exchange.getOrders()
}
var self = this
// NOTE - update at start and wait 60 secs as takes time to retrieve all currency prices
// update again after trade to minimise latency
// do not use same account for manual trades when bot is live!
balance.updateBalance(() => {
console.log('Waiting 60 secs for all data to accrue');
setTimeout(() => {
console.log('started');
setInterval(function(){
compare(self)
}, 50)
// console.log('bitfinex', exchanges[0].orderbook);
// console.log('poloniex', exchanges[1].orderbook);
}, 6000);
})
// data analyzer
function compare(self){
for (bfxKey in exchanges[0].orderbook) {
for (poloKey in exchanges[1].orderbook) {
if(checkKeysMatch(bfxKey, poloKey)) {
bids.push(exchanges[0].orderbook[bfxKey]['bids']['price'])
bids.push(exchanges[1].orderbook[poloKey]['bids']['price'])
asks.push(exchanges[0].orderbook[bfxKey]['asks']['price'])
asks.push(exchanges[1].orderbook[poloKey]['asks']['price'])
// console.log('Pre Math calc - pairname', bfxKey, poloKey, 'bids', bids, 'asks', asks);
maxBid = Math.max.apply(Math, bids)
minAsk = Math.min.apply(Math, asks)
// console.log('pairname', bfxKey, poloKey, 'bids', maxBid, 'asks', minAsk);
// combined fees 0.4% so we check for 0.5% profit to ensure we don't lose money
// if(maxBid>0 && minAsk>0){ // use this during testing just to see what happens
if( maxBid > 0 && minAsk > 0 && maxBid > (minAsk * 1.000001 )){ // test value
// if(maxBid>0 && minAsk>0 && maxBid>minAsk*1.005){
bidExchange = bids.indexOf(maxBid)
askExchange = asks.indexOf(minAsk)
// console.log("bidExchange", bidExchange, askExchange);
// check bid and ask are on different exchanges
if (bidExchange !== askExchange) {
console.log(bidExchange, askExchange);
exchanges[askExchange].mode = 'buy'
exchanges[bidExchange].mode = 'sell'
var buyPair = bidExchange == 0 ? poloKey : bfxKey ;
var sellPair = bidExchange == 0 ? bfxKey : poloKey ;
var buyAccountBalance = balance.getBalance(askExchange, buyPair, exchanges[askExchange].mode)
var sellAccountBalance = balance.getBalance(bidExchange, sellPair, exchanges[bidExchange].mode)
var maxTradeableAmount = Math.min( exchanges[bidExchange].orderbook[sellPair]['bids']['amount'],
exchanges[askExchange].orderbook[buyPair]['asks']['amount'] )
var availableAccountBalance = Math.min(sellAccountBalance, buyAccountBalance / minAsk)
console.log("buyAccountBalance", buyAccountBalance, "sellAccountBalance", sellAccountBalance);
console.log('max bid', maxBid);
console.log('min ask', minAsk);
console.log('trade amount', maxTradeableAmount);
console.log('limit Balance', availableAccountBalance);
if(maxTradeableAmount > availableAccountBalance) {
maxTradeableAmount = availableAccountBalance
}
var pairLimit = 0;
for( key of Object.keys(minOrderLimits) ) {
if ( standardiseKey(buyPair) == key ) {
pairLimit = minOrderLimits[key]
console.log('New pairLimit =', pairLimit);
}
}
if (maxTradeableAmount < pairLimit) {
console.log("insufficent funds to trade, ", buyPair);
console.log("bid exchange, ", sellAccountBalance);
console.log("ask exchange, ", buyAccountBalance);
continue;
}
if (sellAccountBalance >= maxTradeableAmount && buyAccountBalance >= maxTradeableAmount.min) {
console.log('pairname', bfxKey, ' sell on', bidExchange, ' price', maxBid, ' buy on', askExchange, ' price', minAsk);
// complete callback function to create order of ledgers.
// block bot operations until callback success recieved
// return bot to normal function once complete
// exchanges[bidExchange].trade(sellPair, maxBid, maxTradeableAmount, () => {
// })
// exchanges[askExchange].trade(buyPair, minAsk, maxTradeableAmount, () => {
// })
}
}
}
bids.length = 0
asks.length = 0
maxBid = 0
minAsk = 0
}
}
}
}
function standardiseKey (key) {
if(key.includes('_')){
console.log('key1 has an _ whoop');
let convertedkey = key.split('_');
return convertedkey.reverse().join('');
} else {
return key;
}
}
function checkKeysMatch (key1, key2) {
return key1.includes(key2.split('_')[1]) ? true : false ;
}
// Make curency pairs same format
function convertPairName(pair){
// if it contains '_'
//do stuff
}