8000 feat: more timeframes (m5 and h4) (#53) · Leo4815162342/dukascopy-node@0fec595 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fec595

Browse files
feat: more timeframes (m5 and h4) (#53)
feat: more timeframes (m5 and h4)
1 parent 913e3b1 commit 0fec595

File tree

10 files changed

+42
-11
lines changed

10 files changed

+42
-11
lines changed

packages/dukascopy-cli/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ npx dukascopy-cli -i eurusd -from 2006-01-01 -to 2020-01-01 -t d1 -f json
5353
-i, --instrument <value> Trading instrument
5454
-from, --date-from <value> From date (yyyy-mm-dd)
5555
-to, --date-to <value> To date (yyyy-mm-dd)
56-
-t, --timeframe <value> Timeframe aggregation (tick, m1, m15, m30, h1, d1, mn1) (default: "d1")
56+
-t, --timeframe <value> Timeframe aggregation (tick, m1, m5, m15, m30, h1, h4, d1, mn1) (default: "d1")
5757
-p, --price-type <value> Price type: (bid, ask) (default: "bid")
5858
-utc, --utc-offset <value> UTC offset in minutes (default: 0)
5959
-v, --volumes Include volumes (default: false)

packages/dukascopy-cli/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ program
1919
.requiredOption('-to, --date-to <value>', 'To date (yyyy-mm-dd)')
2020
.option(
2121
'-t, --timeframe <value>',
22-
'Timeframe aggregation (tick, m1, m15, m30, h1, d1, mn1)',
22+
'Timeframe aggregation (tick, m1, m5, m15, m30, h1, h4, d1, mn1)',
2323
Timeframe.d1
2424
)
2525
.option('-p, --price-type <value>', 'Price type: (bid, ask)', Price.bid)

packages/dukascopy-node/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const { getHistoricRates } = require('dukascopy-node');
6666
|`dates`|`Object`|true||An object with a date range|
6767
|`dates.from`|<p>`Date`</p><p>`String`</p><p>`Number`</p>|true||Date representing the start of the time range. Can be of Date type, string (e.g. `2021-03-04` or `2021-03-04T00:00:00.000Z`), or timestamp integer (e.g. `1614816000000`)|
6868
|`dates.to`|<p>`Date`</p><p>`String`</p><p>`Number`</p>|true||Date representing the end of the time range Can be of Date type, string (e.g. `2021-03-04` or `2021-03-04T00:00:00.000Z`), or timestamp integer (e.g. `1614816000000`)|
69-
|`timeframe`|`String`|false|`d1`|Granularity of aggregation of OHLC (open, high, low, close) data. Supported values:<ul><li>`tick` (every single tick/price change)</li><li>`m1` (1 minute)</li><li>`m15` (15 minutes)</li><li>`m30` (30 minutes)</li><li>`h1` (1 hour)</li><li>`d1` (1 day)</li><li>`mn1` (1 month)</li></ul>|
69+
|`timeframe`|`String`|false|`d1`|Granularity of aggregation of OHLC (open, high, low, close) data. Supported values:<ul><li>`tick` (every single tick/price change)</li><li>`m1` (1 minute)</li><li>`m5` (5 minutes)</li><li>`m15` (15 minutes)</li><li>`m30` (30 minutes)</li><li>`h1` (1 hour)</li><li>`h4` (4 hours)</li><li>`d1` (1 day)</li><li>`mn1` (1 month)</li></ul>|
7070
|`priceType`|`String`|false|`bid`|Type of price (offer side). Supported values:<ul><li>`bid`</li><li>`ask`</li></ul>|
7171
|`format`|`String`|false|`array`|Format of the generated output. Supported values:<ul><li>`array`</li><li>`json`</li><li>`csv`</li></ul>|
7272< 67E6 /td>
|`utcOffset`|`Number`|false|`0`|UTC offset in minutes.|

packages/dukascopy-node/dev-utils/test-call.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { getHistoricRates, Config } from '../src';
44
(async () => {
55
try {
66
const config: Config = {
7-
instrument: 'eurusd',
7+
instrument: 'btcusd',
88
dates: {
9-
from: new Date('2021-03-30'),
10-
to: new Date('2021-03-31')
9+
from: '2020-12-03T00:00:00.000Z',
10+
to: '2020-12-04T00:00:00.000Z'
1111
},
12-
timeframe: 'tick',
13-
format: 'json'
12+
timeframe: 'h4',
13+
format: 'json',
14+
ignoreFlats: false
1415
};
1516
console.log('fetching started');
1617
console.log(JSON.stringify(config));
@@ -20,6 +21,7 @@ import { getHistoricRates, Config } from '../src';
2021
console.log('fetching success');
2122
// console.log(JSON.stringify(quotes));
2223
console.log(`size: ${quotes.length}`);
24+
console.log(JSON.stringify(quotes, null, 2));
2325
console.log(`time ms: ${d2 - d1}`);
2426
} catch (error) {
2527
console.log('error', error);

packages/dukascopy-node/src/aggregator/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export function aggregate({
3131
return minuteOHLC;
3232
}
3333

34+
if (toTimeframe === Timeframe.m5) {
35+
return splitArrayInChunks(minuteOHLC, 5).map(d => getOHLC(d, ignoreFlats));
36+
}
37+
3438
if (toTimeframe === Timeframe.m15) {
3539
return splitArrayInChunks(minuteOHLC, 15).map(d => getOHLC(d, ignoreFlats));
3640
}
@@ -45,6 +49,10 @@ export function aggregate({
4549
}
4650

4751
if (fromTimeframe === Timeframe.m1) {
52+
if (toTimeframe === Timeframe.m5) {
53+
return splitArrayInChunks(data, 5).map(d => getOHLC(d, ignoreFlats));
54+
}
55+
4856
if (toTimeframe === Timeframe.m15) {
4957
return splitArrayInChunks(data, 15).map(d => getOHLC(d, ignoreFlats));
5058
}
@@ -57,12 +65,19 @@ export function aggregate({
5765
return splitArrayInChunks(data, 60).map(d => getOHLC(d, ignoreFlats));
5866
}
5967

68+
if (toTimeframe === Timeframe.h4) {
69+
return splitArrayInChunks(data, 240).map(d => getOHLC(d, ignoreFlats));
70+
}
71+
6072
if (toTimeframe === Timeframe.d1) {
6173
return [data].map(d => getOHLC(d, ignoreFlats));
6274
}
6375
}
6476

6577
if (fromTimeframe === Timeframe.h1) {
78+
if (toTimeframe === Timeframe.h4) {
79+
return splitArrayInChunks(data, 4).map(d => getOHLC(d, ignoreFlats));
80+
}
6681
if (toTimeframe === Timeframe.d1) {
6782
return splitArrayInChunks(data, 24).map(d => getOHLC(d, ignoreFlats));
6883
}

packages/dukascopy-node/src/config-validator/tests/cases/general_all-invalid.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Instrument } from '../../../config/instruments';
2+
import { Timeframe } from '../../../config/timeframes';
23

34
const config = {
45
instrument: '',
@@ -25,7 +26,7 @@ const expectedOutput = {
2526
},
2627
{
2728
actual: '',
28-
expected: 'tick, m1, m15, m30, h1, d1, mn1',
29+
expected: Object.keys(Timeframe).join(', '),
2930
message: "The 'timeframe' field does not match any of the allowed values."
3031
},
3132
{

packages/dukascopy-node/src/config-validator/tests/cases/timeframe_nonsupported.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Timeframe } from '../../../config/timeframes';
2+
13
const config = {
24
instrument: 'eurusd',
35
dates: {
@@ -16,7 +18,7 @@ const expectedOutput = {
1618
validationErrors: [
1719
{
1820
actual: 'xxxyyyzzz',
19-
expected: 'tick, m1, m15, m30, h1, d1, mn1',
21+
expected: Object.keys(Timeframe).join(', '),
2022
message: "The 'timeframe' field does not match any of the allowed values."
2123
}
2224
]

packages/dukascopy-node/src/config/timeframes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export enum Timeframe {
77
* minutely `(1 minute)` aggregation of OHLC price data
88
*/
99
m1 = 'm1',
10+
/**
11+
* 5-minute `(5 minutes)` aggregation of OHLC price data
12+
*/
13+
m5 = 'm5',
1014
/**
1115
* 15-minute `(15 minutes)` aggregation of OHLC price data
1216
*/
@@ -19,6 +23,10 @@ export enum Timeframe {
1923
* hourly `(1 hour)` aggregation of OHLC price data
2024
*/
2125
h1 = 'h1',
26+
/**
27+
* 4-hour `(4 hour)` aggregation of OHLC price data
28+
*/
29+
h4 = 'h4',
2230
/**
2331
* daily `(1 day)` aggregation of OHLC price data
2432
*/

packages/dukascopy-node/src/utils/range.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ type RangeInferMap = { [key in TimeframeType]: TimeRange[] };
1010
const rangeInferMap: RangeInferMap = {
1111
mn1: ['year', 'month', 'day'],
1212
d1: ['year', 'month', 'day'],
13+
h4: ['month', 'day', 'hour'],
1314
h1: ['month', 'day', 'hour'],
1415
m30: ['day', 'hour'],
1516
m15: ['day', 'hour'],
17+
m5: ['day', 'hour'],
1618
m1: ['day', 'hour'],
1719
tick: ['hour']
1820
};

packages/dukascopy-node/tests/getHistoricRates/cases/_fail_01.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Timeframe } from '../../../src/config/timeframes';
12
import { Instrument } from '../../../src/config/instruments';
23

34
const config = {
@@ -26,7 +27,7 @@ const expectedOutput = [
2627
},
2728
{
2829
actual: 'd1z',
29-
expected: 'tick, m1, m15, m30, h1, d1, mn1',
30+
expected: Object.keys(Timeframe).join(', '),
3031
message: "The 'timeframe' field does not match any of the allowed values."
3132
},
3233
{

0 commit comments

Comments
 (0)
0