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
/
Symbol.js
237 lines (157 loc) · 4.13 KB
/
Symbol.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
var
fs = require('fs'),
path = require('path');
/**
Represents one particular symbol storage in the ticks database. Essentially this is just a wrapper around a
list of *.ticks files with handy access methods.
@param {String} dbPath path to ticks database.
@param {String} symbol symbol name, must be uppercase.
Tick files list is generated from <code>*.ticks</code> in <code>dbPath/SYMBOL/</code>.
Example:
var symbol = new Symbol('/home/tickers', 'AAPL');
if (!symbol.load()) {
console.log("Oops?");
return;
}
var daystamp;
while ((daystamp=symbol.next())) {
console.log("Day: %s", daystamp);
}
*/
function Symbol(dbPath, symbol) {
this.dbPath = dbPath;
this.symbol = symbol;
this.days = [];
this.dayPosition=0;
this.dirPath = this.dbPath+'/'+this.symbol+'/';
this._metaFilename = this.dirPath+'meta.json';
this.meta={};
this._loadMeta();
}
/**
Set active flag for the ticker. This flag is used by downloader to skip download this ticker from
IQFeed if the flag is set. Defaults to false, of course. Don't forget to call <code>.save()</code> after
setting the flag.
*/
Symbol.prototype.setIsActive = function(isActive) {
if (isActive) {
delete this.meta.isInactive;
} else {
this.meta.isInactive = true;
}
};
/**
Get active flag for the ticker.
*/
Symbol.prototype.isActive = function() {
return this.meta.isInactive ? false : true;
};
/**
Load meta information from <code>dbPath/symbol/meta.json</code>.
*/
Symbol.prototype._loadMeta = function() {
if (fs.existsSync(this._metaFilename)) {
try {
var data = fs.readFileSync(this._metaFilename);
data = data.toString();
data = JSON.parse(data);
this.meta = data;
} catch (e) {
this.meta = {};
}
}
};
/**
Save updated information for the ticker. Now this is just the meta information that contains active flag.
*/
Symbol.prototype.save = function() {
this._saveMeta();
};
/**
Save meta information to <code>dbPath/symbol/meta.json</code>.
*/
Symbol.prototype._saveMeta = function() {
if (Object.keys(this.meta).length <= 0) {
return;
}
var data = JSON.stringify(this.meta);
fs.writeFileSync(this._metaFilename+'.tmp', data);
if (fs.existsSync(this._metaFilename)) {
fs.unlinkSync(this._metaFilename);
}
fs.renameSync(this._metaFilename+'.tmp', this._metaFilename);
};
/**
Load the ticker from dbpath. Essentially does load the list of all days available for the ticker and
the meta information.
@return {Boolean} if loaded successfully.
*/
Symbol.prototype.load = function() {
this._loadMeta();
this.days=[];
this.dayPosition=0;
if (!fs.existsSync(this.dirPath)) {
return false;
}
var files = fs.readdirSync(this.dirPath);
files = files.sort();
files = files.filter(function(filename) {
return (filename.substr(0,1)!='.' && filename.substr(filename.length-5,5)=="ticks");
}, this);
this.days = files.map(function(filename) {
return filename.substr(0,8);
}, this);
return true;
};
/**
Single-step iterator over all days.
@param {Function} cb callback.
@param context set <code>this</code> to this.
Callback function will receive one argument: the daystamp, string.
*/
Symbol.prototype.forEachDay = function(cb, context) {
this.days.forEach(cb, context);
};
/**
Rewind current iterator position to the beginning.
*/
Symbol.prototype.rewind = function() {
this.dayPosition=0;
};
/**
Get the next day from list of days.
@return {String} daystamp.
*/
Symbol.prototype.nextDay = function() {
return this.days[this.dayPosition++];
};
/**
Get the first day from list of days.
@return {String} daystamp.
*/
Symbol.prototype.firstDay = function() {
return this.days[0];
};
/**
Get the last day from list of days.
@return {String} daystamp.
*/
Symbol.prototype.lastDay = function() {
return this.days[this.days.length-1];
};
/**
Get the count of available days.
@return {String} count of days.
*/
Symbol.prototype.count = function() {
return this.days.length;
};
/**
Check if this daystamp exists for this ticker.
@param {String} daystamp day to look for.
@return {Boolean}
*/
Symbol.prototype.dayExists = function(daystamp) {
return this.days.indexOf(daystamp)>=0;
};
module.exports = Symbol;