-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdemo.js
More file actions
341 lines (321 loc) · 14.4 KB
/
demo.js
File metadata and controls
341 lines (321 loc) · 14.4 KB
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
/*jslint browser: true, long: true, for: true, unordered: true */
/*global window console demonstrationHelper */
(function () {
// Create a helper function to remove some boilerplate code from the example itself.
const demo = demonstrationHelper({
"responseElm": document.getElementById("idResponse"),
"javaScriptElm": document.getElementById("idJavaScript"),
"accessTokenElm": document.getElementById("idBearerToken"),
"retrieveTokenHref": document.getElementById("idHrefRetrieveToken"),
"tokenValidateButton": document.getElementById("idBtnValidate"),
"accountsList": document.getElementById("idCbxAccount"),
"footerElm": document.getElementById("idFooter")
});
const defaultExchangeId = "EUR_AMS2";
let exchanges = null;
let countries = null;
let currencies = null;
let timeZones = null;
/**
* Find the country in the cached countries list.
* @param {string} countryCode The 2-digit country code (A2 in ISO 3166 standard).
* @return {Object} The country
*/
function getCountry(countryCode) {
const countryObject = countries.find(function (country) {
return country.CountryCode === countryCode;
});
if (countryObject === undefined) {
console.error("CountryCode not found in /countries: " + countryCode);
return {
"DisplayName": "???"
};
}
return countryObject;
}
/**
* Find the time zone in the cached timeZones list.
* @param {Object} exchange The exchange containing the time zone identifier.
* @return {Object} The timeZone object
*/
function getTimeZone(exchange) {
const timeZoneObject = timeZones.find(function (timeZone) {
return timeZone.TimeZoneId === exchange.TimeZone.toString(); // Requested to be fixed at Saxo side (type & name), because this shouldn't be nessesary.
});
if (timeZoneObject === undefined) {
console.error(exchange.Name + " (MIC " + exchange.Mic + ") refers to TimeZone " + exchange.TimeZone + ", which is not available in /ref/v1/timezones - TimeZoneOffset: " + exchange.TimeZoneOffset + (
(exchange.hasOwnProperty("TimeZoneAbbreviation") && exchange.TimeZoneAbbreviation !== "")
? " and TimeZoneAbbreviation: " + exchange.TimeZoneAbbreviation
: ""
) + " (country " + getCountry(exchange.CountryCode).Name + " - " + exchange.CountryCode + ").");
return {
"DisplayName": "???",
"TimeZoneAbbreviation": exchange.TimeZoneAbbreviation,
"ZoneName": "???",
"TimeZoneOffset": exchange.TimeZoneOffset
};
}
return timeZoneObject;
}
/**
* Add options to the dropdowns.
* @return {void}
*/
function populateDropdowns() {
/**
* Remove all items from a combo box.
* @param {Object} listElement The combo box element.
* @return {void}
*/
function clearList(listElement) {
let i;
for (i = listElement.options.length - 1; i >= 0; i -= 1) {
listElement.remove(i);
}
}
/**
* Add an option to a combo box.
* @param {Object} listElement The combo box element.
* @param {string} displayText The text that will be visible.
* @param {string} value The value.
* @param {boolean} isSelected Default selected option.
* @return {void}
*/
function addOption(listElement, name, value, isSelected) {
const option = document.createElement("option");
option.text = name;
option.value = value;
if (isSelected) {
option.setAttribute("selected", true);
}
listElement.add(option);
}
/**
* Sort list and add to a combo box.
* @param {Array[Array<string>}} arr The array with options.
* @param {string} elmId The id of the element.
* @param {string} selectedValue The value to select.
* @return {void}
*/
function populateSelect(arr, elmId, selectedValue) {
const listElement = document.getElementById(elmId);
clearList(listElement);
arr.sort(function (a, b) {
return a[0].localeCompare(b[0], undefined, {sensitivity: "base"});
});
arr.forEach(function (values) {
addOption(listElement, values[0], values[1], values[1] === selectedValue);
});
}
const exchangeNameArray = [];
const micArray = [];
const exchangeIdArray = [];
const countryArray = [];
const countryArrayIndex = [];
exchanges.forEach(function (exchange) {
exchangeNameArray.push([exchange.Name, exchange.ExchangeId]);
micArray.push([exchange.Mic + " (" + exchange.Name + ")", exchange.ExchangeId]);
exchangeIdArray.push([exchange.ExchangeId + " (" + exchange.Name + ")", exchange.ExchangeId]);
if (countryArrayIndex.indexOf(exchange.CountryCode) === -1) {
countryArrayIndex.push(exchange.CountryCode);
countryArray.push([getCountry(exchange.CountryCode).DisplayName + " (" + exchange.CountryCode + ")", exchange.CountryCode]);
}
});
populateSelect(exchangeNameArray, "idCbxExchangeName", defaultExchangeId);
populateSelect(micArray, "idCbxOperationalMic", defaultExchangeId);
populateSelect(exchangeIdArray, "idCbxExchangeId", defaultExchangeId);
populateSelect(countryArray, "idCbxCountry", "NL");
}
/**
* Display trading sessions and other market information.
* @return {void}
*/
function displayExchangeDetails(exchangeId) {
function convertToLocalTime(utcTime, offset) {
// A TimeSpan value can be represented as [-]hh:mm:ss, where the optional minus sign indicates a negative time interval,
// the hh component is hours as measured on a 24-hour clock, mm is minutes, and ss is seconds.
// Examples of offsets are "02:00:00" and "-05:00:00"
// https://docs.microsoft.com/en-us/dotnet/api/system.timespan
const sign = (
offset.charAt(0) === "-"
? "-"
: ""
);
if (sign === "-") {
offset = offset.substring(1);
}
const localTime = new Date();
const hours = parseInt(sign + offset.substring(0, 2), 10);
const minutes = parseInt(sign + offset.substring(3, 5), 10);
const seconds = parseInt(sign + offset.substring(6, 8), 10);
const offsetInMs = hours * 60 * 60 * 1000 + minutes * 60 * 1000 + seconds * 1000;
localTime.setTime(utcTime.getTime() + offsetInMs);
return localTime;
}
function collectSessions(exchangeObject, isExchangeLocalTimeRequested) {
let result = "";
exchangeObject.ExchangeSessions.forEach(function (session) {
const startTime = new Date(session.StartTime);
const endTime = new Date(session.EndTime);
const timeZoneUtc = {
"timeZone": "UTC"
};
if (isExchangeLocalTimeRequested) {
result += " " + convertToLocalTime(startTime, exchangeObject.TimeZoneOffset).toLocaleString(undefined, timeZoneUtc);
result += " - " + convertToLocalTime(endTime, exchangeObject.TimeZoneOffset).toLocaleString(undefined, timeZoneUtc);
} else {
result += " " + startTime.toLocaleString() + " - " + endTime.toLocaleString();
}
result += ": " + session.State;
if (session.State === "AutomatedTrading") {
result += " (regular trading session)";
}
result += "\n";
});
return result;
}
// No need to request the individual exchange, they are all in cache
const exchangeObject = exchanges.find(function (exchange) {
return exchange.ExchangeId === exchangeId;
});
const currencyObject = currencies.find(function (currency) {
return currency.CurrencyCode === exchangeObject.Currency;
});
const timeZoneObject = getTimeZone(exchangeObject);
let details = "Selected Exchange: " + exchangeObject.Name + "\n";
console.log("Collecting exchange details for ExchangeId " + exchangeId);
details += "Country: " + getCountry(exchangeObject.CountryCode).Name + "\n";
details += "Operational MIC (not unique): " + exchangeObject.Mic + "\n";
details += "Currency: " + currencyObject.Name + (
currencyObject.hasOwnProperty("Symbol")
? " (" + currencyObject.Symbol + ")"
: ""
) + "\n";
if (timeZoneObject.DisplayName !== "") {
details += "TimeZone: " + timeZoneObject.DisplayName + " ";
}
if (timeZoneObject.TimeZoneAbbreviation !== "") {
details += "(" + timeZoneObject.TimeZoneAbbreviation + ")";
} else {
details += "ZoneName: " + timeZoneObject.ZoneName;
}
details += "\n\nExchange Sessions (converted to your time, " + (
exchangeObject.AllDay
? ""
: "not "
) + "all day):\n";
if (exchangeObject.ExchangeSessions.length === 0) {
details += "None (is this a NAV trading platform?)\n"; // This can be the case when it is a NAV trading platform
} else {
details += collectSessions(exchangeObject, false);
details += "\nExchange Sessions in Exchange Local Time using UTC-offset " + timeZoneObject.TimeZoneOffset + ":\n";
details += collectSessions(exchangeObject, true);
}
console.log(details + "\n" + JSON.stringify(exchangeObject, null, 4));
}
/**
* This is an example of getting Exchanges and other relevant info and wait for the last response.
* @return {void}
*/
function getData() {
function requestData(urlPath) {
return fetch(
demo.apiUrl + urlPath,
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
);
}
// Do multiple requests in parallel. HTTP/2 handles this.
// IE doesn't support Promise, but there is a polyfill, if you require one.
Promise.all([
requestData("/ref/v1/exchanges?$top=1000"), // Get the first 1.000 (actually there are around 225 exchanges available)
requestData("/ref/v1/countries"),
requestData("/ref/v1/currencies"),
requestData("/ref/v1/timezones")
]).then(function (responses) {
return Promise.all(responses.map(function (response) {
if (response.ok) {
return response.json();
}
demo.processError(response);
}));
}).then(function (responsesJson) {
let requestResult = "";
responsesJson.forEach(function (responseJson, requestIndex) {
switch (requestIndex) {
case 0:
exchanges = responseJson.Data;
requestResult += "Exchanges found: " + responseJson.Data.length + "\n";
break;
case 1:
countries = responseJson.Data;
requestResult += "Countries found: " + responseJson.Data.length + "\n";
break;
case 2:
currencies = responseJson.Data;
requestResult += "Currencies found: " + responseJson.Data.length + "\n";
break;
case 3:
timeZones = responseJson.Data;
requestResult += "TimeZones found: " + responseJson.Data.length + "\n";
break;
default:
throw "Unexpected request index: " + requestIndex;
}
});
console.log(requestResult);
populateDropdowns();
displayExchangeDetails(defaultExchangeId);
}).catch(function (error) {
console.error(error);
});
}
/**
* Show details for the selected exchange.
* @return {void}
*/
function changeExchangeNameList() {
displayExchangeDetails(document.getElementById("idCbxExchangeName").value);
}
/**
* Show details for the selected MIC.
* @return {void}
*/
function changeMicList() {
displayExchangeDetails(document.getElementById("idCbxOperationalMic").value);
}
/**
* Show details for the selected MIC.
* @return {void}
*/
function changeExchangeIdList() {
displayExchangeDetails(document.getElementById("idCbxExchangeId").value);
}
/**
* Show the exchanges located in the selected country.
* @return {void}
*/
function changeCountryList() {
const countryCode = document.getElementById("idCbxCountry").value;
const exchangeNames = [];
exchanges.forEach(function (exchange) {
if (exchange.CountryCode === countryCode) {
exchangeNames.push(" " + exchange.Name);
}
});
exchangeNames.sort();
console.log("Available Exchanges:\n" + exchangeNames.join("\n"));
}
demo.setupEvents([
{"evt": "change", "elmId": "idCbxExchangeName", "func": changeExchangeNameList, "funcsToDisplay": [changeExchangeNameList, displayExchangeDetails, getCountry]},
{"evt": "change", "elmId": "idCbxOperationalMic", "func": changeMicList, "funcsToDisplay": [changeMicList, displayExchangeDetails, getCountry]},
{"evt": "change", "elmId": "idCbxExchangeId", "func": changeExchangeIdList, "funcsToDisplay": [changeExchangeIdList, displayExchangeDetails, getCountry]},
{"evt": "change", "elmId": "idCbxCountry", "func": changeCountryList, "funcsToDisplay": [changeCountryList]},
{"evt": "click", "elmId": "idBtnGetData", "func": getData, "funcsToDisplay": [getData, populateDropdowns]}
]);
demo.displayVersion("ref");
}());