-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
961 lines (847 loc) · 25.7 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
import fs, { constants } from "fs";
import { readFile, writeFile, access } from "fs/promises";
import fsEextra from "fs-extra";
import fetch from "node-fetch";
import { Command } from "commander/esm.mjs";
import promptly from "promptly";
import chalk from "chalk";
import Indicators from "technicalindicators";
import { exit } from "process";
import { spawn } from "child_process";
let SYMBOLS = [];
// put one ticker per line in symbol.list
// example:
// BTCUSDT
// ETHUSDT
// ... and so on
1646697599999;
// -----------------------------------------------------
// HACK these and you are ready to go
// timeframes and number of bars for seeding
const RESOLUTIONS = [
{ interval: "1w", seedPeriod: 999 },
{ interval: "3d", seedPeriod: 999 },
{ interval: "1d", seedPeriod: 999 },
{ interval: "12h", seedPeriod: 999 },
{ interval: "6h", seedPeriod: 999 },
{ interval: "4h", seedPeriod: 999 },
{ interval: "1h", seedPeriod: 999 },
];
// Seconds in a given timeframe
// Used to calculate the update diff
const I2SECS = {
"1h": 3600,
"4h": 14400,
"6h": 21600,
"12h": 43200,
"1d": 86400,
"3d": 259200,
"1w": 604800,
};
// TODO: maybe pass trim as an func argument?
// Number of bars we are intereted in, per timeframe
// Used *only* in output
const GETLASTPERIODS = {
"1w": 5,
"3d": 5,
"1d": 5,
"12h": 8,
"6h": 12,
"4h": 14,
"1h": 14,
};
// End HACK
// -----------------------------------------------------
const SYMBOL_FILENAME = "symbol.list";
const DATA_PATH = "./symbols";
//
//
//
async function fetchData(symbol, interval, limit, startTime) {
const standardOpts = { symbol, interval, limit };
const opts = startTime ? { ...standardOpts, startTime } : standardOpts;
const response = await fetch(
"https://api.binance.com/api/v3/klines?" + new URLSearchParams(opts)
);
return await response.json();
}
//
//
//
async function writeJsonFile(data, flag, filename) {
try {
let writeData;
if (flag === "a") {
const oldJsonData = await readJsonFile(filename);
const oldData = JSON.parse(oldJsonData);
oldData.splice(oldData.length - 1); // always remove the current candle cause it's not complete
writeData = oldData.concat(data);
} else writeData = data;
await writeFile(`${DATA_PATH}/${filename}`, JSON.stringify(writeData), {
flag: "w",
});
} catch (err) {
console.error(`writeJsonFile() :: Error writing ${DATA_PATH}/${filename}`);
throw err;
}
}
async function readJsonFile(filename) {
try {
const data = await readFile(`${DATA_PATH}/${filename}`);
return data;
} catch (err) {
console.error(`readJsonFile() :: Error reading ${DATA_PATH}/${filename}`);
throw err;
}
}
//
//
//
function hasDuplicateSymbols(symbols = SYMBOLS) {
const duplicates = symbols.filter(
(item, index) => index !== symbols.indexOf(item)
);
return duplicates.length !== 0;
}
//
// Integrity functions
//
//
// Current implementation: just returns how many candles are missing.
// The caller has to take this into account
// method = 'offset' | 'ignore' | 'fill'
function handleMissingCandles(symbol, resol, candles, method = "offset") {
const step = I2SECS[resol.interval] * 1000;
let missingCount = 0;
for (let i = 0; i < candles.length - 1; i++) {
if (candles[i][0] + step !== candles[i + 1][0]) {
missingCount += (candles[i + 1][0] - candles[i][0]) / step - 1;
console.log(`handleMissingCandles() :: missing ${missingCount} candles`);
}
}
return missingCount;
}
//
//
//
function examineJson(symbol, resol, candles, infile = true) {
const sourceLog = infile
? `${symbol}_${resol.interval}.json`
: `${symbol} ${resol.interval}`;
let isOK = true;
const diff = I2SECS[resol.interval] * 1000;
for (let i = 0; i < candles.length - 1; i++) {
if (candles[i][0] === candles[i + 1][0]) {
console.log(
`examineJson() :: ${sourceLog}: Duplicate timestamp ${
candles[i + 1][0]
}`
);
// Better exit here. Duplicate timestamps mean that the offsetting fix
// has somehow gone wrong.
// isOK = false;
exit(4);
} else if (candles[i][0] + diff !== candles[i + 1][0]) {
const one = candles[i][0];
const two = candles[i + 1][0];
console.log(
`examineJson() :: ${sourceLog}: Incorrect diff: ${new Date(
one
).toLocaleString()} (${one}) -- ${new Date(
two
).toLocaleString()} (${two}) `
);
isOK = false;
// exit(4);
}
}
return isOK;
}
//
//
//
async function checkSymbolsIntegrity(
symbols = SYMBOLS,
resolutions = RESOLUTIONS,
shouldThrow = false
) {
console.log(
`checkSymbolsIntegrity() :: Checking integrity for everything...`
);
await Promise.all(
symbols.map(async (symbol) => {
await resolutions.reduce(async (memo, resol) => {
await memo;
const { interval } = resol;
let data, candles;
try {
data = await readJsonFile(`${symbol}_${interval}.json`);
candles = JSON.parse(data);
} catch (err) {
console.log(
`checkSymbolsIntegrity() :: error reading ${DATA_PATH}/${symbol}_${interval}.json`,
err
);
throw err;
}
// console.log(`Checking integrity for ${symbol}_${resol.interval}...`);
const isOK = examineJson(symbol, resol, candles, true);
if (!isOK) console.log("\n");
if (!isOK && shouldThrow) throw "error";
}, undefined);
})
);
}
//
// rebuilds checkpoints.json for defined SYMBOLS
//
async function rebuildCheckpointsForSymbols(symbols, resolutions) {
const checkpoints = [];
// https://advancedweb.hu/how-to-use-async-functions-with-array-foreach-in-javascript/
await Promise.all(
symbols.map(async (symbol) => {
await resolutions.reduce(async (memo, resol) => {
await memo;
const { interval } = resol;
let data;
try {
data = await readJsonFile(`${symbol}_${interval}.json`);
const checkpoint = Object.values(JSON.parse(data)).at(-1)[6];
checkpoints.push({
symbol,
interval,
checkpoint,
});
} catch (err) {
console.log(
`rebuildCheckpoints() :: cannot find ${DATA_PATH}/${symbol}_${interval}.json, Dropping checkpoint from checkpoints.json`,
err
);
}
}, undefined);
})
);
// write ticker close datetime
console.log(
"rebuildCheckpointsForSymbols() :: Found following checkpoints\n",
checkpoints
);
writeJsonFile(checkpoints, "w", "checkpoints.json");
return checkpoints;
}
//
// END Integrity functions
//
//
// fetch all symbols
//
async function fetchSymbols(symbols, resolutions) {
let checkpoints = [];
try {
const checkpointsJson = await readJsonFile(`checkpoints.json`);
checkpoints = JSON.parse(checkpointsJson);
} catch (err) {
console.log("fetchSymbols() :: Cannot find checkpoints file", err);
}
let nextCheckpoints = [];
await Promise.all(
symbols.map(async (symbol) => {
await resolutions.reduce(async (memo, resol) => {
await memo;
const { interval, seedPeriod } = resol;
let { checkpoint } = checkpoints.find((obj) => {
return obj.symbol === symbol && obj.interval === interval;
}) || { checkpoint: -1 };
const symbolFname = `${symbol}_${interval}.json`;
let fetchedData;
let didSeed = false;
console.log(`Checkpoint for ${symbol}, ${interval} = ${checkpoint}`);
if (checkpoint === -1) {
try {
await access(`${DATA_PATH}/${symbolFname}`, constants.F_OK);
const json = await readJsonFile(symbolFname);
checkpoint = Object.values(JSON.parse(json)).at(-1)[6];
console.log(
`fetchSymbols() :: ${symbolFname} exists, getting last checkpoint from there.\n*** Please rerun to fetch latest data. ***\n`
);
} catch (err) {
console.log(`fetchSymbols() :: Seeding ${symbol}, ${interval} ...`);
fetchedData = await fetchData(symbol, interval, seedPeriod);
if (fetchedData.msg) {
console.log(
`fetchSymbols() :: Can't find symbol ${symbol}. ${fetchedData.msg}`
);
return false;
}
checkpoint = `${Object.values(fetchedData).at(-1)[6]}`;
didSeed = true;
}
} else {
const nowUTC = Date.parse(new Date());
let diff;
// checkpoint is close datetime. Binance API sets current bar's
// close datetime in the future. So:
// -TF_seconds < nowUTC - checkpoint: current bar update
// nowUTC - checpoint >= 0 previous bar + 1
const needUpdate =
(diff = (nowUTC - checkpoint) / 1000) > -I2SECS[interval];
if (needUpdate) {
const count =
diff < 0 ? 1 : 1 + Math.floor(diff / I2SECS[interval]) + 1;
fetchedData = await fetchData(symbol, interval, count);
console.log(
`fetchSymbols() :: ${symbol} ${interval}: Fetched ${count} candles...`
);
}
}
if (fetchedData) {
writeJsonFile(fetchedData, didSeed ? "w" : "a", symbolFname);
checkpoint = `${Object.values(fetchedData).at(-1)[6]}`;
}
nextCheckpoints.push({
symbol,
interval,
checkpoint,
});
}, undefined);
})
);
if (nextCheckpoints.length !== 0) {
console.log(
`fetchSymbols() :: Writing next checkpoints, ${nextCheckpoints.length} symbol checkpoints`
);
writeJsonFile(nextCheckpoints, "w", "checkpoints.json");
}
}
async function prependSymbolData(symbols = SYMBOLS) {
const resolutions = [
//
// Keep this commented out. Uncomment only when we need more past data.
//
// BEWARE: BINANCE data is not always clean. I hand edited some files.
//
// 3000 ticks should be enough for any TA we do, even for unstable formulas
//
{ interval: "1w" },
{ interval: "3d" },
{ interval: "1d" },
{ interval: "12h" },
{ interval: "6h" },
{ interval: "4h" },
{ interval: "1h" },
];
await Promise.all(
symbols.map(async (symbol) => {
await resolutions.reduce(async (memo, resol) => {
await memo;
const { interval } = resol;
const symbolFname = `${symbol}_${interval}.json`;
let json;
let firstSavedDate;
try {
await access(`${DATA_PATH}/${symbolFname}`, constants.F_OK);
json = await readJsonFile(symbolFname);
firstSavedDate = JSON.parse(json).at(0)[0];
console.log(
`prependSymbolData() :: ${symbolFname} exists, getting first date from there.`
);
} catch (err) {
console.log(`prependSymbolData() :: exiting hard...`);
exit(5);
}
// fetch from sometime way back in the past to discover the ticker's
// very first candle timestamp
const prefetchJson = await fetchData(
symbol,
interval,
1,
"1302928000000" // April 16, 2011 7:26:40 AM GMT+03:00 DST
);
const introDate = prefetchJson[0][0];
console.log(
`prependSymbolData() :: intro date for ${symbol} = ${new Date(
introDate
).toLocaleString()}`
);
// we have all Binance data, return
if (firstSavedDate === introDate) {
console.log(
`prependSymbolData() :: we have all data for ${symbol} ${interval}, returning...\n`
);
return;
}
// haven't finished yet, there is more date to fetch
const maxLimit = 1000;
const lhs = firstSavedDate - I2SECS[interval] * 1000 * maxLimit;
const getBack =
lhs >= introDate
? maxLimit
: (firstSavedDate - introDate) / 1000 / I2SECS[interval];
console.log(
`prependSymbolData() :: ----> Fetching ${symbol}: ${getBack} ${interval}, ...`
);
const fetchedData = await fetchData(
symbol,
interval,
getBack,
firstSavedDate - I2SECS[interval] * 1000 * getBack
);
// decide what to do, depending on the fetchedData integrity and length
if (fetchedData.length === 0) {
console.log(
`prependSymbolData() :: fetched data is empty, ${fetchedData.length}, probably rate limiting hit us, try again\n`
);
} else if (fetchedData.msg)
console.log(`prependSymbolData() :: ${fetchedData.msg}\n`);
else {
const isOK = examineJson(symbol, resol, fetchedData, false);
if (!isOK) {
const count = handleMissingCandles(
symbol,
resol,
fetchedData,
false
);
console.log(
`prependSymbolData() :: ${symbol} ${interval}: ${count} missing candles in fetched data. Offsetting and prepending...\n`
);
fetchedData.splice(-count, count);
}
const json2update = JSON.parse(json);
json2update.unshift(...fetchedData);
writeJsonFile(json2update, "w", symbolFname);
}
}, undefined);
})
);
}
//
//
//
function runIndicator(indicatorFn, showAll = false) {
return async function (symbols = SYMBOLS, resolutions = RESOLUTIONS) {
console.log(`Running ${indicatorFn.name}\n`);
const result = await Promise.all(
symbols.map(async (symbol) => {
const tfs4symbol = await resolutions.reduce(
async (memo, resolution) => {
const { interval } = resolution;
const json = await readJsonFile(`${symbol}_${interval}.json`);
const data = JSON.parse(json);
// ...await memo. Am I using the wrong tool for the job?
// Async makes dev work time consuming.
// Python? Amitrader? R? or just sync version?
// https://stackoverflow.com/questions/41243468/javascript-array-reduce-with-async-await
const series = indicatorFn(data);
return {
...(await memo),
[interval]: showAll
? series
: series.slice(-GETLASTPERIODS[interval]),
};
},
{}
);
return { [symbol]: tfs4symbol };
})
);
// arr of Obj -> Obj[symbol]={ 4h:[], 1d: [], ...}
return result.reduce((acc, curr) => {
const [[key, value], _] = Object.entries(curr);
acc[key] = value;
return acc;
}, {});
};
}
//
// Generic output function
//
function output({
results, // results as we get them from our indicator
filterFn,
timeframes = RESOLUTIONS.map((r) => r.interval), // used only when no filtering
symbols = SYMBOLS,
filter = true,
}) {
symbols.forEach((symbol) => {
if (filter) {
const filtered = filterFn(symbol, results[symbol]);
if (filtered.signals.length !== 0) {
console.log(filtered);
console.log();
} else console.log(`symbol: ${symbol}: no signal`);
} else {
// just print supertrend series
console.log(`===== ${symbol} =====`);
// this to print only specified timeframes
const clone = ((timeframes, symResults) => {
return timeframes.reduce((acc, tf) => {
acc[tf] = symResults[tf];
return acc;
}, {});
})(timeframes, results[symbol]);
console.log(clone);
console.log();
}
});
}
// that's the filterFn argument of output function
// was: default timeframes are 4h and 12h
// current: all timeframes
function filterSupertrend(
predicateNo = "p1",
timeframes = RESOLUTIONS.map((r) => r.interval)
) {
// filter SuperTrend results
// LONG only filter
// if latest trend is UP and previous is DOWN -> that's a pivot point.
// if both (the default) 4h, 12h and 1d timeframes exhibit, this is buy signal
const predicate1 = (timeframe, series) => {
const last = -1;
const OneB4Last = -2;
const TwoB4Last = -3;
const ThreeB4Last = -4;
if (timeframe === "4h") {
if (series.at(last).trend === 1 && series.at(OneB4Last).trend === -1)
return "buy";
if (
series.at(last).trend === 1 &&
series.at(OneB4Last).trend === 1 &&
series.at(TwoB4Last) === -1
)
return "buy";
if (
series.at(last).trend === 1 &&
series.at(OneB4Last).trend === 1 &&
series.at(TwoB4Last) === 1 &&
series.at(ThreeB4Last) === -1
)
return "buy";
if (series.at(last).trend === -1 && series.at(OneB4Last).trend === 1)
return "sell";
if (
series.at(last).trend === -1 &&
series.at(OneB4Last).trend === -1 &&
series.at(TwoB4Last) === 1
)
return "sell";
if (
series.at(last).trend === -1 &&
series.at(OneB4Last).trend === -1 &&
series.at(TwoB4Last) === -1 &&
series.at(ThreeB4Last) === 1
)
return "sell";
}
if (timeframe === "12h") {
if (series.at(last).trend === 1 && series.at(OneB4Last).trend === -1)
return "buy";
if (
series.at(last).trend === 1 &&
series.at(OneB4Last).trend === 1 &&
series.at(TwoB4Last).trend === -1
)
return "buy";
if (series.at(last).trend === -1 && series.at(OneB4Last).trend === 1)
return "sell";
if (
series.at(last).trend === -1 &&
series.at(OneB4Last).trend === -1 &&
series.at(TwoB4Last).trend === 1
)
return "sell";
}
if (timeframe === "1d") {
if (series.at(last).trend === 1 && series.at(OneB4Last).trend === -1)
return "buy";
if (
series.at(last).trend === 1 &&
series.at(OneB4Last).trend === 1 &&
series.at(TwoB4Last).trend === -1
)
return "buy";
if (series.at(last).trend === -1 && series.at(OneB4Last).trend === 1)
return "sell";
if (
series.at(last).trend === -1 &&
series.at(OneB4Last).trend === -1 &&
series.at(TwoB4Last).trend === 1
)
return "sell";
}
return false;
};
const predicate2 = (timeframe, series) => {
console.log("---> p2 needs implementation");
return false;
};
const predicate3 = (timeframe, series) => {
console.log("---> p3 needs implementation");
return false;
};
const predicateFn =
predicateNo === "p1"
? predicate1
: predicateNo === "p2"
? predicate2
: predicateNo === "p3"
? predicate3
: (timeframe, series) => {
console.log("---> this is a noop, Specify p1, p2 or p3 with -p");
};
return (symbol, symbolResults) => {
const signals = timeframes.map((tf) => {
const series = symbolResults[tf];
if (predicateFn(tf, series) === "buy") return { [tf]: "buy" };
else if (predicateFn(tf, series) === "sell") return { [tf]: "sell" };
else return { [tf]: "" };
});
return {
symbol,
// signals,
signals: signals.filter((s) => Object.values(s)[0] !== ""),
};
};
}
//
//
//
function SuperTrend(atrPeriod = 10, multiplier = 2) {
console.log(
`SuperTrend indicator primed: lookback: ${atrPeriod}, ATR multiplier: ${multiplier}`
);
return function __supertrend(data) {
const openDT = data.map((pt) => pt[0]);
const closeDT = data.map((pt) => pt[6]);
const src = data.map((pt) => (parseFloat(pt[2]) + parseFloat(pt[3])) / 2);
const highs = data.map((pt) => pt[2]);
const lows = data.map((pt) => pt[3]);
const closes = data.map((pt) => pt[4]);
const ATR = Indicators.atr({
period: atrPeriod, // SuperTrend param
high: highs,
low: lows,
close: closes,
});
// (H - L) /2
let bot1 = src[atrPeriod] - multiplier * ATR[0];
let top1 = src[atrPeriod] + multiplier * ATR[0];
let trend = 1;
let trend1 = 1;
const band = ATR.map((atr, idx) => {
const closeIdx = atrPeriod + idx;
const close = closes[closeIdx];
const bot = src[idx + atrPeriod] - multiplier * atr;
bot1 = closes[closeIdx - 1] > bot1 ? Math.max(bot, bot1) : bot;
const top = src[idx + atrPeriod] + multiplier * atr;
top1 = closes[closeIdx - 1] < top1 ? Math.min(top, top1) : top;
trend =
trend === -1 && closes[closeIdx] > top1
? 1
: trend === 1 && closes[closeIdx] < bot1
? -1
: trend;
const buySignal = trend === 1 && trend1 === -1;
const sellSignal = trend === -1 && trend1 === 1;
trend1 = trend;
return {
openDT: new Date(openDT[idx + atrPeriod]).toLocaleString("en-US"),
closeDT: new Date(closeDT[idx + atrPeriod]).toLocaleString("en-US"),
bottom: bot1,
top: top1,
trend,
buySignal,
sellSignal,
};
});
return band;
};
}
//
//
//
function prepFSStruct() {
if (!fs.existsSync(DATA_PATH)) {
fs.mkdirSync(DATA_PATH);
console.log(`prepFSStruct() :: ${DATA_PATH} dir has been created.`);
}
if (!fs.existsSync(SYMBOL_FILENAME)) {
fs.writeFileSync(SYMBOL_FILENAME, "", { encoding: "utf8" });
console.log(`prepFSStruct() :: ${SYMBOL_FILENAME} file has been created.`);
}
}
//
//
//
function readSymbols() {
let list;
try {
list = fs.readFileSync(SYMBOL_FILENAME, { encoding: "utf8", flag: "r" });
} catch (err) {
console.log(
`readSymbols() :: Can not find symbols file ${SYMBOL_FILENAME}`,
err
);
exit(2);
}
if (list.length === 0) {
console.log(
`readSymbols() :: symbol file ${SYMBOL_FILENAME} is empty.\nOne pair, e.g. BTCUSDT, per line`
);
exit(3);
}
const symbols = list
.split("\n")
.filter((s) => s.length !== 0)
.map((s) => s.trim());
return symbols;
}
//
//
//
function toCSV() {
const toCSV = spawn("/bin/bash", ["./tools/json-to-csv.sh"]);
toCSV.stdout.on("data", (data) => {
console.log(`${data}`);
});
toCSV.stderr.on("data", (data) => {
console.log(chalk.red(`main() :: stderr: ${data}`));
});
toCSV.on("error", (error) => {
console.log(chalk.red(`main() :: error: ${error.message}`));
});
toCSV.on("close", (code) => {
console.log(
`main() :: child process (json to csv bash script) exited with code ${code}`
);
});
}
//
//
//
function cpDir(source, dest) {
fsEextra.copy(source, dest, function (err) {
if (err) {
console.log("cpDir() :: An error occured while copying the folder.");
return console.error(err);
}
console.log("Copy completed!");
});
}
//
//
//
function testThings() {
console.log("testThings() :: This is a dummy test. Write something.");
}
//
//
//
async function main() {
prepFSStruct();
// global
SYMBOLS = readSymbols();
if (hasDuplicateSymbols(SYMBOLS)) {
console.log("main() :: exit code 1: Duplicate symbols");
process.exit(1);
}
const program = new Command();
program
//
// for fetching and checking datapoint only.
//
.option("-r, --fetch-symbols", "fetch symbols, in code")
.option("-x, --convert-CSV", "convert files to csv")
.option(
"-b, --rebuild-from-symbols",
"rebuild checkpoints file from symbols data"
)
.option("-c --check-integrity", "check symbols timestamp order")
.option("-e --tEst", "test/dry run things")
//
// OBSOLETE, USE PYTHON
//
.option(
"-s, --run-screener [symbols...]",
"(DEPRECATED, run python). Run supertrend on fetched symbols"
)
.option(
"-o --output-timeframes [timeframes...]",
"(DEPRECATED, run python). Specify which timeframes to show. Options: 4h, 6h, 12h, 1d, 3d, 1w"
)
.option("-a, --show-all", "show all data points, do not trim", false)
.option(
"-f, --filter",
"(DEPRECATED, run python). Filter results (using a predicate), else show the supertrend series",
false
)
.option(
"-p --predicate [predicate]",
"(DEPRECATED, run python). Select predicate. Options: p1, p2, p3. Read code",
"p1"
);
program.parse();
const options = program.opts();
// fetching, checking and rebuilding
// will exit if there is an integrity error
if (options.fetchSymbols) {
await fetchSymbols(SYMBOLS, RESOLUTIONS);
try {
console.log("\n");
await checkSymbolsIntegrity(SYMBOLS, RESOLUTIONS);
} catch (e) {
console.log("\nmain() :: Error with symbol integrity, exiting...");
exit(4);
}
}
if (options.rebuildFromSymbols) {
const answer = await promptly.confirm(
"Sure? Checkpoint file will be recreated:"
);
answer && rebuildCheckpointsForSymbols(SYMBOLS, RESOLUTIONS);
}
if (options.checkIntegrity) await checkSymbolsIntegrity(SYMBOLS, RESOLUTIONS);
if (options.convertCSV) {
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
console.log("\nmain() :: Will convert json to csv... Wait a bit...");
await delay(5000);
toCSV();
}
if (options.tEst) {
testThings();
// leave it here. this shouldn't be exposed casually to the CLI
prependSymbolData(SYMBOLS);
}
//
// OBSOLETE NOTE, RUN PYTHON
//
if (options.runScreener) {
let results;
const fnST = SuperTrend();
const filterFn = filterSupertrend(
options.predicate,
options.outputTimeframes
);
// -s only, all symbols
if (options.runScreener === true) {
results = await runIndicator(fnST, options.showAll)();
output({
results,
filterFn,
filter: options.filter,
timeframes: options.outputTimeframes,
});
} else {
// -s symbol
results = await runIndicator(fnST, options.showAll)(options.runScreener);
output({
results,
filterFn,
timeframes: options.outputTimeframes,
symbols: options.runScreener,
filter: options.filter,
});
}
}
}
main();