Skip to content

Release/v0.2.0 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Parser for the MetaTrader History (.hst) File Format in Node.js

### Table of Contents

* [Summary](#)
* [Table of Contents](#)
* [Installation](#)
* [Usage](#)
* [Author](#)
* [Thank You](#)
* [Licence](#)
* [Summary](#summary)
* [Table of Contents](#table-of-contents)
* [Installation](#installation)
* [Usage](#usage)
* [Author](#author)
* [Thank You](#thank-you)
* [Licence](#licence)

### Installation

Expand All @@ -29,14 +29,14 @@ bower install hst-parser --save
### Usage

```javascript
const HSTReader = require('hst-parser').HSTReader;
const HSTParser = require('hst-parser');

const tradeHistory = new HSTReader('./data/USDGBP.hst'); // your .hst file
const tradeHistory = new HSTParser('./data/USDGBP.hst'); // your .hst file
```


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

#### isValidFormat()

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

#### getCandleNumberAsync(candleNumber)

Returns promise that resolves to candle data at specified position
```javascript
tradeHistory.getCandleNumber(200).then(candle => {
console.log(candle);
}).catch(err => {
console.log(err);
})
// output: {Candle}
```

#### getNextCandle()

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

#### getNextCandleAsync()

Returns promise that resolves to next set of candle data
```javascript
tradeHistory.getNextCandleAsync().then(candle => {
console.log(candle);
}).catch(err => {
console.log(err);
})
// output: {Candle}
```

#### getPrevCandle()

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

#### getPrevCandleAsync()

Returns promise that resolves to previous set of candle data
```javascript
tradeHistory.getPrevCandleAsync().then(candle => {
console.log(candle);
}).catch(err => {
console.log(err);
})
// output: {Candle}
```

#### getCandleAt(date, ...[startDate, i])

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

#### getCandleAtAsync(date)

Returns promise that resolves to set of candle data at specified date and time
```javascript
const myBirthday = new Date(1996, 0, 26);
tradeHistory.getCandleAt(myBirthday).then(candle => {
console.log(candle);
}).catch(err => {
console.log(err);
})
// output: {Candle}
```

### Author

Kyron Taylor (gitbugr)
Expand Down
21 changes: 19 additions & 2 deletions dist/hst_reader.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Candle from "./interfaces/candle";
export default class HSTReader {
export default class HSTParser {
version: number;
symbol: string;
period: number;
start: Date;
candleNumber: number;
endOfFile: boolean;
private fd;
private byteOffset;
private candleByteSize;
Expand All @@ -22,22 +21,40 @@ export default class HSTReader {
* @return {Candle} returns the next candle in the file
*/
getNextCandle(): Candle;
/**
* @return {Promise} returns promise that resolves to the next candle in the file
*/
getNextCandleAsync(): Promise<Candle>;
/**
* @return {Candle} returns the previous candle in the file
*/
getPrevCandle(): Candle;
/**
* @return {Promise} returns promise that resolves to the previous candle in the file
*/
getPrevCandleAsync(): Promise<Candle>;
/**
* @param {number} candleNumber finds candle from number
* @return {Candle} returns specified candle
*/
getCandleNumber(candleNumber: number): Candle;
/**
* @param {number} candleNumber finds candle from number
* @return {Promise} returns promise that resolves to specified candle
*/
getCandleNumberAsync(candleNumber: number): Promise<Candle>;
/**
* @param {Date} date finds candle at specified date and time
* @param {Date=} startDate optional: due to missing candles, we try to find based on most likely position
* @param {number=} i optional: count attempts to limit search area
* @return {Candle} returns the candle if found
*/
getCandleAt(date: Date, startDate?: Date, i?: number): Candle;
/**
* @param {Date} date finds candle at specified date and time
* @return {Promise} returns promise, resolves to Candle object
*/
getCandleAtAsync(date: Date): Promise<Candle>;
/**
* @return {Candle} returns candle at byte position
*/
Expand Down
89 changes: 73 additions & 16 deletions dist/hst_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
// parser dataa
const parser_data_1 = __importDefault(require("./parser_data"));
class HSTReader {
class HSTParser {
/**
* @param {string} filename Reads headers of specified file and stores into class variables.
*/
constructor(filename) {
// check if file exists
if (!fs.existsSync(filename)) {
throw new Error(`Can't find this file: ${filename}`);
throw new Error("Could not find file");
}
// open file and store file descriptor as variable
this.fd = fs.openSync(filename, 'r');
this.fd = fs.openSync(filename, "r+");
// verify filesize is larger than header size
this.filesize = fs.statSync(filename).size;
if (this.filesize < 148) {
throw new Error(`Damn, that's a small file. Too small.`);
throw new Error("File too small");
}
// set up buffers
const version = Buffer.alloc(4);
Expand All @@ -50,7 +50,6 @@ class HSTReader {
this.symbol = symbol.toString("utf8");
this.period = period.readInt32LE(0);
this.start = new Date(start.readInt32LE(0) * 1000);
this.endOfFile = false;
// set byte offset to end of header-block
this.candleByteSize = this.version === 400 ? 44 : 60;
this.byteOffset = 148 - this.candleByteSize;
Expand All @@ -69,32 +68,78 @@ class HSTReader {
if (this.byteOffset + this.candleByteSize <= this.filesize) {
this.byteOffset += this.candleByteSize;
this.candleNumber += 1;
this.endOfFile = false;
}
else {
this.endOfFile = true;
throw new Error("Already at end of file");
}
return this.readCandle();
}
/**
* @return {Promise} returns promise that resolves to the next candle in the file
*/
getNextCandleAsync() {
return new Promise((resolve, reject) => {
try {
resolve(this.getNextCandle());
}
catch (err) {
reject(err);
}
});
}
/**
* @return {Candle} returns the previous candle in the file
*/
getPrevCandle() {
if (this.byteOffset - this.candleByteSize >= 148) {
this.byteOffset -= this.candleByteSize;
this.candleNumber -= 1;
this.endOfFile = false;
}
else {
throw new Error("Already at start of file");
}
return this.readCandle();
}
/**
* @return {Promise} returns promise that resolves to the previous candle in the file
*/
getPrevCandleAsync() {
return new Promise((resolve, reject) => {
try {
resolve(this.getPrevCandle());
}
catch (err) {
reject(err);
}
});
}
/**
* @param {number} candleNumber finds candle from number
* @return {Candle} returns specified candle
*/
getCandleNumber(candleNumber) {
this.candleNumber = candleNumber;
this.byteOffset = 148 + (candleNumber * this.candleByteSize);
return this.readCandle();
const newByteOffset = 148 + (candleNumber * this.candleByteSize);
if (newByteOffset < this.filesize) {
return this.readCandle();
}
else {
throw new Error("File too small");
}
}
/**
* @param {number} candleNumber finds candle from number
* @return {Promise} returns promise that resolves to specified candle
*/
getCandleNumberAsync(candleNumber) {
return new Promise((resolve, reject) => {
try {
resolve(this.getCandleNumber(candleNumber));
}
catch (err) {
reject(err);
}
});
}
/**
* @param {Date} date finds candle at specified date and time
Expand All @@ -116,9 +161,23 @@ class HSTReader {
return this.getCandleAt(date, candle.timestamp, i + 1);
}
else {
throw new Error("Could not find candle");
throw new Error("Candle not found");
}
}
/**
* @param {Date} date finds candle at specified date and time
* @return {Promise} returns promise, resolves to Candle object
*/
getCandleAtAsync(date) {
return new Promise((resolve, reject) => {
try {
resolve(this.getCandleAt(date));
}
catch (err) {
reject(err);
}
});
}
/**
* @return {Candle} returns candle at byte position
*/
Expand All @@ -129,7 +188,7 @@ class HSTReader {
const parserFunctions = {
date: (buffer) => {
let timestamp = buffer.readInt32LE(0);
timestamp = timestamp < 9999999999 ? timestamp * 1000 : timestamp;
timestamp = this.version === 400 ? timestamp * 1000 : timestamp;
return new Date(timestamp);
},
double: (buffer) => buffer.readDoubleLE(0),
Expand All @@ -150,7 +209,5 @@ class HSTReader {
return candleData;
}
}
exports.default = HSTReader;
module.exports = {
HSTReader,
};
exports.default = HSTParser;
module.exports = HSTParser;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hst-parser",
"description": "Parser for the MetaTrader History (.hst) File Format",
"author": "gitbugr <kyronataylor@gmail.com>",
"version": "0.1.2",
"version": "0.2.0",
"license": "MIT",
"main": "dist/hst_reader.js",
"repository": {
Expand Down
Loading