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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
152 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
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 null. | ||
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() { | ||
for(var 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 (e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = MinuteIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters