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
/
MinuteIndex.js
153 lines (119 loc) · 3.3 KB
/
MinuteIndex.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
var
util = require('util'),
compress = require('compress-buffer').compress,
uncompress = require('compress-buffer').uncompress;
/**
Internal class used by TickStorage to index minute data within a single ticks file.
About the only thing you may be interested in here is the <code>.index[]</code> array
which contains an array of minutes, from 0 to 1440-1. Each entry is a hash with the
following keys:
* **o**: position of the first tick in this minute (see <code>TickStorage.position</code>);
* **c**: position of the last tick in this minute;
* **v**: total volume in this minute;
* **h**: high for this minute;
* **l**: low for this minute;
For aftermarket minutes only the <code>o</code> and <code>c</code> are stored with <code>v</code> set to <code>0</code>
and <code>h</code> and <code>l</code> set to <code>null</code>.
If there were no data at all for a certain minute within the day, then <code>index[minute]</code> will be <code>null</code>.
You can access the instance of Minute Index as <code>TickStorage.minuteIndex[]</code>. So use
<code>TickStorage.minuteIndex.index[minute]</code> to get to the minute data.
*/
function MinuteIndex() {
this.index = [];
this.resetIndex();
this._startUnixtime = null;
}
/**
@private
*/
MinuteIndex.prototype.resetIndex = function() {
var m;
for(m=0;m<=1440;m++) {
this.index[m]=null;
}
};
/**
@private
*/
MinuteIndex.prototype.setStartUnixtime = function(unixtime) {
this._startUnixtime = unixtime;
};
/**
Handy developer tool: dump minute index in human-readable format.
@param {Integer} fromMinute begin dump since this minute, default = 0;
@param {Integer} toMinute end the dump with this minue, default = 1440-1.
*/
MinuteIndex.prototype.dump = function(fromMinute, toMinute) {
fromMinute = fromMinute || 0;
toMinute = toMinute || 1440-1;
var i;
for(i=fromMinute;i<=toMinute;i++) {
util.debug(util.format("%d: %s", i, util.inspect(this.index[i])));
}
};
/**
@private
*/
MinuteIndex.prototype.addTick = function(position, unixtime, volume, price, isMarket) {
var minute = Math.floor((unixtime-this._startUnixtime)/60);
if (minute>=1440 || minute<0) {
throw new Error(util.format("Cannot add minute %d", minute));
}
if (position==null) {
return;
}
if (this.index[minute]===null) {
this.index[minute]={
o: position,
c: position,
v: 0,
h: null,
l: null
};
// set ending position
} else {
this.index[minute].c = position;
}
// we shall ignore aftermarket prices for the price index.
if (!isMarket) {
return;
}
this.index[minute].v+=volume;
if (this.index[minute].h === null) {
this.index[minute].h = price;
} else {
this.index[minute].h = Math.max(this.index[minute].h, price);
}
if (this.index[minute].l === null) {
this.index[minute].l = price;
} else {
this.index[minute].l = Math.min(this.index[minute].l, price);
}
};
/**
@private
*/
MinuteIndex.prototype.toGzip = function() {
return compress(new Buffer(JSON.stringify(this.index)));
};
/**
@private
*/
MinuteIndex.prototype.fromGzip = function(buffer) {
var uncompressed;
try {
uncompressed = uncompress(buffer);
} catch (e) {
return false;
}
if (!uncompressed) {
return false;
}
try {
this.index = JSON.parse(uncompressed.toString());
} catch (ee) {
return false;
}
return true;
};
module.exports = MinuteIndex;