-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patheex.js
458 lines (411 loc) · 17.2 KB
/
eex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* eslint-disable max-len */
/*
Copyright 2019 - 2024, Robin de Gruijter ([email protected])
This file is part of com.gruijter.powerhour.
com.gruijter.powerhour is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
com.gruijter.powerhour is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with com.gruijter.powerhour. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const https = require('https');
const qs = require('querystring');
// const util = require('util');
const defaultHost = 'webservice-eex.gvsi.com'; // EOD and EGSI
const defaultPort = 443;
const defaultTimeout = 30000;
const biddingZones = {
TTF_EOD: 'TTF_EOD',
TTF_EGSI: 'TTF_EGSI',
CEGH_VTP_EOD: 'CEGH_VTP_EOD',
CEGH_VTP_EGSI: 'CEGH_VTP_EGSI',
CZ_VTP_EOD: 'CZ_VTP_EOD',
CZ_VTP_EGSI: 'CZ_VTP_EGSI',
ETF_EOD: 'ETFF_EOD',
ETF_EGSI: 'ETF_EGSI',
THE_EOD: 'THE_EOD',
THE_EGSI: 'THE_EGSI',
PEG_EOD: 'PEG_EOD',
PEG_EGSI: 'PEG_EGSI',
ZTP_EOD: 'ZTP_EOD',
ZTP_EGSI: 'ZTP_EGSI',
PVB_EOD: 'PVB_EOD',
PVB_EGSI: 'PVB_EGSI',
NBP_EOD: 'NBP_EOD',
NBP_EGSI: 'NBP_EGSI',
};
// mapping for eex page,
const biddingZonesMap = {
TTF_EOD: '"#E.TTF_GND1"',
CEGH_VTP_EOD: '"#E.CEGH_GND1"',
CZ_VTP_EOD: '"#E.OTE_GSND"',
ETF_EOD: '"#E.ETF_GND1"',
THE_EOD: '"#E.THE_GND1"',
PEG_EOD: '"#E.PEG_GND1"',
ZTP_EOD: '"#E.ZTP_GTND"',
PVB_EOD: '"#E.PVB_GSND"',
NBP_EOD: '"#E.NBP_GPND"',
TTF_EGSI: '"$E.EGSI_TTF_DAY"',
CEGH_VTP_EGSI: '"$E.EGSI_CEHG_VTP_DAY"',
CZ_VTP_EGSI: '"$E.EGSI_CZ_VTP"',
ETF_EGSI: '"$E.EGSI_ETF_DAY"',
THE_EGSI: '"$E.EGSI_THE_DAY"',
PEG_EGSI: '"$E.EGSI_PEG_DAY"',
ZTP_EGSI: '"$E.EGSI_ZTP_DAY"',
PVB_EGSI: '"$E.EGSI_PVB_DAY"',
NBP_EGSI: '"$E.EGSI_NBP_DAY"',
};
const padMissingHours = (data) => {
const paddedData = [];
data.forEach((currentEntry, idx) => {
let hoursDiff = 1;
if (idx > 0) {
const previousEntry = { ...data[idx - 1] };
const currTime = new Date(currentEntry.time);
const prevTime = new Date(previousEntry.time);
const prevPrice = previousEntry.price; // Set previous price for potential gaps
hoursDiff = (currTime - prevTime) / (1000 * 60 * 60); // Calculate the difference in hours
while (hoursDiff > 1) { // If more than 1 hour difference, fill the gap
prevTime.setUTCHours(prevTime.getUTCHours() + 1);
paddedData.push({
time: new Date(prevTime), // new hour
price: prevPrice, // use the previous price
});
hoursDiff--;
}
}
if (hoursDiff > 0) paddedData.push(data[idx]); // Push the current entry to the result, remove double hours
});
return (paddedData);
};
// Represents a session to the PowerNext API.
class EEX {
constructor(opts) {
const options = opts || {};
this.host = options.host || defaultHost;
this.port = options.port || defaultPort;
this.timeout = options.timeout || defaultTimeout;
this.biddingZone = options.biddingZone;
this.biddingZones = biddingZones;
this.lastResponse = undefined;
}
getBiddingZones() {
return this.biddingZones;
}
/**
* Get the prices
* @returns {(Promise.[priceInfo])}
* @property {string} [biddingZone] - e.g. '132733/137/17'
* @property {string} [dateStart = today] - date Object or date string, e.g. '2022-02-21T20:36:10.665Z'
* @property {string} [dateEnd = tomorrow ] - date Object or date string, e.g. '2022-02-21T20:36:10.665Z'
*/
async getPrices(options) {
try {
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
const opts = options || {};
const zone = opts.biddingZone || this.biddingZone;
const start = opts.dateStart ? new Date(opts.dateStart) : today;
const end = opts.dateEnd ? new Date(opts.dateEnd) : tomorrow;
start.setMinutes(0);
start.setSeconds(0);
start.setMilliseconds(0);
end.setMinutes(0);
end.setSeconds(0);
end.setMilliseconds(0);
let path = '';
this.tmpZone = zone;
const start2 = new Date(start);
start2.setDate(start.getDate() - 1); // start earlier to make sure weekend is fetched
const [chartstartdate] = start2.toISOString().split('T'); // '2022-11-30'
const [chartstopdate] = end.toISOString().split('T'); // '2023-01-15'
const priceSymbol = biddingZonesMap[zone]; // '"#E.TTF_GND1"' // "#E.EGSI_TTF_DAY" // weekend: '"#E.TTF_GWE1"' "#E.EGSI_TTF_WEEKEND"
const query = qs.stringify({
priceSymbol,
chartstartdate,
chartstopdate,
// dailybarinterval: 'Days',
// aggregatepriceselection: 'First',
});
path = `/query/json/getDaily/close/tradedatetimegmt/?${query}`;
// path = `/query/json/getDaily/close/onexchtradevolumeeex/tradedatetimegmt/?${query}`;
const res = await this._makeRequest(path, '');
if (!res || !res[0] || !res[0].date) throw Error('no gas price info found');
// make array with concise info per day in euro / 1000 m3 gas
const priceInfo = res
.filter((info) => info.descr === zone)
.map((day) => {
const dayStart = new Date(day.date.setHours(0));
const timeZoneOffset = new Date(dayStart.toLocaleString('en-US', { timeZone: 'Europe/Amsterdam' })) - dayStart;
dayStart.setMilliseconds(-timeZoneOffset); // convert date CET/CEST to UTC
const time = new Date(dayStart.getTime() + (6 * 60 * 60 * 1000)); // add 6 hours
return {
time,
price: day.rawPrice * 9.7694,
};
})
.filter((day) => day.time >= new Date(start - 24 * 60 * 60 * 1000)); // filter out too old days; // [];
// pad info to fill all hours in a day
const [lastPriceHour] = priceInfo.slice(-1);
const endTime = new Date(lastPriceHour.time);
endTime.setDate(endTime.getDate() + 1); // add a day, knows about DST I hope...
endTime.setHours(endTime.getHours() - 1);
priceInfo.push({
time: endTime,
price: lastPriceHour.price,
});
let info = padMissingHours(priceInfo);
info = info // remove out of bounds data
.filter((hourInfo) => hourInfo.time >= start)
.filter((hourInfo) => hourInfo.time < end);
return Promise.resolve(info);
} catch (error) {
return Promise.reject(error);
}
}
async _makeRequest(path, postMessage, timeout) {
try {
const headers = {
'content-type': 'application/json',
Origin: 'https://www.eex.com',
Referer: 'https://www.eex.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
Connection: 'keep-alive',
};
const options = {
hostname: this.host,
port: this.port,
path,
headers,
method: 'GET',
};
// console.log(options);
const result = await this._makeHttpsRequest(options, postMessage, timeout);
this.lastResponse = result.body || result.statusCode;
const contentType = result.headers['content-type'];
// find errors
if (result.statusCode !== 200) {
this.lastResponse = result.statusCode;
throw Error(`HTTP request Failed. Status Code: ${result.statusCode}`);
}
if (headers['content-type'] !== contentType) {
const body = typeof result.body === 'string' ? result.body.slice(0, 20) : '';
throw Error(`Expected ${headers['content-type']} but received ${contentType}: ${body}`);
}
// console.log(contentType, result.body);
// parse JSON
if (contentType.includes('json')) {
// parse daily info
const respJSON = JSON.parse(result.body);
if (!respJSON.results || !respJSON.results.items) throw Error('Invalid info');
const dailyInfo = respJSON.results.items;
const lastDaily = dailyInfo.slice(-1)[0]; // {close:55.665, onexchtradevolumeeex:3277272, tradedatetimegmt:'1/16/2023 12:00:00 PM'}
// console.dir(dailyInfo, { depth: null });
// console.dir(lastDaily, { depth: null });
// compensate for bug in EEX
if (this.lastDailyInfo) {
dailyInfo.forEach((dayInfo, index) => {
if ((index > dailyInfo.length - 4) && (index < dailyInfo.length - 1)) {
const previousInfo = this.lastDailyInfo.find((lastDayInfo) => lastDayInfo.tradedatetimegmt === dayInfo.tradedatetimegmt);
if (previousInfo && previousInfo.close !== dayInfo.close) {
// console.log('need to compensate!', dailyInfo, previousInfo);
dailyInfo[index].close = previousInfo.close;
}
}
});
}
this.lastDailyInfo = [...dailyInfo];
// fetch weekend info ONLY FOR EOD (not EGSI)
let weekendInfo;
if (this.tmpZone.includes('EOD')) {
options.path = options.path.replace('ND', 'WE');
options.path = options.path.replace('DAY', 'WEEKEND');
const resultW = await this._makeHttpsRequest(options, postMessage, timeout);
const respWJSON = JSON.parse(resultW.body);
weekendInfo = respWJSON.results && respWJSON.results.items;
}
// fetch today settle
const priceSymbol = biddingZonesMap[this.tmpZone]; // '"#E.TTF_GND1"' // weekend: '"#E.TTF_GWE1"'
const query = qs.stringify({ priceSymbol });
options.path = `/query/json/getQuotes/settledate/dir/?${query}`;
const resultSettle = await this._makeHttpsRequest(options, postMessage, timeout);
const respSettle = JSON.parse(resultSettle.body); // { settledate: '1/16/2023', dir: 55.25 } || { settledate: null, dir: 55.25 }
const lastSettleDate = respSettle.results && respSettle.results.items
&& respSettle.results.items[0] && respSettle.results.items[0].settledate;
// check if lastDaily is settled (closed)
const lastDailyIsClosed = lastDaily.tradedatetimegmt && (lastDaily.tradedatetimegmt.split(' ')[0] === lastSettleDate);
// create array with daily values
const resp = [];
dailyInfo.forEach((day, idx) => { // { close: 55.665, onexchtradevolumeeex: 3277272, tradedatetimegmt: '1/16/2023 12:00:00 PM' }
const date = new Date(day.tradedatetimegmt.split(' ')[0]);
date.setDate(date.getDate() + 1); // is day ahead, duh...
let mappedDay = {
date,
rawPrice: Number(day.close),
descr: this.tmpZone,
};
// Check if last daily entry is closed // after 20:00 CET?
if ((idx === dailyInfo.length - 1)
&& mappedDay.date.getDay() !== 6 // is actually price for monday after the weekend
&& !lastDailyIsClosed) mappedDay = null; // ignore last day info because it is not closed yet
// Check if last entry is weekend info, and is closed
if (weekendInfo && mappedDay && mappedDay.date.getDay() === 6) {
// console.log('it is weekend');
const [weekend] = weekendInfo.filter((dayW) => dayW.tradedatetimegmt === day.tradedatetimegmt);
const satTime = new Date(weekend.tradedatetimegmt.split(' ')[0]);
satTime.setDate(satTime.getDate() + 1); // is day ahead, duh...
const sat = {
date,
rawPrice: Number(weekend.close),
descr: this.tmpZone,
};
const sun = { ...sat };
sun.date = new Date(sun.date);
sun.date.setDate(sun.date.getDate() + 1); // add a day, knows about DST I hope...
const mon = { ...mappedDay };
mon.date = new Date(mon.date);
mon.date.setDate(mon.date.getDate() + 2); // add a weekend, knows about DST I hope...
resp.push(sat, sun, mon); // add weekend and monday
mappedDay = null; // skipp adding normal weekday info
}
// Add normal weekday info
if (mappedDay) resp.push(mappedDay);
});
// console.dir(resp, { depth: null });
// console.dir(resp.filter((info) => info.descr === this.biddingZone), { depth: null });
return Promise.resolve(resp);
}
throw Error(`No XML or JSON received: ${contentType}`);
} catch (error) {
return Promise.reject(error);
}
}
_makeHttpsRequest(options, postData, timeout) {
return new Promise((resolve, reject) => {
if (!this.httpsAgent) {
const agentOptions = {
rejectUnauthorized: false,
};
this.httpsAgent = new https.Agent(agentOptions);
}
const opts = options;
opts.timeout = timeout || this.timeout;
const req = https.request(opts, (res) => {
let resBody = '';
res.on('data', (chunk) => {
resBody += chunk;
});
res.once('end', () => {
this.lastResponse = resBody;
if (!res.complete) {
return reject(Error('The connection was terminated while the message was still being sent'));
}
res.body = resBody;
return resolve(res);
});
});
req.on('error', (e) => {
req.destroy();
this.lastResponse = e;
return reject(e);
});
req.on('timeout', () => {
req.destroy();
});
req.end(postData);
});
}
}
module.exports = EEX;
// // START TEST HERE
// const test = async () => {
// const next = new EEX({ biddingZone: 'TTF_EGSI' }); // 'TTF_EOD'
// // next.lastDailyInfo = [
// // {
// // close: 59.83,
// // onexchtradevolumeeex: 3369840,
// // tradedatetimegmt: '1/16/2023 12:00:00 PM',
// // },
// // {
// // close: 59.83,
// // onexchtradevolumeeex: 3679632,
// // tradedatetimegmt: '1/17/2023 12:00:00 PM',
// // },
// // {
// // close: 60.01,
// // onexchtradevolumeeex: 3719736,
// // tradedatetimegmt: '1/18/2023 12:00:00 PM',
// // },
// // {
// // close: 61.271,
// // onexchtradevolumeeex: 3815928,
// // tradedatetimegmt: '1/19/2023 12:00:00 PM',
// // },
// // {
// // close: 62.125,
// // onexchtradevolumeeex: 554952,
// // tradedatetimegmt: '1/20/2023 12:00:00 PM',
// // },
// // ];
// const today = new Date();
// today.setHours(0);
// const yesterday = new Date(today);
// const tomorrow = new Date(today);
// yesterday.setDate(yesterday.getDate() - 1);
// tomorrow.setDate(tomorrow.getDate() + 3);
// const result = await next.getPrices({ dateStart: yesterday, dateEnd: tomorrow }).catch((error) => console.log(error));
// console.dir(result, { depth: null });
// // const result2 = await next.getPrices({ dateStart: '2024-04-06T22:00:00.000Z', dateEnd: '2024-04-08T22:00:00.000Z' }).catch((error) => console.log(error));
// // console.dir(result2, { depth: null });
// };
// test();
/*
[
{ time: 2022-12-13T23:00:00.000Z, price: 1344.6309078 },
{ time: 2022-12-14T00:00:00.000Z, price: 1344.6309078 },
{ time: 2022-12-14T01:00:00.000Z, price: 1344.6309078 },
{ time: 2022-12-14T02:00:00.000Z, price: 1344.6309078 },
{ time: 2022-12-14T03:00:00.000Z, price: 1344.6309078 },
{ time: 2022-12-14T04:00:00.000Z, price: 1344.6309078 },
{ time: 2022-12-14T05:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T06:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T07:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T08:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T09:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T10:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T11:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T12:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T13:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T14:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T15:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T16:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T17:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T18:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T19:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T20:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T21:00:00.000Z, price: 1348.5972841999999 },
{ time: 2022-12-14T22:00:00.000Z, price: 1348.5972841999999 }
]
today:
https://webservice-eex.gvsi.com/query/json/getQuotes/ontradeprice/onexchsingletradevolume/close/onexchtradevolumeeex/?priceSymbol=%22%23E.TTF_GND1%22
close: null
onexchsingletradevolume: 1200
onexchtradevolumeeex: 307728
ontradeprice: 56.7
today graph: results.items
https://webservice-eex.gvsi.com/query/json/getQuotes/settledate/dir/?priceSymbol=%22%23E.TTF_GND1%22
dir: 56.75
settledate: null
all graph: results.item
https://webservice-eex.gvsi.com/query/json/getDaily/close/onexchtradevolumeeex/tradedatetimegmt/?priceSymbol=%22%23E.TTF_GND1%22&chartstartdate=2022%2F12%2F01&chartstopdate=2023%2F01%2F16&dailybarinterval=Days&aggregatepriceselection=First
close: 56.75
onexchtradevolumeeex: 324264
tradedatetimegmt: "1/16/2023 12:00:00 PM"
*/