Skip to content

Instantly share code, notes, and snippets.

@gbrlmza
Last active June 10, 2020 00:17
Show Gist options
  • Save gbrlmza/5178971f502b79538ef5ef9b73052d65 to your computer and use it in GitHub Desktop.
Save gbrlmza/5178971f502b79538ef5ef9b73052d65 to your computer and use it in GitHub Desktop.
Google Sheet Script Finance Utils
var cache = CacheService.getScriptCache();
var cacheTTL = 120 * 60 // Cache TTL in seconds
//=========================================================================================================================
// Get MD5 hash
//=========================================================================================================================
function md5(str) {
return Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5,str));
}
//=========================================================================================================================
// Get URL content
//=========================================================================================================================
function getUrlContent(url) {
var response = UrlFetchApp.fetch(url);
return response.getContentText();
}
//=========================================================================================================================
// Get BOND price
//=========================================================================================================================
function GetBondPrice(code, useCache) {
// Check Cache
var cacheKey = md5("GetBondPrice" + code);
var cacheValue = cache.get(cacheKey)
if (useCache && cacheValue != null) {
return parseFloat(cacheValue);
}
// Get Value from URL
var url = 'http://bonos.ecobolsar.com.ar/eco/ticker.php?t=' + code
var content = getUrlContent(url);
var regex = new RegExp("<td class=\"precioticker\">([0-9.,]+)<\/td>");
var price = parseFloat(regex.exec(content)[1].replace(".","").replace(",","."));
// Save in cache
cache.put(cacheKey, price.toString(), cacheTTL);
return price
}
//=========================================================================================================================
// Get BCRA primary variable value
//=========================================================================================================================
function GetBCRAVariable(variable, useCache) {
// Check Cache
var cacheKey = md5("GetBCRAVariable" + variable);
var cacheValue = cache.get(cacheKey)
if (useCache && cacheValue != null) {
return parseFloat(cacheValue);
}
// Get Value from URL
var url = 'http://www.bcra.gov.ar/PublicacionesEstadisticas/Principales_variables.asp'
var content = getUrlContent(url);
var regex = new RegExp("<td><a href='.*>.*" + variable + ".*<\/a><\/td>\\s+<td.*\\s+<td.*>(.*)<\/td>");
var value = parseFloat(regex.exec(content)[1].replace(".","").replace(",","."));
// Save in cache
cache.put(cacheKey, value.toString(), cacheTTL);
return value
}
//=========================================================================================================================
// Cache common requests
//=========================================================================================================================
function cacheCommonRequest() {
try {
GetBondPrice("AY24C", false);
GetBondPrice("AY24D", false);
GetBondPrice("AY24", false);
GetBCRAVariable("Unidad de Valor Adquisitivo", false);
GetBCRAVariable("Tipo de Cambio Minorista", false);
} catch (error) {
//ignore
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment