-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
1175 lines (990 loc) · 39 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
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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict'
const async = require('async');
const { spawn, execSync, spawnSync } = require("child_process");
const AWS = require('aws-sdk');
const handlebars = require('handlebars');
const superagent = require('superagent');
const fs = require('fs').promises;
// design:
// dashboard panel with columns
// func to call whale API to get some data
// func to bucket the data and flush to disk?
// AWS -> Terraform using single account but multiple stages by keeping track of local tf state files
let RUNTIME_ENV = "dev";
// TODO - move to external config file?
let config = {
"STATIC_CONTENT_PATH": {"dev": "./", "test": "/opt/", "qa": "/opt/"},
"DEFAULT_AWS_CONFIG_PATH": "",
"DEFAULT_UPDATE_DB_FETCH_RANGE": 60 * 5,
"DEFAULT_CHART_LENGTH": 3600 * 24 * 1000,
"DEFAULT_CHART_UPDATE_LENGTH": 600 * 1000,
"BINANCE_FETCH_SIZE": 3600 * 6 * 1000,
"BINANCE_PRED_FETCH_SIZE": 1800 * 1000
}
// single entry point from tha gateway API to reduce amoount of boiler-plate code in terraform files
module.exports.dashboard = async (event, context) => {
// take in args for what cmd to run
// one for returning the buckets
// one for returning the html template
console.log('Event: ', JSON.stringify(event, null, '\t'));
console.log('Context: ', JSON.stringify(context, null, '\t'));
// we check for AWS_LAMBDA_FUNCTION_NAME env var to see if we are running inside lambda or somewhere else
if ('RUNTIME_ENV' in process.env)
RUNTIME_ENV = process.env.RUNTIME_ENV;
// if local dev then we need to explicitly load the default aws config file with regions, etc
// https://stackoverflow.com/questions/31039948/configuring-region-in-node-js-aws-sdk
// TODO - correctly load from config file instead!
if(RUNTIME_ENV == "dev")
AWS.config.update({region:'ap-northeast-1'});
let cmd = event.queryStringParameters.cmd;
let func = null;
let router = new Router();
switch ( cmd ) {
case 'get_page':
func = router.get_page;
break;
case 'get_front':
func = router.get_front;
break;
case 'get_whale_buckets':
func = router.get_whale_buckets;
break;
case 'get_db_buckets':
func = router.get_db_buckets;
break;
case 'update_db':
func = router.update_db;
break;
case 'get_config':
func = router.get_config;
break;
case 'generate_dataset':
func = router.generate_dataset;
break;
case 'get_price_pred':
func = router.price_pred;
break;
default:
// return a proper error msg to the user?? -> list valid commands -> swagger!!!
console.log( "Command not recognized!", event.queryStringParameters );
break;
}
// waitforGeeksforGeeks();
// console.log("post step");
// return;
// let callback_func = (data) => { callback(null, data); };
// this yields as soon as we hit the first await -> so we must do everything itside it
try {
let res = await func.bind(router)(event.queryStringParameters);
// console.log( "dashbaord res:", res );
return res;
} catch (err) {
console.log(err);
return err;
}
}
AWS.config.update({region:'ap-northeast-1'});
var documentClient = new AWS.DynamoDB.DocumentClient();
const insertAccount = async (e) => {
const params = {
Item: e,
TableName: 'wtb_api_events-dev'
};
return documentClient.put(params).promise(); // convert to Promise
}
function resolvelater() {
return new Promise(resolve => {
setTimeout(() => {
resolve('GeeksforGeeks');
}, 2000);
});
}
async function waitforGeeksforGeeks() {
console.log('calling');
const result = await resolvelater();
console.log(result);
}
class Router
{
constructor()
{
this.alert_api = new WhaleAlertApi();
this.db_handle = new DBStore();
this.binance = new BinanceApi();
this.ds = new Dataset( this.binance );
this.predictor = new PricePredictor( this.binance, this.ds );
}
respond( {body, type = "html", status = 200} )
{
let type_header = 'text/html; charset=utf-8';
if( type == "json" )
type_header = 'application/json';
var response = {
statusCode: status,
headers: { 'Content-Type': type_header },
body: body
}
return response;
}
async price_pred()
{
let res = await this.predictor.pred();
return this.respond( {body:JSON.stringify( res ), type:"text" } );
}
async generate_dataset()
{
let res = await this.binance.dataset_generator();
return this.respond( {body:JSON.stringify( res ), type:"text" } );
}
async get_config( params )
{
return this.respond( {body: JSON.stringify(config, null, '\t'), type:"json" } );
}
// how to know if we should look localy or in opt?????
async get_page( params )
{
let source_path = config.STATIC_CONTENT_PATH[RUNTIME_ENV] + 'static/main.handlebars';
let source = await fs.readFile( source_path, "utf8"); //, (err, source) => {
// not sure how to handle errors yet
// if (err) throw err;
// var template = handlebars.compile(source);
var html = source; // we will render everything client-side instead
return this.respond( {body:html } );;
}
async get_front( params )
{
return this.respond( {body:'<p>Hello world!</p>' } );
}
// second end-point to fetch bucket data
async get_whale_buckets( params )
{
console.log( params );
// let api_data = await this.alert_api.get_data( params );
let api_data = await this.db_handle.get_from_db( params );
let buckets = this.alert_api.bucket_data_exchange( api_data );
if( !buckets ) return { statusCode: 500, body: 'Something wrong getting data buckets!' };
return this.respond( {body:JSON.stringify( buckets ), type:"json" } );
}
// second end-point to fetch bucket data
async update_db( params )
{
console.log( params );
let api_data = await this.alert_api.get_data( params );
console.log("got data");
if( !api_data ) return { statusCode: 500, body: 'Error fetching data from downstream API!' };
let save_res = await this.db_handle.save_to_db( api_data );
console.log( "saved data", save_res );
if( !save_res ) return { statusCode: 500, body: 'Error saving data!' };
let count = api_data.transactions.length;
var response = this.respond( {body:'{"msg":"all '+count+' records saved to db"}', type:"json" } );
console.log( "returning from update_db" );
return response;
}
// second end-point to fetch bucket data
async get_db_buckets( params )
{
//DRY!!!
console.log( params );
let api_data = await this.db_handle.get_from_db( params );
let buckets = this.alert_api.bucket_data_exchange( api_data );
let buckets_timeseries_exchanges = this.alert_api.bucket_data_timeseries( {data:api_data} );
let buckets_timeseries_total = this.alert_api.bucket_data_timeseries( {data:api_data, do_allow_cross_unknown_flow:true} )
buckets["buckets_timeseries_exchanges"] = buckets_timeseries_exchanges;
buckets["buckets_timeseries_total"] = buckets_timeseries_total;
if( !buckets ) return { statusCode: 500, body: 'Something wrong getting aws data buckets!' };
return this.respond( {body:JSON.stringify( buckets ), type:"json" } );
}
}
class DatasetHeader
{
constructor( winsize = 0, whitelist = null )
{
// 27 features
this.features = { "raw":[], "bps":[], "macd1":[], "macd5":[], "macd30":[] };
this.features.raw = ["open", "high", "low", "close", "vol", "quote_asset_vol", "num_of_trades", "taker_vol", "taker_buy_asset_vol"];
this.features.bps = ["high_bps", "low_bps", "close_bps"]; // no open_bps because it will always be 0 dy definition
this.features.macd1 = ["macd1_ema12", "macd1_ema26", "macd1_macd", "macd1_signal", "macd1_hist"];
this.features.macd5 = ["macd5_ema12", "macd5_ema26", "macd5_macd", "macd5_signal", "macd5_hist"];
this.features.macd30 = ["macd30_ema12", "macd30_ema26", "macd30_macd", "macd30_signal", "macd30_hist"];
this.raw_features_indexes = {
"open":1,
"high":2,
"low":3,
"close":4,
"vol":5,
"quote_asset_vol":7,
"num_of_trades":8,
"taker_vol":9,
"taker_buy_asset_vol":10
};
this.winsize = winsize;
this.whitelist = whitelist
}
// asume feature name is valid
is_feature_in_header( feature_name )
{
if( !this.whitelist )
return true;
if( this.whitelist && this.whitelist.indexOf( feature_name ) >= 0 )
return true;
return false;
}
// this calls some other toString before getting here for some reason
get_str() {
let header = "";
let is_first = true;
for( let category in this.features )
{
for( let feature of this.features[category] )
{
if( ! this.is_feature_in_header( feature ) )
continue;
if( !is_first )
header += ", ";
header += feature;
is_first = false;
}
}
//add a N-frame lookback window -> 30 expands to 810 features
let header_with_window = header;
for(let i = 1; i <= this.winsize; i++)
{
let win_columns = "";
let columns = header.split(",");
for( let column of columns )
win_columns += ", " + column.trim() + "_" + i.toString().padStart(2, "0");
header_with_window += win_columns + "";
}
header = header_with_window;
// console.log( header );
return header;
}
}
class Dataset
{
constructor( web_api )
{
this.web_api = web_api;
}
async dataset_generator_cmd( days, winsize, end_dateiso, whitelist )
{
await this._dataset_generator_impl( Number(days), Number(winsize), end_dateiso, whitelist );
}
async data_fetcher( days, end_dateiso )
{
console.log("Fetching more data", days, end_dateiso);
let end = new Date(end_dateiso)
let new_date = end.setDate(end.getDate() - days );
let start_date = new Date( new_date );
let iso_date = start_date.getUTCFullYear() + '-' + ('0'+ (start_date.getUTCMonth()+1)).slice(-2) + '-' + ('0'+ start_date.getUTCDate()).slice(-2);
console.log( iso_date );
await this.save_data(iso_date, end_dateiso);
console.log("done");
return "all good!";
}
async dataset_generator()
{
// 1 day => 1440 frames
let days = 1; // about 1sec per day? //37~49sec for 50days // 90sec for 100days
let winsize = 0; // 27 * (winsize + 1) 30 => 837 features
// let whitelist = ["open", "close", "high", "low", "vol", "num_of_trades"];
let whitelist = ["open", "close", "macd1_hist", "macd5_hist"];
// "macd5_hist", "macd30_hist"
// "macd1_macd", "macd1_signal"
let end_dateiso = "2021-01-17";
// 50+ starts to throw errors -> either throttle or use websockets -> throttle the cheap'n'easy way
// for( days of [1, 2, 10, 20, 50, 100] )
for( days of [200] )
// for( days of [10] )
{
// for( winsize of [0, 1, 10, 20, 30, 50] )
for( winsize of [0, 1, 2, 4, 10] )
{
console.log(days, winsize );
await this.save_data("01/01/2021", "01/28/2021");
// await this._dataset_generator_impl( days, winsize, end_dateiso, whitelist );
}
}
console.log("done");
return "all good!";
}
async _dataset_generator_impl( days, winsize, end_dateiso, whitelist = null )
{
// let end_timestamp = Math.floor( Date.now() );
let end_date = new Date(end_dateiso);
console.log( end_date );
let start_date = new Date(end_date);
start_date.setDate( end_date.getDate() - days + 1 );
// let datestamp = this.date2datestamp( end_date ) + "_" + this.date2datestamp( start_date );
let datestamp = this.date2datestamp( end_date );
let postfix = "win"+winsize.toString().padStart(2, "0") +"_"+ days.toString().padStart(3, "0");
let json_filepath = './datasets/data_binance_'+datestamp+"_"+postfix+'.json';
let csv_filepath = './datasets/data_binance_'+datestamp+"_"+postfix+'_data.csv';
let label_filepath = './datasets/data_binance_'+datestamp+"_"+postfix+'_labels.csv';
let sample_filepath = './datasets/data_binance_'+datestamp+"_"+postfix+'_samples.csv';
console.log( start_date.toISOString(), end_date.toISOString() );
let data = await this.load_data( start_date.toISOString(), end_date.toISOString() );
console.log( data.length );
// await fs.writeFile(json_filepath, JSON.stringify( data, null, "\t" ), 'utf8');
console.log( csv_filepath );
let data_obj = await this.json2csv( data, winsize, whitelist );
this.save_csv( csv_filepath, data_obj["csv_data"], data_obj["csv_header"] );
this.save_csv( label_filepath, data_obj["label_data"], data_obj["label_header"] );
this.save_csv( sample_filepath, data_obj["sample_data"], data_obj["sample_header"] );
}
async save_data( start_date, end_date )
{
var start = new Date(start_date);
var end = new Date(end_date);
var loop = new Date(start);
while(loop <= end){
let start_timestamp = new Date(loop); //we clone the obj
start_timestamp.setHours(0);
start_timestamp.setMinutes(0);
start_timestamp.setSeconds(0);
let end_timestamp = new Date(loop); //we clone the obj
end_timestamp.setHours(23);
end_timestamp.setMinutes(59);
end_timestamp.setSeconds(59);
let datestamp = this.date2datestamp( loop );
let data = await this.web_api.get_data( Math.floor(start_timestamp), Math.floor(end_timestamp) );
console.log( start_timestamp, end_timestamp, "=>", data.length );
await fs.writeFile( "./datasets/data_binance_raw_"+datestamp+".json",
JSON.stringify( data, null, "\t" ), 'utf8');
var newDate = loop.setDate(loop.getDate() + 1);
loop = new Date(newDate);
}
}
date2datestamp( date_obj )
{
return (date_obj.getYear()-100).toString() +
(date_obj.getMonth()+1).toString().padStart(2, "0") +
(date_obj.getDate()).toString().padStart(2, "0");
}
async json2csv( json_obj, winsize, feature_whitelist = null, is_sample_mode = false )
{
console.log("json2csv");
// let json_str = await fs.readFile( json_filepath, "utf8");
// let json_obj = JSON.parse( json_str );
// console.log( json_obj[0] );
let header = new DatasetHeader( winsize, feature_whitelist );
// console.log( header.get_str() );
let label_header = "next_close_val, next_close_dir, next_close_bps";
label_header += ", next_close_val5, next_close_dir5, next_close_bps5";
label_header += ", next_close_val30, next_close_dir30, next_close_bps30";
label_header += ", next_close_val60, next_close_dir60, next_close_bps60";
label_header += ", next_close_val120, next_close_dir120, next_close_bps120";
// TODO RSI + Stochastic? + 2std-dev(BB) + bps hist window
// extract close price into an array so we can calculate ema on it
let close_vec = [];
for( let entry of json_obj )
close_vec.push( {x:entry[0], y:entry[4]} );
let macd = this.calculate_macd( close_vec, [ 1, 5, 30 ] );
let csv_data = [];
let label_data = [];
let sample_data = [];
// console.log( "winsize:" + winsize + " close_vec:" + close_vec.length );
//ignore last 120 entries because we got no label for 'em
// and start at winsize position because we got no lookback window before that frame
let sample_count = json_obj.length * 0.1; // we assume last 10% of data will be used for samples
let loop_max = json_obj.length-120;
// except when we are in sample mode - then we load everything into samples and we go till the most recent entry
if( is_sample_mode == true )
{
sample_count = json_obj.length;
loop_max = json_obj.length;
}
// console.log( "loop_max:", loop_max );
for( let i = winsize; i < loop_max; i++)
{
let entry = json_obj[i];
// console.log( entry );
let line = this.extract_line( header, json_obj, i );
line += this.prefix_comma( this.extract_bps( header, json_obj, i ));
line += this.prefix_comma( this.extract_macd( header, macd, i ));
// build the lookback window - it needs to look at the previous data not next data!!!
for(let q = -1; q >= -winsize; q--)
{
line += this.prefix_comma( this.extract_line( header, json_obj, i+q ));
line += this.prefix_comma( this.extract_bps( header, json_obj, i+q ));
line += this.prefix_comma( this.extract_macd( header, macd, i+q ));
}
if( i > (json_obj.length - sample_count) )
sample_data.push( line );
else
csv_data.push( line );
// label calcs
var label_line = "";
if( i < loop_max-120)
{
label_line = this.calculate_label( close_vec[i+1].y, entry );
label_line += this.prefix_comma( this.calculate_label( close_vec[i+5].y, entry ));
label_line += this.prefix_comma( this.calculate_label( close_vec[i+30].y, entry ));
label_line += this.prefix_comma( this.calculate_label( close_vec[i+60].y, entry ));
label_line += this.prefix_comma( this.calculate_label( close_vec[i+120].y, entry ));
// if( i < 10 || i > loop_max - 130)
// console.log( label_line );
}
else
label_line = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"; // blank, but we must keep dims the same??
label_data.push( label_line );
}
return { "csv_header":header.get_str(), "csv_data":csv_data,
"label_header":label_header, "label_data":label_data,
"sample_header":"", "sample_data":sample_data };
}
async save_csv( filepath, data, header = null )
{
if( header )
await fs.writeFile(filepath, header+`\n`, 'utf8');
else
await fs.writeFile(filepath, "", 'utf8');
for( const line of data )
await fs.appendFile(filepath, line+`\n`, 'utf8');
}
prefix_comma( line )
{
if( line != "" )
return ", " + line;
return line;
}
extract_line( header, json_obj, index )
{
let entry = json_obj[index];
// console.log( entry, index );
// don't even bother with timestamps
let line = "";
let is_first = true;
for( let feature in header.raw_features_indexes )
if( header.is_feature_in_header( feature ) )
{
if( !is_first )
line += ", ";
line += `${entry[ header.raw_features_indexes[feature] ]}`
is_first = false;
}
return line;
}
extract_bps( header, json_obj, index )
{
let entry = json_obj[index];
let open = entry[1];
let high_bps = this.num2bps( entry[2], open );
let low_bps = this.num2bps( entry[3], open );
let close_bps = this.num2bps( entry[4], open );
// assert high is larger than low!!!!
if( high_bps < low_bps )
console.log( "bps values look wrong!" );
let line = "";
let is_first = true;
if( header.is_feature_in_header( "high_bps" ) )
{
line += high_bps;
is_first = false;
}
if( header.is_feature_in_header( "low_bps" ) )
{
if( !is_first )
line += ", ";
line += high_bps;
is_first = false;
}
if( header.is_feature_in_header( "close_bps" ) )
{
if( !is_first )
line += ", ";
line += high_bps;
is_first = false;
}
return line;
}
extract_macd( header, macd_obj, index )
{
let macd = macd_obj;
let i = index;
let line = "";
let is_first = true;
for( let macd_prefix of ["1", "5", "30"] )
for( let feature of ["ema12", "ema26", "macd", "signal", "hist"] )
if( header.is_feature_in_header( "macd" + macd_prefix + "_" + feature ) )
{
// console.log( macd_prefix, " ", feature, ", ", i );
if( !is_first )
line += ", ";
line += `${macd[ macd_prefix ][ feature ][ i ].y }`
is_first = false;
}
// let line = `${macd["1"]["ema12"][i].y}, ${macd["1"]["ema26"][i].y}, ${macd["1"]["macd"][i].y}, ${macd["1"]["signal"][i].y}, ${macd["1"]["hist"][i].y}`;
// line += `, ${macd["5"]["ema12"][i].y}, ${macd["5"]["ema26"][i].y}, ${macd["5"]["macd"][i].y}, ${macd["5"]["signal"][i].y}, ${macd["5"]["hist"][i].y}`;
// line += `, ${macd["30"]["ema12"][i].y}, ${macd["30"]["ema26"][i].y}, ${macd["30"]["macd"][i].y}, ${macd["30"]["signal"][i].y}, ${macd["30"]["hist"][i].y}`;
return line;
}
calculate_label( future_close, entry )
{
var next_close_dir = future_close < entry[4] ? -1 : 1;
var next_close_bps = this.num2bps( future_close, entry[1] );
var label_line = `${future_close}, ${next_close_dir}, ${next_close_bps}`;
return label_line;
}
num2bps( val, base)
{
return ( (100 / base * val) - 100 ) * 100;
}
calculate_macd( dps, counts )
{
let macd_list = {};
for( let base_count of counts)
{
let ema12 = this.calculateEMA( dps, base_count * 12 );
let ema26 = this.calculateEMA( dps, base_count * 26 );
let macd = [], hist =[];
for(var i = 0; i < ema12.length; i++)
macd.push({x: ema12[i].x, y: (ema12[i].y - ema26[i].y)});
var ema9 = this.calculateEMA(macd, base_count * 9);
for(var i = 0; i < ema12.length; i++)
{
let score = (macd[i].y - ema9[i].y);
hist.push({x: ema12[i].x, y: score });
}
macd_list[base_count] = {"ema12":ema12, "ema26":ema26, "macd":macd, "signal":ema9, "hist":hist };
}
return macd_list;
}
calculateEMA(dps,count) {
var k = 2/(count + 1);
var emaDps = [{x: dps[0].x, y: dps[0].y.length ? dps[0].y[3] : dps[0].y}];
for (var i = 1; i < dps.length; i++) {
emaDps.push({x: dps[i].x, y: (dps[i].y.length ? dps[i].y[3] : dps[i].y) * k + emaDps[i - 1].y * (1 - k)});
}
return emaDps;
}
async load_data( start_date, end_date )
{
let prices = [];
var start = new Date(start_date);
var end = new Date(end_date);
var loop = new Date(start);
while(loop <= end)
// while( start_timestamp < end_timestamp )
{
let datestamp = (new Date(loop).getYear()-100).toString() +
(new Date(loop).getMonth()+1).toString().padStart(2, "0") +
(new Date(loop).getDate()).toString().padStart(2, "0");
var filename = "./datasets/data_binance_raw_"+datestamp+".json";
// console.log( "datestamp:", datestamp, filename );
let json_str = await fs.readFile( filename, "utf8"); //why are new-line chars not removed!!????1
// console.log( json_str );
let json_obj = JSON.parse( json_str ); //.slice(3, 5);
prices = prices.concat(json_obj);
// start_timestamp += 1000 * 3600 * 24;
var newDate = loop.setDate(loop.getDate() + 1);
loop = new Date(newDate);
}
// console.log( prices );
return prices;
}
}
class BinanceApi
{
constructor()
{
}
async get_latest()
{
let api_url = "https://fapi.binance.com/fapi/v1/continuousKlines";
// console.log( new Date(start_timestamp) );
let start_timestamp = Date.now() - config.BINANCE_PRED_FETCH_SIZE;
let end_timestamp = start_timestamp + config.BINANCE_PRED_FETCH_SIZE;
console.log( start_timestamp, end_timestamp );
let params = {"pair":"btcusdt", "interval":"1m", "contractType":"PERPETUAL",
"startTime": start_timestamp, "endTime": end_timestamp };
let prices = await this.fetch_url( api_url, params )
return prices;
}
async get_data( start_timestamp, end_timestamp )
{
// https://fapi.binance.com/fapi/v1/continuousKlines?pair=btcusdt&interval=5m&contractType=PERPETUAL
let api_url = "https://fapi.binance.com/fapi/v1/continuousKlines";
let prices = [];
// the API gets only ~8h of data per call, so we loop over
// let end_timestamp = Math.floor(Date.now());
// let start_timestamp = end_timestamp - config.DEFAULT_CHART_LENGTH; //24h
// console.log( start_timestamp, "<", end_timestamp);
while( start_timestamp < end_timestamp )
{
// console.log( new Date(start_timestamp) );
let chunk_end_timestamp = start_timestamp + config.BINANCE_FETCH_SIZE;
// console.log( start_timestamp + config.BINANCE_FETCH_SIZE, end_timestamp );
let params = {"pair":"btcusdt", "interval":"1m", "contractType":"PERPETUAL",
"startTime": start_timestamp, "endTime": chunk_end_timestamp };
let new_prices = await this.fetch_url( api_url, params )
prices = [...prices, ...new_prices ];
start_timestamp += config.BINANCE_FETCH_SIZE;
}
return prices;
}
async fetch_url( url, query = null )
{
console.log( url, query );
return await superagent.get( url )
.query( query )
.then( data => { return (data.body); } )
.catch( (err) => {
console.log("Too many requests to the API?");
console.log(err);
resolve(false);
});
console.log("returning true from get_data");
// if( query )
// {
// if( url.slice(-1) != "?" )
// url += "?";
// for( let key in query )
// {
// url += key + "=" + query[key] + "&";
// }
// }
// // console.log("fetching", url);
// return await fetch( url )
// .then(response => response.json())
// .then(data => {
// return data;
// });
}
}
class PricePredictor
{
constructor( web_api, dataset )
{
this.web_api = web_api;
this.dataset = dataset;
}
async pred()
{
// get last ~20 frames from Binance
let prices = await this.web_api.get_latest();
let winsize = 10;
let whitelist = ["open", "close", "vol", "num_of_trades", "macd1_hist", "macd1_macd", "macd1_signal", "macd5_hist", "macd5_macd", "macd5_signal"];
// format it using dataset class calls to get a single sample point
let csv_obj = await this.dataset.json2csv( prices, winsize, whitelist, true )
// console.log( csv_obj["sample_data"].slice(-1) );
// then call our pred model
// get model details -> published model.meta file ???!!!!!!
let mode = "pred";
let model = "model_210205.xgboost";
let ppv = 0.636; //fake value we should be pulling from model meta file
let metric = "bps60+10";
const pred = spawnSync("./PriceRunner", [ mode, model, csv_obj["sample_data"].slice(-1) ]);
let output = String(pred.stdout);
let score = output.split("\n")[3].slice(5,-1)
// console.log( );
return { "model":model, "ppv":ppv, "pred":score, "metric":metric };
}
}
class WhaleAlertApi
{
// timestamp => date "+%s"
// https://api.whale-alert.io/v1/transactions?api_key=oCAcALPSl98tCbEnzMuq2n0gwbYPClZy&start=1609437963
constructor()
{
// TODO - move out to some out-of-source env config tool for keeping track of keys - AWS <something>?
this.api_key = "oCAcALPSl98tCbEnzMuq2n0gwbYPClZy";
}
async get_data( params )
{
return new Promise(resolve => {
// let start_timestamp = Math.floor(Date.now()/1000) - 3540 ; // last 59min
let start_timestamp = Math.floor(Date.now()/1000) - config.DEFAULT_UPDATE_DB_FETCH_RANGE ; // last 5min
let url = "https://api.whale-alert.io/v1/transactions?";
// https://api.whale-alert.io/v1/transactions?api_key=oCAcALPSl98tCbEnzMuq2n0gwbYPClZy&start=1609806534
// let api_key = "oCAcALPSl98tCbEnzMuq2n0gwbYPClZy";
// let m_url = url + "api_key="+this.api_key+"&start="+start_timestamp;
// we are going to roll our own proxy in php - like 5 lines of code :D - I forget how easy php is
// let f_url = "https://ixspeed.com/proxy.php?"+m_url;
let query = { "api_key": this.api_key, "start": start_timestamp };
if( "cursor" in params )
query["cursor"] = params.cursor;
console.log( url, query, params );
superagent.get( url )
.query( query )
// .then( data => { /*console.log( data.body );*/ return callback_func( data.body ) } )
.then( data => { resolve(data.body); } )
.catch( (err) => {
console.log("Too many requests to the API?");
console.log(err);
resolve(false);
});
console.log("returning true from get_data");
});
}
// assume 1m freq
bucket_data_timeseries( {data:data, do_allow_cross_unknown_flow} )
{
let buckets = {};
let min = 0;
let max = 0;
for( const item of data.Items )
{
if( ! this._is_entry_supported({ item:item, do_allow_cross_unknown_flow:do_allow_cross_unknown_flow})) continue;
let amount_usd = item.amount_usd;
let timestamp = item.timestamp;
let from = item.from.owner_type;
if( from != "unknown" ) from = item.from.owner;
let to = item.to.owner_type;
if( to != "unknown" ) to = item.to.owner;
// floor to nearest minute
let time_bucket = Math.floor(timestamp / 60) * 60 * 1000;
if( ! buckets[time_bucket] )
buckets[time_bucket] = { "inflow":0, "outflow":0, "intraflow":0, "transaction_count":0 };
// console.log( timestamp, "=>", time_bucket, ":", symbol, from, "=>", to, amount_usd.toFixed(0) );
if( from == "unknown" )
buckets[time_bucket]["inflow"] += amount_usd;
else if( to == "unknown" )
buckets[time_bucket]["outflow"] += amount_usd;
else
buckets[time_bucket]["intraflow"] += amount_usd;
buckets[time_bucket]["transaction_count"]++;
if( min == 0 || time_bucket < min )
min = time_bucket;
if( max == 0 || time_bucket > max )
max = time_bucket;
}
// console.log( buckets, min, max );
let timeseries = [];
for( let timestamp = min; timestamp <= max; timestamp+=60000 )
{
let inflow = 0;
let outflow = 0;
if( timestamp in buckets )
{
inflow = buckets[timestamp]["inflow"];
outflow = buckets[timestamp]["outflow"];
timeseries.push({ "timestamp":timestamp, "transaction_count": buckets[timestamp]["transaction_count"],
"inflow":inflow, "outflow":outflow,
"vol": inflow + outflow , "net": inflow - outflow });
}
}
// console.log( timeseries );
return timeseries;
}
_is_entry_supported( {item, do_allow_cross_unknown_flow = null} )
{
if( item.symbol != "btc" )
return false;
// filter out transfers between same known owner
if( item.from.owner_type != "unknown" && item.from.owner_type == item.to.owner_type )
return false;
// filter out unknown to unknown for now -> not sure how to use them yet
if( !do_allow_cross_unknown_flow && item.from.owner_type == "unknown" && item.to.owner_type == "unknown" )
return false;
return true
}
bucket_data_exchange( data )
{
let buckets = {};
let exchange_inflow = 0;
let exchange_outflow = 0;
let exchange_intraflow = 0;
// console.log( data );
// console.log("cursor:", data.cursor);
// console.log("count:", data.count);
// for( const item of data.transactions )
for( const item of data.Items )
{
// console.log( item );
// assume only btc for now - others get more complex with symbols, etc
if( ! this._is_entry_supported( {item:item} )) continue;
let symbol = item.symbol;
let amount_usd = item.amount_usd;
let timestamp = item.timestamp;
let from = item.from.owner_type;
if( from != "unknown" ) from = item.from.owner;
let to = item.to.owner_type;
if( to != "unknown" ) to = item.to.owner;
// console.log( timestamp, ":", symbol, from, "=>", to, amount_usd.toFixed(0) );
let exchange = "from " + from;
let target = "to " + to;
// new target-exchange mapping
if( ! target in buckets || ! buckets[ target ] )
buckets[ target ] = {};
if( ! exchange in buckets[target] || ! buckets[target][exchange] )
buckets[target][exchange] = 0;
buckets[target][exchange] += amount_usd;
// handle cases where we transfer from exchange A to B -> we should count this twice?
if( from == "unknown" )
exchange_inflow += amount_usd;
else if( to == "unknown" )
exchange_outflow += amount_usd;
else
exchange_intraflow += amount_usd;
}
// need to convert into a nice array for the front-end table
// and for the chart we split the data into an x and y array
let sorted_keys = Object.keys( buckets ).sort();
let sorted_totals = [];
let chart_data = [];
sorted_keys.forEach( (item, index) => {
let sorted_tos = Object.keys( buckets[ item ] ).sort();
let entry = [];
let chart_entry = { x:[], y:[], name:item, type:"bar" };
sorted_tos.forEach( (to, index) => {
entry.push( {"target": to, "amount": buckets[item][to] } );
chart_entry.x.push( to );
chart_entry.y.push( buckets[item][to] );
});
sorted_totals.push( {"bucket":item, "totals":entry } );
chart_data.push( chart_entry );
});
let num_formatter = new Intl.NumberFormat('en-EN', { maximumFractionDigits: 0 });
return {buckets:buckets, sort_order: sorted_keys,
inflow: exchange_inflow,
outflow: exchange_outflow,
intraflow: exchange_intraflow,
api_cursor:data.cursor, api_count:data.count,
sorted_totals: sorted_totals, chart_data:chart_data };
}
}
class DBStore{