This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathTickStorage.js
690 lines (512 loc) · 16.6 KB
/
TickStorage.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
var
util = require('util'),
fs = require('fs'),
path = require('path'),
events = require('events'),
compress = require('compress-buffer').compress,
uncompress = require('compress-buffer').uncompress,
MinuteIndex = require('./MinuteIndex');
require('./ExtraDate');
/**
Raw ticks storage.
Ticks are stored on disk and in memory in a highly-efficient, extremely compact and fast format. Each trading
day is stored in a separate file named with a plain daystamp and ".ticks" extension.
Each file starts with a single line JSON header, which is handy to lookup in shell:
head -1 20110801.ticks
The file path is composed of all three parameters: dbPath/symbol/daystamp.ticks.
@param {String} dbPath path to ticks database;
@param {String} symbol symbol to load;
@param {String} daystamp day to load or create.
Typical read example:
var tickStorage = new TickStorage('/home/tickers', 'AAPL', 20111019);
if (!tickStorage.load()) {
console.log("Oops?");
}
var tick;
while ((tick=tickStorage.nextTick())) {
if (!tick.isMarket) {
continue;
}
ExtraLog.log("%T: %d @ %p",
tick.unixtime, // integer trade unixtime
tick.volume, // integer trade volume
tick.price // integer tick price
);
}
Typical create/write example:
var tickStorage = new TickStorage('/home/tickers', 'AAPL', 20111019);
tickStorage.prepareForNew();
tickStorage.addTick(unixtime, 100, 233700, true);
tickStorage.addTick(unixtime, 100, 233700, true);
...
if (!tickStorage.save()) {
console.log("Oops?!");
}
**Note:** you cannot reuse TickStorage that was just created and stored. TickStorage instance is not
read/write so if you have just created a tick file, you will have to reload it from disk in order to read
it. Something like that:
...
if (!tickStorage.save()) {
console.log("Oops?!");
}
tickStorage = new TickStorage('/home/tickers', 'AAPL', 20111019);
if (!tickStorage.load()) {
console.log("Oops?!");
}
// now you can read!
Each **tick entry** is a hash that consists of:
* **isMarket** boolean, true for market ticks and false for aftermarket;
* **unixtime** integer, cannot be zero or null;
* **volume** integer, can be zero in rare cases the stock exchange sent us such a tick;
* **price** integer, can be zero in rare cases the stock exchange sent us such a tick;
*/
function TickStorage(dbPath, symbol, daystamp) {
this._dbPath = dbPath;
this._symbol = symbol.toUpperCase();
this._daystamp = (daystamp || '');
var day = Date.parseDaystamp(this._daystamp);
this._startUnixtime = day.unixtime();
this._endUnixtime = this._startUnixtime+86400;
this._path = dbPath+'/'+symbol+'/';
this._filename = this._daystamp+'.ticks';
this._bufferData = null;
this.minuteIndex = null;
this._lastUnixtime=null;
this.position=0;
this.count=0;
this.additionalData={};
this._orphanTicks=[];
this.marketTimeOnly = false;
this.nextTick = this._nextTickAll;
}
TickStorage.HEADER_SIZE=1024;
TickStorage.ENTRY_SIZE=13;
TickStorage.CURRENT_VERSION=1;
TickStorage.SEEK_SET = 1;
TickStorage.SEEK_CUR = 2;
TickStorage.SEEK_END = 3;
/**
Filter out aftermarket time. After this call <code>TickStorage.nextTick()</code> will only return market ticks.
*/
TickStorage.prototype.filterMarketTime = function() {
this.marketTimeOnly = true;
if (this.position < this.marketOpenPos) {
this.position = this.marketOpenPos;
}
this.nextTick = this._nextTickMarket;
};
/**
Returns symbol name.
@return {String}
*/
TickStorage.prototype.getSymbol = function() {
return this._symbol;
};
/**
Return true if this file exists in tick database.
@return {Boolean}
*/
TickStorage.prototype.exists = function() {
return fs.existsSync(this._path+this._filename);
};
/**
Will return the day minute to which the current position points to.
@return {Integer} day minute.
*/
TickStorage.prototype.tellMinute = function() {
var _m=null,m;
for (m=0;m<1440;m++) {
if (this.minuteIndex.index[m] && this.position>=this.minuteIndex.index[m].o ) {
_m = m;
}
}
return _m;
};
/**
Seek to the first tick of that day minute.
@param {Integer} minute day minute to seek to.
@return {Boolean} true if we could successfully seek to that minute (i.e. if it exists in tick data); false otherwise
*/
TickStorage.prototype.seekToMinute = function(minute) {
var m = this.minuteIndex.index[minute];
if (m) {
this.position = m.o;
return true;
} else {
return false;
}
};
/**
Seek to the first tick.
@return {Integer} <code>null</code> if we couldn't seek to it; new position otherwise.
*/
TickStorage.prototype.rewind = function() {
return this.seek(0, TickStorage.SEEK_SET);
};
/**
Seek to the position in ticks.
@param {Integer} ticks ticks to seek to, can be negative depending on <code>whence</code>;
@param whence how to seek to. Think unix's <code>fseek()</code> call. Whence can be one of: <code>TickStorage.SEEK\_SET</code>, <code>TickStorage.SEEK\_CUR</code>, <code>TickStorage.SEEK\_END</code>.
If <code>whence</code> is <code>TickStorage.SEEK\_SET</code>, then the <code>ticks</code> parameter is relative to the
beginning of file (and should not be negative). If <code>whence</code> is <code>TickStorage.SEEK\_CUR</code>,
then the <code>ticks</code> parameter is relative to the current position. If <code>whence</code> is
<code>TickStorage.SEEK\_END</code>, then the <code>ticks</code> parameter is relative to the last position (and should not
be positive).
This method is smart, it won't let you seek past the beginning or the end of file.
@return {Integer} new position or <code>null</code> if we couldn't seek.
*/
TickStorage.prototype.seek = function(ticks, whence) {
if (ticks===undefined || !whence) {
return null;
}
var newPosition=null;
switch (whence) {
case TickStorage.SEEK_SET:
newPosition=ticks;
break;
case TickStorage.SEEK_CUR:
newPosition=this.position+ticks;
break;
case TickStorage.SEEK_END:
newPosition=this.count-1+ticks;
break;
default:
return null;
break;
}
if (newPosition<0) {
newPosition=0;
}
if (newPosition>=this.count) {
newPosition = this.count-1;
}
this.position = newPosition;
return this.position;
};
TickStorage.prototype._possiblyCreatePath = function() {
if (!fs.existsSync(this._path)) {
try {
fs.mkdirSync(this._path, 0755);
} catch (e) {
return false;
}
}
return true;
};
/**
Save newly created tick file.
@param {Boolean} quick if set then no index will be regenerated. **Use with care.** It's actually only needed in certain cases if you are rebuilding already existing ticks file.
@return {Boolean} true if save was successful, guess what otherwise.
**Note:** you cannot reuse newly saved TickStorage, you have to reload it.
*/
TickStorage.prototype.save = function(quick) {
if (!quick) {
this._orphanTicks.forEach(function(tick) {
if (tick) {
this.addTick(tick.unixtime, tick.volume, tick.price, tick.isMarket, true);
}
}, this);
this._orphanTicks=[];
this._generateMinuteIndex();
}
var bufferMinuteIndex = this.minuteIndex.toGzip();
var header = {
version: TickStorage.CURRENT_VERSION,
symbol: this._symbol,
daystamp: this._daystamp.toString(),
countOfEntries: this.count || 0,
marketOpenPos: this.marketOpenPos,
marketClosePos: this.marketClosePos,
additionalData: this.additionalData,
minuteIndexSize: bufferMinuteIndex.length
};
if (this._bufferData) {
if (!this._possiblyCreatePath()) {
return false;
}
var fd = fs.openSync(this._path+this._filename+".tmp", "w");
var bufferHeader;
if (this.count>0) {
var bytesLength = this.count*TickStorage.ENTRY_SIZE;
var bufferCompressed = compress(this._bufferData.slice(0, bytesLength));
bufferHeader = this._generateHeader(header);
var targetBuffer = new Buffer(bufferHeader.length + bufferMinuteIndex.length + bufferCompressed.length);
bufferHeader.copy(targetBuffer);
bufferMinuteIndex.copy(targetBuffer, bufferHeader.length);
bufferCompressed.copy(targetBuffer, bufferHeader.length + bufferMinuteIndex.length);
fs.writeSync(fd, targetBuffer, 0, targetBuffer.length);
} else {
// despite that it exists it's not stored, so do not confuse
// any software with it's fake size
header.minuteIndexSize = 0;
bufferHeader = this._generateHeader(header);
fs.writeSync(fd, bufferHeader, 0, bufferHeader.length);
}
fs.closeSync(fd);
if (fs.existsSync(this._path+this._filename)) {
try {
fs.unlinkSync(this._path+this._filename);
} catch (e) {
return false;
}
}
try {
fs.renameSync(this._path+this._filename+".tmp", this._path+this._filename);
} catch (ee) {
return false;
}
}
return true;
};
TickStorage.prototype._generateHeader = function(header) {
var bufHeader = new Buffer(TickStorage.HEADER_SIZE);
bufHeader.fill(0);
bufHeader.write(JSON.stringify(header)+"\n",0,'ascii');
return bufHeader;
};
/**
Remove ticks file. Will not return anything and won't complain on errors.
*/
TickStorage.prototype.remove = function() {
try {
fs.unlinkSync(this._path+this._filename);
} catch(e) {
}
};
/**
Load ticks file.
@return {Boolean} true in case of success, false otherwise.
*/
TickStorage.prototype.load = function() {
this.position=0;
this.count=0;
this._bufferData=null;
this.minuteIndex = new MinuteIndex();
var fd;
try {
fd = fs.openSync(this._path+this._filename, "r");
} catch (e) {
return false;
}
var stats = fs.fstatSync(fd);
var headerValues = this._loadHeader(fd);
if (!headerValues) {
return false;
}
var headerAndMinuteIndexLength = TickStorage.HEADER_SIZE + headerValues.minuteIndexSize;
if (this.count==0) {
return true;
}
var minuteIndexBuffer = this._loadMinuteIndex(fd, headerValues.minuteIndexSize);
if (!this.minuteIndex.fromGzip(minuteIndexBuffer)) {
return false;
}
var bufferLength = stats.size - headerAndMinuteIndexLength;
var buffer = new Buffer(bufferLength);
buffer.fill(0);
fs.readSync(fd, buffer, 0, buffer.length, headerAndMinuteIndexLength);
fs.closeSync(fd);
try {
this._bufferData = uncompress(buffer);
} catch (ee) {
return false;
}
delete buffer;
return this._bufferData ? true : false;
};
TickStorage.prototype._loadMinuteIndex = function(fd, minuteIndexSize) {
var compressedMinuteIndex = new Buffer(minuteIndexSize);
fs.readSync(fd, compressedMinuteIndex, 0, minuteIndexSize, TickStorage.HEADER_SIZE);
return compressedMinuteIndex;
};
TickStorage.prototype._loadHeader = function(fd) {
try {
var headerString = fs.readSync(fd, TickStorage.HEADER_SIZE, 0, 'ascii');
headerString = headerString.toString().split("\n")[0];
var headerData = JSON.parse(headerString);
if (parseInt(headerData.version)>TickStorage.CURRENT_VERSION) {
return null;
}
this.count = headerData.countOfEntries;
this.marketOpenPos = headerData.marketOpenPos;
this.marketClosePos = headerData.marketClosePos;
this.additionalData = headerData.additionalData;
return {
uncompressed: headerData.uncompressed || false,
minuteIndexSize: headerData.minuteIndexSize
};
} catch (e) {
return null;
}
};
/**
Prepare TickStorage for creation of a new ticks file. You must only call this method on a fresh TickStorage instance.
@params {Integer} megs how many megabytes of RAM to preallocate for tick storage. Each tick takes 13 bytes, so allocate wisely. By default it's 100MB which should be enough for everyone.
*/
TickStorage.prototype.prepareForNew = function(megs) {
megs = parseInt(megs) || 100;
this._bufferData = new Buffer(1024*1024*megs);
// we explicitly don't fill because of two reasons:
// a) when we save, we splice() only the part with real data
// b) it slows down this method considerably.
// this._bufferData.fill(0);
this.minuteIndex = new MinuteIndex();
this.minuteIndex.setStartUnixtime(this._startUnixtime);
this._lastUnixtime=0;
this.position = 0;
this.count=0;
this.marketOpenPos = null;
this.marketClosePos = null;
this.additionalData={};
this._orphanTicks=[];
};
/**
Add another brick in the wall.
@param {Integer} unixtime unixtime;
@param {Integer} volume volume;
@param {Integer} price price;
@param {Boolean} isMarket true for market ticks, false or undefined for aftermarket;
@param {Boolean} disableOrphanLogic only set to true in case of terrorist attack.
"Orphan logic" is prevention against weird ticks from the distant past or distant future. It also filters out
duplicate ticks from the distant past or future. Orphan ticks are moved into their respective positions in ticks
history, making it consistent. Actually never disable this logic unless you are doing conversion
of ticks database from one format to another.
*/
TickStorage.prototype.addTick = function(unixtime, volume, price, isMarket, disableOrphanLogic) {
if (unixtime<this._startUnixtime || unixtime>=this._endUnixtime) {
return;
}
// zero price is fine (volume correction ticks), but negative is not.
if (volume<=0 || price<0) {
return;
}
if (!this._lastUnixtime) {
this._lastUnixtime = unixtime;
}
if (!disableOrphanLogic && unixtime < this._lastUnixtime-600) {
var found = this._findPositionOfPreviousTickWithCloseUnixtime(unixtime), z;
if (found!=null) {
for(z=found+1;z<this.position;z++) {
this._orphanTicks.push(this.tickAtPosition(z));
}
// this.dumpOrphanTicks();
this._compressAdditionalTicks();
this.position = found+1;
this.count = this.position;
}
}
this._lastUnixtime = unixtime;
if (!disableOrphanLogic) {
this._possiblyAppendAdditionalTicks();
}
var offset = this.position * TickStorage.ENTRY_SIZE;
this._bufferData.writeUInt32LE(unixtime, offset);
this._bufferData.writeUInt32LE(volume, offset+4);
this._bufferData.writeUInt32LE(price, offset+8);
this._bufferData.writeUInt8(isMarket?1:0, offset+12);
if (isMarket) {
if (this.marketOpenPos===null) {
this.marketOpenPos = this.position;
}
this.marketClosePos = this.position;
}
this.position++;
this.count++;
};
TickStorage.prototype._possiblyAppendAdditionalTicks = function() {
var tick, p;
for (p=0;p<this._orphanTicks.length;p++) {
tick = this._orphanTicks[p];
if (tick && tick.unixtime<this._lastUnixtime) {
this.addTick(tick.unixtime, tick.volume, tick.price, tick.isMarket, true);
this._orphanTicks[p] = null;
}
}
};
TickStorage.prototype._findPositionOfPreviousTickWithCloseUnixtime = function(unixtime) {
var _tick, pos;
for (pos=this.position-1;pos>=this.position-5;pos--) {
_tick = this.tickAtPosition(pos);
if (_tick && Math.abs(_tick.unixtime - unixtime) < 10) {
return pos;
}
}
return null;
};
TickStorage.prototype._generateMinuteIndex = function() {
var tick, pos;
this.minuteIndex.resetIndex();
for (pos=0;pos<this.count;pos++) {
tick = this.tickAtPosition(pos);
this.minuteIndex.addTick(pos, tick.unixtime, tick.volume, tick.price, tick.isMarket);
}
};
/**
Internal development tool: dumps current orphan ticks in buffer.
@private
*/
TickStorage.prototype.dumpOrphanTicks = function() {
this._orphanTicks.forEach(function(entry) {
if (entry) {
console.log(
"%s, %d @ %s is out of order",
Date.parseUnixtime(entry.unixtime).toFormat('HH24:MI:SS'), entry.volume, entry.price
);
}
}, this);
};
TickStorage.prototype._compressAdditionalTicks = function() {
var lastEntry = JSON.stringify(this._orphanTicks[0]);
var clearPool=[this._orphanTicks[0]];
this._orphanTicks.forEach(function(entry) {
if (JSON.stringify(entry) != lastEntry) {
clearPool.push(entry);
}
lastEntry = JSON.stringify(entry);
}, this);
this._orphanTicks = clearPool;
};
/**
Return tick entry at position.
@param {Integer} position position.
@return Tick entry or null.
*/
TickStorage.prototype.tickAtPosition = function(position) {
if (position>=this.count) {
return null;
}
return this._tickAtOffset(position*TickStorage.ENTRY_SIZE);
};
TickStorage.prototype._tickAtOffset = function(offset) {
if (!this._bufferData || offset>=this._bufferData.length || offset < 0) {
return null;
}
return {
unixtime: this._bufferData.readUInt32LE(offset),
volume: this._bufferData.readUInt32LE(offset+4),
price: this._bufferData.readUInt32LE(offset+8),
isMarket: this._bufferData.readUInt8(offset+12) == 1
};
};
TickStorage.prototype._nextTickAll = function() {
return this._tickAtOffset(this.position++*TickStorage.ENTRY_SIZE);
};
TickStorage.prototype._nextTickMarket = function() {
var tick;
do {
tick = this._nextTickAll();
} while (tick && !tick.isMarket && this.position<=this.marketClosePos);
if (tick && !tick.isMarket) {
return null;
}
return tick;
};
/**
An iterator. Get next tick entry or null.
@return tick entry or null.
*/
// trick doc.js:
/*
TickStorage.prototype.nextTick = function() {
*/
module.exports = TickStorage;