Last active
March 20, 2018 01:30
-
-
Save abrkn/6e9160aa3fcc12190d16a73040d17ca9 to your computer and use it in GitHub Desktop.
Cryptowatch Historical Rates for Google Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cryptowatch-historical-rates.gs | |
// Cryptowatch Historical Rates for Google Apps Script | |
// Version: 1.0 | |
// | |
// Usage: | |
// =fetchCryptowatchRateFor("bitfinex", "BCHUSD", "2018-03-01") | |
// =fetchCryptowatchRateFor("bitfinex", "BCHUSD", "2018-03-01", "open") | |
// Defaults to closing price | |
function fetchCryptowatchRateFor(venue, symbol, date, which) { | |
var index = ['open', 'high', 'low', 'close'].indexOf(which || "close"); | |
if (index === -1) throw ("which shoul be open, high, low, or close"); | |
var date = new Date(date + 'T00:00:00.000Z'); | |
var ts = Math.floor((date.getTime() / 1000)).toString(); | |
var url = Utilities.formatString( | |
'https://api.cryptowat.ch/markets/%s/%s/ohlc?before=%s&after=%s&periods=86400', | |
venue, | |
symbol, | |
ts, | |
ts | |
); | |
var res = UrlFetchApp.fetch(url); | |
var raw = res.getContentText(); | |
var points = JSON.parse(raw).result['86400']; | |
var point = points[0]; | |
// 0=date, 1=open, 2=high, 3=low, 4=close, 5=volume | |
return point[index + 1]; | |
} | |
function testFetchCryptowatchRateFor() { | |
Logger.log(fetchCryptowatchRateFor('bitfinex', 'BCHUSD', '2018-03-14')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment