-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.js
More file actions
206 lines (194 loc) · 6.02 KB
/
Copy pathcode.js
File metadata and controls
206 lines (194 loc) · 6.02 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
/**
* @license
* Copyright (c) 2017 EXANTE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* @file Google Apps Script shows how to work with EXANTE API functions
* @author EXANTE
*/
/**
* workaround to ask permissions only for active spreadsheet, see
* https://developers.google.com/apps-script/guides/services/authorization
* for details
* @OnlyCurrentDoc
*/
/**
* base URL part
* @constant {string}
*/
var BASE_URL_API = "/md/1.0";
/**
* base URL part, edit for other environments
* @constant {string}
*/
var BASE_URL_HOST = "https://api-demo.exante.eu";
/**
* base URL
* @constant {string}
*/
var BASE_URL = BASE_URL_HOST + BASE_URL_API;
/**
* fields which are described in /symbol/:symbolId/specification instead
* of default API
* @constant {string[]}
*/
var SYMBOL_SPEC_FIELDS = ["leverage", "lotSize", "contractMultiplier", "priceUnit", "units"];
/**
* auth token, generated from jwt.io. See https://developers.exante.eu/tutorials/auth-basics/
* for more details
* @constant {string}
*/
var TOKEN = "paste-your-token-here";
/**
* default payload for get requests
* @returns {object} payload object, see
* https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
* for details
*/
function _payload() {
return {
"method": "get",
"headers": {
"Authorization": "Bearer " + TOKEN
}
};
}
/**
* default get request worker
* @param {string} url
* url to fetch GET request
* @returns {object} parsed JSON object as is
*/
function _parse(url) {
var response = UrlFetchApp.fetch(url, _payload());
var code = result.getResponseCode();
if (code != 200)
throw new Error(response.message);
return JSON.parse(response.getContentText());
}
/**
* crossrates for currencies
* @param {string} from
* convert from currency
* @param {string} to
* convert to currency
* @param {string} [timestamp]
* dummy parameter for update feature
* @returns {Number} coversion value
* @customfunction
*/
function EXANTECROSSRATES(from, to, timestamp) {
var url = BASE_URL + "/crossrates/" + from + "/" + to;
return _parse(url)["rate"];
}
/**
* symbols group information
* @param {string} group
* group name
* @param {string} field
* property name
* @example
* var name = EXANTEGROUP("Si", "name");
* @returns {number|string} property value for specified group
* @customfunction
*/
function EXANTEGROUP(group, field) {
var url = BASE_URL + "/groups/" + group;
return _parse(url)[field];
}
/**
* nearest symbol of group information
* @param {string} group
* group name
* @param {string} field
* property name
* @example
* var id = EXANTEGROUPNEAREST("Si", "id");
* @returns {number|string} property value for nearest expiration of specified group
* @customfunction
*/
function EXANTEGROUPNEAREST(group, field) {
var url = BASE_URL + "/groups/" + group + "/nearest";
return _parse(url)[field];
}
/**
* open-high-low-close value
* @param {string} symbol
* symbol ID
* @param {string|number} duration
* OHLC duration in seconds
* @param {string} what
* OHCL field, normally one of "open", "high", "low", "close", "timestamp"
* @param {string} [timestamp]
* dummy parameter for update feature
* @example
* var openPrice = EXANTEOHLC("EUR/USD.E.FX", 60, "open");
* var highPrice = EXANTEOHLC("EUR/USD.E.FX", 60, "high");
* var lowPrice = EXANTEOHLC("EUR/USD.E.FX", 60, "low");
* var closePrice = EXANTEOHLC("EUR/USD.E.FX", 60, "close");
* @returns {number} OHLC property for specified symbol and duration
* @customfunction
*/
function EXANTEOHLC(symbol, duration, what, timestamp) {
var url = BASE_URL + "/ohlc/" + encodeURIComponent(symbol) + "/" + duration + "?size=1";
return _parse(url)[0][what];
}
/**
* symbol information
* @param {string} symbol
* symbol ID
* @param {string} field
* property name
* @example
* var description = EXANTESYMBOL("AAPL.NASDAQ", "description");
* @returns {string|number} property value for specified symbol
* @customfunction
*/
function EXANTESYMBOL(symbol, field) {
var url = BASE_URL + "/symbols/" + encodeURIComponent(symbol);
if (field in SYMBOL_SPEC_FIELDS)
url += "/specification";
return _parse(url)[field];
}
/**
* dummy functions which modifies A1 cell and insert current timestamp value
* this method is actually only required for update feature (see timestamp parameter
* in some methods), because Google Sheets does not update functions if no parameters
* were changed, but NOW() function is not allowed to be called inside user defined
* functions
*/
function EXANTEUPDATE() {
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(new Date().toTimeString());
SpreadsheetApp.flush();
}
/**
* mid (average between bid and ask) value
* @param {string} symbol
* symbol ID
* @param {string} [timestamp]
* dummy parameter for update feature
* @example
* var mid = EXANTEMID("EUR/USD.E.FX");
* @returns {number} mid value for specified symbol
* @customfunction
*/
function EXANTEMID(symbol, timestamp) {
return EXANTEOHLC(symbol, 60, "close");
}