dukascopy-node-plus 1.1.4 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,154 @@
1
+ import {
2
+ candleHeaders,
3
+ tickHeaders
4
+ } from "./chunk-ASWACD3L.js";
5
+ import {
6
+ BufferFetcher,
7
+ CacheManager,
8
+ formatBytes,
9
+ generateUrls,
10
+ normaliseDates,
11
+ processData,
12
+ validateConfigNode,
13
+ version
14
+ } from "./chunk-GDKYDWLQ.js";
15
+
16
+ // src/getHistoricalRatesToStream.ts
17
+ import debug from "debug";
18
+ import os from "os";
19
+ import { Readable } from "stream";
20
+ var DEBUG_NAMESPACE = "dukascopy-node";
21
+ function getHistoricalRatesToStream(config) {
22
+ const stream = new Readable({
23
+ objectMode: true,
24
+ async read(_size) {
25
+ }
26
+ });
27
+ try {
28
+ debug(`${DEBUG_NAMESPACE}:version`)(version);
29
+ debug(`${DEBUG_NAMESPACE}:nodejs`)(process.version);
30
+ debug(`${DEBUG_NAMESPACE}:os`)(`${os.type()}, ${os.release()} (${os.platform()})`);
31
+ const { input, isValid, validationErrors } = validateConfigNode(config);
32
+ debug(`${DEBUG_NAMESPACE}:config`)("%O", {
33
+ input,
34
+ isValid,
35
+ validationErrors
36
+ });
37
+ if (!isValid) {
38
+ const error = new Error("Configuration validation failed");
39
+ Object.assign(error, { validationErrors });
40
+ stream.emit("error", error);
41
+ stream.push(null);
42
+ return stream;
43
+ }
44
+ const {
45
+ instrument,
46
+ dates: { from, to },
47
+ timeframe,
48
+ priceType,
49
+ volumes,
50
+ volumeUnits,
51
+ utcOffset,
52
+ ignoreFlats,
53
+ format,
54
+ batchSize,
55
+ pauseBetweenBatchesMs,
56
+ useCache,
57
+ cacheFolderPath,
58
+ retryCount,
59
+ pauseBetweenRetriesMs,
60
+ retryOnEmpty
61
+ } = input;
62
+ const [startDate, endDate] = normaliseDates({
63
+ instrument,
64
+ startDate: from,
65
+ endDate: to,
66
+ timeframe,
67
+ utcOffset
68
+ });
69
+ const [startDateMs, endDateMs] = [+startDate, +endDate];
70
+ const urls = generateUrls({
71
+ instrument,
72
+ timeframe,
73
+ priceType,
74
+ startDate,
75
+ endDate
76
+ });
77
+ debug(`${DEBUG_NAMESPACE}:urls`)(`Generated ${urls.length} urls`);
78
+ debug(`${DEBUG_NAMESPACE}:urls`)("%O", urls);
79
+ const onItemFetch = process.env.DEBUG ? (url, buffer, isCacheHit) => {
80
+ debug(`${DEBUG_NAMESPACE}:fetcher`)(
81
+ url,
82
+ `| ${formatBytes(buffer.length)} |`,
83
+ `${isCacheHit ? "cache" : "network"}`
84
+ );
85
+ } : void 0;
86
+ const bufferFetcher = new BufferFetcher({
87
+ batchSize,
88
+ pauseBetweenBatchesMs,
89
+ cacheManager: useCache ? new CacheManager({ cacheFolderPath }) : void 0,
90
+ retryCount,
91
+ pauseBetweenRetriesMs,
92
+ onItemFetch,
93
+ retryOnEmpty
94
+ });
95
+ const bodyHeaders = timeframe === "tick" /* tick */ ? tickHeaders : candleHeaders;
96
+ let firstLine = true;
97
+ bufferFetcher.fetch(urls).then((bufferredData) => {
98
+ debug(`${DEBUG_NAMESPACE}:fetcher`)(`Fetched ${bufferredData.length} buffer objects`);
99
+ const processedData = processData({
100
+ instrument,
101
+ requestedTimeframe: timeframe,
102
+ bufferObjects: bufferredData,
103
+ priceType,
104
+ volumes,
105
+ volumeUnits,
106
+ ignoreFlats
107
+ });
108
+ debug(`${DEBUG_NAMESPACE}:data`)(
109
+ `Generated ${processedData.length} ${timeframe === "tick" /* tick */ ? "ticks" : "OHLC candles"}`
110
+ );
111
+ const filteredData = processedData.filter(
112
+ ([timestamp]) => timestamp && timestamp >= startDateMs && timestamp < endDateMs
113
+ );
114
+ debug(`${DEBUG_NAMESPACE}:data`)(`Filtered to ${filteredData.length} items`);
115
+ filteredData.forEach((item) => {
116
+ if (format === "array" /* array */) {
117
+ stream.push(item);
118
+ } else if (format === "json" /* json */) {
119
+ const data = item.reduce(
120
+ (all, value, i) => {
121
+ const name = bodyHeaders[i];
122
+ all[name] = value;
123
+ return all;
124
+ },
125
+ {}
126
+ );
127
+ stream.push(data);
128
+ } else if (format === "csv" /* csv */) {
129
+ if (firstLine) {
130
+ const csvHeaders = bodyHeaders.join(",");
131
+ stream.push(csvHeaders);
132
+ firstLine = false;
133
+ }
134
+ stream.push(`
135
+ ${item.join(",")}`);
136
+ }
137
+ });
138
+ stream.push(null);
139
+ }).catch((err) => {
140
+ debug(`${DEBUG_NAMESPACE}:error`)("Error fetching data: %O", err);
141
+ stream.emit("error", err);
142
+ stream.push(null);
143
+ });
144
+ } catch (err) {
145
+ debug(`${DEBUG_NAMESPACE}:error`)("Unexpected error: %O", err);
146
+ stream.emit("error", err);
147
+ stream.push(null);
148
+ }
149
+ return stream;
150
+ }
151
+
152
+ export {
153
+ getHistoricalRatesToStream
154
+ };