Skip to content

Commit 39bd62c

Browse files
authored
Merge pull request #2 from gitbugr/release/v0.2.0
Release/v0.2.0
2 parents e76400d + 5fe4139 commit 39bd62c

File tree

6 files changed

+220
-47
lines changed

6 files changed

+220
-47
lines changed

README.md

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ Parser for the MetaTrader History (.hst) File Format in Node.js
88

99
### Table of Contents
1010

11-
* [Summary](#)
12-
* [Table of Contents](#)
13-
* [Installation](#)
14-
* [Usage](#)
15-
* [Author](#)
16-
* [Thank You](#)
17-
* [Licence](#)
11+
* [Summary](#summary)
12+
* [Table of Contents](#table-of-contents)
13+
* [Installation](#installation)
14+
* [Usage](#usage)
15+
* [Author](#author)
16+
* [Thank You](#thank-you)
17+
* [Licence](#licence)
1818

1919
### Installation
2020

@@ -29,14 +29,14 @@ bower install hst-parser --save
2929
### Usage
3030

3131
```javascript
32-
const HSTReader = require('hst-parser').HSTReader;
32+
const HSTParser = require('hst-parser');
3333

34-
const tradeHistory = new HSTReader('./data/USDGBP.hst'); // your .hst file
34+
const tradeHistory = new HSTParser('./data/USDGBP.hst'); // your .hst file
3535
```
3636

3737

3838
#### Variables
39-
the HSTReader class exposes some variables for you to use
39+
the HSTParser class exposes some variables for you to use
4040
```javascript
4141
console.log(tradeHistory.version); // file version from headers
4242
// example output: 400
@@ -77,7 +77,7 @@ Example:
7777

7878
#### isValidFormat()
7979

80-
Checks if file can be parsed by HSTReader. (currently supports versions 400 and 401 of .hst files)
80+
Checks if file can be parsed by HSTParser. (currently supports versions 400 and 401 of .hst files)
8181
```javascript
8282
if(tradeHistory.isValidFormat()) {
8383
// do stuff
@@ -94,6 +94,18 @@ console.log(tradeHistory.getCandleNumber(200));
9494
// output: {Candle}
9595
```
9696

97+
#### getCandleNumberAsync(candleNumber)
98+
99+
Returns promise that resolves to candle data at specified position
100+
```javascript
101+
tradeHistory.getCandleNumber(200).then(candle => {
102+
console.log(candle);
103+
}).catch(err => {
104+
console.log(err);
105+
})
106+
// output: {Candle}
107+
```
108+
97109
#### getNextCandle()
98110

99111
Returns next set of candle data
@@ -102,6 +114,18 @@ console.log(tradeHistory.getNextCandle());
102114
// output: {Candle}
103115
```
104116

117+
#### getNextCandleAsync()
118+
119+
Returns promise that resolves to next set of candle data
120+
```javascript
121+
tradeHistory.getNextCandleAsync().then(candle => {
122+
console.log(candle);
123+
}).catch(err => {
124+
console.log(err);
125+
})
126+
// output: {Candle}
127+
```
128+
105129
#### getPrevCandle()
106130

107131
Returns previous set of candle data
@@ -110,6 +134,18 @@ console.log(tradeHistory.getPrevCandle());
110134
// output: {Candle}
111135
```
112136

137+
#### getPrevCandleAsync()
138+
139+
Returns promise that resolves to previous set of candle data
140+
```javascript
141+
tradeHistory.getPrevCandleAsync().then(candle => {
142+
console.log(candle);
143+
}).catch(err => {
144+
console.log(err);
145+
})
146+
// output: {Candle}
147+
```
148+
113149
#### getCandleAt(date, ...[startDate, i])
114150

115151
Returns set of candle data at specified date and time
@@ -119,6 +155,19 @@ console.log(tradeHistory.getCandleAt(myBirthday));
119155
// output: {Candle}
120156
```
121157

158+
#### getCandleAtAsync(date)
159+
160+
Returns promise that resolves to set of candle data at specified date and time
161+
```javascript
162+
const myBirthday = new Date(1996, 0, 26);
163+
tradeHistory.getCandleAt(myBirthday).then(candle => {
164+
console.log(candle);
165+
}).catch(err => {
166+
console.log(err);
167+
})
168+
// output: {Candle}
169+
```
170+
122171
### Author
123172

124173
Kyron Taylor (gitbugr)

dist/hst_reader.d.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import Candle from "./interfaces/candle";
2-
export default class HSTReader {
2+
export default class HSTParser {
33
version: number;
44
symbol: string;
55
period: number;
66
start: Date;
77
candleNumber: number;
8-
endOfFile: boolean;
98
private fd;
109
private byteOffset;
1110
private candleByteSize;
@@ -22,22 +21,40 @@ export default class HSTReader {
2221
* @return {Candle} returns the next candle in the file
2322
*/
2423
getNextCandle(): Candle;
24+
/**
25+
* @return {Promise} returns promise that resolves to the next candle in the file
26+
*/
27+
getNextCandleAsync(): Promise<Candle>;
2528
/**
2629
* @return {Candle} returns the previous candle in the file
2730
*/
2831
getPrevCandle(): Candle;
32+
/**
33+
* @return {Promise} returns promise that resolves to the previous candle in the file
34+
*/
35+
getPrevCandleAsync(): Promise<Candle>;
2936
/**
3037
* @param {number} candleNumber finds candle from number
3138
* @return {Candle} returns specified candle
3239
*/
3340
getCandleNumber(candleNumber: number): Candle;
41+
/**
42+
* @param {number} candleNumber finds candle from number
43+
* @return {Promise} returns promise that resolves to specified candle
44+
*/
45+
getCandleNumberAsync(candleNumber: number): Promise<Candle>;
3446
/**
3547
* @param {Date} date finds candle at specified date and time
3648
* @param {Date=} startDate optional: due to missing candles, we try to find based on most likely position
3749
* @param {number=} i optional: count attempts to limit search area
3850
* @return {Candle} returns the candle if found
3951
*/
4052
getCandleAt(date: Date, startDate?: Date, i?: number): Candle;
53+
/**
54+
* @param {Date} date finds candle at specified date and time
55+
* @return {Promise} returns promise, resolves to Candle object
56+
*/
57+
getCandleAtAsync(date: Date): Promise<Candle>;
4158
/**
4259
* @return {Candle} returns candle at byte position
4360
*/

dist/hst_reader.js

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
1414
const fs = __importStar(require("fs"));
1515
// parser dataa
1616
const parser_data_1 = __importDefault(require("./parser_data"));
17-
class HSTReader {
17+
class HSTParser {
1818
/**
1919
* @param {string} filename Reads headers of specified file and stores into class variables.
2020
*/
2121
constructor(filename) {
2222
// check if file exists
2323
if (!fs.existsSync(filename)) {
24-
throw new Error(`Can't find this file: ${filename}`);
24+
throw new Error("Could not find file");
2525
}
2626
// open file and store file descriptor as variable
27-
this.fd = fs.openSync(filename, 'r');
27+
this.fd = fs.openSync(filename, "r+");
2828
// verify filesize is larger than header size
2929
this.filesize = fs.statSync(filename).size;
3030
if (this.filesize < 148) {
31-
throw new Error(`Damn, that's a small file. Too small.`);
31+
throw new Error("File too small");
3232
}
3333
// set up buffers
3434
const version = Buffer.alloc(4);
@@ -50,7 +50,6 @@ class HSTReader {
5050
this.symbol = symbol.toString("utf8");
5151
this.period = period.readInt32LE(0);
5252
this.start = new Date(start.readInt32LE(0) * 1000);
53-
this.endOfFile = false;
5453
// set byte offset to end of header-block
5554
this.candleByteSize = this.version === 400 ? 44 : 60;
5655
this.byteOffset = 148 - this.candleByteSize;
@@ -69,32 +68,78 @@ class HSTReader {
6968
if (this.byteOffset + this.candleByteSize <= this.filesize) {
7069
this.byteOffset += this.candleByteSize;
7170
this.candleNumber += 1;
72-
this.endOfFile = false;
7371
}
7472
else {
75-
this.endOfFile = true;
73+
throw new Error("Already at end of file");
7674
}
7775
return this.readCandle();
7876
}
77+
/**
78+
* @return {Promise} returns promise that resolves to the next candle in the file
79+
*/
80+
getNextCandleAsync() {
81+
return new Promise((resolve, reject) => {
82+
try {
83+
resolve(this.getNextCandle());
84+
}
85+
catch (err) {
86+
reject(err);
87+
}
88+
});
89+
}
7990
/**
8091
* @return {Candle} returns the previous candle in the file
8192
*/
8293
getPrevCandle() {
8394
if (this.byteOffset - this.candleByteSize >= 148) {
8495
this.byteOffset -= this.candleByteSize;
8596
this.candleNumber -= 1;
86-
this.endOfFile = false;
97+
}
98+
else {
99+
throw new Error("Already at start of file");
87100
}
88101
return this.readCandle();
89102
}
103+
/**
104+
* @return {Promise} returns promise that resolves to the previous candle in the file
105+
*/
106+
getPrevCandleAsync() {
107+
return new Promise((resolve, reject) => {
108+
try {
109+
resolve(this.getPrevCandle());
110+
}
111+
catch (err) {
112+
reject(err);
113+
}
114+
});
115+
}
90116
/**
91117
* @param {number} candleNumber finds candle from number
92118
* @return {Candle} returns specified candle
93119
*/
94120
getCandleNumber(candleNumber) {
95121
this.candleNumber = candleNumber;
96-
this.byteOffset = 148 + (candleNumber * this.candleByteSize);
97-
return this.readCandle();
122+
const newByteOffset = 148 + (candleNumber * this.candleByteSize);
123+
if (newByteOffset < this.filesize) {
124+
return this.readCandle();
125+
}
126+
else {
127+
throw new Error("File too small");
128+
}
129+
}
130+
/**
131+
* @param {number} candleNumber finds candle from number
132+
* @return {Promise} returns promise that resolves to specified candle
133+
*/
134+
getCandleNumberAsync(candleNumber) {
135+
return new Promise((resolve, reject) => {
136+
try {
137+
resolve(this.getCandleNumber(candleNumber));
138+
}
139+
catch (err) {
140+
reject(err);
141+
}
142+
});
98143
}
99144
/**
100145
* @param {Date} date finds candle at specified date and time
@@ -116,9 +161,23 @@ class HSTReader {
116161
return this.getCandleAt(date, candle.timestamp, i + 1);
117162
}
118163
else {
119-
throw new Error("Could not find candle");
164+
throw new Error("Candle not found");
120165
}
121166
}
167+
/**
168+
* @param {Date} date finds candle at specified date and time
169+
* @return {Promise} returns promise, resolves to Candle object
170+
*/
171+
getCandleAtAsync(date) {
172+
return new Promise((resolve, reject) => {
173+
try {
174+
resolve(this.getCandleAt(date));
175+
}
176+
catch (err) {
177+
reject(err);
178+
}
179+
});
180+
}
122181
/**
123182
* @return {Candle} returns candle at byte position
124183
*/
@@ -129,7 +188,7 @@ class HSTReader {
129188
const parserFunctions = {
130189
date: (buffer) => {
131190
let timestamp = buffer.readInt32LE(0);
132-
timestamp = timestamp < 9999999999 ? timestamp * 1000 : timestamp;
191+
timestamp = this.version === 400 ? timestamp * 1000 : timestamp;
133192
return new Date(timestamp);
134193
},
135194
double: (buffer) => buffer.readDoubleLE(0),
@@ -150,7 +209,5 @@ class HSTReader {
150209
return candleData;
151210
}
152211
}
153-
exports.default = HSTReader;
154-
module.exports = {
155-
HSTReader,
156-
};
212+
exports.default = HSTParser;
213+
module.exports = HSTParser;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "hst-parser",
33
"description": "Parser for the MetaTrader History (.hst) File Format",
44
"author": "gitbugr <kyronataylor@gmail.com>",
5-
"version": "0.1.2",
5+
"version": "0.2.0",
66
"license": "MIT",
77
"main": "dist/hst_reader.js",
88
"repository": {

0 commit comments

Comments
 (0)