forked from niwacode/quickfingersscanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
345 lines (303 loc) · 11.6 KB
/
index.ts
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { Slack } from './slack';
import { Binance } from './exchanges/binance.exchange';
import { Poloniex } from './exchanges/poloniex.exchange';
import { Bittrex } from './exchanges/bittrex.exchange';
import { HitBtc } from './exchanges/hitbtc.exchange';
import { Ticker } from './ticker';
const vorpal = require('vorpal');
import {Exchange} from './exchange';
import {CurrencyPair} from './currencypair';
var fs = require('fs');
var hitbtc = new HitBtc();
var bittrex = new Bittrex();
var poloniex = new Poloniex();
var binance = new Binance();
var slackkey = "";
// Check if a file exists
var FileExists = (filePath: string) : boolean => {
try{
fs.statSync(filePath);
}catch(err){
if(err.code == 'ENOENT') return false;
}
return true;
}
// Print current settings
var PrintSettings = () => {
console.log(`
Ignorehours: ${CurrencyPair.ignorehours}
Mindrop: ${CurrencyPair.mindrop}
Minhours: ${CurrencyPair.minhours}
Maxbottomdist: ${CurrencyPair.maxbottomdistance}
Maxskipcandles: ${CurrencyPair.maxskipcandles}
Ignorenewer: ${CurrencyPair.ignoreNewer}
UseSlack: ${CurrencyPair.useSlack}
SlackKey: ${slackkey}`);
}
// Read settings for this currency pair. The settings are the slack thread id and whether to ignore the currency pair (in case of delisting etc)
var ReadSettings = () => {
let settingsFile = './data/scanner.settings.json';
if(FileExists(settingsFile)) {
let settings = JSON.parse(fs.readFileSync(settingsFile, 'utf8'));
if(settings.ignoreHours)
CurrencyPair.ignorehours = settings.ignoreHours;
if(settings.minDrop)
CurrencyPair.mindrop = settings.minDrop;
if(settings.minHours)
CurrencyPair.minhours = settings.minHours;
if(settings.maxBottomDist)
CurrencyPair.maxbottomdistance = settings.maxBottomDist;
if(settings.maxSkipCandles)
CurrencyPair.maxskipcandles = settings.maxSkipCandles;
if(settings.ignoreNewer)
CurrencyPair.ignoreNewer = settings.ignoreNewer;
if(settings.useSlack)
CurrencyPair.useSlack = settings.useSlack;
if(settings.slackKey)
slackkey = settings.slackKey;
}
}
// Write settings to file
var WriteSettings = () => {
let settingsFile = './data/scanner.settings.json';
let settings = {
ignoreHours: CurrencyPair.ignorehours,
minDrop: CurrencyPair.mindrop,
minHours: CurrencyPair.minhours,
maxBottomDist: CurrencyPair.maxbottomdistance,
maxSkipCandles: CurrencyPair.maxskipcandles,
ignoreNewer: CurrencyPair.ignoreNewer,
useSlack: CurrencyPair.useSlack,
slackKey: slackkey
};
fs.writeFileSync(settingsFile, JSON.stringify(settings), 'utf8');
}
ReadSettings();
if(slackkey && slackkey.length > 0)
Slack.setKey(slackkey);
// Download history for one exchange at a time to avoid high load on Coinigy servers
// Start subscribing to HitBTC ticker, 1000ms refresh rate
//hitbtc.TickerTimer(1000);
// Get all currency pairs on HitBTC and download history
//hitbtc.GetCurrencyPairs(() => {
// console.log("HitBTC history downloaded");
// Start subscribing to BitTrex ticker
bittrex.TickerTimer(1000);
// Get all currency pairs on BitTrex and download history
bittrex.GetCurrencyPairs(() => {
console.log("BitTrex history downloaded");
// Start subscribing to Poloniex ticker
poloniex.TickerTimer(1000);
// Get all currenct pairs on Poloniex and download history
poloniex.GetCurrencyPairs(() => {
console.log("Poloniex history downloaded");
// Start subscribing to Binance ticker
binance.TickerTimer(1000);
binance.GetCurrencyPairs(() => {
console.log("Binance history downloaded");
});
});
});
//});
// Set up vorpal CLI
var cli = vorpal().delimiter('crypto-cli~$');
cli.command('ignorehours <hours>').description('Ignore last <hours> hours when looking for a drop.').action((a,cb) => {
CurrencyPair.ignorehours = a.hours;
console.log(`Ignoring last ${a.hours} hours\n`);
PrintSettings();
cb();
});
cli.command('mindrop <pc>').description('Minimum drop below last low.').action((a,cb) => {
CurrencyPair.mindrop = a.pc;
console.log(`Minimum drop set to ${a.pc}%\n`);
PrintSettings();
cb();
});
cli.command('minhours <hours>').description('Minimum time since last low.').action((a, cb) => {
CurrencyPair.minhours = a.hours;
console.log(`Minimum time set to ${a.hours} hours\n`);
PrintSettings();
cb();
});
cli.command('maxbottomdist <pc>').description('Maximum distance below previous low.').action((a, cb) => {
CurrencyPair.maxbottomdistance = a.pc;
console.log(`Maximum bottom distance set to ${a.pc}%\n`);
PrintSettings();
cb();
});
cli.command('maxskipcandles <candles>').description('Maximum number of higher candles to skip when tracking bottom.').action((a, cb) => {
CurrencyPair.maxskipcandles = a.candles;
console.log(`Maximum skip candles set to ${a.candles}\n`);
PrintSettings();
cb();
});
cli.command('ignorenewer <hours>').description('Number of hours to ignore newer bases after finding an older base.').action((a, cb) => {
CurrencyPair.ignoreNewer = a.hours;
console.log(`Number of hours set to ${a.hours}\n`);
PrintSettings();
cb();
});
cli.command('slack <use>').description('Send alerts to Slack').action((a, cb) => {
CurrencyPair.useSlack = a.use;
console.log('Sending to Slack ' + (CurrencyPair.useSlack?"enabled":"disabled"));
PrintSettings();
cb();
});
cli.command('slackkey <key>').description('Slack api key').action((a, cb) => {
slackkey = a.key;
Slack.setKey(slackkey);
console.log('Setting Slack key to ' + a.key);
PrintSettings();
cb();
});
cli.command('save').description('Save settings').action((a, cb) => {
WriteSettings();
console.log('Setting saved');
cb();
});
// Rescan all charts by resetting last-values. Will trigger new alerts. Used after changing settings.
cli.command('rescan').description('Rescan all charts.').action((a, cb) => {
bittrex.currencyPairs.forEach(x => {
x.lastLowerTime = 0;
x.lastDropState = false;
x.lastCurrentLow = 0;
x.lastBaseCandle = null;
});
hitbtc.currencyPairs.forEach(x => {
x.lastLowerTime = 0;
x.lastDropState = false;
x.lastCurrentLow = 0;
x.lastBaseCandle = null;
});
poloniex.currencyPairs.forEach(x => {
x.lastLowerTime = 0;
x.lastDropState = false;
x.lastCurrentLow = 0;
x.lastBaseCandle = null;
});
binance.currencyPairs.forEach(x => {
x.lastLowerTime = 0;
x.lastDropState = false;
x.lastCurrentLow = 0;
x.lastBaseCandle = null;
});
cb();
});
cli.command('settings').description('Show current settings.').action((a, cb) => {
PrintSettings();
cb();
});
cli.command('pairdata').description('Show data for all pairs.').action((a, cb) => {
console.log('\n\nBitTrex\n\n');
bittrex.currencyPairs.forEach(x => {
console.log(`${x.base_curr}/${x.primary_curr} LastLowerTime: ${x.lastLowerTime}, LastDropState: ${x.lastDropState}, LastCurrentLow: ${x.lastCurrentLow}, LastBaseCandle: ${x.lastBaseCandle?JSON.stringify(x.lastBaseCandle):"null"}`);
});
console.log('\n\nHitBtc\n\n');
hitbtc.currencyPairs.forEach(x => {
console.log(`${x.base_curr}/${x.primary_curr} LastLowerTime: ${x.lastLowerTime}, LastDropState: ${x.lastDropState}, LastCurrentLow: ${x.lastCurrentLow}, LastBaseCandle: ${x.lastBaseCandle?JSON.stringify(x.lastBaseCandle):"null"}`);
});
console.log('\n\nPoloniex\n\n');
poloniex.currencyPairs.forEach(x => {
console.log(`${x.base_curr}/${x.primary_curr} LastLowerTime: ${x.lastLowerTime}, LastDropState: ${x.lastDropState}, LastCurrentLow: ${x.lastCurrentLow}, LastBaseCandle: ${x.lastBaseCandle?JSON.stringify(x.lastBaseCandle):"null"}`);
});
console.log('\n\nBinance\n\n');
binance.currencyPairs.forEach(x => {
console.log(`${x.base_curr}/${x.primary_curr} LastLowerTime: ${x.lastLowerTime}, LastDropState: ${x.lastDropState}, LastCurrentLow: ${x.lastCurrentLow}, LastBaseCandle: ${x.lastBaseCandle?JSON.stringify(x.lastBaseCandle):"null"}`);
});
cb();
});
cli.command('ignore <exchange> <curr1> <curr2>').description("Don't alert for currency pair").action((a, cb) => {
let exchange: Exchange = null;
switch(a.exchange.toLowerCase()) {
case 'plnx':
exchange = poloniex;
break;
case 'btrx':
exchange = bittrex;
break;
case 'bina':
exchange = binance;
break;
}
if(exchange) {
let pairs = exchange.currencyPairs.filter(x => (x.base_curr.toLowerCase() == a.curr1.toLowerCase() && x.primary_curr.toLowerCase() == a.curr2.toLowerCase()) || (x.base_curr.toLowerCase() == a.curr2.toLowerCase() && x.primary_curr.toLowerCase() == a.curr1.toLowerCase()));
pairs.forEach(x => {
x.SetIgnore(true);
});
if(pairs.length == 0) {
console.log('Currency pair not found');
}
} else {
console.log('Invalid exchange code');
}
cb();
});
cli.command('unignore <exchange> <curr1> <curr2>').description("Stop ignoring currency pair").action((a, cb) => {
let exchange: Exchange = null;
switch(a.exchange.toLowerCase()) {
case 'plnx':
exchange = poloniex;
break;
case 'btrx':
exchange = bittrex;
break;
case 'bina':
exchange = binance;
break;
}
if(exchange) {
let pairs = exchange.currencyPairs.filter(x => (x.base_curr.toLowerCase() == a.curr1.toLowerCase() && x.primary_curr.toLowerCase() == a.curr2.toLowerCase()) || (x.base_curr.toLowerCase() == a.curr2.toLowerCase() && x.primary_curr.toLowerCase() == a.curr1.toLowerCase()));
pairs.forEach(x => {
x.SetIgnore(false);
});
if(pairs.length == 0) {
console.log('Currency pair not found');
}
} else {
console.log('Invalid exchange code');
}
cb();
});
cli.command('stats').description('Print statistics.').action((a, cb) => {
console.log('Number of currency pairs:');
console.log('Bittrex: ' + bittrex.currencyPairs.length);
console.log('HitBTC: ' + hitbtc.currencyPairs.length);
console.log('Poloniex: ' + poloniex.currencyPairs.length);
console.log('Binance: ' + binance.currencyPairs.length);
let btrx1h = 0;
let btrx1m = 0;
bittrex.currencyPairs.forEach(x => {
btrx1h += x.candles_1h.length;
btrx1m += x.candles_1m.length;
});
let hitb1h = 0;
let hitb1m = 0;
hitbtc.currencyPairs.forEach(x => {
hitb1h += x.candles_1h.length;
hitb1m += x.candles_1m.length;
});
let plnx1h = 0;
let plnx1m = 0;
poloniex.currencyPairs.forEach(x => {
plnx1h += x.candles_1h.length;
plnx1m += x.candles_1m.length;
});
let bina1h = 0;
let bina1m = 0;
binance.currencyPairs.forEach(x => {
bina1h += x.candles_1h.length;
bina1m += x.candles_1m.length;
});
console.log('\nNumber of 1h candles: ');
console.log('Bittrex: ' + btrx1h);
console.log('HitBTC: ' + hitb1h);
console.log('Poloniex: ' + plnx1h);
console.log('Binance: ' + bina1h);
console.log('\nNumber of 1m candles: ');
console.log('Bittrex: ' + btrx1m);
console.log('HitBTC: ' + hitb1m);
console.log('Poloniex: ' + plnx1m);
console.log('Binance: ' + bina1m);
cb();
});
cli.show();