-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdemo.js
114 lines (110 loc) · 5.91 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
/*jslint this: true, browser: true, long: 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")
});
/**
* This is an example of getting the margin.
* @param {string} url The URL with query parameters to get balances.
* @param {result} result Logging of the steps taken, to show in the console.
* @return {void}
*/
function getBalances(url, result) {
fetch(
demo.apiUrl + url,
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
response.json().then(function (responseJson) {
// Add some formatting, to respect the number of decimals
let initialMarginAvailable = responseJson.MarginAvailableForTrading.toLocaleString(undefined, {
minimumFractionDigits: responseJson.CurrencyDecimals,
maximumFractionDigits: responseJson.CurrencyDecimals
});
let marginUtilisation = responseJson.MarginUtilizationPct.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
result += "\n\nMargin available for trading: " + responseJson.Currency + " " + initialMarginAvailable + " (utilisation " + marginUtilisation + "%)";
result += "\n\nResponse: " + JSON.stringify(responseJson, null, 4);
console.log(result);
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
/**
* This is an example of getting the margin.
* @return {void}
*/
function getMargin() {
// First, get the account details to see if it is cross margined...
fetch(
demo.apiUrl + "/port/v1/accounts/" + encodeURIComponent(demo.user.accountKey),
{
"method": "GET",
"headers": {
"Authorization": "Bearer " + document.getElementById("idBearerToken").value
}
}
).then(function (response) {
if (response.ok) {
// Second, identify the margin settings for this account...
response.json().then(function (responseJson) {
let result = "Account settings for the margin calculation (account " + responseJson.AccountKey + "):";
let balancesUrl = "/port/v1/balances?FieldGroups=MarginOverview&ClientKey=" + encodeURIComponent(responseJson.ClientKey);
if (responseJson.IsMarginTradingAllowed) {
result += "\nCanUseCashPositionsAsMarginCollateral: " + responseJson.CanUseCashPositionsAsMarginCollateral;
result += "\nUseCashPositionsAsMarginCollateral: " + responseJson.UseCashPositionsAsMarginCollateral;
result += "\nMarginCalculationMethod: " + responseJson.MarginCalculationMethod;
if (responseJson.IndividualMargining) {
// Margin is calculated based on the individual account:
balancesUrl += "&AccountKey=" + encodeURIComponent(responseJson.AccountKey);
result += "\nIndividualMargining applies. Margin is calculated on Account level..";
} else {
// Margin might be calculated on accountGroup level (if supported), or on client level:
if (responseJson.AccountGroupKey === responseJson.ClientKey) {
result += "\nCrossMargining, but no AccountGroups available. Margin is calculated on Client level..";
} else {
// AccountGroups are supported for this client - request on account group level:
balancesUrl += "&AccountGroupKey=" + encodeURIComponent(responseJson.AccountGroupKey);
result += "\nCrossMargining. Margin is calculated on AccountGroup level..";
}
}
result += "\n\nUsing endpoint: GET " + balancesUrl;
// Third, request the margin details by calling GET /balances...
getBalances(balancesUrl, result);
} else {
result += "\n\nMarging trading is not allowed for this account with type " + responseJson.AccountType + " (IsMarginTradingAllowed=false)";
console.log(result);
}
});
} else {
demo.processError(response);
}
}).catch(function (error) {
console.error(error);
});
}
demo.setupEvents([
{"evt": "change", "elmId": "idCbxAccount", "func": getMargin, "funcsToDisplay": [getMargin, getBalances], "isDelayedRun": true},
{"evt": "click", "elmId": "idBtnGetMargin", "func": getMargin, "funcsToDisplay": [getMargin, getBalances]}
]);
demo.displayVersion("port");
}());