-
Notifications
You must be signed in to change notification settings - Fork 25
/
demo.js
308 lines (299 loc) · 13.7 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
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
/*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")
});
let lastOrderId = "0";
/**
* Helper function to convert the json string to an object, with error handling.
* @return {Object} The newOrderObject from the input field - null if invalid
*/
function getOrderObjectFromJson() {
let newOrderObject = null;
try {
newOrderObject = JSON.parse(document.getElementById("idNewOrderObject").value);
if (newOrderObject.hasOwnProperty("AccountKey")) {
// This is the case for single orders, or conditional/related orders
// This function is used for other order types as well, so more order types are considered
newOrderObject.AccountKey = demo.user.accountKey;
}
if (newOrderObject.hasOwnProperty("Orders")) {
// This is the case for OCO, related and conditional orders
newOrderObject.Orders.forEach(function (order) {
if (order.hasOwnProperty("AccountKey")) {
order.AccountKey = demo.user.accountKey;
}
});
}
document.getElementById("idNewOrderObject").value = JSON.stringify(newOrderObject, null, 4);
} catch (e) {
console.error(e);
}
return newOrderObject;
}
/**
* Add the strategies which are allowed for this instrument.
* @param {Array} Strategies The strategies to be added.
* @return {void}
*/
function populateSupportedStrategies(strategies) {
const cbxStrategy = document.getElementById("idCbxStrategy");
let option;
let i;
for (i = cbxStrategy.options.length - 1; i >= 0; i -= 1) {
cbxStrategy.remove(i);
}
strategies.sort();
strategies.forEach(function (strategy) {
option = document.createElement("option");
option.text = strategy;
option.value = strategy;
cbxStrategy.add(option);
});
}
/**
* Modify the order object so the elements comply to the strategy.
* @return {void}
*/
function selectStrategy() {
const selectedStrategy = document.getElementById("idCbxStrategy").value;
fetch(
demo.apiUrl + "/ref/v1/algostrategies/" + encodeURIComponent(selectedStrategy),
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
const newOrderObject = getOrderObjectFromJson();
const algoOrderData = {
"StrategyName": selectedStrategy,
"Arguments": {}
};
let explanation = "Strategy: " + responseJson.Description;
// Populate the strategy arguments:
responseJson.Parameters.forEach(function (parameter) {
let value; // Used to display (dummy!) data
explanation += "\n\nInput field " + parameter.UiOrderingIndex + ": " + parameter.Description;
explanation += "\nDisplayName: " + parameter.DisplayName + " (" + (
parameter.IsMandatory === true
? "required"
: "optional"
);
if (parameter.hasOwnProperty("UiDefaultValue")) {
value = parameter.UiDefaultValue;
explanation += ", default " + value;
} else {
// Just add some random values:
switch (parameter.DataType.toLowerCase()) {
case "char": // Single character string
value = "a";
break;
case "int": // Integer
value = 42;
break;
case "price": // Price specified as a decimal number
value = 23.0037;
break;
case "qty": // Quantity specified as an integer
value = 100;
break;
case "string": // A "choose one from a list" value like a dropdown
value = "Bladiebla"; // Little chance there is no list..
break;
case "utctimestamp": // TimeStamp (not a full date) provided as a string (3 2-digit numeric inputs)
value = "13:27:53";
break;
default:
console.error("Unsupported DataType: " + parameter.DataType);
}
}
if (parameter.hasOwnProperty("UiStepSize")) {
explanation += ", steps of " + parameter.UiStepSize;
}
if (parameter.hasOwnProperty("MinFloatValue")) {
explanation += ", min. " + parameter.MinFloatValue;
// Make sure the dummy value fits into the range:
value = Math.max(parameter.MinFloatValue, value);
}
if (parameter.hasOwnProperty("MaxFloatValue")) {
explanation += ", max. " + parameter.MaxFloatValue;
// Make sure the dummy value fits into the range:
value = Math.min(parameter.MaxFloatValue, value);
}
explanation += ")";
if (parameter.hasOwnProperty("ParameterValues")) {
explanation += "\nValues:";
parameter.ParameterValues.forEach(function (parameterValue, i) {
if (i === 0) {
value = parameterValue.Value;
}
explanation += "\n- " + parameterValue.Name + ": " + parameterValue.Value;
});
}
algoOrderData.Arguments[parameter.Name] = value;
});
// And add it to the newOrderData:
newOrderObject.AlgoOrderData = algoOrderData;
document.getElementById("idNewOrderObject").value = JSON.stringify(newOrderObject, null, 4);
console.log(explanation + "\n\nResponse:\n" + JSON.stringify(responseJson, null, 4));
if (responseJson.SupportedOrderTypes.indexOf(newOrderObject.OrderType) === -1) {
window.alert("Unsupported order type. Supported types are: " + responseJson.SupportedOrderTypes.join(", "));
}
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* Get the instrument details too lookup the strategies.
* @return {void}
*/
function getStrategies() {
const newOrderObject = getOrderObjectFromJson();
fetch(
demo.apiUrl + "/ref/v1/instruments/details/" + newOrderObject.Uic + "/" + newOrderObject.AssetType + "?AccountKey=" + encodeURIComponent(demo.user.accountKey) + "&FieldGroups=OrderSetting",
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
const instrumentName = responseJson.AssetType + " " + responseJson.Description + " (Uic " + responseJson.Uic + ")";
if (responseJson.hasOwnProperty("SupportedStrategies") && responseJson.SupportedStrategies.length > 0) {
populateSupportedStrategies(responseJson.SupportedStrategies);
console.log("Found strategies for " + instrumentName + ":\n\n" + responseJson.SupportedStrategies.join("\n"));
} else {
console.error("Instrument " + instrumentName + " doesn't support Algo Orders.");
}
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* This is an example of placing an OCO order.
* @return {void}
*/
function placeNewOrder() {
const newOrderObject = getOrderObjectFromJson();
const headersObject = {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value,
"Content-Type": "application/json; charset=utf-8"
};
fetch(
demo.apiUrl + "/trade/v2/orders",
{
"method": "POST",
"headers": headersObject,
"body": JSON.stringify(newOrderObject)
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
const xRequestId = response.headers.get("X-Request-ID");
console.log("Successful request:\n" + JSON.stringify(responseJson, null, 4) + (
xRequestId === null
? ""
: "\nX-Request-ID response header: " + xRequestId
));
lastOrderId = responseJson.OrderId;
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* This is an example of updating a single leg order.
* @return {void}
*/
function modifyLastOrder() {
const newOrderObject = getOrderObjectFromJson();
const headersObject = {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value,
"Content-Type": "application/json; charset=utf-8"
};
newOrderObject.OrderId = lastOrderId;
fetch(
demo.apiUrl + "/trade/v2/orders",
{
"method": "PATCH",
"headers": headersObject,
"body": JSON.stringify(newOrderObject)
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
const xRequestId = response.headers.get("X-Request-ID");
console.log("Successful request:\n" + JSON.stringify(responseJson, null, 4) + (
xRequestId === null
? ""
: "\nX-Request-ID response header: " + xRequestId
));
});
} else {
// If you get a 404 NotFound, the order might already be executed!
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* This is an example of removing an order.
* @return {void}
*/
function cancelLastOrder() {
fetch(
demo.apiUrl + "/trade/v2/orders/" + lastOrderId + "?AccountKey=" + encodeURIComponent(demo.user.accountKey),
{
"method": "DELETE",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
// Response must have an OrderId
console.log(JSON.stringify(responseJson, null, 4));
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
demo.setupEvents([
{"evt": "change", "elmId": "idCbxStrategy", "func": selectStrategy, "funcsToDisplay": [selectStrategy]},
{"evt": "click", "elmId": "idBtnGetStrategies", "func": getStrategies, "funcsToDisplay": [getStrategies]},
{"evt": "click", "elmId": "idBtnPlaceNewOrder", "func": placeNewOrder, "funcsToDisplay": [placeNewOrder]},
{"evt": "click", "elmId": "idBtnModifyLastOrder", "func": modifyLastOrder, "funcsToDisplay": [modifyLastOrder]},
{"evt": "click", "elmId": "idBtnCancelLastOrder", "func": cancelLastOrder, "funcsToDisplay": [cancelLastOrder]}
]);
demo.displayVersion("trade");
}());