forked from egorFiNE/node-stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CandlesCalculator.js
291 lines (218 loc) · 7.05 KB
/
CandlesCalculator.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
/*jslint devel: false, node: true, sloppy: true, eqeq: true, es5: true, vars: true, white: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
var
util = require('util'),
TickStorage = require('./TickStorage');
/**
Candles calculator from data supplied by TickStorage.
Every candle is an <code>Object</code> (read - a hash array) like this:
{ h: 261000, l: 261000, o: 261000, c: 261000, v: 100, t: 1, hour: 10, minute: 0, m: 600 }
* <code>h</code> - High
* <code>l</code> - Low
* <code>o</code> - Open
* <code>c</code> - Close
* <code>v</code> - Volume
* <code>t</code> - Ticks count
* <code>m</code> - Candle day minute (<code>575</code> for 9:35)
* <code>hour</code> - Candle hour
* <code>minute</code> - Candle minute
High, low, open, close and volume are calculated only on market ticks. Ticks count is calculated using all ticks,
including aftermarket (doesn't matter, though - there are not so many aftermarket ticks during the trade session).
*Every candle is named after it's last minute time.* I.e. candle of minute 575 (9:35) is calculated over all the ticks
between 9:00:00 and 9:34:59 inclusive.
Example:
var tickStorage = new TickStorage(dbPath, 'AAPL', 20111117);
test.ok(tickStorage.load());
candlesCalculator = new CandlesCalculator(tickStorage, 5);
var candle = candlesCalculator.getCandle(600); // get candle for 9:55-10:00
console.log("Opened at %d", candle.o); // remember that prices are integer
@param {TickStorage} tickStorage TickStorage instance, must be already <code>load()</code>'ed.
@param {Integer} periodSizeInMinutes obvious. By default it's 1.
*/
function CandlesCalculator(tickStorage, periodSizeInMinutes) {
this.periodSize = periodSizeInMinutes || 1;
this._tickStorage = tickStorage;
this._calculatedMinuteIndex=[];
this.candles={};
this._calculateMinutes();
this._calculate();
}
CandlesCalculator.prototype._findOc = function(openPos, closePos) {
var i, tick;
var open=null, close=null, _openPos=null;
for (i=openPos;i<=closePos;i++) {
tick = this._tickStorage.tickAtPosition(i);
if (tick.isMarket) {
open=tick.price;
_openPos = i;
break;
}
}
if (!open) {
return null;
}
for (i=closePos;i>=_openPos;i--) {
tick = this._tickStorage.tickAtPosition(i);
if (tick.isMarket) {
close=tick.price;
break;
}
}
if (!close) {
return null;
}
return {
o: open,
c: close
};
};
/**
Debug tool: dump one-minute sized candles.
@param {Integer} from dump since this minute
@param {Integer} to dump till this minute
*/
CandlesCalculator.prototype.dumpMinutes = function(from, to) {
var i;
for (i=from;i<=to;i++) {
console.log("%d: %s", i, util.inspect(this._calculatedMinuteIndex[i]));
}
};
/**
Debug tool: dump calculated candles.
@param {Integer} from dump since this minute
@param {Integer} to dump till this minute
*/
CandlesCalculator.prototype.dumpCandles = function(from, to) {
var i;
for (i=from;i<=to;i+=this.periodSize) {
console.log("%d: %s", i, util.inspect(this.candles[i]));
}
};
/**
Get calculated candle.
@param {Integer} minute candle minute to get to. Always the last minute of a candle, so ask for "575" to get data for 9:30-9:35.
@return candle structure (see above) or null if there is no candle for this minute.
*/
CandlesCalculator.prototype.getCandle = function(minute) {
return this.candles[minute] || null;
};
CandlesCalculator.prototype._calculate = function() {
var i;
for(i=this.periodSize;i<=1440;i+=this.periodSize) {
this.candles[i] = this._calculatePeriod(i);
CandlesCalculator._setCandleHourMinute(this.candles[i], i);
}
};
CandlesCalculator.prototype._calculatePeriod = function(period) {
var open=0, close=0, high=Number.MIN_VALUE, low=Number.MAX_VALUE, volume=0, ticks=0;
var m;
for (m=period-this.periodSize;m<period;m++) {
var _minute = this._calculatedMinuteIndex[m];
if (!open) {
open = _minute.o;
close = _minute.c;
}
if (_minute.c) {
close = _minute.c;
high = Math.max(high, _minute.h);
low = Math.min(low, _minute.l);
volume+=_minute.v;
ticks+=_minute.t;
}
}
if (open) {
return {
h: high,
l: low,
o: open,
c: close,
v: volume,
t: ticks
};
} else {
return null;
}
};
CandlesCalculator.prototype._calculateMinutes = function() {
var _newMinuteIndex=[];
var m;
for (m=0;m<1440;m++) {
_newMinuteIndex[m]={
h: 0,
l: 0,
o: 0,
c: 0
};
var minute = this._tickStorage.minuteIndex.index[m];
if (minute) {
var oc = this._findOc(minute.o, minute.c);
if (oc) {
_newMinuteIndex[m].h=minute.h;
_newMinuteIndex[m].l=minute.l;
_newMinuteIndex[m].o=oc.o;
_newMinuteIndex[m].c=oc.c;
_newMinuteIndex[m].v=minute.v;
_newMinuteIndex[m].t=minute.c-minute.o+1;
}
}
}
this._calculatedMinuteIndex = _newMinuteIndex;
};
CandlesCalculator._setCandleHourMinute = function(candle, minute) {
if (candle) {
var d = new Date();
d.clearTime();
d.setCurrentDayMinute(minute);
candle.hour = d.getHours();
candle.minute = d.getMinutes();
candle.m = minute;
}
};
/**
Utility method: all of the above in a single call.
@param {TickStorage} tickStorage TickStorage that was already <code>load()</code>'ed.
@param {Integer} period Period size in minutes.
@param {Integer} from Get candles starting from this minute.
@param {Integer} to Get candles till this minute.
@return {Array} List of candles, zero-based, guaranteed to have each period.
For periods that have no data (there was no ticks in that period) the entry will only contain <code>hour</code>,
<code>minute</code> and <code>m</code> keys.
Example data:
[
{ h: 261000, l: 261000, o: 261000, c: 261000, v: 100, t: 1, hour: 10, minute: 0, m: 600 },
{ h: 261000, l: 261000, o: 261000, c: 261000, v: 200, t: 2, hour: 10, minute: 1, m: 601 },
{ hour: 10, minute: 2, m: 602 }, // no data for 602
{ h: 265000, l: 260000, o: 260000, c: 264100, v: 500, t: 3, hour: 10, minute: 3, m: 603 },
...
];
*/
CandlesCalculator.getCandlesOfTickStorage = function(tickStorage, period, from, to) {
var _result=[];
from = from||0;
to = to||1440;
var candles = new CandlesCalculator(tickStorage, period);
var i;
for(i=from+period;i<=to;i+=period) {
var candle = candles.getCandle(i) || {};
CandlesCalculator._setCandleHourMinute(candle, i);
_result.push(candle);
}
return _result;
};
/**
Utility method: just like <code>getCandlesOfTickStorage()</code>, but will also instantiate and load the TickStorage for you. See
<code>getCandlesOfTickStorage()</code> for description.
@param {String} dbPath Path to database (see TickStorage).
@param {String} symbol Symbol to load.
@param {Integer} daystamp Daystamp to load.
@param {Integer} period Period size in minutes.
@param {Integer} from Get candles starting from this minute.
@param {Integer} to Get candles till this minute.
*/
CandlesCalculator.getCandles = function(dbPath, symbol, daystamp, period, from, to) {
var tickStorage = new TickStorage(dbPath, symbol, daystamp);
if (!tickStorage.load()) {
return null;
}
return CandlesCalculator.getCandlesOfTickStorage(tickStorage, period, from, to);
};
module.exports = CandlesCalculator;