-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-sheets-functions.js
484 lines (453 loc) · 17.4 KB
/
google-sheets-functions.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/**
* This file has several functions as crypto market tools which can be used with Google Sheets.
*
* In Google Sheets click on 'Tools -> Script Editor...' in the menu and save the code there.
* To get the value of a function simply enter '=functionName(parameter1, parameter2)' in a cell.
* For example: '=getTicker("GDAX", "last", "ETH-USD")'.
* If the Spreadsheet is in a locale which uses a comma as decimal mark you, need to use a semicolon
* as a parameter seperator. Like this: '=getTicker("GDAX"; "last"; "ETH-USD")'.
*
* It is very helpful to put a cell with a current date as an extra parameter to the functions
* like this: '=getTicker("GDAX", "last", "ETH-USD", A1)'. Every time the date is updated all the
* functions will update themselves as well. A refresh button in the menu for this is implemented with
* the 'onOpen' and the 'refreshLastUpdate' functions.
*/
/* globals SpreadsheetApp, UrlFetchApp */
/* exported refreshLastUpdate, onOpen, gdaxEthUsd, getTicker, getGdaxAverageUsd, getKrakenAverageEuro, updateHyperlinks */
/**
* Refreshes the time of last update.
*/
function refreshLastUpdate() {
SpreadsheetApp.getActiveSpreadsheet().getRange('B1').setValue(new Date().toTimeString());
}
/**
* Adds menu entries and opening commands.
*/
function onOpen() {
refreshLastUpdate();
var entries = [{
name: 'Refresh',
functionName: 'refreshLastUpdate'
},
{
name: 'Update Hyperlinks',
functionName: 'updateHyperlinks'
}
];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Custom', entries);
}
/**
* Simple function to get the last price of ETH/USD at gdax.com.
*
* @customfunction
*/
function gdaxEthUsd() {
return parseFloat(JSON.parse(UrlFetchApp.fetch('https://api.gdax.com/products/ETH-USD/ticker').getContentText())
.price);
}
/**
* Not so simple function to get the ticker of a specified exchange for a specific currency pair.
*
* @param {string} exchange The exchange to get ticker from. Options: 'GDAX', 'Poloniex', 'Kraken', 'Bittrex', 'Liqui', 'Coinmarketcap', 'Etherscan'.
* @param {string} type The type of ticker. For example: 'Last', 'Ask', 'Bid', 'High', 'Low', 'Average', 'Volume', 'QuoteVolume'.
* @param {string} pair The currency pair. For example: 'ETH-USD' for GDAX, 'BTC_ETH' for Poloniex, ''XXBTZEUR' for Kraken, 'wings_btc' for Liqui, 'BTC-WINGS' for Bittrex, 'ethusd' for Etherscan and 'ETHEREUM' for Coinmarketcap.
* @return The current ticker.
* @customfunction
*/
function getTicker(exchange, type, pair) {
var apiPath;
var response;
var data;
var high;
var low;
var result;
switch (exchange.toUpperCase()) {
case 'GDAX':
// Gets the ticker of the input currency pair at gdax.com.
var apiclass;
switch (type.toUpperCase()) {
case 'LAST':
case 'PRICE':
type = 'price';
apiclass = 'ticker';
break;
case 'ASK':
case 'SELL':
type = 'ask';
apiclass = 'ticker';
break;
case 'BID':
case 'BUY':
type = 'bid';
apiclass = 'ticker';
break;
case 'HIGH':
type = 'high';
apiclass = 'stats';
break;
case 'LOW':
type = 'low';
apiclass = 'stats';
break;
case 'AVG':
case 'AVERAGE': // not volume weighted
type = 'average';
apiclass = 'stats';
break;
case 'VOL':
case 'VOLUME':
case 'BASEVOLUME':
type = 'volume';
apiclass = 'ticker';
break;
case 'VOL_30D':
case 'VOLUME_30DAY':
type = 'volume_30day';
apiclass = 'stats';
break;
default:
throw new Error('Type "' + type + '" not found!');
}
apiPath = 'https://api.gdax.com/products/' + pair + '/' + apiclass;
response = UrlFetchApp.fetch(apiPath);
data = JSON.parse(response.getContentText());
if (type !== 'average') {
return parseFloat(data[type]);
} else {
// Calculate average
high = parseFloat(data.high);
low = parseFloat(data.low);
result = (high + low) / 2;
return +result.toFixed(8); // Only 8 decimal points
}
break;
case 'POLONIEX':
// Gets the ticker of the input currency pair at poloniex.com.
switch (type.toUpperCase()) {
case 'LAST':
case 'PRICE':
type = 'last';
break;
case 'ASK':
case 'SELL':
type = 'lowestAsk';
break;
case 'BID':
case 'BUY':
type = 'highestBid';
break;
case 'HIGH':
type = 'high24hr';
break;
case 'LOW':
type = 'low24hr';
break;
case 'AVG':
case 'AVERAGE': // not volume weighted
type = 'average';
break;
case 'VOL':
case 'VOLUME':
case 'BASEVOLUME':
type = 'baseVolume';
break;
case 'QUOTEVOLUME':
case 'VOLUME_CURRENCY':
type = 'quoteVolume';
break;
case 'QUOTEVOLUME':
case 'VOLUME_CURRENCY':
type = 'quoteVolume';
break;
case 'PERCENTCHANGE':
type = 'percentChange';
break;
default:
throw new Error('Type "' + type + '" not found!');
}
apiPath = 'https://poloniex.com/public?command=returnTicker';
response = UrlFetchApp.fetch(apiPath);
data = JSON.parse(response.getContentText());
if (type !== 'average') {
return parseFloat(data[pair][type]);
} else {
// Calculate average
high = parseFloat(data[pair].high24hr);
low = parseFloat(data[pair].low24hr);
result = (high + low) / 2;
return +result.toFixed(8); // Only 8 decimal points
}
break;
case 'KRAKEN':
// Gets the ticker of the input currency pair at kraken.com.
var index = -1;
switch (type.toUpperCase()) {
case 'LAST':
case 'PRICE':
type = 'c';
index = 0; // current
break;
case 'ASK':
case 'SELL':
type = 'a';
index = 0; // current
break;
case 'BID':
case 'BUY':
type = 'b';
index = 0; // current
break;
case 'HIGH':
type = 'h';
index = 1; // last 24h
break;
case 'LOW':
type = 'l';
index = 1; // last 24h
break;
case 'AVG':
case 'AVERAGE': // volume weighted
type = 'p';
index = 1; // last 24h
break;
case 'VOL':
case 'VOLUME':
case 'BASEVOLUME':
type = 'v';
index = 1; // last 24h
break;
default:
throw new Error('Type "' + type + '" not found!');
}
apiPath = 'https://api.kraken.com/0/public/Ticker?pair=' + pair;
response = UrlFetchApp.fetch(apiPath);
data = JSON.parse(response.getContentText());
return parseFloat(data.result[pair][type][index]);
case 'LIQUI':
// Gets the ticker of the input currency pair at liqui.io over the last 24h.
switch (type.toUpperCase()) {
case 'LAST':
case 'PRICE':
type = 'last';
break;
case 'ASK':
case 'SELL':
type = 'sell';
break;
case 'BID':
case 'BUY':
type = 'buy';
break;
case 'HIGH':
type = 'high';
break;
case 'LOW':
type = 'low';
break;
case 'AVG':
case 'AVERAGE': // not volume weighted
type = 'avg';
break;
case 'VOL':
case 'VOLUME':
case 'BASEVOLUME':
type = 'vol';
break;
case 'QUOTEVOLUME':
case 'VOLUME_CURRENCY':
type = 'vol_cur';
break;
default:
throw new Error('Type "' + type + '" not found!');
}
apiPath = 'https://api.liqui.io/api/3/ticker/' + pair;
response = UrlFetchApp.fetch(apiPath);
data = JSON.parse(response.getContentText());
return parseFloat(data[pair][type]);
case 'BITTREX':
// Gets the ticker of the input currency pair at bittrex.com.
switch (type.toUpperCase()) {
case 'LAST':
case 'PRICE':
type = 'Last';
break;
case 'ASK':
case 'SELL':
type = 'Ask';
break;
case 'BID':
case 'BUY':
type = 'Bid';
break;
case 'HIGH':
type = 'High';
break;
case 'LOW':
type = 'Low';
break;
case 'AVG':
case 'AVERAGE': // not volume weighted
type = 'Average';
break;
case 'VOL':
case 'VOLUME':
case 'BASEVOLUME':
type = 'BaseVolume';
break;
case 'VOL_CUR':
case 'QUOTEVOLUME':
case 'VOLUME_CURRENCY':
type = 'Volume';
break;
default:
throw new Error('Type "' + type + '" not found!');
}
apiPath = 'https://bittrex.com/api/v1.1/public/getmarketsummary?market=' + pair;
response = UrlFetchApp.fetch(apiPath);
data = JSON.parse(response.getContentText());
if (type !== 'Average') {
return parseFloat(data.result[0][type]);
} else {
// Calculate average
high = parseFloat(data.result[0].High);
low = parseFloat(data.result[0].Low);
result = (high + low) / 2;
return +result.toFixed(8); // Only 8 decimal points
}
break;
case 'ETHERSCAN':
// Gets the ticker of the input currency pair at etherscan.io.
if (type.toUpperCase() !== 'LAST') {
throw new Error('Type "' + type + '" not found!');
}
apiPath = 'https://api.etherscan.io/api?module=stats&action=ethprice';
response = UrlFetchApp.fetch(apiPath);
data = JSON.parse(response.getContentText());
return parseFloat(data.result[pair.toLowerCase()]);
case 'COINMARKETCAP':
case 'CMC':
// See https://coinmarketcap.com/api/ for types
apiPath = 'https://api.coinmarketcap.com/v1/ticker/' + pair; // actually just one currency
response = UrlFetchApp.fetch(apiPath);
data = JSON.parse(response.getContentText());
return parseFloat(data[0][type]);
default:
throw new Error('Exchange "' + exchange + '" not found!');
}
}
/**
* Looks up the not volume weighted average price of the currency against USD on the specified day at gdax.com.
*
* @param {date} date The date.
* @param {string} currency The currency (eg: 'Bitcoin').
* @return The not volume weighted average price of that day.
* @customfunction
*/
function getGdaxAverageUsd(date, currency) {
switch (currency) {
case 'Bitcoin':
currency = 'BTC-USD';
break;
case 'Ether':
currency = 'ETH-USD';
break;
case 'EtherBitcoin':
currency = 'ETH-BTC';
break;
case 'USD':
return 1;
default:
return 'Error, invalid currency!';
}
date.setTime(date.getTime() - date.getTimezoneOffset() * 1000 * 60); // Remove timezone differences
var dateTimestamp = Math.floor(date.getTime() / 1000);
var nextDay = new Date();
nextDay.setTime(date.getTime() + 1000 * 60 * 60 * (24 + 2)); // 26h to make sure with daylight savings
var apiPath = 'https://api.gdax.com/products/' + currency + '/candles?granularity=86400&start=' +
date.toISOString().substr(0, 10) + '&end=' + nextDay.toISOString().substr(0, 10);
var response = UrlFetchApp.fetch(apiPath);
var data = JSON.parse(response.getContentText());
var dayBucket = data[0]; // [ time, low, high, open, close, volume ]
if (dayBucket[0] !== dateTimestamp) {
return 'Error in internal function';
}
var result = (parseFloat(dayBucket[3]) + parseFloat(dayBucket[4])) / 2; // (open + close) / 2
return result;
}
/**
* Looks up the volume weighted average price of the currency against euro on the specified day at kraken.com.
*
* @param {date} date The date.
* @param {string} currency The currency (eg: 'Bitcoin').
* @return The volume weighted average price of that day.
* @customfunction
*/
function getKrakenAverageEuro(date, currency) {
switch (currency) {
case 'Bitcoin':
currency = 'XXBTZEUR';
break;
case 'Ether':
currency = 'XETHZEUR';
break;
case 'EtherBitcoin':
currency = 'XETHXXBT';
break;
case 'Tether':
// To convert to EUR, you need a cell with the formuala '=GOOGLEFINANCE("CURRENCY:EURUSD")'
// and make it a named range called 'EURUSD'
currency = 'USDTZUSD';
break;
case 'Euro':
return 1;
default:
return 'Error, invalid currency!';
}
date.setTime(date.getTime() - date.getTimezoneOffset() * 1000 * 60); // Remove timezone differences
var dateTimestamp = Math.floor(date.getTime() / 1000);
var apiPath = 'https://api.kraken.com/0/public/OHLC?pair=' + currency + '&interval=1440';
var response = UrlFetchApp.fetch(apiPath);
var data = JSON.parse(response.getContentText());
data = data.result[currency];
var dayBucket = data[data.map(function(e) {
return e[0];
}).indexOf(dateTimestamp)];
if (dayBucket[0] !== dateTimestamp) {
return 'Error in internal function';
}
var result = parseFloat(dayBucket[5]); // VWAP
// If it needs to be converted from USD (see comment above)
if (currency === 'USDTZUSD') {
result /= SpreadsheetApp.getActiveSpreadsheet().getRangeByName('EURUSD').getValue();
}
return result;
}
/**
* Makes blockchain explorer links out of IDs in the selected range.
*/
function updateHyperlinks() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var range = spreadsheet.getActiveRange();
var seperator = spreadsheet.getSpreadsheetLocale() === 'de_DE' ? ';' : ',';
for (var i = 1; i <= range.getNumRows(); i++) {
for (var j = 1; j <= range.getNumColumns(); j++) {
if (!range.getCell(i, j).isBlank()) {
var currentValue = range.getCell(i, j).getValue();
if (currentValue.slice(0, 2) === '0x') {
if (currentValue.length === 66) {
range.getCell(i, j).setFormula('=HYPERLINK("https://etherscan.io/tx/' +
currentValue + '"' + seperator + '"ETH-TXID: ' + currentValue +
'")');
} else if (currentValue.length === 42) {
range.getCell(i, j).setFormula('=HYPERLINK("https://etherscan.io/address/' +
currentValue + '"' + seperator + '"ETH-ADDR: ' + currentValue +
'")');
}
} else if (currentValue.length === 64) {
range.getCell(i, j).setFormula('=HYPERLINK("https://tradeblock.com/bitcoin/tx/' +
currentValue + '"' + seperator + '"BTC-TXID: ' + currentValue + '")');
}
}
}
}
}