-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathactivetick.js
More file actions
342 lines (314 loc) · 7.81 KB
/
activetick.js
File metadata and controls
342 lines (314 loc) · 7.81 KB
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
var moment = require('moment');
var toolset = require('toolset');
var _ = require('underscore');
var progressbar = require('progress');
require('date-utils');
var activetick = function(options) {
// Options
this.options = _.extend({
host: '127.0.0.1',
port: 5000,
d: 'd',
h: 'h',
l: 'l',
c: 'c',
o: 'o',
v: 'v'
}, options);
// Query
this.query = {
symbol: 'AAPL',
range: {
from: moment(new Date()).subtract(7, 'days'),
to: new Date()
},
type: 0,
period: 5,
timeframe: '1H'
};
this.log = true; // Logging
return this;
}
// Symbol to fetch
activetick.prototype.open = function(symbol) {
this.query.symbol = symbol;
return this;
}
// Range: From
activetick.prototype.from = function(date) {
this.query.range.from = date;
return this;
}
// Range: To
activetick.prototype.to = function(date) {
if (date.getTime() > (new Date()).getTime()) {
date = new Date();
}
this.query.range.to = date;
return this;
}
// Type: Intraday, day, week
activetick.prototype.type = function(type) {
switch (type) {
case 0:
case "i":
case "intra":
case "intraday":
this.query.type = 0;
break;
case 1:
case "d":
case "day":
case "daily":
this.query.type = 1;
break;
case 2:
case "w":
case "week":
case "weekly":
this.query.type = 2;
break;
default:
toolset.error('Error: type()', 'Invalid value: '+type+'. Options: intraday, day, week.');
break;
}
return this;
}
// Period, for intraday only
activetick.prototype.period = function(period) {
this.query.period = period;
return this;
}
// Period, for intraday only
activetick.prototype.timeframe = function(timeframe) {
this.query.timeframe = timeframe;
return this;
}
// Period, for intraday only
activetick.prototype.baseurl = function(period) {
return 'http://'+this.options.host+':'+this.options.port+'/';
}
activetick.prototype.fetchCustom = function(callback) {
var scope = this;
toolset.file.read(this.queryUrl(), function(response) {
callback(scope.parseResponse(response));
});
}
activetick.prototype.queryUrl = function() {
var scope = this;
switch (this.query.type) {
case 0:
var url = this.baseurl()+'barData?symbol='+this.query.symbol+'&historyType='+this.query.type+'&intradayMinutes='+this.query.period+'&beginTime='+this.toActiveTickDate(this.query.range.from)+'&endTime='+this.toActiveTickDate(this.query.range.to)+'';
break;
default:
var url = this.baseurl()+'barData?symbol='+this.query.symbol+'&historyType='+this.query.type+'&beginTime='+this.toActiveTickDate(this.query.range.from)+'&endTime='+this.toActiveTickDate(this.query.range.to)+'';
break;
}
return url;
}
activetick.prototype.fetch = function(callback) {
var scope = this;
var timeframe;
switch (this.query.timeframe.toLowerCase()) {
case "weekly":
timeframe = {
type: 'weekly',
period: '',
limit: 365
};
break;
case "daily":
timeframe = {
type: 'daily',
period: '',
limit: 365
};
break;
case "12h":
timeframe = {
type: 'intraday',
period: 60*12,
limit: 30
};
break;
case "8h":
timeframe = {
type: 'intraday',
period: 60*8,
limit: 30
};
break;
case "6h":
timeframe = {
type: 'intraday',
period: 60*6,
limit: 30
};
break;
case "2h":
timeframe = {
type: 'intraday',
period: 60*2,
limit: 30
};
break;
default:
case "1h":
timeframe = {
type: 'intraday',
period: 60,
limit: 30
};
break;
case "30m":
timeframe = {
type: 'intraday',
period: 30,
limit: 30
};
break;
case "15m":
timeframe = {
type: 'intraday',
period: 15,
limit: 30
};
break;
case "5m":
timeframe = {
type: 'intraday',
period: 5,
limit: 30
};
break;
case "1m":
timeframe = {
type: 'intraday',
period: 1,
limit: 30
};
break;
}
// Calculate how many days are in the range
var daysInRange = this.query.range.from.getDaysBetween(this.query.range.to);
//toolset.log("daysInRange", daysInRange);
// Calculate the number of batches
var batchCount = Math.ceil(daysInRange/timeframe.limit);
//toolset.log("batchCount", batchCount);
var ranges = [];
if (batchCount > 1) {
// Split into smaller ranges
var cursorDate = new Date(this.query.range.from.getTime());
var i;
for (i=0;i<batchCount;i++) {
if (i==batchCount-1) {
ranges.push({
from: new Date(cursorDate.getTime()),
to: this.query.range.to
});
} else {
ranges.push({
from: new Date(cursorDate.getTime()),
to: new Date(cursorDate.addDays(timeframe.limit))
});
}
}
} else {
ranges = [{
from: this.query.range.from,
to: this.query.range.to
}];
}
//console.log("ranges", ranges);
switch (timeframe.type) {
case "weekly":
timeframe.typeInt = 2;
break;
case "daily":
timeframe.typeInt = 1;
break;
case "intraday":
timeframe.typeInt = 0;
break;
}
var stack = new toolset.stack();
var data = [];
if (this.log) {
var bar = new progressbar('Downloading the data [:bar] :percent [:current/:total] :etas', {
complete: '=',
incomplete: ' ',
width: 20,
total: ranges.length
});
}
_.each(ranges, function(range) {
stack.add(function(p, cb) {
switch (timeframe.type) {
case "intraday":
var url = scope.baseurl()+'barData?symbol='+scope.query.symbol+'&historyType='+timeframe.typeInt+'&intradayMinutes='+timeframe.period+'&beginTime='+scope.toActiveTickDate(range.from)+'&endTime='+scope.toActiveTickDate(range.to)+'';
break;
default:
var url = this.baseurl()+'barData?symbol='+scope.query.symbol+'&historyType='+timeframe.typeInt+'&beginTime='+scope.toActiveTickDate(range.from)+'&endTime='+scope.toActiveTickDate(range.to)+'';
break;
}
toolset.file.read(url, function(response) {
if (scope.log) {
bar.tick();
}
data = data.concat(scope.parseResponse(response));
cb();
});
//toolset.info("url", url);
});
});
stack.process(function() {
callback(data);
}, false);
/*
switch (this.query.type) {
case 0:
var url = this.baseurl()+'barData?symbol='+this.query.symbol+'&historyType='+this.query.type+'&intradayMinutes='+this.query.period+'&beginTime='+this.toActiveTickDate(this.query.range.from)+'&endTime='+this.toActiveTickDate(this.query.range.to)+'';
break;
default:
var url = this.baseurl()+'barData?symbol='+this.query.symbol+'&historyType='+this.query.type+'&beginTime='+this.toActiveTickDate(this.query.range.from)+'&endTime='+this.toActiveTickDate(this.query.range.to)+'';
break;
}
toolset.file.read(url, function(response) {
callback(scope.parseResponse(response));
});*/
}
// Get the base API url
activetick.prototype.baseurl = function(period) {
return 'http://'+this.options.host+':'+this.options.port+'/';
}
// Convert a date to ActiveTick's format
activetick.prototype.toActiveTickDate = function(date) {
return moment(date).format('YYYYMMDDHHMMSS');
}
// Convert a date to ActiveTick's format
activetick.prototype.fromActiveTickDate = function(date) {
var splits = [4,2,2,2,2,2];
var parts = [];
var cursor = 0;
_.each(splits, function(index){
parts.push(date.substr(cursor, index));
cursor += index;
});
return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5], 0);
}
// Parse the API response into JSON
activetick.prototype.parseResponse = function(raw) {
var scope = this;
return _.map(_.compact(raw.split('\r\n')), function(line) {
var parts = line.split(',');
var output = {};
output[scope.options['d']]= scope.fromActiveTickDate(parts[0]);
output[scope.options['o']]= parseFloat(parts[1]);
output[scope.options['h']]= parseFloat(parts[2]);
output[scope.options['l']]= parseFloat(parts[3]);
output[scope.options['c']]= parseFloat(parts[4]);
output[scope.options['v']]= parseInt(parts[5]);
return output;
});
}
module.exports = activetick;