-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmarket.js
194 lines (174 loc) · 6.48 KB
/
market.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const action = require('./action');
const log = require('./logging');
const config = require('config');
const loader = require('./loader');
const { filter, round } = require('./utils');
const { reportMarkets } = require('./report');
async function loadMarket(exchange, reload) {
return new Promise(async (resolve, reject) => {
try {
const markets = await exchange.loadMarkets(reload);
const filtered = filterMarkets(markets);
if (filtered.length > 0) {
filtered.sort(compareMarkets);
log.debug(exchange.id, 'loaded');
resolve({
id: exchange.id,
markets: filtered
});
} else {
log.debug(exchange.id, 'empty');
resolve();
}
}
catch (e) {
let msg = e.toString();
log.warn(exchange.id, 'loadMarket failed', msg.indexOf('\n') > 0 ? msg.substring(0, msg.indexOf('\n')) : msg);
reject(e);
}
});
}
exports.loadMarket = loadMarket;
function filterMarkets(markets) {
const filtered = [];
for (const value of Object.values(markets)) {
if (includeMarket(value)) {
filtered.push(value);
}
}
return filtered;
}
function includeMarket(market) {
return market.active && !market.darkpool
&& filter(market.type ? market.type.toLowerCase() : market.type, config.markets)
&& filter(market.base.toLowerCase(), config.bases)
&& filter(market.quote.toLowerCase(), config.quotes)
&& !(market.info && market.info.prohibitedIn && market.info.prohibitedIn.includes('US'));
}
function compareMarkets(a, b) {
return a.symbol.localeCompare(b.symbol);
}
function findCommonMarkets(markets) {
const found = new Map();
//find common markets
for (const ex of markets) {
for( const {symbol} of ex.markets) {
if(found.has(symbol)) {
found.set(symbol, found.get(symbol)+1);
} else {
found.set(symbol, 1);
}
}
}
//remove unique markets
for (const ex of markets) {
for(let i=0; i < ex.markets.length; i++) {
if(found.get(ex.markets[i].symbol) === 1) {
ex.markets.splice(i, 1);
}
}
}
return markets;
}
exports.getGaps = async function (markets) {
log.debug('getting gaps');
const jobs = [];
markets = findCommonMarkets(markets);
for (const m of markets) {
jobs.push(fetchMarketPrices(m));
}
let loaded = [];
await Promise.allSettled(jobs).then((results) => {
for (const result of results) {
if (result.status === 'fulfilled' && result.value) {
loaded = loaded.concat(result.value);
}
}
});
reportMarkets(loaded);
const filtered = filterGaps(loaded);
log.debug(filtered);
return filtered;
};
async function fetchMarketPrices(marketSet) {
return new Promise(async (resolve, reject) => {
let prices = [];
const exchange = loader.getExchange(marketSet.id);
for (const m of marketSet.markets) {
try {
let orderbook = await exchange.fetchOrderBook(m.symbol);
let bid = orderbook && orderbook.bids && orderbook.bids.length ? orderbook.bids[0][0] : undefined;
let ask = orderbook && orderbook.asks && orderbook.asks.length ? orderbook.asks[0][0] : undefined;
if (bid && ask) {
log.debug(exchange.id, m.symbol, 'loaded market price');
prices.push({ time: Date.now(), exchange: exchange.id, symbol: m.symbol, bid, ask, maker: m.maker, taker: m.taker, percentage: (m.percentage || m.percentage === undefined) });
}
}
catch (e) {
log.error(e, marketSet.id, m.symbol, 'fetchMarketPrices failed');
}
}
resolve(prices);
});
}
function filterGaps(data) {
let prices = new Map();
for (const item of data) {
let symbol = item.symbol;
let watched = action.getWatched(symbol);
let shortable = !config.get('exchanges').shorts || config.get('exchanges').shorts.includes(item.exchange);
let values = prices.has(symbol) ? prices.get(symbol) : { date: new Date(), symbol };
// determine high and low bids for optimal gap unless already watching a combo
if (!watched && (!values.low || item.bid < values.low.bid) || watched && watched.low.exchange === item.exchange) {
values.low = item;
}
if (!watched && (!values.high || item.bid > values.high.bid) || watched && watched.high.exchange === item.exchange) {
values.high = item;
}
if (!watched && shortable && (!values.short || item.bid > values.short.bid) || watched && watched.short.exchange === item.exchange) {
values.short = item;
}
prices.set(symbol, values);
}
let results = [];
for (const item of prices.values()) {
item.gap = {};
item.gapPercent = {};
let watching = action.watching(item.symbol);
if (item.low && item.high && (watching || item.low.exchange !== item.high.exchange && item.high.bid)) {
item.gap.best = getGap(item.high, item.low);
if (item.short) item.gap.short = getGap(item.short, item.low);
// gap percent factors in buying and selling fees to get more accurate profit percent
item.gapPercent.best = getGapPercent(item.gap.best, item.high, item.low);
if (item.short) item.gapPercent.short = getGapPercent(item.gap.short, item.short, item.low);
if (watching || item.gapPercent.best > 0 || item.gapPercent.short > 0) {
results.push(item);
}
}
}
results.sort(comparePrices);
return results;
}
function getGap(high, low) {
let gap = high.bid - low.bid;
if (!high.percentage) {
gap -= Math.max(high.maker, high.taker) * 2;
}
if (!low.percentage) {
gap -= Math.max(low.maker, low.taker) * 2;
}
return round(gap, 8)
}
function getGapPercent(gap, high, low) {
let percent = gap / high.bid;
if (high.percentage) {
percent -= Math.max(high.maker, high.taker) * 2;
}
if (low.percentage) {
percent -= Math.max(low.maker, low.taker) * 2;
}
return round(percent * 100.0, 8)
}
function comparePrices(a, b) {
return b.gapPercent.best - a.gapPercent.best;
}