-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdemo.js
201 lines (194 loc) · 8.95 KB
/
demo.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
/*jslint this: true, browser: true, long: true, bitwise: true, unordered: true */
/*global window console demonstrationHelper ParserProtobuf protobuf priceSubscriptionHelper InstrumentRow */
/**
* Follows WebSocket behaviour defined by spec:
* https://html.spec.whatwg.org/multipage/web-sockets.html
*/
(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"),
"assetTypesList": document.getElementById("idCbxAssetType"), // Optional
"selectedAssetType": "FxSpot", // Only FX has realtime prices, if Live account is not linked
"footerElm": document.getElementById("idFooter"),
"newTokenCallback": function (accessToken) {
// This doesn't work with the Implicit Flow, used in this sample!
priceSubscription.extendSubscription(accessToken);
}
});
const priceSubscription = priceSubscriptionHelper(demo);
/**
* Find futures by FutureSpaceId.
* @param {number} futureSpaceId ID from the search.
* @return {void}
*/
function findFutureContracts(futureSpaceId) {
fetch(
demo.apiUrl + "/ref/v1/instruments/futuresspaces/" + futureSpaceId,
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
const instrumentList = [];
responseJson.Elements.forEach(function (futureContract) {
instrumentList.push(futureContract.Uic);
});
priceSubscription.clearList(); // Empty the instrument list
priceSubscription.subscribeToList(instrumentList, document.getElementById("idCbxAssetType").value);
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* Find options by ContractRootId.
* @param {number} optionRootId ID from the search.
* @return {void}
*/
function findOptionContracts(optionRootId) {
fetch(
demo.apiUrl + "/ref/v1/instruments/contractoptionspaces/" + optionRootId,
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
const instrumentList = [];
responseJson.OptionSpace[0].SpecificOptions.forEach(function (optionContract) {
instrumentList.push(optionContract.Uic);
});
priceSubscription.clearList(); // Empty the instrument list
priceSubscription.subscribeToList(instrumentList, document.getElementById("idCbxAssetType").value);
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* This is an example of getting all exchanges.
* @return {void}
*/
function getExchanges() {
const cbxExchange = document.getElementById("idCbxExchange");
cbxExchange.options.length = 1; // Remove all, except the first
fetch(
demo.apiUrl + "/ref/v1/exchanges?$top=1000", // Get the first 1.000 (actually there are around 200 exchanges available)
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
responseJson.Data.sort(function (a, b) {
const nameA = a.Name.toUpperCase();
const nameB = b.Name.toUpperCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
});
// Populate the list of exchanges, so instruments can be filtered on Exchange
responseJson.Data.forEach(function (exchange) {
const option = document.createElement("option");
option.text = exchange.Name + " (code " + exchange.ExchangeId + ", mic " + exchange.Mic + ")";
option.value = exchange.ExchangeId;
cbxExchange.add(option);
});
console.log("Found " + responseJson.Data.length + " exchanges:\n\n" + JSON.stringify(responseJson, null, 4));
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* Find instruments of the selected AssetType, to create a list with prices.
* @return {void}
*/
function find() {
const assetType = document.getElementById("idCbxAssetType").value;
const keywords = document.getElementById("idInstrumentName").value + (
assetType === "ContractFutures"
? " continuous" // By adding this, non tradable FuturesSpaces can be found
: ""
);
let url = demo.apiUrl + "/ref/v1/instruments?AssetTypes=" + assetType + "&IncludeNonTradable=true&$top=100" + "&AccountKey=" + encodeURIComponent(demo.user.accountKey) + "&Keywords=" + encodeURIComponent(keywords);
if (document.getElementById("idCbxExchange").value !== "-") {
url += "&ExchangeId=" + encodeURIComponent(document.getElementById("idCbxExchange").value);
}
priceSubscription.connect(document.getElementById("idBearerToken").value);
// Search for instruments
// You can search for an ISIN. That will work. But due to market limitations the ISIN won't be in the response.
fetch(
url,
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
const instrumentList = [];
let instrument;
if (responseJson.Data.length > 0) {
instrument = responseJson.Data[0]; // Just take the first instrument - it's a demo
if (assetType === "ContractFutures" && instrument.hasOwnProperty("DisplayHint") && instrument.DisplayHint === "Continuous") {
// We found an future root - get the series
findFutureContracts(instrument.Identifier);
} else if (instrument.SummaryType === "ContractOptionRoot") {
// We found an option root - get the series
findOptionContracts(instrument.Identifier);
} else {
responseJson.Data.forEach(function (instrument) {
instrumentList.push(instrument.Identifier);
});
priceSubscription.clearList(); // Empty the instrument list
priceSubscription.subscribeToList(instrumentList, document.getElementById("idCbxAssetType").value);
}
} else {
console.error("No instruments found...");
}
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
demo.setupEvents([
{"evt": "click", "elmId": "idBtnGetExchanges", "func": getExchanges, "funcsToDisplay": [getExchanges]},
{"evt": "click", "elmId": "idBtnFind", "func": find, "funcsToDisplay": [find, priceSubscription.subscribeToList]}
]);
demo.displayVersion("trade");
}());