-
Notifications
You must be signed in to change notification settings - Fork 9
/
Tick.js
88 lines (56 loc) · 2.43 KB
/
Tick.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
const EventEmitter = require('./EventEmitter');
const fs = require('fs');
const path = require('path');
const nlines = require('n-readlines');
// BitMEX trade csv columns
const TIMESTAMP=0, SYMBOL=1, SIDE=2, SIZE=3, PRICE=4;
const TICK_DIR=5, TRADE_ID=6, GROSS_VAL=7, HOME_NOT=8, FOREIGN_NOT=9;
class Tick extends EventEmitter {
constructor( options ) {
super();
this.index = -1;
this.symbol = '';
this.path = options.files;
this.reader = null;
this.files = fs.readdirSync( options.files );
// Get CSV files only, exclude zipped copies and sort the files assuming correctly dated filenames as provided by BitMEX
this.files = ( this.files.filter( f => f.includes('.csv') && !f.includes('.csv.gz') ) ).sort();
this.lastemit = null;
this.nozeroes = options.nozeroes;
}
_next( ) {
//// ? new nlines( `${this.path}/${this.files[ ++this.index ]}` )
return this.index < this.files.length-1
? new nlines( path.join(this.path, this.files[ ++this.index ]))
: false;
}
get _isfinal() { return this.index == this.files.length - 1; }
start( symbol ) {
let line;
// Iterate all files in the directory
while ( this.reader = this._next() ) {
// Iterate all lines in the file
while (line = this.reader.next()) {
let text = line.toString('ascii');
if ( text.includes( symbol )) {
let cells = text.split(',');
let em = {
timestamp: new Date( Date.parse( cells[ TIMESTAMP ].replace('D', 'T') + 'Z' )), // 'D'/'T' Don't ask me why, I have no idea
symbol: cells[ SYMBOL ],
price: Number( cells[ PRICE ] ),
side: cells[ SIDE ],
tick: cells[ TICK_DIR ],
size: Number( cells[ SIZE ] )
}
if ( em.tick.includes('Zero') && this.nozeroes ) {
// Do nothing, logic is easier for my chimp brain this way.
} else {
this.emit( 'tick', em );
}
}
}
}
this.emit('eof');
}
}
module.exports = Tick;